mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
fix: robust auth setup and login helpers for E2E analytics tests
This commit is contained in:
@@ -8,11 +8,20 @@ import { expect, test } from "@playwright/test";
|
|||||||
// given/declined, the user's state changes. Tests run serially and
|
// given/declined, the user's state changes. Tests run serially and
|
||||||
// each builds on the state left by the previous test.
|
// each builds on the state left by the previous test.
|
||||||
|
|
||||||
async function loginFresh(page: import("@playwright/test").Page) {
|
async function loginAndGetToHome(page: import("@playwright/test").Page) {
|
||||||
await page.goto("/login");
|
await page.goto("/login");
|
||||||
await page.getByLabel("Username").fill("admin");
|
await page.getByLabel("Username").fill("admin");
|
||||||
await page.getByLabel("Password").fill("admin");
|
await page.getByLabel("Password").fill("admin");
|
||||||
await page.getByRole("button", { name: /login/i }).click();
|
await page.getByRole("button", { name: /login/i }).click();
|
||||||
|
// May hit consent page or go straight to home
|
||||||
|
try {
|
||||||
|
const acceptBtn = page.getByRole("button", { name: /sure, sounds good/i });
|
||||||
|
await acceptBtn.waitFor({ state: "visible", timeout: 5_000 });
|
||||||
|
await acceptBtn.click();
|
||||||
|
await page.waitForURL("/", { timeout: 30_000 });
|
||||||
|
} catch {
|
||||||
|
await page.waitForURL("/", { timeout: 30_000 });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
test.describe("Analytics consent page", () => {
|
test.describe("Analytics consent page", () => {
|
||||||
@@ -25,8 +34,7 @@ test.describe("Analytics consent page", () => {
|
|||||||
// The auth setup project already accepted analytics consent for the admin
|
// The auth setup project already accepted analytics consent for the admin
|
||||||
// user, so a fresh browser session logging in as admin should go straight
|
// user, so a fresh browser session logging in as admin should go straight
|
||||||
// to the home page without being redirected to /analytics-consent.
|
// to the home page without being redirected to /analytics-consent.
|
||||||
await loginFresh(page);
|
await loginAndGetToHome(page);
|
||||||
await page.waitForURL("/", { timeout: 30_000 });
|
|
||||||
await expect(page).toHaveURL("/");
|
await expect(page).toHaveURL("/");
|
||||||
|
|
||||||
// Navigate away and back — consent page should NOT reappear
|
// Navigate away and back — consent page should NOT reappear
|
||||||
@@ -50,8 +58,7 @@ test.describe("Analytics consent page", () => {
|
|||||||
|
|
||||||
test("settings toggle works after accepting analytics", async ({ page }) => {
|
test("settings toggle works after accepting analytics", async ({ page }) => {
|
||||||
// User already accepted in previous test — login should go straight to home
|
// User already accepted in previous test — login should go straight to home
|
||||||
await loginFresh(page);
|
await loginAndGetToHome(page);
|
||||||
await page.waitForURL("/", { timeout: 30_000 });
|
|
||||||
await expect(page).toHaveURL("/");
|
await expect(page).toHaveURL("/");
|
||||||
|
|
||||||
// Open Settings dialog — look for the gear icon or settings button
|
// Open Settings dialog — look for the gear icon or settings button
|
||||||
|
|||||||
@@ -36,11 +36,13 @@ async function loginFresh(page: import("@playwright/test").Page) {
|
|||||||
await page.getByLabel("Password").fill("admin");
|
await page.getByLabel("Password").fill("admin");
|
||||||
await page.getByRole("button", { name: /login/i }).click();
|
await page.getByRole("button", { name: /login/i }).click();
|
||||||
// May land on "/" or "/analytics-consent" depending on user state
|
// May land on "/" or "/analytics-consent" depending on user state
|
||||||
await page.waitForURL(/\/(analytics-consent)?$/, { timeout: 30_000 });
|
try {
|
||||||
if (page.url().includes("/analytics-consent")) {
|
const acceptBtn = page.getByRole("button", { name: /sure, sounds good/i });
|
||||||
// Accept consent so the test can proceed to the home page
|
await acceptBtn.waitFor({ state: "visible", timeout: 5_000 });
|
||||||
await page.getByRole("button", { name: /sure, sounds good/i }).click();
|
await acceptBtn.click();
|
||||||
await page.waitForURL("/", { timeout: 15_000 });
|
await page.waitForURL("/", { timeout: 30_000 });
|
||||||
|
} catch {
|
||||||
|
await page.waitForURL("/", { timeout: 30_000 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { expect, test as setup } from "@playwright/test";
|
import { expect, test as setup } from "@playwright/test";
|
||||||
|
|
||||||
const authFile =
|
const authFile = path.join(__dirname, "..", "..", "test-results", ".auth", "analytics-user.json");
|
||||||
// Support both playwright.docker.config and playwright.analytics.config
|
|
||||||
path.join(__dirname, "..", "..", "test-results", ".auth", "analytics-user.json");
|
|
||||||
|
|
||||||
setup("authenticate", async ({ page }) => {
|
setup("authenticate", async ({ page }) => {
|
||||||
await page.goto("/login");
|
await page.goto("/login");
|
||||||
@@ -11,15 +9,19 @@ setup("authenticate", async ({ page }) => {
|
|||||||
await page.getByLabel("Password").fill("admin");
|
await page.getByLabel("Password").fill("admin");
|
||||||
await page.getByRole("button", { name: /login/i }).click();
|
await page.getByRole("button", { name: /login/i }).click();
|
||||||
|
|
||||||
// After login, may land on "/" or "/analytics-consent" (fresh user)
|
// Wait for login to complete — page leaves "/login"
|
||||||
await page.waitForURL(/\/(analytics-consent)?$/, { timeout: 30_000 });
|
await page.waitForURL((url) => !url.pathname.startsWith("/login"), {
|
||||||
|
timeout: 30_000,
|
||||||
|
});
|
||||||
|
|
||||||
// If redirected to consent page, accept analytics to proceed
|
// If we landed on the consent page, accept it
|
||||||
if (page.url().includes("/analytics-consent")) {
|
if (page.url().includes("/analytics-consent")) {
|
||||||
await page.getByRole("button", { name: /sure, sounds good/i }).click();
|
await page.getByRole("button", { name: /sure, sounds good/i }).click();
|
||||||
await page.waitForURL("/", { timeout: 15_000 });
|
// Consent page does window.location.href = "/" (full reload)
|
||||||
|
await page.waitForURL("/", { timeout: 30_000 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// At this point we should be on the home page
|
||||||
await expect(page).toHaveURL("/");
|
await expect(page).toHaveURL("/");
|
||||||
await page.context().storageState({ path: authFile });
|
await page.context().storageState({ path: authFile });
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user