Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(webhook): pin that the failure list omits retained event bodies
Yudhi Armyndharisa685279b9ee1
diff --git a/src/modules/webhook/delivery-failures-scope.spec.ts b/src/modules/webhook/delivery-failures-scope.spec.tsindex f7af86c8c..8bdbc21bf 100644--- a/src/modules/webhook/delivery-failures-scope.spec.ts+++ b/src/modules/webhook/delivery-failures-scope.spec.ts@@ -61,3 +61,49 @@ describe('WebhookService.listDeliveryFailures session scoping', () => {expect(rows.map(r => r.sessionId)).toEqual(['sessB']);});});++// With payload retention on, a failure row keeps the whole event it was built from, which can be a message+// body. The route serializes the rows it gets as they are, so the column staying out of the read is what+// keeps that text from every key that may list failures.+describe('WebhookService.listDeliveryFailures retained event body', () => {+ let ds: DataSource;+ let service: WebhookService;++ beforeEach(async () => {+ ds = new DataSource({+ type: 'better-sqlite3',+ database: ':memory:',+ entities: [WebhookDeliveryFailure],+ synchronize: true,+ });+ await ds.initialize();+ const failureRepo = ds.getRepository(WebhookDeliveryFailure);+ const cfg = {+ get: (key: string, fallback?: unknown) => (key === 'webhook.failurePayloadRetentionHours' ? 24 : fallback),+ };+ service = new WebhookService({} as never, failureRepo, {} as never, cfg as never, {} as never);+ await failureRepo.save(+ failureRepo.create({+ webhookId: 'wh-1',+ sessionId: 'sessA',+ event: 'message.received',+ url: 'https://sessA.example/hook',+ idempotencyKey: 'key-1',+ attempts: 3,+ lastError: 'ECONNREFUSED',+ payload: { body: 'private message text' },+ }),+ );+ });++ afterEach(async () => {+ await ds.destroy();+ });++ it('flags a row as replayable without returning the event body it retains', async () => {+ const rows = await service.listDeliveryFailures({}, null);++ expect(rows.map(r => r.replayable)).toEqual([true]);+ expect(JSON.stringify(rows)).not.toContain('private message text');+ });+});diff --git a/test/session-scope.e2e-spec.ts b/test/session-scope.e2e-spec.tsindex 693cedca7..3245fd7b7 100644--- a/test/session-scope.e2e-spec.ts+++ b/test/session-scope.e2e-spec.ts@@ -22,6 +22,8 @@ import { WebhookDeliveryFailure } from './../src/modules/webhook/entities/webhoo* which the unit specs mock away: a session-restricted ADMIN key must never read another session's rows.*/describe('Session-scoped query endpoints (e2e)', () => {+ // Text the failure rows below retain for a redrive, which the list route must never carry.+ const RETAINED_BODY = 'retained event body';let app: INestApplication<App>;let sessA: string;let sessB: string;@@ -62,6 +64,7 @@ describe('Session-scoped query endpoints (e2e)', () => {url: `https://${sessionId}.example/hook`,attempts: 3,lastError: 'ECONNREFUSED',+ payload: { body: RETAINED_BODY },}),);}@@ -166,6 +169,17 @@ describe('Session-scoped query endpoints (e2e)', () => {expect(sessions).toContain(sessA);expect(sessions).toContain(sessB);});++ it('never returns the event body a failure row retains for a redrive', async () => {+ const res = await request(app.getHttpServer())+ .get('/api/webhooks/delivery-failures')+ .set('X-API-Key', adminKey)+ .expect(200);+ expect((res.body as WebhookDeliveryFailure[]).map(r => r.sessionId)).toEqual(+ expect.arrayContaining([sessA, sessB]),+ );+ expect(JSON.stringify(res.body)).not.toContain(RETAINED_BODY);+ });});/**