manicanldes-backend/test/b2b-samples.spec.ts

297 lines
8.0 KiB
TypeScript

import { randomUUID } from 'node:crypto';
import { identityApp, type IdentityApp } from './helpers/identity-app';
import { secondActor, seedProduct } from './helpers/commerce';
describe('B2B samples', () => {
let ctx: IdentityApp;
const auth = { type: 'bearer' as const };
const url = '/api/v1/b2b/samples';
async function account(verified = true) {
const created = await ctx
.api()
.post('/api/v1/b2b/accounts')
.auth(ctx.token, auth)
.send({
code: `SM-${randomUUID().slice(0, 8)}`,
legalName: 'Sample Partner Limited',
tradingName: null,
registrationNumber: randomUUID(),
taxIdentifier: null,
countryCode: 'IN',
contactEmail: 'samples@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;
}
const input = (accountId: string, variantId: string) => ({
accountId,
variantId,
quantity: 4,
purpose: 'Fragrance evaluation',
idempotencyKey: randomUUID(),
});
const decision = (kind: 'APPROVED' | 'REJECTED') => ({
decision: kind,
note: 'Sample request reviewed',
idempotencyKey: randomUUID(),
});
beforeAll(async () => {
ctx = await identityApp();
}, 60000);
afterAll(async () => {
await ctx?.close();
});
beforeEach(async () => {
await ctx.clearLimits();
});
it('requires verified account and published variant, snapshots the item, and replays safely', async () => {
const unverified = await account(false);
const { variant } = await seedProduct(ctx, true);
expect(
(
await ctx
.api()
.post(url)
.auth(ctx.token, auth)
.send(input(unverified, variant.id))
.expect(409)
).body.code,
).toBe('B2B_SAMPLE_ACCOUNT_UNVERIFIED');
const accountId = await account();
const draft = await seedProduct(ctx);
expect(
(
await ctx
.api()
.post(url)
.auth(ctx.token, auth)
.send(input(accountId, draft.variant.id))
.expect(409)
).body.code,
).toBe('B2B_SAMPLE_VARIANT_INELIGIBLE');
const request = input(accountId, variant.id);
const created = await ctx
.api()
.post(url)
.auth(ctx.token, auth)
.send(request)
.expect(201);
expect(created.body).toMatchObject({
status: 'REQUESTED',
sku: variant.sku,
variantName: variant.name,
quantity: 4,
events: [],
});
expect(
(
await ctx
.api()
.post(url)
.auth(ctx.token, auth)
.send(request)
.expect(201)
).body.id,
).toBe(created.body.id);
expect(
(
await ctx
.api()
.post(url)
.auth(ctx.token, auth)
.send({ ...request, quantity: 5 })
.expect(409)
).body.code,
).toBe('B2B_SAMPLE_REQUEST_REPLAY_CONFLICT');
const listed = await ctx
.api()
.get(`${url}?accountId=${accountId}&status=REQUESTED`)
.auth(ctx.token, auth)
.expect(200);
expect(listed.body.map((row: { id: string }) => row.id)).toContain(
created.body.id,
);
});
it('requires independent review and records dispatch exactly once', async () => {
const accountId = await account();
const { variant } = await seedProduct(ctx, true);
const created = await ctx
.api()
.post(url)
.auth(ctx.token, auth)
.send(input(accountId, variant.id))
.expect(201);
const id = created.body.id as string;
const approve = decision('APPROVED');
expect(
(
await ctx
.api()
.post(`${url}/${id}/decision`)
.auth(ctx.token, auth)
.send(approve)
.expect(403)
).body.code,
).toBe('B2B_SAMPLE_SELF_REVIEW');
const reviewer = await secondActor(ctx, true, [
'b2b.sample.review',
'b2b.sample.dispatch',
]);
const approved = await ctx
.api()
.post(`${url}/${id}/decision`)
.auth(reviewer.token, auth)
.send(approve)
.expect(201);
expect(approved.body).toMatchObject({
status: 'APPROVED',
events: [{ kind: 'APPROVED', sequence: 1 }],
});
expect(
(
await ctx
.api()
.post(`${url}/${id}/decision`)
.auth(reviewer.token, auth)
.send(approve)
.expect(201)
).body.events,
).toHaveLength(1);
const dispatch = {
dispatchReference: 'MANUAL-123',
note: 'Handed to courier',
idempotencyKey: randomUUID(),
};
const sent = await ctx
.api()
.post(`${url}/${id}/dispatch`)
.auth(reviewer.token, auth)
.send(dispatch)
.expect(201);
expect(sent.body).toMatchObject({
status: 'DISPATCHED',
events: [
{ kind: 'APPROVED' },
{ kind: 'DISPATCHED', sequence: 2, dispatchReference: 'MANUAL-123' },
],
});
expect(
(
await ctx
.api()
.post(`${url}/${id}/dispatch`)
.auth(reviewer.token, auth)
.send(dispatch)
.expect(201)
).body.events,
).toHaveLength(2);
expect(
(
await ctx
.api()
.post(`${url}/${id}/dispatch`)
.auth(reviewer.token, auth)
.send({ ...dispatch, dispatchReference: 'CHANGED' })
.expect(409)
).body.code,
).toBe('B2B_SAMPLE_EVENT_REPLAY_CONFLICT');
});
it('rejects requests, enforces transition state, tenant scope, and permissions', async () => {
const accountId = await account();
const { variant } = await seedProduct(ctx, true);
const created = await ctx
.api()
.post(url)
.auth(ctx.token, auth)
.send(input(accountId, variant.id))
.expect(201);
const id = created.body.id as string;
const reviewer = await secondActor(ctx, true, [
'b2b.sample.review',
'b2b.sample.dispatch',
]);
expect(
(
await ctx
.api()
.post(`${url}/${id}/dispatch`)
.auth(reviewer.token, auth)
.send({
dispatchReference: 'EARLY-1',
note: 'Not approved',
idempotencyKey: randomUUID(),
})
.expect(409)
).body.code,
).toBe('B2B_SAMPLE_DISPATCH_STATE');
const rejected = await ctx
.api()
.post(`${url}/${id}/decision`)
.auth(reviewer.token, auth)
.send(decision('REJECTED'))
.expect(201);
expect(rejected.body).toMatchObject({
status: 'REJECTED',
events: [{ kind: 'REJECTED' }],
});
expect(
(
await ctx
.api()
.post(`${url}/${id}/decision`)
.auth(reviewer.token, auth)
.send(decision('APPROVED'))
.expect(409)
).body.code,
).toBe('B2B_SAMPLE_REVIEW_STATE');
const outsider = await secondActor(ctx, false, ['b2b.sample.read']);
expect(
(
await ctx
.api()
.get(`${url}/${id}`)
.auth(outsider.token, auth)
.expect(404)
).body.code,
).toBe('B2B_SAMPLE_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(input(accountId, variant.id))
.expect(403);
});
});