-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbilling.controller.unit.tests.js
More file actions
195 lines (148 loc) · 7.52 KB
/
Copy pathbilling.controller.unit.tests.js
File metadata and controls
195 lines (148 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* Module dependencies.
*/
import { jest, beforeEach, afterEach } from '@jest/globals';
/**
* Unit tests for billing webhook controller
*/
describe('Billing webhook controller unit tests:', () => {
let BillingWebhookController;
let mockBillingWebhookService;
let mockStripeInstance;
let res;
beforeEach(async () => {
jest.resetModules();
mockBillingWebhookService = {
handleCheckoutCompleted: jest.fn().mockResolvedValue(),
handleSubscriptionUpdated: jest.fn().mockResolvedValue(),
handleSubscriptionDeleted: jest.fn().mockResolvedValue(),
handleInvoicePaymentFailed: jest.fn().mockResolvedValue(),
};
jest.unstable_mockModule('../services/billing.webhook.service.js', () => ({
default: mockBillingWebhookService,
}));
mockStripeInstance = {
webhooks: {
constructEvent: jest.fn(),
},
};
jest.unstable_mockModule('stripe', () => ({
default: jest.fn(() => mockStripeInstance),
}));
res = {
status: jest.fn().mockReturnThis(),
json: jest.fn().mockReturnThis(),
};
});
afterEach(() => {
jest.restoreAllMocks();
});
test('should return 400 when stripe is not configured', async () => {
jest.unstable_mockModule('../../../config/index.js', () => ({
default: { stripe: {} },
}));
const mod = await import('../controllers/billing.webhook.controller.js');
BillingWebhookController = mod.default;
const req = { headers: {}, body: '' };
await BillingWebhookController.handleWebhook(req, res);
expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({ error: 'Stripe is not configured' });
});
test('should return 400 when signature verification fails', async () => {
jest.unstable_mockModule('../../../config/index.js', () => ({
default: { stripe: { secretKey: 'sk_test_1', webhookSecret: 'whsec_1' } },
}));
mockStripeInstance.webhooks.constructEvent.mockImplementation(() => {
throw new Error('bad signature');
});
const mod = await import('../controllers/billing.webhook.controller.js');
BillingWebhookController = mod.default;
const req = { headers: { 'stripe-signature': 'sig_bad' }, body: 'raw' };
await BillingWebhookController.handleWebhook(req, res);
expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({ error: 'Webhook signature verification failed' });
});
test('should handle checkout.session.completed event', async () => {
jest.unstable_mockModule('../../../config/index.js', () => ({
default: { stripe: { secretKey: 'sk_test_2', webhookSecret: 'whsec_2' } },
}));
const eventData = { type: 'checkout.session.completed', data: { object: { id: 'cs_123' } } };
mockStripeInstance.webhooks.constructEvent.mockReturnValue(eventData);
const mod = await import('../controllers/billing.webhook.controller.js');
BillingWebhookController = mod.default;
const req = { headers: { 'stripe-signature': 'sig_ok' }, body: 'raw' };
await BillingWebhookController.handleWebhook(req, res);
expect(mockBillingWebhookService.handleCheckoutCompleted).toHaveBeenCalledWith({ id: 'cs_123' });
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith({ received: true });
});
test('should handle customer.subscription.updated event', async () => {
jest.unstable_mockModule('../../../config/index.js', () => ({
default: { stripe: { secretKey: 'sk_test_3', webhookSecret: 'whsec_3' } },
}));
const eventData = { type: 'customer.subscription.updated', data: { object: { id: 'sub_123' } } };
mockStripeInstance.webhooks.constructEvent.mockReturnValue(eventData);
const mod = await import('../controllers/billing.webhook.controller.js');
BillingWebhookController = mod.default;
const req = { headers: { 'stripe-signature': 'sig_ok' }, body: 'raw' };
await BillingWebhookController.handleWebhook(req, res);
expect(mockBillingWebhookService.handleSubscriptionUpdated).toHaveBeenCalledWith({ id: 'sub_123' }, eventData);
expect(res.status).toHaveBeenCalledWith(200);
});
test('should handle customer.subscription.deleted event', async () => {
jest.unstable_mockModule('../../../config/index.js', () => ({
default: { stripe: { secretKey: 'sk_test_4', webhookSecret: 'whsec_4' } },
}));
const eventData = { type: 'customer.subscription.deleted', data: { object: { id: 'sub_del' } } };
mockStripeInstance.webhooks.constructEvent.mockReturnValue(eventData);
const mod = await import('../controllers/billing.webhook.controller.js');
BillingWebhookController = mod.default;
const req = { headers: { 'stripe-signature': 'sig_ok' }, body: 'raw' };
await BillingWebhookController.handleWebhook(req, res);
expect(mockBillingWebhookService.handleSubscriptionDeleted).toHaveBeenCalledWith({ id: 'sub_del' });
expect(res.status).toHaveBeenCalledWith(200);
});
test('should handle invoice.payment_failed event', async () => {
jest.unstable_mockModule('../../../config/index.js', () => ({
default: { stripe: { secretKey: 'sk_test_5', webhookSecret: 'whsec_5' } },
}));
const eventData = { type: 'invoice.payment_failed', data: { object: { id: 'inv_fail' } } };
mockStripeInstance.webhooks.constructEvent.mockReturnValue(eventData);
const mod = await import('../controllers/billing.webhook.controller.js');
BillingWebhookController = mod.default;
const req = { headers: { 'stripe-signature': 'sig_ok' }, body: 'raw' };
await BillingWebhookController.handleWebhook(req, res);
expect(mockBillingWebhookService.handleInvoicePaymentFailed).toHaveBeenCalledWith({ id: 'inv_fail' });
expect(res.status).toHaveBeenCalledWith(200);
});
test('should return 200 for unknown event types', async () => {
jest.unstable_mockModule('../../../config/index.js', () => ({
default: { stripe: { secretKey: 'sk_test_6', webhookSecret: 'whsec_6' } },
}));
const eventData = { type: 'unknown.event', data: { object: {} } };
mockStripeInstance.webhooks.constructEvent.mockReturnValue(eventData);
const mod = await import('../controllers/billing.webhook.controller.js');
BillingWebhookController = mod.default;
const req = { headers: { 'stripe-signature': 'sig_ok' }, body: 'raw' };
await BillingWebhookController.handleWebhook(req, res);
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith({ received: true });
});
test('should return 500 when handler throws', async () => {
jest.unstable_mockModule('../../../config/index.js', () => ({
default: { stripe: { secretKey: 'sk_test_7', webhookSecret: 'whsec_7' } },
}));
const eventData = { type: 'checkout.session.completed', data: { object: { id: 'cs_err' } } };
mockStripeInstance.webhooks.constructEvent.mockReturnValue(eventData);
mockBillingWebhookService.handleCheckoutCompleted.mockRejectedValue(new Error('DB error'));
const mod = await import('../controllers/billing.webhook.controller.js');
BillingWebhookController = mod.default;
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const req = { headers: { 'stripe-signature': 'sig_ok' }, body: 'raw' };
await BillingWebhookController.handleWebhook(req, res);
expect(consoleSpy).toHaveBeenCalledWith('Stripe webhook handler error:', expect.any(Error));
expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith({ error: 'Webhook handler failed' });
consoleSpy.mockRestore();
});
});