Files
crosscode-platform/apps/website/backend/tests/signin.test.ts
T
joshuaandClaude Opus 5.5 f6a536ad64 Accounts 1.7.1, Website 1.0.2, Events 3.7.1: link right after an install; Preview behind the proxy
Found installing the Website for cross-demo2.

- All three: a connect involving an install that isn't in the cached copy
  of the Hub's list yet asks the Hub for a fresh list (one heartbeat, at
  most once a minute) instead of refusing. A new Website's admin couldn't
  sign in for up to 30 minutes.
- Accounts: a heartbeat on every start.
- Website: /admin links to Accounts on the spot (at most every 15 s), one
  connect to Accounts at a time, and the problem page says "not linked
  yet" (ACCOUNTS_NOT_LINKED). Preview redirected to the server's own
  http://localhost:<web port>: the preview routes now answer with a
  relative Location.
- Events: a new Website can connect for the event feed straight away.
- docs: VERSIONS, changelogs, WHATS-NEW.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 16:14:20 +02:00

172 lines
8.8 KiB
TypeScript

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import request from 'supertest';
import { cookieHeader, cookiesFrom, harness, SITE } from './support/harness.js';
const OWNER = '11111111-1111-4111-8111-111111111111';
const OTHER = '22222222-2222-4222-8222-222222222222';
/** Signs in from `returnTo` through the stand-in Accounts; returns the callback's response and the cookies after it. */
async function signIn(h: Awaited<ReturnType<typeof harness>>, returnTo = '/admin/pages?x=1', cookies: Record<string, string> = {}, mfa = false) {
const start = await request(h.app).get(`/api/auth/signin?returnTo=${encodeURIComponent(returnTo)}${mfa ? '&mfa=1' : ''}`).set('Cookie', cookieHeader(cookies));
assert.equal(start.status, 303);
const after = cookiesFrom(start, cookies);
const back = await request(h.app).get('/api/auth/callback?code=abc&state=xyz').set('Cookie', cookieHeader(after));
return { start, back, cookies: cookiesFrom(back, after) };
}
describe('signing in to the admin through Accounts', () => {
it('sends people to Accounts with the page they were on', async () => {
const h = await harness();
const res = await request(h.app).get('/api/auth/signin?returnTo=%2Fadmin%2Fpages');
assert.equal(res.status, 303);
assert.match(res.headers.location!, /^https:\/\/accounts\.hope\.test\//);
assert.deepEqual(h.fake.started, [{ returnTo: '/admin/pages', requireTwoFactor: false }]);
assert.match(String(res.headers['set-cookie']), /ccw_signin=.*Path=\/api\/auth.*HttpOnly/);
});
it('refuses a foreign return address (back to the home page instead)', async () => {
const h = await harness();
await request(h.app).get('/api/auth/signin?returnTo=https%3A%2F%2Fevil.test%2F');
assert.equal(h.fake.started[0]!.returnTo, '/');
});
it('says so when the Website isn\'t linked to Accounts yet', async () => {
const h = await harness({ linked: false });
const res = await request(h.app).get('/api/auth/signin?returnTo=%2Fadmin');
assert.equal(res.status, 303);
assert.equal(res.headers.location, `${SITE}/auth/problem?code=ACCOUNTS_NOT_LINKED&returnTo=%2Fadmin`);
});
it('makes the Hub\'s admin email (verified) the first Owner and lands on the page they started from', async () => {
const h = await harness();
h.fake.next = { sub: OWNER, email: 'Pastor@Hope.test' };
const { back, cookies } = await signIn(h);
assert.equal(back.headers.location, `${SITE}/admin/pages?x=1`);
assert.ok(cookies.ccw_session);
assert.match(String(back.headers['set-cookie']), /ccw_session=.*HttpOnly/);
const me = await request(h.app).get('/api/v1/admin/me').set('Cookie', cookieHeader(cookies));
assert.equal(me.status, 200);
assert.equal(me.body.user.role, 'owner');
assert.equal(me.body.user.email, 'pastor@hope.test');
assert.equal(me.body.mfa, true);
const audit = await h.db.auditLog.findMany({ where: { action: 'first_owner_signed_in' } });
assert.equal(audit.length, 1);
});
it('never makes a second Owner, nor one from an unverified email', async () => {
const h = await harness();
h.fake.next = { sub: OWNER, email_verified: false };
assert.match((await signIn(h)).back.headers.location!, /code=NO_ACCESS/);
assert.equal(await h.db.websiteUser.count(), 0);
h.fake.next = { sub: OWNER };
await signIn(h);
h.fake.next = { sub: OTHER };
const { back, cookies } = await signIn(h);
assert.match(back.headers.location!, /code=NO_ACCESS/);
assert.equal(cookies.ccw_session, undefined);
assert.equal(await h.db.websiteUser.count(), 1);
});
it('gives someone without a role on the Website no session', async () => {
const h = await harness();
h.fake.next = { sub: OTHER, email: 'member@hope.test' };
const { back, cookies } = await signIn(h);
assert.match(back.headers.location!, /\/auth\/problem\?code=NO_ACCESS/);
assert.equal(cookies.ccw_session, undefined);
});
it('asks Accounts again with a second step for an Owner who signed in without one, once', async () => {
const h = await harness();
h.fake.next = { sub: OWNER, amr: ['pwd'] };
const first = await signIn(h, '/admin');
assert.equal(first.back.headers.location, `${SITE}/api/auth/signin?mfa=1&returnTo=%2Fadmin`);
assert.equal(first.cookies.ccw_session, undefined);
// Accounts still says no second step: explained, not looped.
const second = await signIn(h, '/admin', first.cookies, true);
assert.equal(h.fake.started.at(-1)!.requireTwoFactor, true);
assert.match(second.back.headers.location!, /code=TWO_FACTOR_NEEDED/);
// With it: signed in.
h.fake.next = { sub: OWNER, amr: ['pwd', 'mfa'] };
const third = await signIn(h, '/admin', {}, true);
assert.ok(third.cookies.ccw_session);
});
it('lets an Editor in without a second step, and Owners too when the church turned the rule off', async () => {
const h = await harness();
await h.db.websiteUser.create({ data: { id: OTHER, name: 'Ed', email: 'ed@hope.test', role: 'editor' } });
h.fake.next = { sub: OTHER, email: 'ed@hope.test', amr: ['pwd'] };
assert.ok((await signIn(h)).cookies.ccw_session);
await h.ctx.settings.set('two_factor_required_for_admins', 'false');
h.fake.next = { sub: OWNER, amr: ['pwd'] };
assert.ok((await signIn(h)).cookies.ccw_session);
});
it('keeps the admin API to signed-in people', async () => {
const h = await harness();
const res = await request(h.app).get('/api/v1/admin/me');
assert.equal(res.status, 401);
assert.equal(res.body.error.code, 'SIGNED_OUT');
const me = await request(h.app).get('/api/auth/me').set('Cookie', 'ccw_session=made-up');
assert.equal(me.status, 401);
});
it('signs out here, then at Accounts (all the church\'s sites)', async () => {
const h = await harness();
h.fake.next = { sub: OWNER };
const { cookies } = await signIn(h);
const out = await request(h.app).post('/api/auth/signout').set('Cookie', cookieHeader(cookies));
assert.equal(out.status, 303);
assert.equal(out.headers.location, `https://accounts.hope.test/api/oidc/session/end?id_token_hint=id-token-for-${OWNER}`);
assert.equal(await h.db.session.count(), 0);
assert.equal((await request(h.app).get('/api/v1/admin/me').set('Cookie', cookieHeader(cookies))).status, 401);
});
it('ends the person\'s sessions when Accounts says they signed out somewhere', async () => {
const h = await harness();
h.fake.next = { sub: OWNER };
const { cookies } = await signIn(h);
const res = await request(h.app).post('/api/auth/backchannel-logout').type('form').send({ logout_token: JSON.stringify({ sub: OWNER, sid: 'abc' }) });
assert.equal(res.status, 200);
assert.equal((await request(h.app).get('/api/v1/admin/me').set('Cookie', cookieHeader(cookies))).status, 401);
assert.equal((await request(h.app).post('/api/auth/backchannel-logout').type('form').send({ logout_token: 'nonsense' })).status, 400);
});
});
describe("Accounts' calls", () => {
const call = (type: string, extra: object) => JSON.stringify({ schema: 'crosscode.identity/1', callId: '33333333-3333-4333-8333-333333333333', occurredAt: new Date().toISOString(), sub: OWNER, type, ...extra });
it('refuses a call Accounts didn\'t sign', async () => {
const h = await harness();
const res = await request(h.app).post('/api/accounts/v1/events').set('Content-Type', 'application/json').send(call('user.updated', {}));
assert.equal(res.status, 401);
});
it('keeps the admin\'s name and email current, exports them, and removes them when the account goes', async () => {
const h = await harness();
await h.db.websiteUser.create({ data: { id: OWNER, name: 'Sam', email: 'pastor@hope.test', role: 'owner' } });
await h.db.session.create({ data: { tokenHash: 'x', userId: OWNER, expiresAt: new Date(Date.now() + 1e9) } });
const send = (body: string) => request(h.app).post('/api/accounts/v1/events').set('Content-Type', 'application/json').set('x-test-signed', 'yes').send(body);
const profile = { name: 'Pastor Samuel', email: 'Sam@Hope.test', email_verified: true, phone_number: null, news_consent: { email: false, whatsapp: false } };
assert.deepEqual((await send(call('user.updated', { profile }))).body, { schema: 'crosscode.identity/1', ok: true });
const u = await h.db.websiteUser.findUnique({ where: { id: OWNER } });
assert.equal(u!.name, 'Pastor Samuel');
assert.equal(u!.email, 'sam@hope.test');
const exp = await send(call('user.export', { exportId: '44444444-4444-4444-8444-444444444444' }));
assert.equal(exp.body.product, 'website');
assert.equal(exp.body.sections[0].records[0].role, 'owner');
const del = await send(call('user.deleted', {}));
assert.equal(del.body.status, 'deleted');
assert.equal(await h.db.websiteUser.count(), 0);
assert.equal(await h.db.session.count(), 0);
});
});