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/02-invitation.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, saveCredentials, signedIn, watch, type Authenticator, type Problems, newClient } from "../lib/harness";4445// The owner invites a viewer from Settings; the viewer completes the setup in another browser and is confined to46// what a viewer may see — in the navigation, on direct navigation, and at the API.47test.describe.serial("invitations and permissions", () => {48 let owner: { context: BrowserContext; page: Page; auth: Authenticator; problems: Problems };49 let viewerContext: BrowserContext;50 let viewer: Page;51 let viewerProblems: Problems;52 let link = "";5354 test.beforeAll(async ({ browser }) => {55 owner = await signedIn(browser, "e2e-owner");56 viewerContext = await newClient(browser);57 viewerProblems = await watch(viewerContext);58 viewer = await viewerContext.newPage();59 });60 test.afterAll(async () => {61 await owner.context.close();62 await viewerContext.close();63 });6465 test("the owner creates an invitation: the link is shown once, with a locally drawn QR code", async () => {66 const { page } = owner;67 const external: string[] = [];68 page.on("request", (r) => { if (!r.url().startsWith("http://localhost:8750/")) external.push(r.url()); });69 await page.goto("/settings/invitations");70 await expect(page.getByTestId("screen-settings-invitations")).toBeVisible();71 await page.getByTestId("invite-create").click();72 await page.getByTestId("invite-role").selectOption("viewer");73 await page.getByTestId("invite-handle").fill("e2e-viewer");74 await page.getByTestId("invite-name").fill("E2E Viewer");75 await page.getByTestId("invite-submit").click();76 await expect(page.getByTestId("invite-link")).toBeVisible();77 link = ((await page.getByTestId("invite-link").textContent()) ?? "").trim();78 expect(link).toMatch(/^http:\/\/localhost:8750\/setup#[A-Za-z0-9_-]{20,}$/);79 await expect(page.getByTestId("invite-qr").locator("svg path.fg")).toHaveCount(1);80 expect(external, "the QR code and the page make no third-party request").toEqual([]);81 // closing drops the secret from the page; the list shows the invitation as pending, without the link82 await page.getByTestId("invite-done").click();83 await expect(page.getByTestId("invite-link")).toHaveCount(0);84 await expect(page.locator('[data-testid="invite-row"][data-status="pending"]').filter({ hasText: "e2e-viewer" })).toHaveCount(1);85 expect(await page.content()).not.toContain(link.split("#")[1] ?? "never");86 const stored = await page.evaluate(() => JSON.stringify({ ...localStorage }) + JSON.stringify({ ...sessionStorage }));87 expect(stored).not.toContain(link.split("#")[1] ?? "never");88 });8990 test("the invited person sees who invites them and with which role, then creates a passkey", async () => {91 const auth = await addAuthenticator(viewer);92 await viewer.goto(link);93 await expect(viewer.getByTestId("setup-identity")).toBeVisible();94 expect(viewer.url()).not.toContain("#");95 await expect(viewer.getByTestId("setup-identity")).toContainText("E2E Owner"); // inviter96 await expect(viewer.getByTestId("setup-identity")).toContainText("E2E Viewer");97 await expect(viewer.getByTestId("setup-identity")).toContainText("@e2e-viewer");98 await expect(viewer.getByTestId("setup-identity")).toContainText("Lecteur");99 await viewer.getByTestId("setup-create-passkey").click();100 await expect(viewer.getByTestId("setup-done")).toBeVisible();101 await saveCredentials("e2e-viewer", auth);102 await viewer.getByTestId("setup-continue").click();103 await expect(viewer.getByTestId("account-menu")).toBeVisible();104 });105106 test("the invitation is completed and cannot be replayed", async ({ browser }) => {107 await owner.page.reload();108 await expect(owner.page.locator('[data-testid="invite-row"][data-status="completed"]').filter({ hasText: "e2e-viewer" })).toHaveCount(1);109 await expect(owner.page.locator('[data-testid="invite-row"][data-status="pending"]').filter({ hasText: "e2e-viewer" })).toHaveCount(0);110 const other = await newClient(browser, { conditional: true });111 const p = await other.newPage();112 await p.goto(link);113 await expect(p.getByTestId("setup-dead")).toHaveAttribute("data-reason", "gone");114 await other.close();115 });116117 test("a viewer has no administration entry in the navigation", async () => {118 const nav = viewer.getByRole("navigation", { name: "Navigation principale" });119 await expect(nav.getByRole("link", { name: "Nœuds" })).toBeVisible();120 await expect(nav.getByRole("link", { name: "Audit" })).toHaveCount(0);121 await viewer.goto("/settings");122 await expect(viewer.getByTestId("screen-settings-profile")).toBeVisible();123 for (const section of ["users", "invitations", "grants", "agents", "system"]) {124 await expect(viewer.getByTestId(`settings-nav-${section}`)).toHaveCount(0);125 }126 await expect(viewer.getByTestId("settings-nav-security")).toBeVisible();127 });128129 test("direct navigation to an admin route shows a 403 naming the permission", async () => {130 for (const [path, permission] of [["/audit", "audit.read"], ["/settings/users", "users.manage"], ["/settings/invitations", "invitations.manage"], ["/settings/agents", "agents.manage"], ["/settings/system", "settings.manage"]] as const) {131 await viewer.goto(path);132 await expect(viewer.getByTestId("forbidden").first(), path).toContainText(permission);133 }134 });135136 test("the API itself refuses the viewer", async () => {137 const result = await viewer.evaluate(async () => {138 const me = (await (await fetch("/api/v1/me")).json()) as { csrf_token: string; permissions: string[]; user: { role: string } };139 const get = async (p: string) => {140 const r = await fetch(`/api/v1${p}`);141 return { status: r.status, body: (await r.json()) as { error?: { code?: string; details?: { permission?: string } } } };142 };143 const post = await fetch("/api/v1/invitations", { method: "POST", headers: { "Content-Type": "application/json", "X-MCC-CSRF": me.csrf_token }, body: JSON.stringify({ role: "owner", ttl_minutes: 15 }) });144 return { role: me.user.role, permissions: me.permissions, audit: await get("/audit"), users: await get("/users"), invitations: await get("/invitations"), create: post.status };145 });146 expect(result.role).toBe("viewer");147 expect(result.permissions).not.toContain("audit.read");148 expect(result.audit.status).toBe(403);149 expect(result.audit.body.error?.code).toBe("forbidden");150 expect(result.audit.body.error?.details?.permission).toBe("audit.read");151 expect(result.users.status).toBe(403);152 expect(result.invitations.status).toBe(403);153 expect(result.create).toBe(403);154 });155156 test("an invitation without identity asks for the profile first", async ({ browser }) => {157 const { page } = owner;158 await page.goto("/settings/invitations");159 await page.getByTestId("invite-create").click();160 await page.getByTestId("invite-role").selectOption("operator");161 await page.getByTestId("invite-submit").click();162 const url = ((await page.getByTestId("invite-link").textContent()) ?? "").trim();163 await page.getByTestId("invite-done").click();164165 const ctx = await newClient(browser);166 const problems = await watch(ctx);167 const p = await ctx.newPage();168 const auth = await addAuthenticator(p);169 await p.goto(url);170 await expect(p.getByTestId("setup-profile-submit")).toBeVisible();171 await expect(p.getByTestId("setup-identity")).toContainText("Opérateur");172 await p.getByLabel("Nom affiché").fill("E2E Operator");173 await p.getByLabel("Identifiant").fill("E2E Owner"); // invalid characters174 await p.getByTestId("setup-profile-submit").click();175 await expect(p.getByText("Identifiant invalide.")).toBeVisible();176 await p.getByLabel("Identifiant").fill("e2e-owner"); // taken177 await p.getByTestId("setup-profile-submit").click();178 await expect(p.getByText("Cet identifiant est déjà pris.")).toBeVisible();179 await p.getByLabel("Identifiant").fill("e2e-operator");180 await p.getByTestId("setup-profile-submit").click();181 await p.getByTestId("setup-create-passkey").click();182 await expect(p.getByTestId("setup-done")).toBeVisible();183 await saveCredentials("e2e-operator", auth);184 problems.assertClean();185 await ctx.close();186 });187188 test("no CSP violation and no console error", async () => {189 owner.problems.assertClean();190 viewerProblems.assertClean();191 });192});193