import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import request from 'supertest'; import { hashToken } from '../src/lib/crypto.js'; import { previewContent, siteContent } from '../src/site/content.js'; import { publish } from '../src/site/snapshot.js'; import { harness, SITE } from './support/harness.js'; async function site() { const h = await harness({ linked: false }); const people = { ann: '00000000-0000-4000-8000-00000000000a', ben: '00000000-0000-4000-8000-00000000000b', lee: '00000000-0000-4000-8000-00000000000c' }; const kids = await h.db.ministry.create({ data: { slug: 'kids', name: 'Kids' } }); const youth = await h.db.ministry.create({ data: { slug: 'youth', name: 'Youth' } }); await h.db.websiteUser.create({ data: { id: people.ann, name: 'Ann', email: 'ann@hope.test', role: 'editor' } }); await h.db.websiteUser.create({ data: { id: people.ben, name: 'Ben', email: 'ben@hope.test', role: 'editor' } }); await h.db.websiteUser.create({ data: { id: people.lee, name: 'Lee', email: 'lee@hope.test', role: 'ministry_leader', ministryId: kids.id } }); for (const [k, id] of Object.entries(people)) await h.db.session.create({ data: { tokenHash: hashToken(k), userId: id, expiresAt: new Date(Date.now() + 864e5) } }); const as = (who: keyof typeof people) => (m: 'get' | 'post' | 'put', p: string, b?: object) => { const r = request(h.app)[m](`/api/v1/admin${p}`).set('Cookie', `ccw_session=${who}`).set('Origin', SITE); return b ? r.send(b) : r; }; return { ...h, as, kids, youth }; } describe('two editors at once (§4)', () => { it("doesn't overwrite someone else's save, and says who", async () => { const { as, db } = await site(); const page = await db.page.create({ data: { kind: 'about', slug: 'about', title: 'About' } }); const loaded = (await as('ann')('get', `/pages/${page.id}`)).body.updatedAt; await new Promise((r) => setTimeout(r, 5)); assert.equal((await as('ben')('put', `/pages/${page.id}`, { title: "Ben's title", ifUpdatedAt: loaded })).status, 200); const ann = await as('ann')('put', `/pages/${page.id}`, { title: "Ann's title", ifUpdatedAt: loaded }); assert.equal(ann.status, 409); assert.match(ann.body.error.message, /^Ben changed this/); assert.equal((await as('ann')('get', `/pages/${page.id}`)).body.title, "Ben's title"); // Her own earlier save never blocks her. const fresh = (await as('ann')('get', `/pages/${page.id}`)).body.updatedAt; assert.equal((await as('ann')('put', `/pages/${page.id}`, { title: 'One', ifUpdatedAt: fresh })).status, 200); assert.equal((await as('ann')('put', `/pages/${page.id}`, { title: 'Two', ifUpdatedAt: fresh })).status, 200); }); }); describe("a Ministry leader's events (§10)", () => { it('adds and changes their own ministry\'s events only, and an Editor publishes them', async () => { const { as, kids, youth } = await site(); const startsAt = new Date(Date.now() + 864e5).toISOString(); assert.equal((await as('lee')('post', '/events', { title: 'Youth night', startsAt, ministryId: youth.id })).status, 403); assert.equal((await as('lee')('post', '/events', { title: 'No ministry', startsAt })).status, 403); const mine = await as('lee')('post', '/events', { title: 'Kids party', startsAt, ministryId: kids.id }); assert.equal(mine.status, 200); assert.equal((await as('lee')('put', `/events/${mine.body.id}`, { title: 'Kids party!', startsAt, ministryId: kids.id })).status, 200); assert.equal((await as('lee')('post', `/events/${mine.body.id}/state`, { published: true })).status, 403); const other = await as('ann')('post', '/events', { title: 'Picnic', startsAt }); assert.equal((await as('lee')('put', `/events/${other.body.id}`, { title: 'Mine now', startsAt, ministryId: kids.id })).status, 403); assert.equal((await as('ann')('post', `/events/${mine.body.id}/state`, { published: true })).status, 200); }); }); describe('choosing a template with the church\'s own content', () => { it('previews the content in another template without saving anything', async () => { const { ctx, db, as, app } = await site(); await db.siteDesign.create({ data: { id: 1, template: 'heritage', variants: {}, home: [], style: {} } }); const c = await previewContent(ctx, { asTemplate: 'bold' }); assert.equal(c.mode === 'published' && c.snapshot.design.template, 'bold'); assert.equal((await as('ann')('get', '/design')).body.design.template, 'heritage', 'nothing saved'); const viaHeader = await request(app).get('/api/v1/site/preview').set('Cookie', 'ccw_session=ann').set('x-preview-template', 'clean'); assert.equal(viaHeader.body.snapshot.design.template, 'clean'); assert.equal(viaHeader.body.preview.canPublish, false, 'no publishing from a try-on'); }); }); describe('an address that changed (§6)', () => { it('sends the old address on to the new one once published', async () => { const { ctx, db, as } = await site(); const page = await db.page.create({ data: { kind: 'custom', slug: 'youth-camp', title: 'Youth camp' } }); await as('ann')('put', `/pages/${page.id}`, { slug: 'camp' }); await as('ann')('put', `/pages/${page.id}`, { slug: 'summer-camp' }); await publish(ctx, { id: 'x', role: 'system' }); const c = await siteContent(ctx); assert.deepEqual(c.mode === 'published' && c.snapshot.redirects, { 'youth-camp': '/summer-camp', camp: '/summer-camp' }); // A new page taking an old address wins over its redirect. await db.page.create({ data: { kind: 'custom', slug: 'camp', title: 'Camp info' } }); await publish(ctx, { id: 'x', role: 'system' }); const after = await siteContent(ctx); assert.deepEqual(after.mode === 'published' && after.snapshot.redirects, { 'youth-camp': '/summer-camp' }); }); });