manicanldes-backend/test/b2b-artwork-file.spec.ts

53 lines
1.9 KiB
TypeScript

import {
inspectArtwork,
MAX_ARTWORK_BYTES,
verifiedArtwork,
} from '../src/b2b/artwork-file';
const pdf = Buffer.from('%PDF-1.4\n1 0 obj\n<<>>\nendobj\n%%EOF');
const png = Buffer.from('89504e470d0a1a0a0000000049454e4400000000', 'hex');
const jpeg = Buffer.from('ffd8ffe00000ffd9', 'hex');
const file = (buffer: Buffer, originalname: string, mimetype: string) => ({
buffer,
size: buffer.length,
originalname,
mimetype,
});
describe('B2B artwork file policy', () => {
it.each([
['brand.pdf', 'application/pdf', pdf],
['brand.png', 'image/png', png],
['brand.jpg', 'image/jpeg', jpeg],
])('accepts and hashes %s', (name, type, bytes) => {
const inspected = inspectArtwork(file(bytes, name, type));
expect(inspected.size).toBe(bytes.length);
expect(verifiedArtwork(inspected.content, inspected.sha256)).toEqual(bytes);
});
it('rejects missing, oversized, misleading, and malformed files', () => {
expect(() => inspectArtwork()).toThrow(
expect.objectContaining({ code: 'B2B_ARTWORK_FILE_REQUIRED' }),
);
expect(() =>
inspectArtwork(
file(Buffer.alloc(MAX_ARTWORK_BYTES + 1), 'large.png', 'image/png'),
),
).toThrow(expect.objectContaining({ code: 'B2B_ARTWORK_FILE_TOO_LARGE' }));
expect(() =>
inspectArtwork(file(pdf, '../bad.pdf', 'application/pdf')),
).toThrow(
expect.objectContaining({ code: 'B2B_ARTWORK_FILENAME_INVALID' }),
);
expect(() => inspectArtwork(file(pdf, 'brand.png', 'image/png'))).toThrow(
expect.objectContaining({ code: 'B2B_ARTWORK_FILE_INVALID' }),
);
expect(() =>
inspectArtwork(file(png, 'brand.png', 'application/pdf')),
).toThrow(expect.objectContaining({ code: 'B2B_ARTWORK_FILE_INVALID' }));
expect(() => verifiedArtwork(pdf, '0'.repeat(64))).toThrow(
expect.objectContaining({ code: 'B2B_ARTWORK_INTEGRITY_FAILURE' }),
);
});
});