2026-03-22 19:28:57 +08:00
|
|
|
import fs from "node:fs";
|
2026-03-25 09:27:12 +08:00
|
|
|
import path from "node:path";
|
2026-05-09 09:02:29 +08:00
|
|
|
import { test as setup } from "@playwright/test";
|
2026-03-22 19:28:57 +08:00
|
|
|
|
2026-07-27 15:37:30 +08:00
|
|
|
const authFile = process.env.PLAYWRIGHT_AUTH_FILE;
|
|
|
|
|
if (!authFile) {
|
|
|
|
|
throw new Error("PLAYWRIGHT_AUTH_FILE was not initialized by playwright.config.ts");
|
|
|
|
|
}
|
2026-03-22 19:28:57 +08:00
|
|
|
|
|
|
|
|
setup("authenticate", async ({ page }) => {
|
|
|
|
|
// Ensure directory exists
|
|
|
|
|
const dir = path.dirname(authFile);
|
|
|
|
|
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
|
|
|
|
|
|
|
|
await page.goto("/login");
|
|
|
|
|
await page.getByLabel("Username").fill("admin");
|
|
|
|
|
await page.getByLabel("Password").fill("admin");
|
|
|
|
|
await page.getByRole("button", { name: /login/i }).click();
|
|
|
|
|
|
2026-06-24 13:30:18 +08:00
|
|
|
// Wait for login to complete (the token lands in localStorage)
|
|
|
|
|
await page.waitForFunction(() => localStorage.getItem("snapotter-token"), null, {
|
2026-04-24 01:24:15 +08:00
|
|
|
timeout: 15_000,
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-24 14:45:37 +08:00
|
|
|
// After login the app redirects to "/" on its own. Wait for that redirect to
|
|
|
|
|
// settle before forcing navigation, otherwise page.goto races the in-flight
|
|
|
|
|
// client-side redirect and aborts ("interrupted by another navigation").
|
2026-04-30 16:11:33 +08:00
|
|
|
await page.waitForURL((url) => url.pathname === "/", { timeout: 30_000 }).catch(() => {});
|
2026-06-24 14:45:37 +08:00
|
|
|
await page.goto("/", { waitUntil: "domcontentloaded" });
|
2026-04-30 16:11:33 +08:00
|
|
|
await page.waitForLoadState("load");
|
2026-03-22 19:28:57 +08:00
|
|
|
|
2026-07-27 15:37:30 +08:00
|
|
|
// Fail fast on a misconfigured e2e service. A correctly configured API
|
|
|
|
|
// (SKIP_MUST_CHANGE_PASSWORD=true, fresh per-run DB) lands the admin on "/".
|
2026-06-19 18:15:20 +08:00
|
|
|
const landedPath = new URL(page.url()).pathname;
|
|
|
|
|
if (landedPath !== "/") {
|
|
|
|
|
throw new Error(
|
2026-07-27 15:37:30 +08:00
|
|
|
`Auth setup landed on "${landedPath}" instead of "/" using web endpoint ` +
|
|
|
|
|
`${process.env.PLAYWRIGHT_WEB_URL ?? "<unknown>"} and API endpoint ` +
|
|
|
|
|
`${process.env.API_URL ?? "<unknown>"}. The isolated service is misconfigured.`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prove the browser's /api proxy reaches THIS run's API rather than some other
|
|
|
|
|
// checkout's server that happens to answer on a shared port. A session token
|
|
|
|
|
// exists only in the run-owned database, so replaying the token the browser
|
|
|
|
|
// just obtained against the resolved API endpoint is an identity check: it
|
|
|
|
|
// passes only when both paths terminate at the same instance. Reaching "an
|
|
|
|
|
// API" would still satisfy a health probe, which is how an earlier sweep
|
|
|
|
|
// silently mutated an unrelated instance's settings, sessions and jobs.
|
|
|
|
|
const apiUrl = process.env.API_URL;
|
|
|
|
|
if (!apiUrl) {
|
|
|
|
|
throw new Error("API_URL was not initialized by playwright.config.ts");
|
|
|
|
|
}
|
|
|
|
|
const proxiedToken = await page.evaluate(() => localStorage.getItem("snapotter-token"));
|
|
|
|
|
if (!proxiedToken) {
|
|
|
|
|
throw new Error("Login through the web endpoint produced no session token");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const identity = await page.request.get(`${apiUrl}/api/auth/session`, {
|
|
|
|
|
headers: { authorization: `Bearer ${proxiedToken}` },
|
|
|
|
|
});
|
|
|
|
|
if (!identity.ok()) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`The session minted through ${process.env.PLAYWRIGHT_WEB_URL ?? "<unknown>"} is unknown to ` +
|
|
|
|
|
`the run-owned API at ${apiUrl} (status ${identity.status()}). That web endpoint is ` +
|
|
|
|
|
"proxying /api to a different instance.",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Negative control: without it the check above would also pass against an API
|
|
|
|
|
// running with authentication disabled, which accepts any bearer value.
|
|
|
|
|
const forged = await page.request.get(`${apiUrl}/api/auth/session`, {
|
|
|
|
|
headers: { authorization: "Bearer not-a-real-session-token" },
|
|
|
|
|
});
|
|
|
|
|
if (forged.status() !== 401) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`The run-owned API at ${apiUrl} accepted a forged session token (status ${forged.status()}), ` +
|
|
|
|
|
"so the identity check above proves nothing. Expected 401.",
|
2026-06-19 18:15:20 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 19:28:57 +08:00
|
|
|
// Save storage state (includes localStorage with the token)
|
|
|
|
|
await page.context().storageState({ path: authFile });
|
|
|
|
|
});
|