manicanldes-backend/test/supplier-documents.spec.ts

230 lines
6.8 KiB
TypeScript

import { randomUUID } from 'node:crypto';
import { identityApp, type IdentityApp } from './helpers/identity-app';
import { secondActor } from './helpers/commerce';
const pdf = Buffer.from('%PDF-1.4\n1 0 obj\n<<>>\nendobj\n%%EOF');
describe('supplier document API', () => {
let ctx: IdentityApp;
const auth = { type: 'bearer' as const };
beforeAll(async () => {
ctx = await identityApp();
}, 60000);
afterAll(async () => {
await ctx?.close();
});
beforeEach(async () => {
await ctx.clearLimits();
});
async function supplier() {
const code = randomUUID().slice(0, 8);
return (
await ctx
.api()
.post('/api/v1/suppliers')
.auth(ctx.token, auth)
.send({
name: `Supplier ${code}`,
code: `S-${code}`,
contactName: 'Asha Shah',
email: `${code}@example.com`,
phone: '+919876543210',
})
.expect(201)
).body as { id: string };
}
async function document() {
const source = await supplier();
const path = `/api/v1/suppliers/${source.id}/documents`;
const created = await ctx
.api()
.post(path)
.auth(ctx.token, auth)
.send({
code: 'tax-cert',
kind: 'TAX_CERTIFICATE',
title: 'Supplier tax certificate',
})
.expect(201);
return { supplierId: source.id, id: created.body.id as string, path };
}
const upload = (id: string, token: string, bytes = pdf) =>
ctx
.api()
.post(`/api/v1/supplier-documents/${id}/versions`)
.auth(token, auth)
.attach('file', bytes, {
filename: 'certificate.pdf',
contentType: 'application/pdf',
});
it('keeps bytes private, versions immutable, and requires another reviewer', async () => {
const source = await document();
const first = await upload(source.id, ctx.token).expect(201);
expect(first.body).toMatchObject({
version: 1,
size: pdf.length,
filename: 'certificate.pdf',
decision: null,
});
expect(first.body.content).toBeUndefined();
const detail = await ctx
.api()
.get(`/api/v1/supplier-documents/${source.id}`)
.auth(ctx.token, auth)
.expect(200);
expect(detail.body.versions[0].content).toBeUndefined();
const reader = await secondActor(ctx, true, ['supplier_document.read']);
const download = `/api/v1/supplier-document-versions/${first.body.id}/download`;
expect(
(await ctx.api().get(download).auth(reader.token, auth).expect(403)).body
.code,
).toBe('DOCUMENT_PENDING_REVIEW');
const decide = `/api/v1/supplier-document-versions/${first.body.id}/decision`;
expect(
(
await ctx
.api()
.post(decide)
.auth(ctx.token, auth)
.send({ outcome: 'APPROVED' })
.expect(403)
).body.code,
).toBe('DOCUMENT_SELF_VALIDATION_DENIED');
const validator = await secondActor(ctx, true, [
'supplier_document.read',
'supplier_document.validate',
]);
const approved = await ctx
.api()
.post(decide)
.auth(validator.token, auth)
.send({ outcome: 'APPROVED' })
.expect(201);
expect(approved.body.decision.outcome).toBe('APPROVED');
expect(
(await ctx.api().get(download).auth(reader.token, auth).expect(200)).body,
).toEqual(pdf);
expect(
(
await ctx
.api()
.post(decide)
.auth(validator.token, auth)
.send({ outcome: 'REJECTED', reason: 'Again' })
.expect(409)
).body.code,
).toBe('DOCUMENT_DECISION_EXISTS');
const second = await upload(
source.id,
ctx.token,
Buffer.from('%PDF-1.5\n%%EOF'),
).expect(201);
expect(second.body.version).toBe(2);
const reject = `/api/v1/supplier-document-versions/${second.body.id}/decision`;
expect(
(
await ctx
.api()
.post(reject)
.auth(validator.token, auth)
.send({ outcome: 'REJECTED' })
.expect(400)
).body.code,
).toBe('DOCUMENT_REJECTION_REASON_REQUIRED');
await ctx
.api()
.post(reject)
.auth(validator.token, auth)
.send({ outcome: 'REJECTED', reason: 'Expired' })
.expect(201);
expect(
(
await ctx
.api()
.get(`/api/v1/supplier-document-versions/${second.body.id}/download`)
.auth(reader.token, auth)
.expect(403)
).body.code,
).toBe('DOCUMENT_REJECTED');
expect(
(await ctx.api().get(source.path).auth(reader.token, auth).expect(200))
.body,
).toHaveLength(1);
});
it('rejects unsafe uploads and duplicate document codes', async () => {
const source = await document();
expect(
(
await ctx
.api()
.post(source.path)
.auth(ctx.token, auth)
.send({
code: 'TAX-CERT',
kind: 'TAX_CERTIFICATE',
title: 'Duplicate',
})
.expect(409)
).body.code,
).toBe('DOCUMENT_CODE_CONFLICT');
expect(
(await upload(source.id, ctx.token, Buffer.from('not a pdf')).expect(400))
.body.code,
).toBe('DOCUMENT_FILE_INVALID');
expect(
(
await ctx
.api()
.post(`/api/v1/supplier-documents/${source.id}/versions`)
.auth(ctx.token, auth)
.expect(400)
).body.code,
).toBe('DOCUMENT_FILE_REQUIRED');
});
it('separates permissions and organization scope', async () => {
const source = await document();
const reader = await secondActor(ctx, true, ['supplier_document.read']);
await ctx.api().get(source.path).auth(reader.token, auth).expect(200);
await ctx
.api()
.post(source.path)
.auth(reader.token, auth)
.send({ code: 'NEW', kind: 'OTHER', title: 'No access' })
.expect(403);
const foreign = await secondActor(ctx, false, [
'supplier_document.read',
'supplier_document.manage',
]);
expect(
(
await ctx
.api()
.get(`/api/v1/supplier-documents/${source.id}`)
.auth(foreign.token, auth)
.expect(404)
).body.code,
).toBe('DOCUMENT_NOT_FOUND');
await upload(source.id, foreign.token).expect(404);
});
it('caps version history without replacing prior bytes', async () => {
const source = await document();
for (let version = 1; version <= 20; version += 1) {
expect(
(await upload(source.id, ctx.token).expect(201)).body.version,
).toBe(version);
}
expect((await upload(source.id, ctx.token).expect(409)).body.code).toBe(
'DOCUMENT_VERSION_LIMIT',
);
const detail = await ctx
.api()
.get(`/api/v1/supplier-documents/${source.id}`)
.auth(ctx.token, auth)
.expect(200);
expect(detail.body.versions).toHaveLength(20);
});
});