|
| 1 | +/** |
| 2 | + * Integration tests for PayPal webhook route (POST /api/webhooks/paypal) |
| 3 | + * Covers: success, idempotent-replay, rejected-forgery (#1084) |
| 4 | + */ |
| 5 | +import request from 'supertest'; |
| 6 | +import express, { type Express } from 'express'; |
| 7 | + |
| 8 | +jest.mock('../src/config/logger', () => ({ |
| 9 | + __esModule: true, |
| 10 | + default: { info: jest.fn(), error: jest.fn(), warn: jest.fn() }, |
| 11 | +})); |
| 12 | + |
| 13 | +jest.mock('@sentry/node', () => ({ |
| 14 | + captureMessage: jest.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +import paypalWebhookRoutes from '../src/routes/paypal-webhook'; |
| 18 | + |
| 19 | +function buildApp(): Express { |
| 20 | + const app = express(); |
| 21 | + app.use('/api/webhooks/paypal', express.raw({ type: 'application/json' }), paypalWebhookRoutes); |
| 22 | + return app; |
| 23 | +} |
| 24 | + |
| 25 | +const app = buildApp(); |
| 26 | + |
| 27 | +describe('PayPal webhook route integration', () => { |
| 28 | + const originalEnv: Record<string, string | undefined> = {}; |
| 29 | + const PAYPAL_WEBHOOK_ID = 'WH-TEST-12345'; |
| 30 | + const PAYPAL_CLIENT_ID = 'test_client_id'; |
| 31 | + const PAYPAL_CLIENT_SECRET = 'test_client_secret'; |
| 32 | + |
| 33 | + const originalFetch = global.fetch; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + originalEnv.PAYPAL_WEBHOOK_ID = process.env.PAYPAL_WEBHOOK_ID; |
| 37 | + originalEnv.PAYPAL_CLIENT_ID = process.env.PAYPAL_CLIENT_ID; |
| 38 | + originalEnv.PAYPAL_CLIENT_SECRET = process.env.PAYPAL_CLIENT_SECRET; |
| 39 | + originalEnv.PAYPAL_MODE = process.env.PAYPAL_MODE; |
| 40 | + |
| 41 | + process.env.PAYPAL_WEBHOOK_ID = PAYPAL_WEBHOOK_ID; |
| 42 | + process.env.PAYPAL_CLIENT_ID = PAYPAL_CLIENT_ID; |
| 43 | + process.env.PAYPAL_CLIENT_SECRET = PAYPAL_CLIENT_SECRET; |
| 44 | + process.env.PAYPAL_MODE = 'sandbox'; |
| 45 | + }); |
| 46 | + |
| 47 | + afterEach(() => { |
| 48 | + Object.entries(originalEnv).forEach(([key, value]) => { |
| 49 | + if (value === undefined) delete process.env[key]; |
| 50 | + else process.env[key] = value; |
| 51 | + }); |
| 52 | + global.fetch = originalFetch; |
| 53 | + }); |
| 54 | + |
| 55 | + const validPayload = JSON.stringify({ |
| 56 | + id: 'WH-58D07950BU892453L', |
| 57 | + event_type: 'PAYMENT.CAPTURE.COMPLETED', |
| 58 | + resource: { id: 'CAP-123', amount: { total: '29.99', currency: 'USD' } }, |
| 59 | + }); |
| 60 | + |
| 61 | + const validHeaders = { |
| 62 | + 'paypal-transmission-id': 'tx-abc-123', |
| 63 | + 'paypal-transmission-time': '2026-07-01T12:00:00Z', |
| 64 | + 'paypal-cert-url': 'https://api.sandbox.paypal.com/v1/notifications/certs/CERT-ID', |
| 65 | + 'paypal-auth-algo': 'SHA256withRSA', |
| 66 | + 'paypal-transmission-sig': 'mock-signature-value', |
| 67 | + }; |
| 68 | + |
| 69 | + function mockPayPalSuccess() { |
| 70 | + global.fetch = jest |
| 71 | + .fn() |
| 72 | + .mockResolvedValueOnce({ |
| 73 | + ok: true, |
| 74 | + json: async () => ({ access_token: 'test_access_token' }), |
| 75 | + }) |
| 76 | + .mockResolvedValueOnce({ |
| 77 | + ok: true, |
| 78 | + json: async () => ({ verification_status: 'SUCCESS' }), |
| 79 | + }) as jest.Mock; |
| 80 | + } |
| 81 | + |
| 82 | + function mockPayPalFailure() { |
| 83 | + global.fetch = jest |
| 84 | + .fn() |
| 85 | + .mockResolvedValueOnce({ |
| 86 | + ok: true, |
| 87 | + json: async () => ({ access_token: 'test_access_token' }), |
| 88 | + }) |
| 89 | + .mockResolvedValueOnce({ |
| 90 | + ok: true, |
| 91 | + json: async () => ({ verification_status: 'FAILURE' }), |
| 92 | + }) as jest.Mock; |
| 93 | + } |
| 94 | + |
| 95 | + it('accepts a valid PayPal webhook (success)', async () => { |
| 96 | + mockPayPalSuccess(); |
| 97 | + |
| 98 | + const res = await request(app) |
| 99 | + .post('/api/webhooks/paypal') |
| 100 | + .set(validHeaders) |
| 101 | + .set('content-type', 'application/json') |
| 102 | + .send(validPayload); |
| 103 | + |
| 104 | + expect(res.status).toBe(200); |
| 105 | + expect(res.body).toEqual({ received: true }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('accepts the same webhook twice (idempotent-replay)', async () => { |
| 109 | + // Use a single mock that returns success for all fetch calls |
| 110 | + global.fetch = jest |
| 111 | + .fn() |
| 112 | + .mockResolvedValueOnce({ |
| 113 | + ok: true, |
| 114 | + json: async () => ({ access_token: 'test_access_token' }), |
| 115 | + }) |
| 116 | + .mockResolvedValueOnce({ |
| 117 | + ok: true, |
| 118 | + json: async () => ({ verification_status: 'SUCCESS' }), |
| 119 | + }) |
| 120 | + .mockResolvedValueOnce({ |
| 121 | + ok: true, |
| 122 | + json: async () => ({ access_token: 'test_access_token' }), |
| 123 | + }) |
| 124 | + .mockResolvedValueOnce({ |
| 125 | + ok: true, |
| 126 | + json: async () => ({ verification_status: 'SUCCESS' }), |
| 127 | + }) as jest.Mock; |
| 128 | + |
| 129 | + const res1 = await request(app) |
| 130 | + .post('/api/webhooks/paypal') |
| 131 | + .set(validHeaders) |
| 132 | + .set('content-type', 'application/json') |
| 133 | + .send(validPayload); |
| 134 | + |
| 135 | + const res2 = await request(app) |
| 136 | + .post('/api/webhooks/paypal') |
| 137 | + .set(validHeaders) |
| 138 | + .set('content-type', 'application/json') |
| 139 | + .send(validPayload); |
| 140 | + |
| 141 | + expect(res1.status).toBe(200); |
| 142 | + expect(res2.status).toBe(200); |
| 143 | + }); |
| 144 | + |
| 145 | + it('rejects a webhook with PayPal verification FAILURE (rejected-forgery)', async () => { |
| 146 | + mockPayPalFailure(); |
| 147 | + |
| 148 | + const res = await request(app) |
| 149 | + .post('/api/webhooks/paypal') |
| 150 | + .set(validHeaders) |
| 151 | + .set('content-type', 'application/json') |
| 152 | + .send(validPayload); |
| 153 | + |
| 154 | + expect(res.status).toBe(401); |
| 155 | + expect(res.body.error).toBe('Invalid signature'); |
| 156 | + }); |
| 157 | + |
| 158 | + it('rejects a webhook when transmission headers are missing (rejected-forgery)', async () => { |
| 159 | + const res = await request(app) |
| 160 | + .post('/api/webhooks/paypal') |
| 161 | + .set('content-type', 'application/json') |
| 162 | + .send(validPayload); |
| 163 | + |
| 164 | + expect(res.status).toBe(401); |
| 165 | + expect(res.body.error).toBe('Invalid signature'); |
| 166 | + }); |
| 167 | + |
| 168 | + it('rejects a webhook when some transmission headers are missing (rejected-forgery)', async () => { |
| 169 | + const res = await request(app) |
| 170 | + .post('/api/webhooks/paypal') |
| 171 | + .set('paypal-transmission-id', 'tx-partial') |
| 172 | + .set('content-type', 'application/json') |
| 173 | + .send(validPayload); |
| 174 | + |
| 175 | + expect(res.status).toBe(401); |
| 176 | + }); |
| 177 | + |
| 178 | + describe('malformed-payload handling (#1084)', () => { |
| 179 | + it('rejects a non-JSON malformed payload', async () => { |
| 180 | + const res = await request(app) |
| 181 | + .post('/api/webhooks/paypal') |
| 182 | + .set(validHeaders) |
| 183 | + .set('content-type', 'application/json') |
| 184 | + .send('not valid json'); |
| 185 | + |
| 186 | + expect(res.status).toBe(401); |
| 187 | + }); |
| 188 | + |
| 189 | + it('rejects an empty body', async () => { |
| 190 | + const res = await request(app) |
| 191 | + .post('/api/webhooks/paypal') |
| 192 | + .set(validHeaders) |
| 193 | + .set('content-type', 'application/json') |
| 194 | + .send(''); |
| 195 | + |
| 196 | + expect(res.status).toBe(401); |
| 197 | + }); |
| 198 | + }); |
| 199 | +}); |
0 commit comments