Events 3.0: auto-connect, demo people in Accounts, e2e; import.finish checks the subs
- contracts 1.8.0 / Accounts 1.1.0: import.finish lists the subs the product relies on; Accounts checks each is held, not deleted and known to it (the count+hash failed a second move after Undo once someone had joined through Accounts). - Events: connects to the church's Accounts once the Hub lists it; moveNewPeople (demo data, people added after the move); set-demo moves demo people; e2e/sign-in-accounts.spec.ts with Accounts in memory and an e2e-only /api rewrite. test:accounts 14/14; Jest auto-connect tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
import path from "path";
|
||||
import { test, expect, loginAs, API, type Page } from "./fixtures";
|
||||
|
||||
// Signing in through the church's Accounts (3.0.0, docs/EVENTS-3.0-PLAN.md §12, the ROADMAP-v3 §9
|
||||
// release checks): an admin checks and moves sign-in on Settings → Sign-in; then a member signs in
|
||||
// at Accounts with their old Cross Events password and lands on the exact page they started from; a
|
||||
// foreign return address ends on the home page; Events' own sign-in says it moved. Accounts runs in
|
||||
// memory (e2e/support/accounts-server.mts). Afterwards sign-in goes back to Cross Events, so the
|
||||
// other specs see the site as before.
|
||||
/* eslint-disable @typescript-eslint/no-require-imports -- the backend's own modules, as prepare-db.js uses them */
|
||||
const backend = path.join(__dirname, "..", "backend");
|
||||
require(require.resolve("dotenv", { paths: [backend] })).config({ path: path.join(backend, ".env") });
|
||||
const { Client } = require(require.resolve("pg", { paths: [backend] }));
|
||||
const { e2eDatabaseUrl } = require("./support/db-url");
|
||||
const signing = require(require.resolve("@crosscode/contracts/signing", { paths: [backend] }));
|
||||
const { encrypt } = require(path.join(backend, "src", "utils", "encryption"));
|
||||
/* eslint-enable @typescript-eslint/no-require-imports */
|
||||
|
||||
const ACCOUNTS_PAGES = "http://127.0.0.1:3221";
|
||||
const ACCOUNTS_API = "http://127.0.0.1:5221";
|
||||
const ACCOUNTS_ID = "7e2d1c0b-9a8f-4e6d-8c5b-4a3f2e1d0c9b";
|
||||
const EVENTS_ID = "5c4b3a29-1807-4f6e-9d5c-4b3a29180716";
|
||||
const SITE = "http://localhost:3100";
|
||||
|
||||
async function sql<T = Record<string, unknown>>(text: string, values: unknown[] = []): Promise<T[]> {
|
||||
const client = new Client({ connectionString: e2eDatabaseUrl() });
|
||||
await client.connect();
|
||||
try {
|
||||
return (await client.query(text, values)).rows as T[];
|
||||
} finally {
|
||||
await client.end();
|
||||
}
|
||||
}
|
||||
const setSetting = (key: string, value: string) =>
|
||||
sql(`INSERT INTO "AppSetting" (key, value, "updatedAt") VALUES ($1, $2, now()) ON CONFLICT (key) DO UPDATE SET value = $2, "updatedAt" = now()`, [key, value]);
|
||||
|
||||
let installIdBefore: string | null = null;
|
||||
|
||||
test.describe.serial("signing in through the church's Accounts", () => {
|
||||
test.beforeAll(async () => {
|
||||
// Connect, as Linked products would: the same secret on both sides.
|
||||
const secret = signing.newLinkSecret();
|
||||
installIdBefore = (await sql<{ value: string }>(`SELECT value FROM "AppSetting" WHERE key = 'hub_install_id'`))[0]?.value ?? null;
|
||||
const eventsId = installIdBefore ?? EVENTS_ID;
|
||||
if (!installIdBefore) await setSetting("hub_install_id", eventsId);
|
||||
const linked = await fetch(`${ACCOUNTS_API}/test/link`, { method: "POST", body: JSON.stringify({ installId: eventsId, url: SITE, secret }) });
|
||||
expect(linked.ok).toBe(true);
|
||||
await sql(`DELETE FROM "ProductLink" WHERE product = 'accounts'`);
|
||||
await sql(
|
||||
`INSERT INTO "ProductLink" ("installId", product, url, "publicKey", secret, "connectedAt", "createdAt", "updatedAt") VALUES ($1, 'accounts', $2, 'x', $3, now(), now(), now())`,
|
||||
[ACCOUNTS_ID, ACCOUNTS_PAGES, encrypt(secret)],
|
||||
);
|
||||
});
|
||||
|
||||
test.afterAll(async () => {
|
||||
// Back to Cross Events' own sign-in for the specs that follow.
|
||||
await sql(`DELETE FROM "AppSetting" WHERE key IN ('auth_mode', 'auth_moved_at', 'auth_move_report', 'auth_move_running', 'accounts_auto_connected')`);
|
||||
await sql(`UPDATE "User" SET "accountId" = NULL`);
|
||||
await sql(`DELETE FROM "ProductLink" WHERE product = 'accounts'`);
|
||||
if (!installIdBefore) await sql(`DELETE FROM "AppSetting" WHERE key = 'hub_install_id'`);
|
||||
});
|
||||
|
||||
test("an admin checks the move, then moves sign-in", async ({ page }) => {
|
||||
page.on("dialog", (d) => d.accept());
|
||||
await loginAs(page, "admin");
|
||||
await page.goto("/dashboard/admin/settings?tab=signin");
|
||||
await expect(page.getByRole("heading", { name: "Sign-in", exact: true })).toBeVisible();
|
||||
await page.getByRole("button", { name: "Check the move" }).click();
|
||||
await expect(page.getByText(/people move with their current password/)).toBeVisible();
|
||||
// Ada stays an Events admin only (an Accounts admin would need two-step sign-in to sign in).
|
||||
const admins = page.locator("fieldset").filter({ hasText: "Accounts admins" });
|
||||
for (const box of await admins.getByRole("checkbox").all()) await box.uncheck();
|
||||
await page.getByRole("button", { name: "Move sign-in to Accounts" }).click();
|
||||
await expect(page.getByText("Sign-in now goes through your church's Accounts.")).toBeVisible();
|
||||
await expect(page.getByText("Through Accounts")).toBeVisible();
|
||||
await expect(page.getByRole("button", { name: "Undo the move" })).toBeVisible();
|
||||
});
|
||||
|
||||
async function signInAtAccounts(page: Page, email: string, password: string) {
|
||||
await expect(page).toHaveURL(new RegExp(`^${ACCOUNTS_PAGES}/signin`));
|
||||
await expect(page.getByRole("heading", { name: "Sign in to E2E Church" })).toBeVisible();
|
||||
await page.getByLabel("Email address").fill(email);
|
||||
await page.getByLabel("Password").fill(password);
|
||||
await page.getByRole("button", { name: "Sign in" }).click();
|
||||
// A privacy notice published by an earlier spec, or the news question: answer and carry on.
|
||||
const accept = page.getByRole("button", { name: "I've read it" });
|
||||
const cont = page.getByRole("button", { name: "Continue" });
|
||||
for (let i = 0; i < 10 && new URL(page.url()).origin !== SITE; i++) {
|
||||
if (await accept.isVisible()) await accept.click();
|
||||
else if (await cont.isVisible()) await cont.click();
|
||||
else await page.waitForTimeout(500);
|
||||
}
|
||||
await page.waitForURL((u) => u.origin === SITE);
|
||||
}
|
||||
|
||||
test("a member signs in with their Cross Events password and lands on the page they started on", async ({ page }) => {
|
||||
await page.goto("/login?redirect=/dashboard/user/profile");
|
||||
await signInAtAccounts(page, "user@e2e.test", "E2e-password-123");
|
||||
await expect(page).toHaveURL(`${SITE}/dashboard/user/profile`);
|
||||
await expect(page.getByRole("heading", { name: "On your church account" })).toBeVisible();
|
||||
await expect(page.locator("#profile-email-address")).toHaveValue("user@e2e.test");
|
||||
await expect(page.locator("#profile-email-address")).toHaveAttribute("readonly", "");
|
||||
});
|
||||
|
||||
test("a foreign return address ends on the home page", async ({ page }) => {
|
||||
await page.goto(`/login?redirect=${encodeURIComponent("//evil.example/steal")}`);
|
||||
await signInAtAccounts(page, "staff@e2e.test", "E2e-password-123");
|
||||
await expect(page).toHaveURL(new RegExp(`^${SITE}/dashboard`));
|
||||
});
|
||||
|
||||
test("Events' own sign-in says it moved; old reset links say so too", async ({ page, request }) => {
|
||||
const res = await request.post(`${API}/api/v2/auth/login`, { data: { identifier: "user@e2e.test", password: "E2e-password-123" } });
|
||||
expect(res.status()).toBe(409);
|
||||
expect((await res.json()).error.code).toBe("SIGN_IN_MOVED");
|
||||
await page.goto("/reset-password?token=old");
|
||||
await expect(page.getByRole("heading", { name: "Sign-in has moved" })).toBeVisible();
|
||||
});
|
||||
|
||||
test("signing out ends the session here and at Accounts", async ({ page }) => {
|
||||
await page.goto("/login?redirect=/dashboard/user");
|
||||
await signInAtAccounts(page, "user@e2e.test", "E2e-password-123");
|
||||
await expect(page).toHaveURL(`${SITE}/dashboard/user`);
|
||||
const token = await page.evaluate(() => localStorage.getItem("hope_events_token"));
|
||||
const out = await page.request.post(`${API}/api/auth/signout`, { headers: { authorization: `Bearer ${token}` }, data: {} });
|
||||
expect((await out.json()).data.url).toMatch(new RegExp(`^${ACCOUNTS_PAGES}/api/oidc/session/end`));
|
||||
const me = await page.request.get(`${API}/api/v2/me`, { headers: { authorization: `Bearer ${token}` } });
|
||||
expect(me.status()).toBe(401);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* The church's Accounts for Cross Events' end-to-end tests (3.0.0, playwright.config.ts): the real
|
||||
* Accounts backend on 127.0.0.1:5221 with its in-memory database (no Postgres of its own), behind
|
||||
* its pages on 127.0.0.1:3221 (`next dev` in apps/accounts/frontend). Its own address (127.0.0.1)
|
||||
* differs from the Events site's (localhost), so their cookies stay apart as on two domains.
|
||||
*
|
||||
* Test helper: POST /test/link { installId, url, secret } links Events, as Connect would.
|
||||
*/
|
||||
import http from 'node:http';
|
||||
import { ed25519PublicToPem, generateEd25519 } from '@crosscode/contracts/signing';
|
||||
import { createApp } from '../../../accounts/backend/src/app.ts';
|
||||
import { createCtx } from '../../../accounts/backend/src/context.ts';
|
||||
import { loadConfig } from '../../../accounts/backend/src/lib/config.ts';
|
||||
import { MemoryMessenger } from '../../../accounts/backend/src/lib/messages.ts';
|
||||
import { createProvider } from '../../../accounts/backend/src/oidc/provider.ts';
|
||||
import { memoryDb } from '../../../accounts/backend/tests/support/memoryDb.ts';
|
||||
|
||||
const WEB = 'http://127.0.0.1:3221';
|
||||
const db = memoryDb();
|
||||
const ctx = createCtx(loadConfig({ ACCOUNTS_URL: WEB, NODE_ENV: 'test', JOBS_ENABLED: 'false', LINKS_ALLOW_PRIVATE: 'true', HUB_URL: 'http://127.0.0.1:1', TRUST_PROXY: 'true' }), db as never, { messenger: new MemoryMessenger() });
|
||||
await ctx.settings.setMany({
|
||||
hub_install_id: '7e2d1c0b-9a8f-4e6d-8c5b-4a3f2e1d0c9b',
|
||||
church_profile: JSON.stringify({ name: 'E2E Church', shortName: 'E2E' }),
|
||||
});
|
||||
const provider = await createProvider(ctx);
|
||||
const app = createApp(ctx, provider);
|
||||
|
||||
http.createServer((req, res) => {
|
||||
if (req.method === 'POST' && req.url === '/test/link') {
|
||||
let body = '';
|
||||
req.on('data', (c) => { body += c; });
|
||||
req.on('end', async () => {
|
||||
const { installId, url, secret } = JSON.parse(body) as { installId: string; url: string; secret: string };
|
||||
const data = { product: 'events', url, publicKey: ed25519PublicToPem(generateEd25519().publicKey), secret: ctx.secrets.encrypt(secret), connectedAt: new Date() };
|
||||
await db.productLink.upsert({ where: { installId }, create: { installId, ...data }, update: data });
|
||||
res.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify({ ok: true }));
|
||||
});
|
||||
return;
|
||||
}
|
||||
app(req, res);
|
||||
}).listen(5221, '127.0.0.1', () => console.log('[e2e] Accounts on 5221'));
|
||||
Reference in New Issue
Block a user