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>(text: string, values: unknown[] = []): Promise { 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); }); });