import express from 'express'; import request from 'supertest'; const mockCalls: { name: string; body?: unknown; query?: unknown }[] = []; jest.mock('../src/controllers/webhookController', () => ({ handleYocoWebhook: (req: { rawBody: string }, res: { json(b: unknown): void }) => { mockCalls.push({ name: 'yoco', body: req.rawBody }); res.json({ ok: true }); } })); jest.mock('../src/payments/webhook', () => ({ handleProviderWebhook: (id: string) => (_req: unknown, res: { json(b: unknown): void }) => { mockCalls.push({ name: id }); res.json({ ok: true }); } })); jest.mock('../src/controllers/whatsappController', () => ({ handleWebhook: (req: { body: unknown; query: unknown }, res: { json(b: unknown): void }) => { mockCalls.push({ name: 'whatsapp', body: req.body, query: req.query }); res.json({ ok: true }); } })); jest.mock('../src/controllers/privacyController', () => ({ oneClickRedirect: (req: { params: { token: string } }, res: { redirect(c: number, u: string): void }) => res.redirect(302, `https://site.example/unsubscribe?t=${req.params.token}`), oneClickUnsubscribe: (req: { params: { token: string }; body: unknown }, res: { json(b: unknown): void }) => { mockCalls.push({ name: 'one-click', body: req.body }); res.json({ ok: true }); }, })); import { webhooksRouter, linksRouter, legacyLinkRedirects, legacyV1Router, v1Removed } from '../src/v2/links'; // The same order and raw-body handling as app.js. function app() { const a = express(); a.use((req, _res, next) => { if (req.path.startsWith('/api/v2/webhooks')) { let raw = ''; req.setEncoding('utf8'); req.on('data', (c: string) => { raw += c; }); req.on('end', () => { (req as unknown as { rawBody: string }).rawBody = raw; next(); }); } else next(); }); a.use('/api/v2/webhooks', webhooksRouter); a.use(express.json()); a.use('/api/v2', linksRouter); a.use(legacyLinkRedirects); a.use('/api', legacyV1Router); a.use('/api', v1Removed); a.use((_req, res) => { res.status(404).json({ notFound: true }); }); return a; } beforeEach(() => { mockCalls.length = 0; }); describe('incoming webhooks on v2', () => { test('Yoco gets the exact bytes it signed', async () => { const body = '{"type":"payment.succeeded","payload":{"id":"p1"}}'; await request(app()).post('/api/v2/webhooks/yoco').set('Content-Type', 'application/json').send(body); expect(mockCalls).toEqual([{ name: 'yoco', body }]); }); test('PayFast reaches the PayFast handler', async () => { await request(app()).post('/api/v2/webhooks/payfast').type('form').send('m_payment_id=c1'); expect(mockCalls[0].name).toBe('payfast'); }); test('WhatsApp gets its JSON parsed and keeps the secret in the query', async () => { await request(app()).post('/api/v2/webhooks/whatsapp?secret=s3').send({ event: 'message', payload: { body: 'hi' } }); expect(mockCalls[0]).toEqual({ name: 'whatsapp', body: { event: 'message', payload: { body: 'hi' } }, query: { secret: 's3' } }); }); }); describe('links in sent messages', () => { test('an old calendar link answers 308 with its v2 address', async () => { const res = await request(app()).get('/api/events/e1/ics?x=1'); expect(res.status).toBe(308); expect(res.headers.location).toBe('/api/v2/events/e1/calendar.ics?x=1'); }); test('opening an old one-click link goes to v2, which sends the person to the page', async () => { const old = await request(app()).get('/api/privacy/unsubscribe/tok/one-click'); expect(old.status).toBe(308); expect(old.headers.location).toBe('/api/v2/unsubscribe/tok/one-click'); const v2 = await request(app()).get('/api/v2/unsubscribe/tok/one-click'); expect(v2.status).toBe(302); expect(v2.headers.location).toBe('https://site.example/unsubscribe?t=tok'); }); test("a mail app's one-click POST works on v2, and on the old address without a redirect", async () => { await request(app()).post('/api/v2/unsubscribe/tok/one-click').type('form').send('List-Unsubscribe=One-Click'); expect(mockCalls[0]).toEqual({ name: 'one-click', body: { 'List-Unsubscribe': 'One-Click' } }); const old = await request(app()).post('/api/privacy/unsubscribe/tok/one-click').type('form').send('List-Unsubscribe=One-Click'); expect(old.status).toBe(200); expect(mockCalls[1]).toEqual({ name: 'one-click', body: { 'List-Unsubscribe': 'One-Click' } }); }); }); describe('v1, removed in 2.0', () => { test("WAWP's oldest webhook address still reaches the handler", async () => { await request(app()).post('/api/whatsapp/webhook?secret=s3').send({ event: 'message' }); expect(mockCalls[0]).toEqual({ name: 'whatsapp', body: { event: 'message' }, query: { secret: 's3' } }); }); test('any other v1 address answers 410 and points at v2', async () => { const res = await request(app()).get('/api/events'); expect(res.status).toBe(410); expect(res.body).toMatchObject({ code: 'API_V1_REMOVED', message: expect.stringContaining('/api/v2') }); expect((await request(app()).post('/api/users/login').send({})).status).toBe(410); }); test('an unknown v2 address is still a 404', async () => { expect((await request(app()).get('/api/v2/nothing-here')).status).toBe(404); }); });