From 9f784e08fdf58ecddb38f5ee72bc85c1a1aafaef Mon Sep 17 00:00:00 2001 From: phucnguyen1707 Date: Mon, 15 Jun 2026 10:23:23 +0700 Subject: [PATCH] Fix webhook delete route params --- src/app/api/webhooks/[id]/route.js | 2 +- src/app/api/webhooks/[id]/route.test.js | 40 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/app/api/webhooks/[id]/route.test.js diff --git a/src/app/api/webhooks/[id]/route.js b/src/app/api/webhooks/[id]/route.js index d858952..d66a7e2 100644 --- a/src/app/api/webhooks/[id]/route.js +++ b/src/app/api/webhooks/[id]/route.js @@ -4,7 +4,7 @@ import { withAuth } from '@/lib/api/middleware/auth.js'; /** Delete a webhook by ID */ export const DELETE = withAuth(async (event) => { const { supabase, user } = event.locals; - const { id } = params; + const { id } = (await event.context?.params) || {}; const { error } = await supabase .from('webhooks') diff --git a/src/app/api/webhooks/[id]/route.test.js b/src/app/api/webhooks/[id]/route.test.js new file mode 100644 index 0000000..b869949 --- /dev/null +++ b/src/app/api/webhooks/[id]/route.test.js @@ -0,0 +1,40 @@ +import { describe, expect, it, vi } from 'vitest'; + +const mocks = vi.hoisted(() => ({ + deleteMock: vi.fn(), + eqMock: vi.fn() +})); + +vi.mock('@/lib/api/middleware/auth.js', () => ({ + withAuth: (handler) => (request, context) => + handler({ + request, + locals: { + supabase: { + from: vi.fn(() => ({ + delete: mocks.deleteMock + })) + }, + user: { id: 'auth-user-id' } + }, + context + }) +})); + +describe('DELETE /api/webhooks/[id]', () => { + it('reads the webhook id from route context params', async () => { + mocks.eqMock.mockReturnThis(); + mocks.deleteMock.mockReturnValue({ eq: mocks.eqMock }); + + const { DELETE } = await import('./route.js'); + const response = await DELETE(new Request('https://qrypt.chat/api/webhooks/hook-1'), { + params: Promise.resolve({ id: 'hook-1' }) + }); + const body = await response.json(); + + expect(response.status).toBe(200); + expect(body).toEqual({ success: true }); + expect(mocks.eqMock).toHaveBeenCalledWith('id', 'hook-1'); + expect(mocks.eqMock).toHaveBeenCalledWith('user_id', 'auth-user-id'); + }); +});