Skip to content

Commit dadcc33

Browse files
committed
test: add integration test for webhook deletion and verify no future deliveries
1 parent 12ab713 commit dadcc33

3 files changed

Lines changed: 47 additions & 5 deletions

File tree

src/modules/webhooks/webhook.controllers.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function createMockResponse() {
2525
res.status = jest.fn().mockReturnValue(res);
2626
res.json = jest.fn().mockReturnValue(res);
2727
res.setHeader = jest.fn().mockReturnValue(res);
28+
res.end = jest.fn().mockReturnValue(res);
2829
return res as Response;
2930
}
3031

@@ -120,15 +121,15 @@ describe('deleteWebhookHandler', () => {
120121
expect(res.status).toHaveBeenCalledWith(404);
121122
});
122123

123-
it('returns 200 on successful deletion', async () => {
124+
it('returns 204 on successful deletion', async () => {
124125
mockService.deleteWebhook.mockResolvedValue({ id: 'wh-1' });
125126

126127
const req = createMockSignedRequest('creator-1', { webhookId: 'wh-1' });
127128
const res = createMockResponse();
128129

129130
await deleteWebhookHandler(req, res);
130131

131-
expect(res.status).toHaveBeenCalledWith(200);
132+
expect(res.status).toHaveBeenCalledWith(204);
132133
expect(mockService.deleteWebhook).toHaveBeenCalledWith('wh-1', 'creator-1');
133134
});
134135
});

src/modules/webhooks/webhook.controllers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export async function deleteWebhookHandler(
8383
sendNotFound(res, 'Webhook');
8484
return;
8585
}
86-
sendSuccess(res, result, 200, 'Webhook deleted successfully');
86+
res.status(204).end();
8787
} catch {
8888
sendError(res, 500, ErrorCode.INTERNAL_ERROR, 'Failed to delete webhook');
8989
}

src/modules/webhooks/webhook.integration.test.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,7 @@ describe('DELETE /api/v1/creators/:id/webhooks/:webhookId', () => {
177177
.delete(`/api/v1/creators/${creatorId}/webhooks/${webhookId}`)
178178
.set(authHeaders('DELETE', `/api/v1/creators/${creatorId}/webhooks/${webhookId}`, creatorId));
179179

180-
expect(deleteRes.status).toBe(200);
181-
expect(deleteRes.body.success).toBe(true);
180+
expect(deleteRes.status).toBe(204);
182181

183182
const verifyRes = await supertest(app)
184183
.get(`/api/v1/creators/${creatorId}/webhooks`)
@@ -195,6 +194,48 @@ describe('DELETE /api/v1/creators/:id/webhooks/:webhookId', () => {
195194

196195
expect(res.status).toBe(404);
197196
});
197+
198+
it('stops future deliveries when a webhook is deleted (#506)', async () => {
199+
// Register a webhook and confirm it exists
200+
const webhook = await prisma.webhook.create({
201+
data: {
202+
id: 'webhook-deletion-test-506',
203+
creatorId,
204+
callbackUrl: 'https://example.com/deleted-hook',
205+
events: { set: ['BUY', 'SELL'] },
206+
},
207+
});
208+
209+
// Delete the webhook and assert the response is 204
210+
const deleteRes = await supertest(app)
211+
.delete(`/api/v1/creators/${creatorId}/webhooks/${webhook.id}`)
212+
.set(authHeaders('DELETE', `/api/v1/creators/${creatorId}/webhooks/${webhook.id}`, creatorId));
213+
214+
expect(deleteRes.status).toBe(204);
215+
216+
// Webhook record no longer exists after deletion
217+
const deletedWebhook = await prisma.webhook.findUnique({ where: { id: webhook.id } });
218+
expect(deletedWebhook).toBeNull();
219+
220+
// Simulate a trade event after deletion
221+
const { dispatchWebhookEvent } = await import('./webhook.service');
222+
await dispatchWebhookEvent({
223+
type: 'buy',
224+
creatorId,
225+
buyerOrSellerAddress: 'GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF',
226+
amount: '100',
227+
price: '10.5',
228+
feePaid: '0.5',
229+
timestamp: new Date().toISOString(),
230+
});
231+
232+
// Assert no delivery attempt was made for the deleted webhook
233+
const events = await prisma.webhookEvent.findMany({
234+
where: { webhookId: webhook.id },
235+
});
236+
237+
expect(events.length).toBe(0);
238+
});
198239
});
199240

200241
describe('webhook dispatch', () => {

0 commit comments

Comments
 (0)