feat(website): people and roles, messages with the YouTube feed, simple events
People: Owners and Admins add someone by email (people.ensure through Accounts), change roles, remove access; only Owners touch Owners, nobody changes themselves, a lower role ends sessions at once. Messages: from a YouTube channel or playlist every hour (public feed, no key) or by hand with a YouTube or Spotify link; own publish, hide. Simple events: own publish, cancel, remove. Tests and e2e with axe on the new screens. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
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 { publish } from '../src/site/snapshot.js';
|
||||
import { parseFeed, parseSource, syncYoutube } from '../src/site/youtube.js';
|
||||
import { harness, SITE } from './support/harness.js';
|
||||
|
||||
const FEED = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<feed xmlns:yt="http://www.youtube.com/xml/schemas/2015" xmlns:media="http://search.yahoo.com/mrss/" xmlns="http://www.w3.org/2005/Atom">
|
||||
<title>Hope Family Church</title>
|
||||
<entry>
|
||||
<id>yt:video:abcDEF12345</id>
|
||||
<yt:videoId>abcDEF12345</yt:videoId>
|
||||
<title>Hope & joy | Sunday 23 Nov</title>
|
||||
<published>2026-11-23T08:00:00+00:00</published>
|
||||
<media:group><media:thumbnail url="https://i1.ytimg.com/vi/abcDEF12345/hqdefault.jpg" width="480" height="360"/></media:group>
|
||||
</entry>
|
||||
<entry>
|
||||
<yt:videoId>zzzYYY98765</yt:videoId>
|
||||
<title>Bread for the road</title>
|
||||
<published>2026-11-16T08:00:00+00:00</published>
|
||||
</entry>
|
||||
<entry><title>No id</title><published>2026-11-01T08:00:00+00:00</published></entry>
|
||||
</feed>`;
|
||||
|
||||
async function signedIn() {
|
||||
const h = await harness({ linked: false });
|
||||
await h.db.websiteUser.create({ data: { id: '00000000-0000-4000-8000-000000000003', name: 'Ed', email: 'ed@hope.test', role: 'editor' } });
|
||||
await h.db.session.create({ data: { tokenHash: hashToken('t'), userId: '00000000-0000-4000-8000-000000000003', expiresAt: new Date(Date.now() + 864e5) } });
|
||||
const go = (m: 'get' | 'post' | 'put' | 'delete', 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;
|
||||
};
|
||||
await publish(h.ctx, { id: 'x', role: 'system' });
|
||||
return { ...h, go };
|
||||
}
|
||||
|
||||
describe('messages from YouTube (§7)', () => {
|
||||
it('reads a channel id, a channel address or a playlist', () => {
|
||||
assert.deepEqual(parseSource('UCaBcDeFgHiJkLmNoPqRsTuV'), { kind: 'channel', id: 'UCaBcDeFgHiJkLmNoPqRsTuV' });
|
||||
assert.deepEqual(parseSource('https://www.youtube.com/channel/UCaBcDeFgHiJkLmNoPqRsTuV/videos'), { kind: 'channel', id: 'UCaBcDeFgHiJkLmNoPqRsTuV' });
|
||||
assert.deepEqual(parseSource('https://www.youtube.com/playlist?list=PLabcdefghij123'), { kind: 'playlist', id: 'PLabcdefghij123' });
|
||||
assert.equal(parseSource('https://www.youtube.com/@hopefamily'), null);
|
||||
});
|
||||
|
||||
it('parses the feed, leaving out entries it can\'t use', () => {
|
||||
const v = parseFeed(FEED);
|
||||
assert.equal(v.length, 2);
|
||||
assert.equal(v[0]!.title, 'Hope & joy | Sunday 23 Nov');
|
||||
assert.equal(v[0]!.thumbnail, 'https://i1.ytimg.com/vi/abcDEF12345/hqdefault.jpg');
|
||||
});
|
||||
|
||||
it('adds new videos as live messages, once, and says when YouTube can\'t be reached', async () => {
|
||||
const h = await harness({ linked: false });
|
||||
await h.ctx.settings.set('youtube_source', JSON.stringify({ kind: 'channel', id: 'UCaBcDeFgHiJkLmNoPqRsTuV' }));
|
||||
let asked = '';
|
||||
const ok = async (u: string) => { asked = u; return { ok: true, status: 200, text: async () => FEED }; };
|
||||
assert.deepEqual(await syncYoutube(h.ctx, ok), { added: 2 });
|
||||
assert.equal(asked, 'https://www.youtube.com/feeds/videos.xml?channel_id=UCaBcDeFgHiJkLmNoPqRsTuV');
|
||||
assert.deepEqual(await syncYoutube(h.ctx, ok), { added: 0 });
|
||||
await publish(h.ctx, { id: 'x', role: 'system' });
|
||||
const c = await siteContent(h.ctx);
|
||||
assert.equal(c.mode === 'published' && c.messages[0]!.title, 'Hope & joy | Sunday 23 Nov');
|
||||
const down = await syncYoutube(h.ctx, async () => ({ ok: false, status: 503, text: async () => '' }));
|
||||
assert.match(String(down.error), /503/);
|
||||
assert.match(String(await h.ctx.settings.get('youtube_last_error')), /503/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('messages and events in the admin (decision 9)', () => {
|
||||
it('adds a message by link, publishes it on its own, and refuses a duplicate or a stranger link', async () => {
|
||||
const { go, ctx } = await signedIn();
|
||||
const m = await go('post', '/messages', { title: 'Grace', date: '2026-11-22', mediaUrl: 'https://youtu.be/abcDEF12345', speaker: 'Thandi', series: 'Hope' });
|
||||
assert.equal(m.status, 200);
|
||||
let c = await siteContent(ctx);
|
||||
assert.equal(c.mode === 'published' && c.messages.length, 0, 'not live until its own Publish');
|
||||
await go('post', `/messages/${m.body.id}/publish`, { live: true });
|
||||
c = await siteContent(ctx);
|
||||
assert.equal(c.mode === 'published' && c.messages[0]!.series, 'Hope');
|
||||
assert.equal((await go('post', '/messages', { title: 'Again', date: '2026-11-22', mediaUrl: 'https://www.youtube.com/watch?v=abcDEF12345' })).body.error.code, 'DUPLICATE');
|
||||
assert.equal((await go('post', '/messages', { title: 'X', date: '2026-11-22', mediaUrl: 'https://evil.example/v' })).status, 400);
|
||||
const spotify = await go('post', '/messages', { title: 'Podcast', date: '2026-11-22', mediaUrl: 'https://open.spotify.com/episode/4rOoJ6Egrf8K2IrywzwOMk' });
|
||||
assert.equal(spotify.body.source, 'spotify');
|
||||
});
|
||||
|
||||
it('keeps a feed message from being deleted (it would come back): hide it instead', async () => {
|
||||
const { go, db } = await signedIn();
|
||||
const row = await db.message.create({ data: { slug: 'x', title: 'From YouTube', date: new Date(), source: 'youtube_feed', mediaUrl: 'https://www.youtube.com/watch?v=abcDEF12345', externalId: 'abcDEF12345' } });
|
||||
assert.equal((await go('delete', `/messages/${row.id}`)).body.error.code, 'FROM_FEED');
|
||||
});
|
||||
|
||||
it('creates, publishes, cancels and removes a simple event', async () => {
|
||||
const { go, ctx } = await signedIn();
|
||||
const start = new Date(Date.now() + 7 * 864e5).toISOString();
|
||||
const bad = await go('post', '/events', { title: 'Picnic', startsAt: start, endsAt: new Date(Date.now()).toISOString() });
|
||||
assert.ok(bad.body.error.fields.endsAt);
|
||||
const e = await go('post', '/events', { title: 'Picnic', startsAt: start, location: 'The park', link: 'https://forms.example/picnic' });
|
||||
assert.equal(e.status, 200);
|
||||
assert.equal(e.body.slug, 'picnic');
|
||||
await go('post', `/events/${e.body.id}/state`, { published: true });
|
||||
let c = await siteContent(ctx);
|
||||
assert.equal(c.mode === 'published' && c.events[0]!.url, 'https://forms.example/picnic');
|
||||
await go('post', `/events/${e.body.id}/state`, { cancelled: true });
|
||||
c = await siteContent(ctx);
|
||||
assert.equal(c.mode === 'published' && c.events.length, 0);
|
||||
assert.equal((await go('delete', `/events/${e.body.id}`)).status, 200);
|
||||
assert.equal((await go('get', '/events')).body.length, 0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,69 @@
|
||||
import { describe, it } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import request from 'supertest';
|
||||
import { hashToken } from '../src/lib/crypto.js';
|
||||
import { harness, SITE } from './support/harness.js';
|
||||
|
||||
type Role = 'owner' | 'admin' | 'editor' | 'ministry_leader' | 'viewer';
|
||||
|
||||
/** A site linked to Accounts, with an Owner and an Admin signed in (each with their own cookie). */
|
||||
async function site() {
|
||||
const h = await harness();
|
||||
const people: Record<string, { id: string; cookie: string }> = {};
|
||||
for (const [n, role] of (['owner', 'admin', 'editor'] as Role[]).entries()) {
|
||||
const id = `00000000-0000-4000-8000-00000000000${n + 1}`;
|
||||
await h.db.websiteUser.create({ data: { id, name: role, email: `${role}@hope.test`, role } });
|
||||
await h.db.session.create({ data: { tokenHash: hashToken(`t-${role}`), userId: id, expiresAt: new Date(Date.now() + 864e5) } });
|
||||
people[role] = { id, cookie: `ccw_session=t-${role}` };
|
||||
}
|
||||
const as = (role: string) => ({
|
||||
get: (p: string) => request(h.app).get(`/api/v1/admin${p}`).set('Cookie', people[role]!.cookie),
|
||||
post: (p: string, b: object) => request(h.app).post(`/api/v1/admin${p}`).set('Cookie', people[role]!.cookie).set('Origin', SITE).send(b),
|
||||
put: (p: string, b: object) => request(h.app).put(`/api/v1/admin${p}`).set('Cookie', people[role]!.cookie).set('Origin', SITE).send(b),
|
||||
del: (p: string) => request(h.app).delete(`/api/v1/admin${p}`).set('Cookie', people[role]!.cookie).set('Origin', SITE),
|
||||
});
|
||||
return { ...h, people, as };
|
||||
}
|
||||
|
||||
describe('who can use the admin (§10)', () => {
|
||||
it('adds someone by email through Accounts, who can then sign in with that role', async () => {
|
||||
const { as, fake, db } = await site();
|
||||
const r = await as('admin').post('/people', { name: 'New Editor', email: 'New@Hope.test', role: 'editor' });
|
||||
assert.equal(r.status, 200);
|
||||
assert.equal(r.body.newAccount, true);
|
||||
assert.deepEqual(fake.requests[0], { type: 'people.ensure', actor: '00000000-0000-4000-8000-000000000002', name: 'New Editor', email: 'new@hope.test', phone: null, returnUrl: `${SITE}/admin` });
|
||||
const u = await db.websiteUser.findUnique({ where: { id: r.body.id } });
|
||||
assert.equal(u!.role, 'editor');
|
||||
assert.equal((await as('admin').post('/people', { name: 'Again', email: 'new@hope.test', role: 'viewer' })).body.error.code, 'ALREADY_ADDED');
|
||||
const list = (await as('owner').get('/people')).body;
|
||||
assert.equal(list.length, 4);
|
||||
});
|
||||
|
||||
it('needs a ministry for a Ministry leader', async () => {
|
||||
const { as, db } = await site();
|
||||
assert.equal((await as('admin').post('/people', { name: 'L', email: 'l@hope.test', role: 'ministry_leader' })).status, 400);
|
||||
const m = await db.ministry.create({ data: { slug: 'kids', name: 'Kids' } });
|
||||
const r = await as('admin').post('/people', { name: 'L', email: 'l@hope.test', role: 'ministry_leader', ministryId: m.id });
|
||||
assert.equal(r.status, 200);
|
||||
assert.equal((await as('admin').get('/people')).body.find((p: { id: string }) => p.id === r.body.id).ministry, 'Kids');
|
||||
});
|
||||
|
||||
it('keeps Owners to Owners, and nobody changes themselves', async () => {
|
||||
const { as, people } = await site();
|
||||
assert.equal((await as('admin').post('/people', { name: 'O', email: 'o@hope.test', role: 'owner' })).body.error.code, 'OWNERS_ONLY');
|
||||
assert.equal((await as('admin').put(`/people/${people.owner!.id}`, { role: 'viewer' })).body.error.code, 'OWNERS_ONLY');
|
||||
assert.equal((await as('admin').del(`/people/${people.owner!.id}`)).body.error.code, 'OWNERS_ONLY');
|
||||
assert.equal((await as('owner').put(`/people/${people.owner!.id}`, { role: 'viewer' })).body.error.code, 'SELF');
|
||||
assert.equal((await as('editor').get('/people')).status, 403);
|
||||
assert.equal((await as('owner').post('/people', { name: 'Second owner', email: 'o@hope.test', role: 'owner' })).status, 200);
|
||||
});
|
||||
|
||||
it('a lower role takes effect at once; removing someone ends their sessions', async () => {
|
||||
const { as, people, db } = await site();
|
||||
assert.equal((await as('owner').put(`/people/${people.editor!.id}`, { role: 'viewer' })).status, 200);
|
||||
assert.equal(await db.session.count({ where: { userId: people.editor!.id } }), 0);
|
||||
assert.equal((await as('owner').del(`/people/${people.admin!.id}`)).status, 200);
|
||||
assert.equal(await db.websiteUser.count({ where: { id: people.admin!.id } }), 0);
|
||||
assert.equal((await as('admin').get('/people')).status, 401);
|
||||
});
|
||||
});
|
||||
@@ -20,6 +20,8 @@ export const ACCOUNTS_INSTALL_ID = '9a4c2f1e-7b3d-4e5a-8c6f-0d1e2f3a4b5c';
|
||||
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 }[];
|
||||
};
|
||||
|
||||
export function claims(over: Partial<IdentityClaims> & { sub: string }): IdentityClaims {
|
||||
@@ -63,6 +65,13 @@ function fakeAuthClient(fake: FakeAccounts): AuthClientModule {
|
||||
throw new SignInError('bad', 'BAD_TOKEN');
|
||||
}
|
||||
},
|
||||
/** people.ensure: the same sub for the same email; "new@…" is a new account. */
|
||||
request: async (input: { type: string; email?: string; actor?: string | null }) => {
|
||||
fake.requests.push(input);
|
||||
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 };
|
||||
},
|
||||
verifyAccountsCall: (raw: Buffer, headers: Record<string, unknown>) => {
|
||||
if (headers['x-test-signed'] !== 'yes') throw new SignInError('That call isn\'t signed by Accounts.', 'BAD_SIGNATURE');
|
||||
return JSON.parse(raw.toString('utf8'));
|
||||
@@ -73,7 +82,7 @@ function fakeAuthClient(fake: FakeAccounts): AuthClientModule {
|
||||
|
||||
export async function harness(opts: { linked?: boolean; env?: Record<string, string> } = {}) {
|
||||
const db = memoryDb();
|
||||
const fake: FakeAccounts = { next: { sub: '11111111-1111-4111-8111-111111111111' }, started: [] };
|
||||
const fake: FakeAccounts = { next: { sub: '11111111-1111-4111-8111-111111111111' }, started: [], requests: [] };
|
||||
const config = loadConfig({ WEBSITE_URL: SITE, HUB_ADMIN_EMAIL: ADMIN_EMAIL.toUpperCase(), ...opts.env });
|
||||
const ctx = createCtx(config, db as unknown as Db, { authClient: async () => fakeAuthClient(fake) });
|
||||
await ctx.settings.set('hub_install_id', INSTALL_ID);
|
||||
|
||||
Reference in New Issue
Block a user