251 lines
7.2 KiB
TypeScript
251 lines
7.2 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('B2B artwork approvals', () => {
|
|
let ctx: IdentityApp;
|
|
const auth = { type: 'bearer' as const };
|
|
const url = '/api/v1/b2b/artworks';
|
|
|
|
async function account(verified = true) {
|
|
const created = await ctx
|
|
.api()
|
|
.post('/api/v1/b2b/accounts')
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
code: `AR-${randomUUID().slice(0, 8)}`,
|
|
legalName: 'Artwork Partner Limited',
|
|
tradingName: null,
|
|
registrationNumber: randomUUID(),
|
|
taxIdentifier: null,
|
|
countryCode: 'IN',
|
|
contactEmail: 'artwork@example.com',
|
|
contactPhone: null,
|
|
billingLine1: '10 Market Lane',
|
|
billingCity: 'Pune',
|
|
billingRegion: 'Maharashtra',
|
|
billingPostalCode: '411001',
|
|
})
|
|
.expect(201);
|
|
const id = created.body.id as string;
|
|
if (verified) {
|
|
await ctx
|
|
.api()
|
|
.post(`/api/v1/b2b/accounts/${id}/submit`)
|
|
.auth(ctx.token, auth)
|
|
.send({ idempotencyKey: randomUUID() })
|
|
.expect(201);
|
|
const reviewer = await secondActor(ctx, true, ['b2b.verify']);
|
|
await ctx
|
|
.api()
|
|
.post(`/api/v1/b2b/accounts/${id}/decision`)
|
|
.auth(reviewer.token, auth)
|
|
.send({
|
|
decision: 'APPROVED',
|
|
note: 'Registration checked',
|
|
idempotencyKey: randomUUID(),
|
|
})
|
|
.expect(201);
|
|
}
|
|
return id;
|
|
}
|
|
async function artwork() {
|
|
const accountId = await account();
|
|
const created = await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
accountId,
|
|
name: 'Seasonal gift branding',
|
|
placement: 'PACKAGING',
|
|
brief: 'Use the supplied mark on a matte kraft box',
|
|
})
|
|
.expect(201);
|
|
return { id: created.body.id as string, accountId };
|
|
}
|
|
const upload = (id: string, token: string, bytes = pdf) =>
|
|
ctx
|
|
.api()
|
|
.post(`${url}/${id}/versions`)
|
|
.auth(token, auth)
|
|
.attach('file', bytes, {
|
|
filename: 'brand.pdf',
|
|
contentType: 'application/pdf',
|
|
});
|
|
|
|
beforeAll(async () => {
|
|
ctx = await identityApp();
|
|
}, 60000);
|
|
afterAll(async () => {
|
|
await ctx?.close();
|
|
});
|
|
beforeEach(async () => {
|
|
await ctx.clearLimits();
|
|
});
|
|
|
|
it('requires verification and keeps file bytes out of metadata responses', async () => {
|
|
const unverified = await account(false);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
accountId: unverified,
|
|
name: 'Unapproved brand',
|
|
placement: 'LABEL',
|
|
brief: 'Partner label artwork',
|
|
})
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('B2B_ARTWORK_ACCOUNT_UNVERIFIED');
|
|
const project = await artwork();
|
|
const first = await upload(project.id, ctx.token).expect(201);
|
|
expect(first.body).toMatchObject({
|
|
version: 1,
|
|
filename: 'brand.pdf',
|
|
size: pdf.length,
|
|
decision: null,
|
|
});
|
|
expect(first.body.content).toBeUndefined();
|
|
const detail = await ctx
|
|
.api()
|
|
.get(`${url}/${project.id}`)
|
|
.auth(ctx.token, auth)
|
|
.expect(200);
|
|
expect(detail.body.versions[0].content).toBeUndefined();
|
|
expect((await upload(project.id, ctx.token).expect(409)).body.code).toBe(
|
|
'B2B_ARTWORK_VERSION_PENDING',
|
|
);
|
|
const listed = await ctx
|
|
.api()
|
|
.get(`${url}?accountId=${project.accountId}`)
|
|
.auth(ctx.token, auth)
|
|
.expect(200);
|
|
expect(listed.body.map((item: { id: string }) => item.id)).toContain(
|
|
project.id,
|
|
);
|
|
});
|
|
|
|
it('requires independent review, protects pending/rejected files, and versions revisions', async () => {
|
|
const project = await artwork();
|
|
const first = await upload(project.id, ctx.token).expect(201);
|
|
const versionUrl = `/api/v1/b2b/artwork-versions/${first.body.id}`;
|
|
const reader = await secondActor(ctx, true, ['b2b.artwork.read']);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.get(`${versionUrl}/download`)
|
|
.auth(reader.token, auth)
|
|
.expect(403)
|
|
).body.code,
|
|
).toBe('B2B_ARTWORK_DOWNLOAD_PENDING');
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(`${versionUrl}/decision`)
|
|
.auth(ctx.token, auth)
|
|
.send({ outcome: 'APPROVED', note: 'Brand mark is legible' })
|
|
.expect(403)
|
|
).body.code,
|
|
).toBe('B2B_ARTWORK_SELF_REVIEW');
|
|
const reviewer = await secondActor(ctx, true, [
|
|
'b2b.artwork.read',
|
|
'b2b.artwork.review',
|
|
]);
|
|
const rejected = await ctx
|
|
.api()
|
|
.post(`${versionUrl}/decision`)
|
|
.auth(reviewer.token, auth)
|
|
.send({ outcome: 'REJECTED', note: 'Brand mark is too small' })
|
|
.expect(201);
|
|
expect(rejected.body.decision.outcome).toBe('REJECTED');
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.get(`${versionUrl}/download`)
|
|
.auth(reader.token, auth)
|
|
.expect(403)
|
|
).body.code,
|
|
).toBe('B2B_ARTWORK_DOWNLOAD_REJECTED');
|
|
const second = await upload(
|
|
project.id,
|
|
ctx.token,
|
|
Buffer.from('%PDF-1.5\n%%EOF'),
|
|
).expect(201);
|
|
expect(second.body.version).toBe(2);
|
|
const secondUrl = `/api/v1/b2b/artwork-versions/${second.body.id}`;
|
|
await ctx
|
|
.api()
|
|
.post(`${secondUrl}/decision`)
|
|
.auth(reviewer.token, auth)
|
|
.send({ outcome: 'APPROVED', note: 'Corrected brand mark' })
|
|
.expect(201);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.get(`${secondUrl}/download`)
|
|
.auth(reader.token, auth)
|
|
.expect(200)
|
|
).body,
|
|
).toEqual(Buffer.from('%PDF-1.5\n%%EOF'));
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(`${secondUrl}/decision`)
|
|
.auth(reviewer.token, auth)
|
|
.send({ outcome: 'REJECTED', note: 'Changed mind' })
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('B2B_ARTWORK_DECISION_EXISTS');
|
|
});
|
|
|
|
it('enforces tenant scope and permission checks', async () => {
|
|
const project = await artwork();
|
|
const first = await upload(project.id, ctx.token).expect(201);
|
|
const outsider = await secondActor(ctx, false, [
|
|
'b2b.artwork.read',
|
|
'b2b.artwork.review',
|
|
]);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.get(`${url}/${project.id}`)
|
|
.auth(outsider.token, auth)
|
|
.expect(404)
|
|
).body.code,
|
|
).toBe('B2B_ARTWORK_NOT_FOUND');
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.get(`/api/v1/b2b/artwork-versions/${first.body.id}`)
|
|
.auth(outsider.token, auth)
|
|
.expect(404)
|
|
).body.code,
|
|
).toBe('B2B_ARTWORK_VERSION_NOT_FOUND');
|
|
const noPermission = await secondActor(ctx);
|
|
await ctx.api().get(url).auth(noPermission.token, auth).expect(403);
|
|
await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(noPermission.token, auth)
|
|
.send({
|
|
accountId: project.accountId,
|
|
name: 'Restricted brand',
|
|
placement: 'LABEL',
|
|
brief: 'Restricted artwork',
|
|
})
|
|
.expect(403);
|
|
});
|
|
});
|