feat(capa): require note and photos via /submit, block PATCH bypass

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-14 11:10:03 +08:00
co-authored by Claude Sonnet 5.5
parent cf5719883c
commit 51986b7ae5
9 changed files with 275 additions and 2 deletions
+35
View File
@@ -0,0 +1,35 @@
// @vitest-environment node
import { describe, it, expect, vi } from 'vitest'
vi.mock('@/lib/auth/get-session', () => ({
getSession: async () => ({ sub: 'owner-1', role: 'capa_owner' }),
}))
vi.mock('@/lib/db/audit', () => ({ writeAuditLog: vi.fn() }))
vi.mock('@/lib/db/with-user', () => ({
withUser: async (_id: string, fn: (tx: unknown) => unknown) =>
fn({
select: () => ({ from: () => ({ where: () => ({ limit: async () => [{ ownerUserId: 'owner-1', status: 'in_progress' }] }) }) }),
update: () => ({ set: () => ({ where: async () => undefined }) }),
}),
}))
import { PATCH } from '@/app/api/capa/[id]/route'
const ctx = { params: Promise.resolve({ id: 'capa-1' }) }
const patch = (body: unknown) =>
new Request('http://localhost/api/capa/capa-1', {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
})
describe('PATCH /api/capa/[id]', () => {
it('rejects pending_verification (must use /submit)', async () => {
const res = await PATCH(patch({ status: 'pending_verification' }) as never, ctx)
expect(res.status).toBe(403)
})
it('still allows in_progress', async () => {
const res = await PATCH(patch({ status: 'in_progress' }) as never, ctx)
expect(res.status).toBe(200)
})
})
+87
View File
@@ -0,0 +1,87 @@
// @vitest-environment node
import { describe, it, expect, vi, beforeEach } from 'vitest'
const h = vi.hoisted(() => ({
session: null as null | { sub: string; role: string },
capa: null as null | { incidentId: string; ownerUserId: string; status: string },
updates: [] as unknown[],
inserts: [] as unknown[],
}))
vi.mock('@/lib/auth/get-session', () => ({ getSession: async () => h.session }))
vi.mock('@/lib/db/audit', () => ({ writeAuditLog: vi.fn() }))
vi.mock('@/lib/storage/evidence', () => ({
uploadEvidenceFile: vi.fn(async () => ({ path: 'p', publicUrl: '/api/evidence/p?token=t', hash: 'h' })),
}))
vi.mock('@/lib/db/with-user', () => ({
withUser: async (_id: string, fn: (tx: unknown) => unknown) => {
const tx = {
select: () => ({ from: () => ({ where: () => ({ limit: async () => (h.capa ? [h.capa] : []) }) }) }),
insert: () => ({ values: (v: unknown) => { h.inserts.push(v); return { returning: async () => [{ id: 'ev-1' }] } } }),
update: () => ({ set: (v: unknown) => { h.updates.push(v); return { where: async () => undefined } } }),
}
return fn(tx)
},
}))
import { writeAuditLog } from '@/lib/db/audit'
import { POST } from '@/app/api/capa/[id]/submit/route'
function req(note: string | null, fileCount: number) {
const fd = new FormData()
if (note !== null) fd.append('note', note)
for (let i = 0; i < fileCount; i++) fd.append('files', new File(['x'], `p${i}.jpg`, { type: 'image/jpeg' }))
return new Request('http://localhost/api/capa/capa-1/submit', { method: 'POST', body: fd })
}
const ctx = { params: Promise.resolve({ id: 'capa-1' }) }
beforeEach(() => {
h.session = { sub: 'owner-1', role: 'capa_owner' }
h.capa = { incidentId: 'inc-1', ownerUserId: 'owner-1', status: 'in_progress' }
h.updates = []
h.inserts = []
vi.mocked(writeAuditLog).mockClear()
})
describe('POST /api/capa/[id]/submit', () => {
it('401 without a session', async () => {
h.session = null
expect((await POST(req('done', 1) as never, ctx)).status).toBe(401)
})
it('404 when the CAPA does not exist', async () => {
h.capa = null
expect((await POST(req('done', 1) as never, ctx)).status).toBe(404)
})
it('403 for a user who is neither owner nor hse/admin', async () => {
h.session = { sub: 'other', role: 'reporter' }
expect((await POST(req('done', 1) as never, ctx)).status).toBe(403)
})
it('409 when status is not in_progress', async () => {
h.capa!.status = 'open'
expect((await POST(req('done', 1) as never, ctx)).status).toBe(409)
})
it('422 when no photo is attached', async () => {
const res = await POST(req('done', 0) as never, ctx)
expect(res.status).toBe(422)
expect((await res.json()).details).toContain('At least 1 photo is required')
})
it('422 when the note is empty', async () => {
expect((await POST(req(' ', 1) as never, ctx)).status).toBe(422)
})
it('stores evidence, sets status and audits on success', async () => {
const res = await POST(req(' fixed the rack ', 2) as never, ctx)
expect(res.status).toBe(200)
expect(h.inserts[0]).toEqual([
expect.objectContaining({ incidentId: 'inc-1', capaId: 'capa-1', stage: 'capa', uploadedBy: 'owner-1' }),
expect.objectContaining({ incidentId: 'inc-1', capaId: 'capa-1', stage: 'capa', uploadedBy: 'owner-1' }),
])
expect(h.updates[0]).toEqual(
expect.objectContaining({ status: 'pending_verification', ownerNotes: 'fixed the rack', completedAt: expect.any(Date) }),
)
expect(writeAuditLog).toHaveBeenCalledWith(
expect.anything(), 'capa_actions', 'capa-1', 'submitted_for_verification',
expect.objectContaining({ status: 'pending_verification' }),
expect.objectContaining({ status: 'in_progress' }),
)
})
})