Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Command Center (MCC)8 * File : apps/web/e2e/specs/01-identity.spec.ts9 * Module : web10 * Repository : https://git.spboucher.ai/maclustr-command-center11 * Project Website : https://clustr.spboucher.ai12 * Created : 2026-11-1813 * Last Modified : 2026-11-1814 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142import { expect, test, type BrowserContext, type Page } from "@playwright/test";43import { addAuthenticator, readState, saveCredentials, signedIn, watch, type Authenticator, type Problems, newClient } from "../lib/harness";4445// Owner bootstrap from the one-time link printed by `mccctl bootstrap-owner`, then sign-out / sign-in.46test.describe.serial("identity: owner setup and sign-in", () => {47 let context: BrowserContext;48 let page: Page;49 let auth: Authenticator;50 let problems: Problems;5152 test.beforeAll(async ({ browser }) => {53 context = await newClient(browser, { conditional: true });54 problems = await watch(context);55 page = await context.newPage();56 auth = await addAuthenticator(page);57 });58 test.afterAll(async () => {59 await context.close();60 });6162 test("sign-in page: CSP header with a nonce, no referrer, no password field", async () => {63 const response = await page.goto("/signin");64 const csp = response?.headers()["content-security-policy"] ?? "";65 expect(csp).toMatch(/script-src 'self' 'nonce-[A-Za-z0-9+/=]+' 'strict-dynamic'/);66 expect(csp).toMatch(/style-src 'self' 'nonce-[A-Za-z0-9+/=]+'/);67 expect(csp).toContain("default-src 'self'");68 expect(csp).toContain("frame-ancestors 'none'");69 expect(csp).toContain("base-uri 'none'");70 expect(csp).toContain("form-action 'self'");71 expect(csp).toContain("connect-src 'self'");72 expect(csp).not.toContain("unsafe-inline");73 expect(csp).not.toContain("unsafe-eval");74 const second = await page.request.get("/signin");75 expect(second.headers()["content-security-policy"]).not.toEqual(csp); // per-request nonce76 await expect(page.locator('meta[name="referrer"]')).toHaveAttribute("content", "no-referrer");77 await expect(page.getByTestId("signin-button")).toBeVisible();78 await expect(page.locator('input[type="password"]')).toHaveCount(0);79 });8081 test("anonymous visitors are sent to the sign-in page", async () => {82 await page.goto("/nodes");83 await expect(page).toHaveURL(/\/signin\?next=%2Fnodes$/);84 });8586 test("invitation link: the fragment leaves the URL and the history at once", async () => {87 const url = readState().ownerSetupUrl;88 const token = url.split("#")[1] ?? "";89 expect(token.length).toBeGreaterThan(20);90 const requests: string[] = [];91 page.on("request", (r) => requests.push(r.url()));92 await page.goto(url);93 await expect(page.getByTestId("setup-identity")).toBeVisible();94 expect(await page.evaluate(() => location.hash)).toBe("");95 expect(page.url()).not.toContain(token);96 // no request URL (document, assets, API) ever carried the token: it only travels in the POST body of the exchange97 expect(requests.filter((u) => u.includes(token))).toEqual([]);98 // going away and back does not resurrect the fragment99 await page.goto("/signin");100 await page.goBack();101 expect(page.url()).not.toContain(token);102 expect(await page.evaluate(() => location.hash)).toBe("");103 // the transaction is resumed from its cookie, not from the token104 await expect(page.getByTestId("setup-identity")).toBeVisible();105 await expect(page.getByTestId("setup-identity")).toContainText("E2E Owner");106 await expect(page.getByTestId("setup-identity")).toContainText(/Propriétaire/);107 });108109 test("reusing the link in another browser is a dead end", async ({ browser }) => {110 const other = await newClient(browser, { conditional: true });111 const p = await other.newPage();112 await p.goto(readState().ownerSetupUrl);113 await expect(p.getByTestId("setup-dead")).toHaveAttribute("data-reason", "gone");114 await expect(p.getByTestId("setup-create-passkey")).toHaveCount(0);115 await expect(p.getByRole("button", { name: /réessayer/i })).toHaveCount(0);116 expect(p.url()).not.toContain("#");117 await other.close();118 });119120 test("the owner creates a passkey and lands in the application", async () => {121 await page.getByTestId("setup-create-passkey").click();122 await expect(page.getByTestId("setup-done")).toBeVisible();123 await saveCredentials("e2e-owner", auth);124 expect((await auth.credentials()).length).toBe(1);125 await page.getByTestId("setup-continue").click();126 await expect(page.getByTestId("account-menu")).toBeVisible();127 await expect(page.getByRole("navigation", { name: "Navigation principale" })).toBeVisible();128 await expect(page.getByTestId("connection-indicator")).toHaveAttribute("data-state", "live");129 });130131 test("reloading the consumed link shows the dead end, never a retry loop", async () => {132 const p = await context.newPage();133 await p.goto(readState().ownerSetupUrl);134 await expect(p.getByTestId("setup-dead")).toHaveAttribute("data-reason", "gone");135 expect(p.url()).not.toContain("#");136 await p.close();137 });138139 test("nothing sensitive is kept in web storage", async () => {140 const dump = await page.evaluate(() => JSON.stringify({ ...localStorage }) + JSON.stringify({ ...sessionStorage }));141 expect(dump).not.toMatch(/csrf|token|session|credential/i);142 });143144145 test("sign out ends the session; conditional mediation signs in again from the autofill request", async () => {146 await auth.presence(false); // the user does not touch the key: the sign-in page must stay anonymous147 await page.getByTestId("account-menu").click();148 await page.getByRole("menuitem", { name: "Se déconnecter" }).click();149 await expect(page).toHaveURL(/\/signin/);150 expect(await page.evaluate(async () => (await fetch("/api/v1/me")).status)).toBe(401);151 await expect(page.getByTestId("signin-button")).toBeVisible();152 // the browser offers the passkey from the field (conditional mediation): picking it signs in without the button153 await auth.presence(true);154 await page.reload();155 await expect(page.getByTestId("account-menu")).toBeVisible();156 await expect(page).toHaveURL(/\/$/);157 });158159 test("explicit sign-in with the button in a fresh browser", async ({ browser }) => {160 const s = await signedIn(browser, "e2e-owner");161 await expect(s.page.getByTestId("connection-indicator")).toHaveAttribute("data-state", "live");162 s.problems.assertClean();163 await s.context.close();164 });165166 test("no CSP violation and no console error", async () => {167 problems.assertClean();168 });169});170