38 lines
1.4 KiB
TypeScript
38 lines
1.4 KiB
TypeScript
import {
|
|
inspectPdf,
|
|
MAX_DOCUMENT_BYTES,
|
|
verifiedContent,
|
|
} from '../src/procurement/supplier-document-file';
|
|
|
|
const pdf = Buffer.from('%PDF-1.4\n1 0 obj\n<<>>\nendobj\n%%EOF');
|
|
const file = (
|
|
buffer: Buffer,
|
|
name = 'certificate.pdf',
|
|
mimetype = 'application/pdf',
|
|
) => ({ buffer, size: buffer.length, originalname: name, mimetype });
|
|
|
|
describe('supplier document file policy', () => {
|
|
it('accepts bounded PDFs and verifies their digest', () => {
|
|
const inspected = inspectPdf(file(pdf));
|
|
expect(inspected.size).toBe(pdf.length);
|
|
expect(verifiedContent(inspected.content, inspected.sha256)).toEqual(pdf);
|
|
});
|
|
it('rejects invalid bytes, names, and oversize files', () => {
|
|
expect(() => inspectPdf()).toThrow(
|
|
expect.objectContaining({ code: 'DOCUMENT_FILE_REQUIRED' }),
|
|
);
|
|
expect(() => inspectPdf(file(Buffer.from('hello')))).toThrow(
|
|
expect.objectContaining({ code: 'DOCUMENT_FILE_INVALID' }),
|
|
);
|
|
expect(() => inspectPdf(file(pdf, '../bad.pdf'))).toThrow(
|
|
expect.objectContaining({ code: 'DOCUMENT_FILENAME_INVALID' }),
|
|
);
|
|
expect(() =>
|
|
inspectPdf(file(Buffer.alloc(MAX_DOCUMENT_BYTES + 1))),
|
|
).toThrow(expect.objectContaining({ code: 'DOCUMENT_FILE_TOO_LARGE' }));
|
|
expect(() => verifiedContent(pdf, '0'.repeat(64))).toThrow(
|
|
expect.objectContaining({ code: 'DOCUMENT_INTEGRITY_FAILURE' }),
|
|
);
|
|
});
|
|
});
|