@@ -11,7 +11,10 @@ console.warn = (...args) => {
11
11
originalWarn ( ...args ) ;
12
12
} ;
13
13
14
- import { processStripeEvent } from '../../../../src/controllers/webhook' ;
14
+ import {
15
+ processStripeEvent ,
16
+ webhookEventHandler ,
17
+ } from '../../../../src/controllers/webhook' ;
15
18
import * as productHandler from '../../../../src/handlers/product' ;
16
19
import * as priceHandler from '../../../../src/handlers/price' ;
17
20
import * as taxRateHandler from '../../../../src/handlers/tax-rate' ;
@@ -20,7 +23,7 @@ import * as paymentHandler from '../../../../src/handlers/payment';
20
23
import * as invoiceHandler from '../../../../src/handlers/invoice' ;
21
24
import { stripe } from '../../../../src/services' ;
22
25
23
- // Mocks
26
+ // Mock modules
24
27
jest . mock ( '../../../../src/handlers/product' ) ;
25
28
jest . mock ( '../../../../src/handlers/customer' ) ;
26
29
jest . mock ( '../../../../src/handlers/price' ) ;
@@ -33,6 +36,9 @@ jest.mock('../../../../src/services', () => ({
33
36
paymentIntents : {
34
37
retrieve : jest . fn ( ) ,
35
38
} ,
39
+ webhooks : {
40
+ constructEvent : jest . fn ( ) ,
41
+ } ,
36
42
} ,
37
43
} ) ) ;
38
44
@@ -161,9 +167,7 @@ describe('processStripeEvent', () => {
161
167
const event = {
162
168
id : 'evt_test' ,
163
169
type,
164
- data : {
165
- object,
166
- } ,
170
+ data : { object } ,
167
171
} ;
168
172
169
173
await processStripeEvent ( event as any ) ;
@@ -222,3 +226,45 @@ describe('processStripeEvent', () => {
222
226
) . toHaveBeenCalledWith ( 'sub_checkout' , 'cus_checkout' , true ) ;
223
227
} ) ;
224
228
} ) ;
229
+
230
+ describe ( 'webhookEventHandler' , ( ) => {
231
+ it ( 'verifies Stripe event and processes it' , async ( ) => {
232
+ const mockEvent = {
233
+ id : 'evt_test' ,
234
+ type : 'product.created' ,
235
+ data : {
236
+ object : { id : 'prod_123' } ,
237
+ } ,
238
+ } ;
239
+
240
+ // Mock constructEvent
241
+ ( stripe . webhooks . constructEvent as jest . Mock ) . mockReturnValue ( mockEvent ) ;
242
+
243
+ const req = {
244
+ rawBody : Buffer . from ( JSON . stringify ( mockEvent ) ) ,
245
+ headers : {
246
+ 'stripe-signature' : 'test-signature' ,
247
+ } ,
248
+ } as any ;
249
+
250
+ const statusMock = jest . fn ( ( ) => res ) ;
251
+ const jsonMock = jest . fn ( ) ;
252
+ const sendMock = jest . fn ( ) ;
253
+
254
+ const res = {
255
+ status : statusMock ,
256
+ json : jsonMock ,
257
+ send : sendMock ,
258
+ } as any ;
259
+
260
+ await webhookEventHandler ( req , res ) ;
261
+
262
+ expect ( stripe . webhooks . constructEvent ) . toHaveBeenCalledWith (
263
+ req . rawBody ,
264
+ 'test-signature' ,
265
+ expect . any ( String )
266
+ ) ;
267
+
268
+ expect ( jsonMock ) . toHaveBeenCalledWith ( { received : true } ) ;
269
+ } ) ;
270
+ } ) ;
0 commit comments