Files
crosscode-platform/apps/accounts/backend/tests/signin.test.ts
T
joshuaandClaude Opus 5.5 fdd53ae4db Accounts: Playwright on the real pages; interaction cookie path fix
The interaction address is now /api/v2/interaction/<uid> (redirecting to
the sign-in page): oidc-provider scopes its cookie to that path, so every
step's API call gets it. Before, a real browser never sent it and every
sign-in 'expired'; the test browser now honours cookie paths too.
8 Playwright tests (deep link, wrong password, foreign return address,
sign-up, reset mid-sign-in, 2FA set-up, account page, admin) on the real
backend with an in-memory database, no Postgres needed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 15:01:32 +02:00

174 lines
9.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Signing in through Accounts end to end (docs/ACCOUNTS-1.0-PLAN.md §3–5, ROADMAP-v3 §9 release
// checks): a real oidc-provider, the real app, a test product on openid-client.
import assert from 'node:assert/strict';
import { after, before, describe, test } from 'node:test';
import { identityClaimsSchema, OIDC_CLIENT_PATHS } from '@crosscode/contracts';
import { Browser, startHarness, type Harness } from './support/harness.js';
import { codeAt } from '../src/lib/totp.js';
let h: Harness;
before(async () => { h = await startHarness(); });
after(async () => { await h.close(); });
const isCallback = (u: URL) => u.pathname === OIDC_CLIENT_PATHS.callback;
describe('signing in from a product', () => {
test('returns to the exact page, with crosscode.identity/1 claims', async () => {
await h.makeAccount({ email: 'thandi@hope.test', password: 'Correct-horse-1', phone: '+27821234567' });
const r = await h.signIn(new Browser(), 'thandi@hope.test', 'Correct-horse-1');
assert.ok('claims' in r, JSON.stringify(r));
assert.equal(r.returnTo, '/events/youth-camp?ticket=adult');
const claims = identityClaimsSchema.safeParse(r.claims);
assert.deepEqual(claims.error?.issues ?? [], []);
assert.equal(r.claims.email, 'thandi@hope.test');
assert.deepEqual(r.claims.amr, ['pwd']);
assert.equal(r.claims.two_factor, null);
});
test('a wrong password is refused without saying whether the account exists', async () => {
const browser = new Browser();
const started = await h.startSignIn('/');
const { url } = await browser.follow(started.url, (u) => u.pathname === '/signin');
const uid = url.searchParams.get('uid')!;
const wrong = await h.api(browser, 'POST', `/api/v2/interaction/${uid}/password`, { email: 'thandi@hope.test', password: 'nope-nope-nope' });
const nobody = await h.api(browser, 'POST', `/api/v2/interaction/${uid}/password`, { email: 'nobody@hope.test', password: 'nope-nope-nope' });
assert.equal(wrong.status, 401);
assert.deepEqual(wrong.body, nobody.body);
});
test('a second sign-in from the same browser goes straight back (one sign-in for every product)', async () => {
const browser = new Browser();
await h.signIn(browser, 'thandi@hope.test', 'Correct-horse-1');
const started = await h.startSignIn('/give');
const { url } = await browser.follow(started.url, (u) => isCallback(u) || u.pathname === '/signin');
assert.ok(isCallback(url), `expected the callback, got ${url}`);
const r = await h.finishSignIn(url, started);
assert.equal(r.returnTo, '/give');
});
});
describe('what the provider refuses', () => {
test('PKCE is required', async () => {
const started = await h.startSignIn('/');
const u = new URL(started.url);
u.searchParams.delete('code_challenge');
u.searchParams.delete('code_challenge_method');
const res = await new Browser().request(u.toString());
const loc = res.headers.get('location') ?? '';
assert.ok(res.status === 400 || /error=invalid_request/.test(loc), `status ${res.status} ${loc}`);
});
test('a redirect_uri that isn\'t the registered one is refused and never followed', async () => {
const started = await h.startSignIn('/');
const u = new URL(started.url);
u.searchParams.set('redirect_uri', 'https://evil.example/api/auth/callback');
const res = await new Browser().request(u.toString());
assert.equal(res.status, 400);
assert.equal(res.headers.get('location'), null);
});
test('an unknown client is refused', async () => {
const started = await h.startSignIn('/');
const u = new URL(started.url);
u.searchParams.set('client_id', '00000000-0000-4000-8000-00000000dead');
assert.equal((await new Browser().request(u.toString())).status, 400);
});
});
describe('two-factor', () => {
test('a product asking for 2FA (acr_values=mfa) gets it set up first, then amr says mfa', async () => {
await h.makeAccount({ email: 'sam@hope.test', password: 'Correct-horse-2', name: 'Sam Supervisor' });
const browser = new Browser();
const r = await h.signIn(browser, 'sam@hope.test', 'Correct-horse-2', '/dashboard', { acr_values: 'mfa' });
assert.ok('pending' in r);
assert.equal(r.pending.stage, 'setup_two_factor');
const setup = await h.api(browser, 'POST', `/api/v2/interaction/${r.uid}/two-factor/setup`, { method: 'totp' });
const code = codeAt(String(setup.body.secret), Math.floor(Date.now() / 30000));
const done = await h.api(browser, 'POST', `/api/v2/interaction/${r.uid}/two-factor/setup/confirm`, { method: 'totp', code });
assert.equal(done.body.stage, 'done', JSON.stringify(done.body));
assert.equal((done.body.recoveryCodes as string[]).length, 10);
const back = await browser.follow(done.body.redirectTo as string, isCallback);
const signed = await h.finishSignIn(back.url, r.started);
assert.deepEqual(signed.claims.amr, ['pwd', 'otp', 'mfa']);
assert.equal(signed.claims.acr, 'mfa');
assert.equal(signed.returnTo, '/dashboard');
});
test('someone with 2FA by email enters the emailed code; a recovery code works instead', async () => {
const a = await h.makeAccount({ email: 'lerato@hope.test', password: 'Correct-horse-3', twoFactorMethod: 'email' });
const { replaceRecoveryCodes } = await import('../src/accounts/twoFactor.js');
const codes = await replaceRecoveryCodes(h.ctx, a.id as string);
const browser = new Browser();
const r = await h.signIn(browser, 'lerato@hope.test', 'Correct-horse-3');
assert.ok('pending' in r);
assert.equal(r.pending.stage, 'two_factor');
const wrong = await h.api(browser, 'POST', `/api/v2/interaction/${r.uid}/two-factor`, { code: '000000' });
assert.equal(wrong.status, 400);
const ok = await h.api(browser, 'POST', `/api/v2/interaction/${r.uid}/two-factor`, { code: h.messenger.lastCode('lerato@hope.test') });
assert.equal(ok.body.stage, 'done');
const b2 = new Browser();
const r2 = await h.signIn(b2, 'lerato@hope.test', 'Correct-horse-3');
assert.ok('pending' in r2);
const rec = await h.api(b2, 'POST', `/api/v2/interaction/${r2.uid}/two-factor`, { recoveryCode: codes[0]!.toUpperCase() });
assert.equal(rec.body.stage, 'done');
const again = await h.api(new Browser(), 'POST', `/api/v2/interaction/${r2.uid}/two-factor`, { recoveryCode: codes[0] });
assert.notEqual(again.status, 200);
});
});
describe('the steps in between still return to the page', () => {
test('sign-up: verify the email, then back to the product', async () => {
const browser = new Browser();
const started = await h.startSignIn('/events/camp/register?ticket=child');
const { url } = await browser.follow(started.url, (u) => u.pathname === '/signin');
const uid = url.searchParams.get('uid')!;
const signup = await h.api(browser, 'POST', `/api/v2/interaction/${uid}/signup`, { name: 'New Person', email: 'New@Hope.test', password: 'Correct-horse-4', acceptNotice: true, newsEmail: true });
assert.equal(signup.body.stage, 'verify_email', JSON.stringify(signup.body));
const verified = await h.api(browser, 'POST', `/api/v2/interaction/${uid}/verify-email`, { code: h.messenger.lastCode('new@hope.test') });
assert.equal(verified.body.stage, 'done', JSON.stringify(verified.body));
const back = await browser.follow(verified.body.redirectTo as string, isCallback);
const r = await h.finishSignIn(back.url, started);
assert.equal(r.returnTo, '/events/camp/register?ticket=child');
assert.equal(r.claims.email_verified, true);
assert.deepEqual(r.claims.news_consent, { email: true, whatsapp: false });
});
test('a password reset in the middle carries on to the product', async () => {
await h.makeAccount({ email: 'forgot@hope.test', password: 'Old-password-1' });
const browser = new Browser();
const started = await h.startSignIn('/my-tickets');
const { url } = await browser.follow(started.url, (u) => u.pathname === '/signin');
const uid = url.searchParams.get('uid')!;
await h.api(browser, 'POST', '/api/v2/password/forgot', { email: 'forgot@hope.test', uid });
const link = new URL(h.messenger.lastLink('forgot@hope.test')!);
const reset = await h.api(browser, 'POST', '/api/v2/password/reset', { token: link.searchParams.get('token'), password: 'New-password-1' });
assert.equal(reset.body.uid, uid);
const step = await h.api(browser, 'POST', `/api/v2/interaction/${uid}/password`, { email: 'forgot@hope.test', password: 'New-password-1' });
const back = await browser.follow(step.body.redirectTo as string, isCallback);
assert.equal((await h.finishSignIn(back.url, started)).returnTo, '/my-tickets');
// The link is single-use.
assert.equal((await h.api(browser, 'POST', '/api/v2/password/reset', { token: link.searchParams.get('token'), password: 'Other-password-1' })).status, 400);
});
test('a new privacy notice is accepted before going back, even when already signed in', async () => {
await h.makeAccount({ email: 'notice@hope.test', password: 'Correct-horse-5' });
const browser = new Browser();
await h.signIn(browser, 'notice@hope.test', 'Correct-horse-5');
const { publish } = await import('../src/privacy/notice.js');
const n = await publish(h.ctx, { title: 'Our new notice', body: 'x'.repeat(80) }, 'admin');
const started = await h.startSignIn('/after-notice');
const { url } = await browser.follow(started.url, (u) => isCallback(u) || u.pathname === '/signin');
assert.equal(url.pathname, '/signin');
const uid = url.searchParams.get('uid')!;
const view = await h.api(browser, 'GET', `/api/v2/interaction/${uid}/state`);
assert.equal(view.body.stage, 'notice');
const done = await h.api(browser, 'POST', `/api/v2/interaction/${uid}/notice`, { version: n.version });
const back = await browser.follow(done.body.redirectTo as string, isCallback);
const r = await h.finishSignIn(back.url, started);
assert.equal(r.returnTo, '/after-notice');
assert.equal(r.claims.privacy_notice_version, n.version);
});
});