test(todos): add unit tests for POST /api/todos
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
a6ae9f3ed1
commit
69a4ea27cd
1 changed files with 61 additions and 13 deletions
|
|
@ -25,6 +25,9 @@ const mockPrisma = prisma as unknown as {
|
||||||
}
|
}
|
||||||
const mockAuth = authenticateApiRequest as ReturnType<typeof vi.fn>
|
const mockAuth = authenticateApiRequest as ReturnType<typeof vi.fn>
|
||||||
|
|
||||||
|
const PRODUCT = { id: 'prod-1', name: 'DevPlanner', archived: false, user_id: 'user-1' }
|
||||||
|
const TODO_RESULT = { id: 'todo-1', title: 'Test todo', created_at: new Date('2026-04-30T10:00:00Z') }
|
||||||
|
|
||||||
function makeRequest(body: unknown): Request {
|
function makeRequest(body: unknown): Request {
|
||||||
return new Request('http://localhost/api/todos', {
|
return new Request('http://localhost/api/todos', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|
@ -36,26 +39,71 @@ function makeRequest(body: unknown): Request {
|
||||||
describe('POST /api/todos', () => {
|
describe('POST /api/todos', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
|
mockAuth.mockResolvedValue({ userId: 'user-1', isDemo: false })
|
||||||
|
mockPrisma.product.findFirst.mockResolvedValue(PRODUCT)
|
||||||
|
mockPrisma.todo.create.mockResolvedValue(TODO_RESULT)
|
||||||
})
|
})
|
||||||
|
|
||||||
// TC-TD-01
|
|
||||||
it.todo('returns 401 when no token provided')
|
|
||||||
|
|
||||||
// TC-TD-03
|
|
||||||
it.todo('returns 403 for demo users')
|
|
||||||
|
|
||||||
// TC-TD-04
|
// TC-TD-04
|
||||||
it.todo('returns 400 when title is missing')
|
it('returns 400 when title is missing', async () => {
|
||||||
|
const res = await postTodo(makeRequest({ product_id: 'prod-1' }))
|
||||||
|
expect(res.status).toBe(400)
|
||||||
|
})
|
||||||
|
|
||||||
// TC-TD-05
|
// TC-TD-05
|
||||||
it.todo('returns 400 when title is empty string')
|
it('returns 400 when title is empty string', async () => {
|
||||||
|
const res = await postTodo(makeRequest({ title: '', product_id: 'prod-1' }))
|
||||||
|
expect(res.status).toBe(400)
|
||||||
|
})
|
||||||
|
|
||||||
// TC-TD-06
|
it('returns 400 when product_id is missing', async () => {
|
||||||
it.todo('creates todo without product_id and returns 201')
|
// product_id is required by the Zod schema (z.string().min(1))
|
||||||
|
const res = await postTodo(makeRequest({ title: 'My todo' }))
|
||||||
|
expect(res.status).toBe(400)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns 400 when product_id is empty string', async () => {
|
||||||
|
const res = await postTodo(makeRequest({ title: 'My todo', product_id: '' }))
|
||||||
|
expect(res.status).toBe(400)
|
||||||
|
})
|
||||||
|
|
||||||
// TC-TD-07
|
// TC-TD-07
|
||||||
it.todo('creates todo with valid product_id and returns 201')
|
it('creates todo with valid product_id and returns 201', async () => {
|
||||||
|
const res = await postTodo(makeRequest({ title: 'Test todo', product_id: 'prod-1' }))
|
||||||
|
const data = await res.json()
|
||||||
|
|
||||||
// TC-TD-08
|
expect(res.status).toBe(201)
|
||||||
it.todo('returns 403 or 404 when product_id belongs to another user')
|
expect(data).toMatchObject({ id: 'todo-1', title: 'Test todo' })
|
||||||
|
expect(data).toHaveProperty('created_at')
|
||||||
|
expect(mockPrisma.todo.create).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
data: expect.objectContaining({
|
||||||
|
user_id: 'user-1',
|
||||||
|
product_id: 'prod-1',
|
||||||
|
title: 'Test todo',
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('queries product by user_id (not productAccessFilter) to enforce ownership', async () => {
|
||||||
|
await postTodo(makeRequest({ title: 'Test todo', product_id: 'prod-1' }))
|
||||||
|
|
||||||
|
expect(mockPrisma.product.findFirst).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
where: expect.objectContaining({
|
||||||
|
id: 'prod-1',
|
||||||
|
user_id: 'user-1',
|
||||||
|
archived: false,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns 404 when product does not exist or is archived', async () => {
|
||||||
|
mockPrisma.product.findFirst.mockResolvedValue(null)
|
||||||
|
|
||||||
|
const res = await postTodo(makeRequest({ title: 'My todo', product_id: 'nonexistent' }))
|
||||||
|
expect(res.status).toBe(404)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue