manicanldes-backend/test/blog-publishing.spec.ts

219 lines
7.8 KiB
TypeScript

import { randomUUID } from 'node:crypto';
import { identityApp, type IdentityApp } from './helpers/identity-app';
import { secondActor } from './helpers/commerce';
describe('versioned blog drafts and public publication snapshots', () => {
let ctx: IdentityApp;
beforeAll(async () => {
ctx = await identityApp();
});
afterAll(async () => {
await ctx.close();
});
beforeEach(async () => {
await ctx.clearLimits();
});
const input = () => ({
slug: 'candle-' + randomUUID(),
title: 'Candle care',
summary: 'How to care for a candle.',
body: 'Trim the wick.\nLet the wax cool.',
idempotencyKey: randomUUID(),
});
const create = (body = input(), token = ctx.token) =>
ctx
.api()
.post('/api/v1/blog-articles')
.auth(token, { type: 'bearer' })
.send(body);
const publish = (id: string, version: number, token = ctx.token) =>
ctx
.api()
.post('/api/v1/blog-articles/' + id + '/publication')
.auth(token, { type: 'bearer' })
.send({ version });
const publicGet = (slug: string) =>
ctx
.api()
.get('/api/v1/storefront/' + ctx.owner.organizationId + '/blog/' + slug);
it('keeps drafts private, freezes a reviewed snapshot and republishes changes at the same slug', async () => {
const values = input();
const draft = await create(values).expect(201);
await publicGet(values.slug).expect(404);
const first = await publish(draft.body.id, 0).expect(201);
expect(first.body).toMatchObject({
version: 1,
publicationVersion: 0,
state: 'PUBLISHED',
});
const live = await publicGet(values.slug).expect(200);
expect(live.body).toMatchObject({
slug: values.slug,
title: values.title,
summary: values.summary,
body: values.body,
seo: { title: values.title, description: values.summary },
});
expect(JSON.stringify(live.body)).not.toMatch(
/requestHash|idempotencyKey|createdById|publishedById|organizationId|version/,
);
const feed = await ctx
.api()
.get('/api/v1/storefront/' + ctx.owner.organizationId + '/blog')
.query({ limit: 1 })
.expect(200);
expect(feed.body[0]).toMatchObject({
slug: values.slug,
title: values.title,
});
expect(feed.body[0]).not.toHaveProperty('body');
await expect(
ctx.executeSql(
`UPDATE blog_articles SET version = version + 1, publication_version = version, published_content = '{"title":"Leaked"}'::jsonb WHERE id = '${draft.body.id}'`,
),
).rejects.toThrow(/reviewed draft/);
const editor = await secondActor(ctx, true, ['blog.manage']);
await ctx
.api()
.patch('/api/v1/blog-articles/' + draft.body.id)
.auth(editor.token, { type: 'bearer' })
.send({ version: 1, title: 'Updated care', body: 'Cool before moving.' })
.expect(200);
expect((await publicGet(values.slug).expect(200)).body).toEqual(live.body);
await publish(draft.body.id, 2, editor.token).expect(403);
expect((await publish(draft.body.id, 1).expect(409)).body.code).toBe(
'BLOG_CHANGED',
);
await publish(draft.body.id, 2).expect(201);
const updated = await publicGet(values.slug).expect(200);
expect(updated.body).toMatchObject({
title: 'Updated care',
body: 'Cool before moving.',
});
await publish(draft.body.id, 0).expect(409);
const repeat = await publish(draft.body.id, 2).expect(201);
expect(repeat.body.version).toBe(3);
expect(
await ctx.db.auditEvent.count({
where: { targetId: draft.body.id, action: 'blog.published' },
}),
).toBe(2);
});
it('makes creation retry-safe and protects versioned edits and reserved slugs', async () => {
const values = input();
const draft = await create(values).expect(201);
expect((await create(values).expect(201)).body.id).toBe(draft.body.id);
expect(
(await create({ ...values, title: 'Changed' }).expect(409)).body.code,
).toBe('IDEMPOTENCY_CONFLICT');
await create({ ...values, idempotencyKey: randomUUID() }).expect(409);
await ctx
.api()
.patch('/api/v1/blog-articles/' + draft.body.id)
.auth(ctx.token, { type: 'bearer' })
.send({ version: 1, title: 'Changed' })
.expect(409);
const results = await Promise.all([
publish(draft.body.id, 0),
ctx
.api()
.patch('/api/v1/blog-articles/' + draft.body.id)
.auth(ctx.token, { type: 'bearer' })
.send({ version: 0, title: 'Revised' }),
]);
expect(results.map((r) => r.status).filter((s) => s < 300)).toHaveLength(1);
expect(results.map((r) => r.status)).toContain(409);
});
it('withdraws immediately and cannot edit, republish or delete withdrawn evidence', async () => {
const values = input();
const draft = await create(values).expect(201);
await publish(draft.body.id, 0).expect(201);
const withdraw = (reason: string) =>
ctx
.api()
.post('/api/v1/blog-articles/' + draft.body.id + '/withdrawal')
.auth(ctx.token, { type: 'bearer' })
.send({ reason });
await withdraw('Editorial correction needed').expect(201);
await withdraw('Editorial correction needed').expect(201);
await withdraw('Another reason').expect(409);
await publicGet(values.slug).expect(404);
const list = await ctx
.api()
.get('/api/v1/storefront/' + ctx.owner.organizationId + '/blog')
.expect(200);
expect(list.body.some((article: any) => article.slug === values.slug)).toBe(
false,
);
await publish(draft.body.id, 1).expect(409);
await ctx
.api()
.patch('/api/v1/blog-articles/' + draft.body.id)
.auth(ctx.token, { type: 'bearer' })
.send({ version: 2, title: 'Changed' })
.expect(409);
await expect(
ctx.executeSql(`DELETE FROM blog_articles WHERE id = '${draft.body.id}'`),
).rejects.toThrow(/deleted/);
expect(
await ctx.db.auditEvent.count({
where: { targetId: draft.body.id, action: 'blog.withdrawn' },
}),
).toBe(1);
});
it('enforces scope, separate editorial permissions and plain text input', async () => {
const values = input();
const draft = await create(values).expect(201);
const customer = await secondActor(ctx);
await create(input(), customer.token).expect(403);
await ctx
.api()
.get('/api/v1/blog-articles')
.auth(customer.token, { type: 'bearer' })
.expect(403);
const foreign = await secondActor(ctx, false, [
'blog.read',
'blog.manage',
'blog.publish',
]);
await ctx
.api()
.get('/api/v1/blog-articles/' + draft.body.id)
.auth(foreign.token, { type: 'bearer' })
.expect(404);
await publish(draft.body.id, 0, foreign.token).expect(404);
await create({ ...input(), body: '<script>bad()</script>' }).expect(400);
await create({ ...input(), body: 'x'.repeat(20001) }).expect(400);
await ctx
.api()
.patch('/api/v1/blog-articles/' + draft.body.id)
.auth(ctx.token, { type: 'bearer' })
.send({ version: 0 })
.expect(400);
await ctx
.api()
.post('/api/v1/blog-articles/' + draft.body.id + '/publication')
.auth(ctx.token, { type: 'bearer' })
.send({ version: 0, publishedContent: {} })
.expect(400);
const staff = await ctx
.api()
.get('/api/v1/blog-articles/' + draft.body.id)
.auth(ctx.token, { type: 'bearer' })
.expect(200);
expect(staff.body).toMatchObject({
state: 'DRAFT',
version: 0,
publishedContent: null,
});
await ctx
.api()
.get('/api/v1/blog-articles')
.auth(ctx.token, { type: 'bearer' })
.query({ limit: 1 })
.expect(200);
await ctx.api().get('/api/v1/blog-articles').expect(401);
await ctx.api().get('/api/v1/storefront/not-a-uuid/blog').expect(400);
});
});