Files
crosscode-platform/apps/website/backend/tests/settings.test.ts
T
joshuaandClaude Opus 5.5 085265392e 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>
2026-11-25 15:30:27 +02:00

71 lines
3.8 KiB
TypeScript

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');
});
});