222 lines
6.4 KiB
TypeScript
222 lines
6.4 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { identityApp, type IdentityApp } from './helpers/identity-app';
|
|
import { secondActor } from './helpers/commerce';
|
|
|
|
describe('B2B business verification', () => {
|
|
let ctx: IdentityApp;
|
|
const auth = { type: 'bearer' as const };
|
|
const profile = () => ({
|
|
code: `B-${randomUUID().slice(0, 8)}`,
|
|
legalName: 'Candle Partner Private Limited',
|
|
tradingName: null,
|
|
registrationNumber: randomUUID(),
|
|
taxIdentifier: null,
|
|
countryCode: 'IN',
|
|
contactEmail: 'partner@example.com',
|
|
contactPhone: null,
|
|
billingLine1: '12 Market Road',
|
|
billingCity: 'Pune',
|
|
billingRegion: 'Maharashtra',
|
|
billingPostalCode: '411001',
|
|
});
|
|
const create = (input = profile()) =>
|
|
ctx.api().post('/api/v1/b2b/accounts').auth(ctx.token, auth).send(input);
|
|
const route = (id: string, suffix = '') =>
|
|
`/api/v1/b2b/accounts/${id}${suffix}`;
|
|
|
|
beforeAll(async () => {
|
|
ctx = await identityApp();
|
|
}, 60000);
|
|
afterAll(async () => {
|
|
await ctx?.close();
|
|
});
|
|
beforeEach(async () => {
|
|
await ctx.clearLimits();
|
|
});
|
|
|
|
it('creates, lists and scopes profiles; distinguishes duplicate identifiers', async () => {
|
|
const input = profile();
|
|
const account = await create(input).expect(201);
|
|
expect(account.body).toMatchObject({
|
|
code: input.code.toUpperCase(),
|
|
status: 'DRAFT',
|
|
profileVersion: 1,
|
|
});
|
|
expect((await create(input).expect(409)).body.code).toBe(
|
|
'BUSINESS_CODE_CONFLICT',
|
|
);
|
|
expect(
|
|
(
|
|
await create({
|
|
...input,
|
|
code: `B-${randomUUID().slice(0, 8)}`,
|
|
}).expect(409)
|
|
).body.code,
|
|
).toBe('BUSINESS_REGISTRATION_CONFLICT');
|
|
const other = await secondActor(ctx, false, ['b2b.read']);
|
|
await ctx
|
|
.api()
|
|
.get(route(account.body.id))
|
|
.auth(other.token, auth)
|
|
.expect(404);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.get('/api/v1/b2b/accounts')
|
|
.auth(ctx.token, auth)
|
|
.expect(200)
|
|
).body,
|
|
).toEqual(
|
|
expect.arrayContaining([
|
|
expect.objectContaining({ id: account.body.id }),
|
|
]),
|
|
);
|
|
});
|
|
|
|
it('submits an immutable profile snapshot, rejects self-review, and approves with a second reviewer', async () => {
|
|
const account = await create().expect(201);
|
|
const id = account.body.id as string;
|
|
const submission = { idempotencyKey: randomUUID() };
|
|
const submit = () =>
|
|
ctx
|
|
.api()
|
|
.post(route(id, '/submit'))
|
|
.auth(ctx.token, auth)
|
|
.send(submission);
|
|
const submitted = await submit().expect(201);
|
|
expect(submitted.body.profileSnapshot).toMatchObject({
|
|
legalName: account.body.legalName,
|
|
profileVersion: 1,
|
|
});
|
|
expect((await submit().expect(201)).body.id).toBe(submitted.body.id);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.patch(route(id))
|
|
.auth(ctx.token, auth)
|
|
.send({ legalName: 'Changed' })
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('BUSINESS_EDIT_STATE');
|
|
const decision = {
|
|
decision: 'APPROVED',
|
|
note: 'Registry details checked by staff',
|
|
idempotencyKey: randomUUID(),
|
|
};
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(route(id, '/decision'))
|
|
.auth(ctx.token, auth)
|
|
.send(decision)
|
|
.expect(403)
|
|
).body.code,
|
|
).toBe('BUSINESS_SELF_REVIEW');
|
|
const reviewer = await secondActor(ctx, true, ['b2b.verify']);
|
|
const approve = () =>
|
|
ctx
|
|
.api()
|
|
.post(route(id, '/decision'))
|
|
.auth(reviewer.token, auth)
|
|
.send(decision);
|
|
const approved = await approve().expect(201);
|
|
expect((await approve().expect(201)).body.id).toBe(approved.body.id);
|
|
expect(
|
|
(await ctx.api().get(route(id)).auth(ctx.token, auth).expect(200)).body
|
|
.status,
|
|
).toBe('VERIFIED');
|
|
const history = await ctx
|
|
.api()
|
|
.get(route(id, '/events'))
|
|
.auth(ctx.token, auth)
|
|
.expect(200);
|
|
expect(history.body.map((event: { kind: string }) => event.kind)).toEqual([
|
|
'SUBMITTED',
|
|
'APPROVED',
|
|
]);
|
|
});
|
|
|
|
it('allows a rejected profile to be corrected and resubmitted, while enforcing replay payloads', async () => {
|
|
const account = await create().expect(201);
|
|
const id = account.body.id as string;
|
|
const reviewer = await secondActor(ctx, true, ['b2b.verify']);
|
|
await ctx
|
|
.api()
|
|
.post(route(id, '/submit'))
|
|
.auth(ctx.token, auth)
|
|
.send({ idempotencyKey: randomUUID() })
|
|
.expect(201);
|
|
const decision = {
|
|
decision: 'REJECTED',
|
|
note: 'Registration number needs correction',
|
|
idempotencyKey: randomUUID(),
|
|
};
|
|
await ctx
|
|
.api()
|
|
.post(route(id, '/decision'))
|
|
.auth(reviewer.token, auth)
|
|
.send(decision)
|
|
.expect(201);
|
|
const updated = await ctx
|
|
.api()
|
|
.patch(route(id))
|
|
.auth(ctx.token, auth)
|
|
.send({ registrationNumber: randomUUID() })
|
|
.expect(200);
|
|
expect(updated.body).toMatchObject({
|
|
status: 'REJECTED',
|
|
profileVersion: 2,
|
|
});
|
|
const key = randomUUID();
|
|
await ctx
|
|
.api()
|
|
.post(route(id, '/submit'))
|
|
.auth(ctx.token, auth)
|
|
.send({ idempotencyKey: key })
|
|
.expect(201);
|
|
const history = await ctx
|
|
.api()
|
|
.get(route(id, '/events'))
|
|
.auth(ctx.token, auth)
|
|
.expect(200);
|
|
expect(history.body.map((event: { kind: string }) => event.kind)).toEqual([
|
|
'SUBMITTED',
|
|
'REJECTED',
|
|
'SUBMITTED',
|
|
]);
|
|
expect(history.body[2].profileSnapshot.profileVersion).toBe(2);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(route(id, '/decision'))
|
|
.auth(reviewer.token, auth)
|
|
.send({ ...decision, idempotencyKey: key })
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('BUSINESS_REPLAY_CONFLICT');
|
|
});
|
|
|
|
it('requires permissions and rejects malformed profiles without recording them', async () => {
|
|
const reader = await secondActor(ctx, true, ['b2b.read']);
|
|
await ctx
|
|
.api()
|
|
.post('/api/v1/b2b/accounts')
|
|
.auth(reader.token, auth)
|
|
.send(profile())
|
|
.expect(403);
|
|
const before = await ctx.db.businessAccount.count({
|
|
where: { organizationId: ctx.owner.organizationId },
|
|
});
|
|
await create({ ...profile(), legalName: '<script>' }).expect(400);
|
|
expect(
|
|
await ctx.db.businessAccount.count({
|
|
where: { organizationId: ctx.owner.organizationId },
|
|
}),
|
|
).toBe(before);
|
|
});
|
|
});
|