feat(website): church details, settings, audit log, search engine data, safer editing

Church details saved through Accounts' church.update (live at once).
Settings: Search Console and Bing codes, Coming soon line, menu order.
Audit log and recent edits. A made share image, Event / VideoObject /
PodcastEpisode / BreadcrumbList structured data, permanent redirects
from old page addresses. Saves refused when someone else saved in
between; Ministry leaders' events; templates previewed with the
church's own content before choosing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 15:30:27 +02:00
co-authored by Claude Opus 5.5
parent d8ed238042
commit 085265392e
37 changed files with 951 additions and 91 deletions
@@ -0,0 +1,87 @@
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' });
});
});
@@ -0,0 +1,70 @@
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import request from 'supertest';
import { hashToken } from '../src/lib/crypto.js';
import { siteContent } from '../src/site/content.js';
import { harness, SITE } from './support/harness.js';
async function site(role: 'owner' | 'editor' | 'viewer' = 'owner') {
const h = await harness();
const id = '00000000-0000-4000-8000-000000000001';
await h.db.websiteUser.create({ data: { id, name: 'Sam', email: 'sam@hope.test', role } });
await h.db.session.create({ data: { tokenHash: hashToken('t'), userId: id, expiresAt: new Date(Date.now() + 864e5) } });
const go = (m: 'get' | 'post' | 'put', p: string, b?: object) => {
const r = request(h.app)[m](`/api/v1/admin${p}`).set('Cookie', 'ccw_session=t').set('Origin', SITE);
return b ? r.send(b) : r;
};
return { ...h, go, id };
}
describe("the church's shared details, saved through Accounts (decision 10)", () => {
it('sends only what changed, as the signed-in admin', async () => {
const { go, fake, id } = await site();
const r = await go('put', '/church', { church: { serviceTimes: [{ day: 'sunday', time: '09:30', label: 'Morning', campus: null, note: null }] }, ifUpdatedAt: '2026-11-25T08:00:00.000Z' });
assert.equal(r.status, 200);
const sent = fake.requests.at(-1)!;
assert.equal(sent.type, 'church.update');
assert.equal(sent.actor, id);
assert.deepEqual(Object.keys(sent.church!), ['serviceTimes']);
});
it('says so when someone changed the profile in between, and checks the fields first', async () => {
const { go } = await site();
const stale = await go('put', '/church', { church: { name: 'Hope' }, ifUpdatedAt: '2000-01-01T00:00:00.000Z' });
assert.equal(stale.body.error.code, 'PROFILE_CHANGED');
const bad = await go('put', '/church', { church: { contact: { email: null, phone: '012 345', whatsapp: null, address: null } } });
assert.equal(bad.status, 400);
assert.ok(bad.body.error.fields['contact.phone']);
});
it("is an Editor's job, not a Viewer's", async () => {
const { go } = await site('viewer');
assert.equal((await go('put', '/church', { church: { name: 'X' } })).status, 403);
});
});
describe('settings, the menu and the audit log', () => {
it('takes a verification code, or the whole meta tag, and the site shows it', async () => {
const { go, ctx } = await site();
const r = await go('put', '/settings', { googleVerification: '<meta name="google-site-verification" content="abc123XYZ_-" />', bingVerification: 'B1NGcode99', comingSoonText: 'Opening at Easter.' });
assert.equal(r.status, 200);
assert.equal(r.body.googleVerification, 'abc123XYZ_-');
const c = await siteContent(ctx);
assert.deepEqual(c.verification, { google: 'abc123XYZ_-', bing: 'B1NGcode99' });
assert.equal(c.mode === 'coming-soon' && c.comingSoonText, 'Opening at Easter.');
assert.equal((await go('put', '/settings', { googleVerification: '<script>' })).status, 400);
});
it('orders the menu, and the audit log says who did what', async () => {
const { go, db } = await site();
const a = await db.page.create({ data: { kind: 'about', slug: 'about', title: 'About', position: 1 } });
const b = await db.page.create({ data: { kind: 'give', slug: 'give', title: 'Give', position: 2 } });
assert.equal((await go('put', '/pages-order', { ids: [b.id, a.id] })).status, 200);
assert.deepEqual((await go('get', '/pages')).body.map((p: { slug: string }) => p.slug), ['give', 'about']);
await go('put', '/settings', { comingSoonText: 'Soon.' });
const log = (await go('get', '/audit')).body;
assert.deepEqual([log[0].who, log[0].action], ['Sam', 'Changed the settings']);
const overview = (await go('get', '/overview')).body;
assert.equal(overview.recent[0].action, 'Changed the settings');
});
});
@@ -21,7 +21,7 @@ export type FakeAccounts = {
next: Partial<IdentityClaims> & { sub: string };
started: { returnTo: string; requireTwoFactor: boolean }[];
/** Requests to Accounts (people.ensure, …). */
requests: { type: string; email?: string; actor?: string | null }[];
requests: { type: string; email?: string; actor?: string | null; church?: Record<string, unknown> }[];
};
export function claims(over: Partial<IdentityClaims> & { sub: string }): IdentityClaims {
@@ -66,8 +66,13 @@ function fakeAuthClient(fake: FakeAccounts): AuthClientModule {
}
},
/** people.ensure: the same sub for the same email; "new@…" is a new account. */
request: async (input: { type: string; email?: string; actor?: string | null }) => {
request: async (input: { type: string; email?: string; actor?: string | null; church?: Record<string, unknown>; ifUpdatedAt?: string }) => {
fake.requests.push(input);
if (input.type === 'church.update') {
// An old ifUpdatedAt: someone saved in between.
if (input.ifUpdatedAt === '2000-01-01T00:00:00.000Z') throw Object.assign(new Error('changed'), { code: 'PROFILE_CHANGED', status: 409 });
return { schema: 'crosscode.identity-request/1', church: { name: 'Hope', ...input.church } };
}
if (input.type !== 'people.ensure') throw new Error(`not faked: ${input.type}`);
const hex = Buffer.from(input.email ?? '').toString('hex').padEnd(12, '0').slice(0, 12);
return { schema: 'crosscode.identity-request/1', sub: `22222222-2222-4222-8222-${hex}`, created: input.email?.startsWith('new@') ?? false, emailSent: true };