Files
serverAppConfigs/tests/e2e/specs/12-crawl.spec.ts
T

55 lines
2.4 KiB
TypeScript

import { expect, test, type Page } from "@playwright/test";
// Clicks every open link on the user homepage and asserts the destination is
// the real app, reached through the guard chain without an authentik prompt.
// The link set is read live from the rendered homepage (it mirrors
// shared/homepage/services.json open entries) so a registry edit cannot leave
// this test asserting a stale world.
const CHALLENGE = "Making sure you're not a bot";
async function settleThroughGuard(page: Page, targetHost: string): Promise<void> {
// Each guarded vhost runs its own Anubis proof-of-work on first visit;
// wait for the challenge to clear before judging the destination.
await expect
.poll(async () => !(await page.locator("body").innerText()).includes(CHALLENGE), { timeout: 90_000 })
.toBe(true);
await expect
.poll(() => new URL(page.url()).hostname, { timeout: 60_000 })
.toBe(targetHost);
expect(await page.locator('input[name="uidField"]').count()).toBe(0);
}
test("every user open link reaches its app", async ({ page }) => {
test.setTimeout(420_000);
// Expected link count comes from the live registry, mirroring the
// showLink=true user entries — a registry edit cannot stale this test.
const registry: { name: string; category: string; showLink: boolean }[] =
await (await page.request.get("/services.json")).json();
const expectedUserLinks = registry.filter((s) => s.category === "user" && s.showLink).length;
await page.goto("/");
await expect(page.locator("#grid a.open")).toHaveCount(expectedUserLinks);
const hrefs = await page
.locator("#grid a.open")
.evaluateAll((els) => els.map((el) => ({ href: (el as HTMLAnchorElement).href, label: el.textContent ?? "" })));
const failures: string[] = [];
for (const link of hrefs) {
const target = new URL(link.href);
try {
await page.goto(link.href);
await settleThroughGuard(page, target.hostname);
const resp = await page.request.get(page.url());
if (resp.status() >= 500) {
failures.push(`${link.label}: ${page.url()} answered ${resp.status()}`);
}
} catch (err) {
failures.push(`${link.label}: ${err instanceof Error ? err.message : String(err)}`);
}
await page.goto("/");
await expect(page.locator("#grid a.open")).toHaveCount(expectedUserLinks);
}
expect(failures, failures.join("\n")).toEqual([]);
});