/** * The admin's flows (WEBSITE-1.0-PLAN ยง13): edit โ†’ preview (click through the site) โ†’ publish โ†’ * the public site changes; discard one change; a shared preview link works signed out and stops * after revoking; restore an older version; switch template in preview without losing anything. * Runs on the e2e server (e2e/server.ts): the demo church on Heritage, an admin signed in directly. */ import AxeBuilder from "@axe-core/playwright"; import { expect, test, type BrowserContext, type Page } from "@playwright/test"; const BACKEND = "http://127.0.0.1:5310"; async function reset(page: Page) { expect((await page.request.post(`${BACKEND}/test/demo?template=heritage`)).ok()).toBeTruthy(); } async function signIn(context: BrowserContext, role = "editor") { const res = await context.request.post(`${BACKEND}/test/session?role=${role}`); const { cookie } = await res.json(); await context.addCookies([{ name: cookie.name, value: cookie.value, url: "http://localhost:3310" }]); } async function aboutTitle(page: Page, title: string) { await page.goto("/admin/pages"); await page.getByRole("link", { name: /^About us\b/ }).first().click(); // The list page has a Title field too (Add a page): wait for the page's own editor. await expect(page).toHaveURL(/\/admin\/pages\/[0-9a-f-]+$/); await expect(page.getByRole("heading", { level: 1, name: "About us" })).toBeVisible(); // The editor opens on the page's content; the title is under Page details. await page.getByRole("tab", { name: "Page details" }).click(); await page.getByLabel("Title", { exact: true }).fill(title); await page.getByRole("button", { name: "Save details" }).click(); await expect(page.getByText("Saved as a draft.")).toBeVisible(); } test.beforeEach(async ({ page }) => reset(page)); test("edit, preview the whole site, publish: then visitors see it", async ({ page, context, browser }) => { await signIn(context); await aboutTitle(page, "Our story"); await page.goto("/admin"); await expect(page.getByRole("link", { name: "1 change not published" })).toBeVisible(); // Preview: the real addresses, with the bar; click through to the changed page. await page.getByRole("link", { name: "Preview", exact: true }).click(); await expect(page.getByRole("region", { name: "Preview" })).toBeVisible(); await page.getByRole("navigation", { name: "Main" }).getByRole("link", { name: "Our story" }).click(); await expect(page.getByRole("heading", { level: 1, name: "Our story" })).toBeVisible(); // Someone else still sees the published site. const visitor = await browser.newPage(); await visitor.goto("/about"); await expect(visitor.getByRole("heading", { level: 1, name: "About us" })).toBeVisible(); await expect(visitor.getByRole("region", { name: "Preview" })).toHaveCount(0); await page.getByRole("button", { name: "Publish" }).click(); await expect(page).toHaveURL(/\/admin\/publish/); await expect(page.getByText("Nothing waiting to be published")).toBeVisible(); await visitor.goto("/about"); await expect(visitor.getByRole("heading", { level: 1, name: "Our story" })).toBeVisible(); await visitor.close(); }); test("the live preview beside the editor shows the draft; the site itself doesn't", async ({ page, context }) => { await signIn(context); await aboutTitle(page, "Draft title"); const frame = page.frameLocator('iframe[title="Live preview of this page"]'); await expect(frame.getByRole("heading", { level: 1, name: "Draft title" })).toBeVisible(); // No preview cookie: the same browser, outside the frame, still sees the live site. await page.goto("/about"); await expect(page.getByRole("heading", { level: 1, name: "About us" })).toBeVisible(); await expect(page.getByRole("region", { name: "Preview" })).toHaveCount(0); // Folded away, it stays folded. await page.goBack(); await page.getByRole("button", { name: "Hide the preview" }).click(); await expect(page.getByRole("button", { name: "Show the live preview" })).toBeVisible(); await page.reload(); await expect(page.getByRole("button", { name: "Show the live preview" })).toBeVisible(); }); test("discard one change", async ({ page, context }) => { await signIn(context); await aboutTitle(page, "Changed my mind"); await page.goto("/admin/publish"); await expect(page.getByRole("heading", { name: "1 change not published" })).toBeVisible(); await page.getByRole("button", { name: "Discard" }).click(); await expect(page.getByText("Discarded: back to the published version.")).toBeVisible(); await expect(page.getByText("Nothing waiting to be published")).toBeVisible(); }); test("a shared preview link works signed out, and stops when revoked", async ({ page, context, browser }) => { await signIn(context); await aboutTitle(page, "For the pastor"); await page.goto("/admin/publish"); await page.getByRole("button", { name: "Make a preview link" }).click(); const url = (await page.locator("code").textContent())!.trim(); const pastor = await browser.newContext(); const p = await pastor.newPage(); await p.goto(new URL(url).pathname); await expect(p.getByRole("region", { name: "Preview" })).toBeVisible(); await expect(p.getByRole("button", { name: "Publish" })).toHaveCount(0); await p.goto("/about"); await expect(p.getByRole("heading", { level: 1, name: "For the pastor" })).toBeVisible(); await page.getByRole("button", { name: "Stop it" }).click(); await expect(page.getByText("The link stopped working.")).toBeVisible(); await p.goto("/about"); await expect(p.getByRole("heading", { level: 1, name: "About us" })).toBeVisible(); await expect(p.getByRole("region", { name: "Preview" })).toHaveCount(0); await pastor.close(); }); test("restore an older version, then publish it", async ({ page, context }) => { await signIn(context); await aboutTitle(page, "Version two"); await page.goto("/admin/publish"); await page.getByRole("button", { name: "Publish all changes" }).click(); await expect(page.getByText("Published: visitors see it now.")).toBeVisible(); await page.getByRole("button", { name: "Restore" }).click(); await expect(page.getByText("Version 1 is back in your drafts")).toBeVisible(); await page.getByRole("button", { name: "Publish all changes" }).click(); // Wait for it, as above: on real Postgres the publish takes long enough to race the page load. await expect(page.getByText("Published: visitors see it now.")).toBeVisible(); await page.goto("/about"); await expect(page.getByRole("heading", { level: 1, name: "About us" })).toBeVisible(); }); test("switch template in the preview, and back, without losing anything", async ({ page, context }) => { await signIn(context); await page.goto("/admin/template"); await page.getByRole("button", { name: "Use Bold" }).click(); await expect(page.getByText("Switched to Bold.")).toBeVisible(); await page.goto("/preview/start"); await expect(page.locator("html")).toHaveAttribute("data-template", "bold"); await expect(page.getByRole("heading", { level: 1, name: "A home for everyone on the hill" })).toBeVisible(); await page.goto("/admin/template"); await page.getByRole("button", { name: "Use Heritage" }).click(); await expect(page.getByText("Switched to Heritage.")).toBeVisible(); await page.goto("/admin/publish"); await expect(page.getByText("Nothing waiting to be published")).toBeVisible(); }); test("signed out, the admin sends people to sign in and back to the same page", async ({ page }) => { const res = await page.request.get("/admin/pages", { maxRedirects: 0 }); expect([303, 307, 308]).toContain(res.status()); expect(res.headers().location).toContain("/api/auth/signin?returnTo=%2Fadmin%2Fpages"); }); test("a Viewer sees the admin but can't publish", async ({ page, context }) => { await signIn(context, "viewer"); await page.goto("/admin/publish"); await expect(page.getByText("Nothing waiting to be published")).toBeVisible(); await expect(page.getByRole("button", { name: /Publish/ })).toHaveCount(0); }); test("an event added and published shows on the site at once (its own Publish)", async ({ page, context }) => { await signIn(context); await page.goto("/admin/events"); await page.getByRole("button", { name: "Add an event" }).click(); await page.getByLabel("Title").fill("Prayer breakfast"); await page.getByLabel("Date", { exact: true }).fill(new Date(Date.now() + 3 * 864e5).toISOString().slice(0, 10)); await page.getByLabel("Where", { exact: true }).fill("The hall"); await page.getByRole("button", { name: "Save" }).click(); await expect(page.getByText("Not published").first()).toBeVisible(); await page.getByRole("listitem").filter({ hasText: "Prayer breakfast" }).getByRole("button", { name: "Publish" }).click(); await expect(page.getByText("On the site now.")).toBeVisible(); await page.goto("/events"); await expect(page.getByRole("link", { name: "Prayer breakfast" })).toBeVisible(); }); test("the admin's screens are accessible", async ({ page, context }) => { // Fifteen screens, each checked by axe (1.4.0: Backups and Background jobs). test.setTimeout(240_000); await signIn(context, "owner"); for (const path of ["/admin", "/admin/template", "/admin/pages", "/admin/ministries", "/admin/leaders", "/admin/messages", "/admin/events", "/admin/linked", "/admin/media", "/admin/publish", "/admin/settings", "/admin/people", "/admin/audit", "/admin/backups", "/admin/jobs"]) { await page.goto(path); await expect(page.getByRole("heading", { level: 1 })).toBeVisible(); const axe = await new AxeBuilder({ page }).withTags(["wcag2a", "wcag2aa", "wcag21aa", "wcag22aa"]).analyze(); expect(axe.violations.map((v) => `${path}: ${v.id} (${v.nodes.length}) ${v.nodes[0]?.target}`)).toEqual([]); } // And a page's editor, with its live preview beside it. await page.goto("/admin/pages"); await page.getByRole("link", { name: /^Home\b/ }).first().click(); await expect(page.frameLocator('iframe[title="Live preview of this page"]').getByRole("heading", { level: 1 })).toBeVisible(); const axe = await new AxeBuilder({ page }).withTags(["wcag2a", "wcag2aa", "wcag21aa", "wcag22aa"]).analyze(); expect(axe.violations.map((v) => `editor: ${v.id} (${v.nodes.length}) ${v.nodes[0]?.target}`)).toEqual([]); }); test("move and zoom a photo in its frame, then delete a photo nothing uses", async ({ page, context }) => { await signIn(context); await page.goto("/admin/pages"); await page.getByRole("link", { name: /^Home\b/ }).first().click(); await expect(page).toHaveURL(/\/admin\/pages\/[0-9a-f-]+$/); await page.getByRole("button", { name: /Top of the page/ }).click(); await page.getByRole("button", { name: "Move and zoom" }).first().click(); const dialog = page.getByRole("dialog", { name: "Move and zoom the photo" }); await expect(dialog).toBeVisible(); await dialog.getByRole("slider").fill("2"); const box = (await dialog.getByLabel(/in its frame/).boundingBox())!; await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2); await page.mouse.down(); await page.mouse.move(box.x + box.width / 2 + 80, box.y + box.height / 2 + 30, { steps: 5 }); await page.mouse.up(); await dialog.getByRole("button", { name: "Done" }).click(); await expect(page.getByText("Moved and zoomed to 200%")).toBeVisible(); await page.getByRole("button", { name: "Save", exact: true }).click(); await expect(page.getByText("Saved as a draft.")).toBeVisible(); // The live preview draws it the same way. const frame = page.frameLocator('iframe[title="Live preview of this page"]'); await expect(frame.locator(".cc-section-hero img").first()).toHaveAttribute("style", /scale\(2\)/); await page.screenshot({ path: "e2e/test-results/framing.png" }); // Upload one nothing uses, then delete it; one in use says where instead. await page.goto("/admin/media"); const before = await page.locator("main li").count(); const png = Buffer.from("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==", "base64"); await page.locator('input[type="file"]').setInputFiles({ name: "dot.png", mimeType: "image/png", buffer: png }); await page.getByLabel("What the photo shows").fill("A dot to delete"); await page.getByRole("button", { name: "Upload" }).click(); await expect(page.locator("main li")).toHaveCount(before + 1); await expect(page.getByText("Used on:").first()).toBeVisible(); page.once("dialog", (d) => d.accept()); await page.locator("main li").filter({ has: page.locator('input[value="A dot to delete"]') }).getByRole("button", { name: "Delete" }).click(); await expect(page.getByText("Deleted.")).toBeVisible(); await expect(page.locator("main li")).toHaveCount(before); });