Skip to content

Commit 46a2ca7

Browse files
committed
test: add more webhook tests
1 parent 79837e1 commit 46a2ca7

File tree

1 file changed

+51
-5
lines changed
  • firestore-stripe-payments/functions/__tests__/tests/unit/controllers

1 file changed

+51
-5
lines changed

Diff for: firestore-stripe-payments/functions/__tests__/tests/unit/controllers/webhook.test.ts

+51-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ console.warn = (...args) => {
1111
originalWarn(...args);
1212
};
1313

14-
import { processStripeEvent } from '../../../../src/controllers/webhook';
14+
import {
15+
processStripeEvent,
16+
webhookEventHandler,
17+
} from '../../../../src/controllers/webhook';
1518
import * as productHandler from '../../../../src/handlers/product';
1619
import * as priceHandler from '../../../../src/handlers/price';
1720
import * as taxRateHandler from '../../../../src/handlers/tax-rate';
@@ -20,7 +23,7 @@ import * as paymentHandler from '../../../../src/handlers/payment';
2023
import * as invoiceHandler from '../../../../src/handlers/invoice';
2124
import { stripe } from '../../../../src/services';
2225

23-
// Mocks
26+
// Mock modules
2427
jest.mock('../../../../src/handlers/product');
2528
jest.mock('../../../../src/handlers/customer');
2629
jest.mock('../../../../src/handlers/price');
@@ -33,6 +36,9 @@ jest.mock('../../../../src/services', () => ({
3336
paymentIntents: {
3437
retrieve: jest.fn(),
3538
},
39+
webhooks: {
40+
constructEvent: jest.fn(),
41+
},
3642
},
3743
}));
3844

@@ -161,9 +167,7 @@ describe('processStripeEvent', () => {
161167
const event = {
162168
id: 'evt_test',
163169
type,
164-
data: {
165-
object,
166-
},
170+
data: { object },
167171
};
168172

169173
await processStripeEvent(event as any);
@@ -222,3 +226,45 @@ describe('processStripeEvent', () => {
222226
).toHaveBeenCalledWith('sub_checkout', 'cus_checkout', true);
223227
});
224228
});
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

Comments
 (0)