@@ -10,6 +10,11 @@ import { INestApplication } from '@nestjs/common'
10
10
import { RecurringDonationStatus } from '@prisma/client'
11
11
import { CreateRecurringDonationDto } from './dto/create-recurring-donation.dto'
12
12
import { RecurringDonation } from '../domain/generated/recurringDonation/entities/recurringDonation.entity'
13
+ import { StripeService } from '../stripe/stripe.service'
14
+ import { CampaignService } from '../campaign/campaign.service'
15
+ import { DonationsService } from '../donations/donations.service'
16
+ import { RealmViewSupporters } from '@podkrepi-bg/podkrepi-types'
17
+
13
18
14
19
const mockCreateRecurring = new CreateRecurringDonationDto ( )
15
20
mockCreateRecurring . amount = 1
@@ -38,6 +43,7 @@ const mockRecurring = {
38
43
describe ( 'RecurringDonationService' , ( ) => {
39
44
let service : RecurringDonationService
40
45
let app : INestApplication
46
+ let stripeService : StripeService
41
47
42
48
const stripeMock = {
43
49
checkout : { sessions : { create : jest . fn ( ) } } ,
@@ -50,10 +56,22 @@ describe('RecurringDonationService', () => {
50
56
RecurringDonationService ,
51
57
MockPrismaService ,
52
58
ConfigService ,
59
+ {
60
+ provide : StripeService ,
61
+ useValue : mockDeep < StripeService > ( )
62
+ } ,
53
63
{
54
64
provide : HttpService ,
55
65
useValue : mockDeep < HttpService > ( ) ,
56
66
} ,
67
+ {
68
+ provide : CampaignService ,
69
+ useValue : mockDeep < CampaignService > ( ) ,
70
+ } ,
71
+ {
72
+ provide : DonationsService ,
73
+ useValue : mockDeep < DonationsService > ( ) ,
74
+ } ,
57
75
Stripe ,
58
76
{
59
77
provide : STRIPE_CLIENT_TOKEN ,
@@ -63,7 +81,7 @@ describe('RecurringDonationService', () => {
63
81
} ) . compile ( )
64
82
65
83
service = module . get < RecurringDonationService > ( RecurringDonationService )
66
-
84
+ stripeService = module . get < StripeService > ( StripeService )
67
85
app = module . createNestApplication ( )
68
86
await app . init ( )
69
87
} )
@@ -86,18 +104,51 @@ describe('RecurringDonationService', () => {
86
104
expect ( result ) . toStrictEqual ( mockRecurring )
87
105
} )
88
106
89
- it ( 'should cancel a subscription in db' , async ( ) => {
90
- prismaMock . recurringDonation . update . mockResolvedValueOnce ( mockRecurring )
91
- await service . cancel ( '1' )
107
+ it ( 'should cancel a subscription in db if admin' , async ( ) => {
108
+ prismaMock . recurringDonation . update . mockResolvedValueOnce ( mockRecurring )
109
+ prismaMock . recurringDonation . findUnique . mockResolvedValueOnce ( mockRecurring )
110
+ const updateDbSpy = jest . spyOn ( service , 'updateStatus' )
111
+ const stripeSpy = jest . spyOn ( stripeService , 'cancelSubscription' )
112
+ const donationBelongsToSpy = jest . spyOn ( service , 'donationBelongsTo' )
113
+ donationBelongsToSpy . mockResolvedValue ( false )
114
+ stripeSpy . mockResolvedValue ( { status : 'active' } as any )
115
+ await service . cancel ( '1' , { sub : '1' , realm_access : { roles : [ RealmViewSupporters . role ] } } as any )
116
+ expect ( stripeSpy ) . toHaveBeenCalledWith ( mockRecurring . extSubscriptionId )
117
+ expect ( updateDbSpy ) . toHaveBeenCalledWith ( mockRecurring . id , RecurringDonationStatus . canceled )
118
+ } )
119
+
120
+ it ( 'should cancel a subscription in db if regular user and own donation' , async ( ) => {
121
+ // prismaMock.recurringDonation.update.mockResolvedValueOnce(mockRecurring)
122
+ const findOneSpy = jest . spyOn ( service , 'findOne' ) . mockResolvedValue ( mockRecurring )
123
+ const donationBelongsToSpy = jest . spyOn ( service , 'donationBelongsTo' )
124
+ donationBelongsToSpy . mockResolvedValue ( true )
125
+ const updateDbSpy = jest . spyOn ( service , 'updateStatus' )
126
+ const stripeSpy = jest . spyOn ( stripeService , 'cancelSubscription' )
127
+ updateDbSpy . mockResolvedValue ( mockRecurring )
128
+ stripeSpy . mockResolvedValue ( { status : 'active' } as any )
129
+ await service . cancel ( mockRecurring . id , { sub : '1' , realm_access : { roles : [ ] } } as any )
130
+ expect ( stripeSpy ) . toHaveBeenCalledWith ( mockRecurring . extSubscriptionId )
131
+ expect ( updateDbSpy ) . toHaveBeenCalledWith ( mockRecurring . id , RecurringDonationStatus . canceled )
132
+
133
+ } )
134
+
135
+ it ( 'should not allow to cancel a subscription in db if regular user and not own donation' , async ( ) => {
136
+ const findOneSpy = jest . spyOn ( service , 'findOne' ) . mockResolvedValue ( mockRecurring )
137
+ const updateStatusSpy = jest . spyOn ( service , 'updateStatus' )
138
+ const donationBelongsToSpy = jest . spyOn ( service , 'donationBelongsTo' )
139
+ donationBelongsToSpy . mockResolvedValue ( false )
92
140
const updateDbSpy = jest . spyOn ( prismaMock . recurringDonation , 'update' )
93
- expect ( updateDbSpy ) . toHaveBeenCalledWith ( {
141
+ const stripeSpy = jest . spyOn ( stripeService , 'cancelSubscription' )
142
+ expect ( service . cancel ( mockRecurring . id , { sub : '1' , realm_access : { roles : [ ] } } as any ) ) . rejects . toThrow ( )
143
+ expect ( stripeSpy ) . not . toHaveBeenCalledWith ( mockRecurring . extSubscriptionId )
144
+ expect ( updateStatusSpy ) . not . toHaveBeenCalledWith ( {
94
145
where : { id : '1' } ,
95
146
data : {
96
147
status : RecurringDonationStatus . canceled ,
97
148
} ,
98
149
} )
99
150
} )
100
-
151
+
101
152
it ( 'should create a subscription in db' , async ( ) => {
102
153
prismaMock . recurringDonation . create . mockResolvedValueOnce ( mockRecurring )
103
154
@@ -123,4 +174,8 @@ describe('RecurringDonationService', () => {
123
174
} ,
124
175
} )
125
176
} )
177
+
178
+ it ( 'should update a recurring donation' , async ( ) => {
179
+ expect ( true ) . toBeTruthy ( )
180
+ } )
126
181
} )
0 commit comments