168 lines
5.1 KiB
TypeScript
168 lines
5.1 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { identityApp, type IdentityApp } from './helpers/identity-app';
|
|
import { secondActor } from './helpers/commerce';
|
|
|
|
describe('seasonal campaign drafts', () => {
|
|
let ctx: IdentityApp;
|
|
const auth = { type: 'bearer' as const };
|
|
const url = '/api/v1/campaigns';
|
|
beforeAll(async () => {
|
|
ctx = await identityApp(true);
|
|
}, 60000);
|
|
afterAll(async () => {
|
|
await ctx?.close();
|
|
});
|
|
beforeEach(async () => {
|
|
await ctx.clearLimits();
|
|
});
|
|
async function input(kind: 'COLLECTION' | 'CATEGORY' = 'COLLECTION') {
|
|
const collection = await ctx
|
|
.api()
|
|
.post('/api/v1/catalog-groups')
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
name: 'Seasonal candles',
|
|
slug: 'collection-' + randomUUID(),
|
|
kind,
|
|
})
|
|
.expect(201);
|
|
return {
|
|
slug: 'campaign-' + randomUUID(),
|
|
title: 'Seasonal candles',
|
|
summary: 'Explore the collection',
|
|
collectionId: collection.body.id as string,
|
|
startsAt: '2030-10-01T00:00:00Z',
|
|
endsAt: '2030-10-02T00:00:00Z',
|
|
idempotencyKey: randomUUID(),
|
|
};
|
|
}
|
|
const create = (body: Awaited<ReturnType<typeof input>>, token = ctx.token) =>
|
|
ctx.api().post(url).auth(token, auth).send(body);
|
|
|
|
it('creates private drafts, replays a single intent, and rejects conflicting keys and slugs', async () => {
|
|
const body = await input();
|
|
const [first, retry] = await Promise.all([
|
|
create(body).expect(201),
|
|
create(body).expect(201),
|
|
]);
|
|
expect(first.body).toMatchObject({
|
|
state: 'DRAFT',
|
|
status: 'DRAFT',
|
|
version: 0,
|
|
slug: body.slug,
|
|
});
|
|
expect(retry.body.id).toBe(first.body.id);
|
|
expect(
|
|
(await create({ ...body, title: 'Different title' }).expect(409)).body
|
|
.code,
|
|
).toBe('CAMPAIGN_REPLAY_CONFLICT');
|
|
expect(
|
|
(await create({ ...body, idempotencyKey: randomUUID() }).expect(409)).body
|
|
.code,
|
|
).toBe('CAMPAIGN_SLUG_EXISTS');
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.get(`${url}/${first.body.id}`)
|
|
.auth(ctx.token, auth)
|
|
.expect(200)
|
|
).body.id,
|
|
).toBe(first.body.id);
|
|
const list = await ctx
|
|
.api()
|
|
.get(`${url}?state=DRAFT&limit=1`)
|
|
.auth(ctx.token, auth)
|
|
.expect(200);
|
|
expect(list.body).toHaveLength(1);
|
|
expect(
|
|
await ctx.db.auditEvent.count({
|
|
where: { targetId: first.body.id, action: 'campaign.created' },
|
|
}),
|
|
).toBe(1);
|
|
});
|
|
|
|
it('edits only draft terms with optimistic concurrency and no-op audit suppression', async () => {
|
|
const body = await input();
|
|
const created = await create(body).expect(201);
|
|
const path = `${url}/${created.body.id}`;
|
|
await ctx
|
|
.api()
|
|
.patch(path)
|
|
.auth(ctx.token, auth)
|
|
.send({ version: 0, title: body.title })
|
|
.expect(200);
|
|
const changes = await Promise.all([
|
|
ctx
|
|
.api()
|
|
.patch(path)
|
|
.auth(ctx.token, auth)
|
|
.send({ version: 0, title: 'New title' }),
|
|
ctx
|
|
.api()
|
|
.patch(path)
|
|
.auth(ctx.token, auth)
|
|
.send({ version: 0, title: 'Other title' }),
|
|
]);
|
|
expect(changes.map((response) => response.status).sort()).toEqual([
|
|
200, 409,
|
|
]);
|
|
const current = (
|
|
await ctx.api().get(path).auth(ctx.token, auth).expect(200)
|
|
).body;
|
|
expect(current.version).toBe(1);
|
|
await ctx
|
|
.api()
|
|
.patch(path)
|
|
.auth(ctx.token, auth)
|
|
.send({ version: 1, endsAt: '2030-09-01T00:00:00Z' })
|
|
.expect(400);
|
|
expect((await create(body).expect(201)).body.title).toBe(current.title);
|
|
expect(
|
|
await ctx.db.auditEvent.count({
|
|
where: { targetId: created.body.id, action: 'campaign.updated' },
|
|
}),
|
|
).toBe(1);
|
|
});
|
|
|
|
it('enforces permissions, organization scope, collection type and ownership validation', async () => {
|
|
const body = await input();
|
|
const created = await create(body).expect(201);
|
|
await ctx.api().get(url).expect(401);
|
|
const noPermission = await secondActor(ctx);
|
|
await create(body, noPermission.token).expect(403);
|
|
await ctx.api().get(url).auth(noPermission.token, auth).expect(403);
|
|
const reader = await secondActor(ctx, true, ['campaigns.read']);
|
|
await ctx.api().get(url).auth(reader.token, auth).expect(200);
|
|
await create(body, reader.token).expect(403);
|
|
const outsider = await secondActor(ctx, false, [
|
|
'campaigns.read',
|
|
'campaigns.manage',
|
|
]);
|
|
await ctx
|
|
.api()
|
|
.get(`${url}/${created.body.id}`)
|
|
.auth(outsider.token, auth)
|
|
.expect(404);
|
|
await create(
|
|
{ ...body, idempotencyKey: randomUUID() },
|
|
outsider.token,
|
|
).expect(404);
|
|
await create(await input('CATEGORY')).expect(404);
|
|
await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(ctx.token, auth)
|
|
.send({ ...body, organizationId: outsider.organizationId })
|
|
.expect(400);
|
|
await ctx
|
|
.api()
|
|
.patch(`${url}/${created.body.id}`)
|
|
.auth(ctx.token, auth)
|
|
.send({ version: 0, slug: 'changed-slug' })
|
|
.expect(400);
|
|
const docs = await ctx.api().get('/api/docs-json').expect(200);
|
|
expect(docs.body.paths[url]).toBeDefined();
|
|
});
|
|
});
|