Files
crosscode-platform/apps/website/backend/tests/people.test.ts
T
joshuaandClaude Opus 5.5 37a1f6acf1 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>
2026-11-25 13:05:33 +02:00

70 lines
4.4 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 { 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);
});
});