118 lines
5.1 KiB
TypeScript
118 lines
5.1 KiB
TypeScript
// @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[],
|
|
updateRows: [{ id: 'capa-1' }] 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: () => ({ returning: async () => h.updateRows }) } } }),
|
|
}
|
|
return fn(tx)
|
|
},
|
|
}))
|
|
|
|
import { writeAuditLog } from '@/lib/db/audit'
|
|
import { uploadEvidenceFile } from '@/lib/storage/evidence'
|
|
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 = []
|
|
h.updateRows = [{ id: 'capa-1' }]
|
|
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' }),
|
|
)
|
|
})
|
|
it('409 on a lost race: no evidence insert, no audit', async () => {
|
|
h.updateRows = []
|
|
const res = await POST(req('done', 1) as never, ctx)
|
|
expect(res.status).toBe(409)
|
|
expect(h.inserts).toHaveLength(0)
|
|
expect(writeAuditLog).not.toHaveBeenCalled()
|
|
})
|
|
it('422 with details and no DB writes when upload fails', async () => {
|
|
vi.mocked(uploadEvidenceFile).mockRejectedValueOnce(new Error('disk full'))
|
|
const res = await POST(req('done', 1) as never, ctx)
|
|
expect(res.status).toBe(422)
|
|
expect((await res.json()).details).toContain('disk full')
|
|
expect(h.inserts).toHaveLength(0)
|
|
expect(h.updates).toHaveLength(0)
|
|
expect(writeAuditLog).not.toHaveBeenCalled()
|
|
})
|
|
it('200 for an hse non-owner, evidence uploadedBy is the hse user', async () => {
|
|
h.session = { sub: 'hse-1', role: 'hse' }
|
|
const res = await POST(req('done', 1) as never, ctx)
|
|
expect(res.status).toBe(200)
|
|
expect(h.inserts[0]).toEqual([expect.objectContaining({ uploadedBy: 'hse-1' })])
|
|
})
|
|
it('422 for 6 files at route level', async () => {
|
|
const res = await POST(req('done', 6) as never, ctx)
|
|
expect(res.status).toBe(422)
|
|
expect((await res.json()).details).toContain('At most 5 photos are allowed')
|
|
})
|
|
})
|