Files
SnapOtter/tests/e2e/auth.setup.ts
T
ashim-hq a062aa9e5f fix: E2E test isolation and analytics consent bugs
- Isolate test web server on port 2349 so E2E tests run alongside Docker
- Use fresh SQLite database per test run via DB_PATH, fixing missing
  roles/audit_log tables from stale migration state
- Fix analytics consent redirect loop when ANALYTICS_ENABLED=false by
  auto-declining consent instead of bare redirect
- Dismiss analytics consent via API in auth setup and RBAC test user
  creation so the consent page never blocks UI tests
- Make Vite port and proxy target configurable via env vars
2026-04-24 01:24:15 +08:00

37 lines
1.3 KiB
TypeScript

import fs from "node:fs";
import path from "node:path";
import { expect, test as setup } from "@playwright/test";
const authFile = path.join(process.cwd(), "test-results", ".auth", "user.json");
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();
// Wait for login to complete and grab the token in one step
const handle = await page.waitForFunction(() => localStorage.getItem("ashim-token"), null, {
timeout: 15_000,
});
const token = await handle.jsonValue();
// Dismiss analytics consent via API so it won't block any test
const apiBase = process.env.API_URL || "http://localhost:13490";
await page.request.put(`${apiBase}/api/v1/user/analytics`, {
headers: { Authorization: `Bearer ${token}` },
data: { enabled: false },
});
// Now navigate to "/" - consent guard is satisfied
await page.goto("/");
await expect(page).toHaveURL("/");
// Save storage state (includes localStorage with the token)
await page.context().storageState({ path: authFile });
});