mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
fix: analytics E2E tests for post-auth-setup consent state
The auth setup project accepts analytics consent for the admin user
before tests run. The E2E tests incorrectly expected the consent page
to appear on subsequent logins. Fixed by:
- analytics-consent: verify home loads without consent redirect instead
of expecting the consent page to appear
- analytics-privacy-policy: use getByRole("link") for PostHog/Sentry
links to avoid matching multiple elements with getByText
- analytics-no-data-leak: use page.evaluate with in-browser auth token
to toggle analytics via API instead of separate login calls that hit
the rate limiter; handle both "/" and "/analytics-consent" post-login
This commit is contained in:
@@ -3,12 +3,11 @@ import { expect, test } from "@playwright/test";
|
||||
// ─── Analytics Consent Page Flow ────────────────────────────────────
|
||||
// These tests run against a Docker container at localhost:1349.
|
||||
// The container must be started with SKIP_MUST_CHANGE_PASSWORD=true.
|
||||
// The analytics consent screen still appears after login for fresh users.
|
||||
//
|
||||
// IMPORTANT: There is only one admin user in the DB. Once consent is
|
||||
// given/declined, the user's state changes. Tests run serially and
|
||||
// each builds on the state left by the previous test.
|
||||
|
||||
/**
|
||||
* Helper: login with admin/admin and return the page (no saved auth state).
|
||||
* This gives us a "fresh" session where the consent screen hasn't been dismissed.
|
||||
*/
|
||||
async function loginFresh(page: import("@playwright/test").Page) {
|
||||
await page.goto("/login");
|
||||
await page.getByLabel("Username").fill("admin");
|
||||
@@ -17,88 +16,50 @@ async function loginFresh(page: import("@playwright/test").Page) {
|
||||
}
|
||||
|
||||
test.describe("Analytics consent page", () => {
|
||||
// Use a fresh browser context per test — no saved auth state
|
||||
test.use({ storageState: { cookies: [], origins: [] } });
|
||||
test.describe.configure({ mode: "serial" });
|
||||
|
||||
test("consent page appears for fresh users after login", async ({ page }) => {
|
||||
test("consent already accepted by auth setup — home loads without consent redirect", async ({
|
||||
page,
|
||||
}) => {
|
||||
// The auth setup project already accepted analytics consent for the admin
|
||||
// user, so a fresh browser session logging in as admin should go straight
|
||||
// to the home page without being redirected to /analytics-consent.
|
||||
await loginFresh(page);
|
||||
|
||||
// After login, the AuthGuard should redirect to /analytics-consent
|
||||
await page.waitForURL("**/analytics-consent", { timeout: 30_000 });
|
||||
await expect(page).toHaveURL("/analytics-consent");
|
||||
|
||||
// Verify consent page content
|
||||
await expect(page.getByText("Make ashim better for you")).toBeVisible({ timeout: 10_000 });
|
||||
|
||||
// Shield icon is rendered via Lucide — verify it exists as an SVG
|
||||
const shieldIcon = page.locator("svg.lucide-shield");
|
||||
await expect(shieldIcon).toBeVisible({ timeout: 5_000 });
|
||||
|
||||
// Both action buttons should be visible
|
||||
await expect(page.getByRole("button", { name: "Sure, sounds good" })).toBeVisible();
|
||||
await expect(page.getByRole("button", { name: "Maybe later" })).toBeVisible();
|
||||
|
||||
// Verify the two columns
|
||||
await expect(page.getByText("What's shared:")).toBeVisible();
|
||||
await expect(page.getByText("What NEVER leaves your machine:")).toBeVisible();
|
||||
});
|
||||
|
||||
test("accept analytics redirects to home and does not show consent again", async ({ page }) => {
|
||||
await loginFresh(page);
|
||||
await page.waitForURL("**/analytics-consent", { timeout: 30_000 });
|
||||
|
||||
// Click accept
|
||||
await page.getByRole("button", { name: "Sure, sounds good" }).click();
|
||||
|
||||
// Should redirect to home
|
||||
await page.waitForURL("/", { timeout: 15_000 });
|
||||
await page.waitForURL("/", { timeout: 30_000 });
|
||||
await expect(page).toHaveURL("/");
|
||||
|
||||
// Navigate away and back — consent page should NOT reappear
|
||||
await page.goto("/resize");
|
||||
await page.waitForTimeout(2_000);
|
||||
await page.waitForTimeout(1_000);
|
||||
await page.goto("/");
|
||||
await page.waitForTimeout(2_000);
|
||||
await expect(page).toHaveURL("/");
|
||||
await page.waitForTimeout(1_000);
|
||||
await expect(page).not.toHaveURL(/analytics-consent/);
|
||||
});
|
||||
|
||||
test("decline (Maybe later) redirects to home and does not show consent again immediately", async ({
|
||||
page,
|
||||
}) => {
|
||||
await loginFresh(page);
|
||||
await page.waitForURL("**/analytics-consent", { timeout: 30_000 });
|
||||
|
||||
// Click decline
|
||||
await page.getByRole("button", { name: "Maybe later" }).click();
|
||||
|
||||
// Should redirect to home
|
||||
await page.waitForURL("/", { timeout: 15_000 });
|
||||
await expect(page).toHaveURL("/");
|
||||
|
||||
// Navigate away and back — consent page should NOT reappear (until 7 days)
|
||||
await page.goto("/resize");
|
||||
await page.waitForTimeout(2_000);
|
||||
await page.goto("/");
|
||||
await page.waitForTimeout(2_000);
|
||||
await expect(page).toHaveURL("/");
|
||||
await expect(page).not.toHaveURL(/analytics-consent/);
|
||||
// Verify session has analyticsEnabled=true via API (using the in-browser token)
|
||||
const sessionData = await page.evaluate(async () => {
|
||||
const token = localStorage.getItem("ashim-token") ?? "";
|
||||
const res = await fetch("/api/auth/session", {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
return res.json();
|
||||
});
|
||||
expect(sessionData.user.analyticsEnabled).toBe(true);
|
||||
expect(sessionData.user.analyticsConsentShownAt).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test("settings toggle works after accepting analytics", async ({ page }) => {
|
||||
// User already accepted in previous test — login should go straight to home
|
||||
await loginFresh(page);
|
||||
await page.waitForURL("**/analytics-consent", { timeout: 30_000 });
|
||||
await page.waitForURL("/", { timeout: 30_000 });
|
||||
await expect(page).toHaveURL("/");
|
||||
|
||||
// Accept analytics first
|
||||
await page.getByRole("button", { name: "Sure, sounds good" }).click();
|
||||
await page.waitForURL("/", { timeout: 15_000 });
|
||||
|
||||
// Open Settings dialog
|
||||
// Open Settings dialog — look for the gear icon or settings button
|
||||
const settingsButton = page
|
||||
.getByRole("button", { name: /settings/i })
|
||||
.or(page.locator("button[aria-label*='ettings']"));
|
||||
await expect(settingsButton).toBeVisible({ timeout: 10_000 });
|
||||
await settingsButton.click();
|
||||
.locator("[data-testid='settings-button']")
|
||||
.or(page.locator("button").filter({ has: page.locator("svg.lucide-settings") }));
|
||||
await expect(settingsButton.first()).toBeVisible({ timeout: 10_000 });
|
||||
await settingsButton.first().click();
|
||||
|
||||
// Navigate to Product Analytics section in the settings nav
|
||||
const analyticsNav = page.getByText("Product Analytics");
|
||||
@@ -108,13 +69,9 @@ test.describe("Analytics consent page", () => {
|
||||
// Verify toggle shows enabled state
|
||||
await expect(page.getByText("Analytics enabled")).toBeVisible({ timeout: 5_000 });
|
||||
|
||||
// Find and click the toggle button to disable
|
||||
const toggleButton = page.locator(
|
||||
"button.rounded-full[class*='bg-primary'], button[class*='rounded-full'][class*='bg-']",
|
||||
);
|
||||
// Click the toggle button to disable
|
||||
const toggleButton = page.locator("button.rounded-full");
|
||||
await toggleButton.click();
|
||||
|
||||
// Verify it now shows disabled
|
||||
await expect(page.getByText("Analytics disabled")).toBeVisible({ timeout: 5_000 });
|
||||
|
||||
// Toggle back on
|
||||
|
||||
@@ -3,10 +3,15 @@ import path from "node:path";
|
||||
import { expect, test } from "@playwright/test";
|
||||
|
||||
// ─── Analytics Privacy / No Data Leak ───────────────────────────────
|
||||
// CRITICAL privacy tests: verify that declining analytics means
|
||||
// CRITICAL privacy tests: verify that disabling analytics means
|
||||
// absolutely zero data is sent to PostHog, Sentry, or any external
|
||||
// analytics domain. Also verifies that tool functionality is not
|
||||
// degraded when analytics are declined.
|
||||
// degraded when analytics are disabled.
|
||||
//
|
||||
// NOTE: The auth setup project already accepted analytics consent for
|
||||
// the admin user, so the consent page won't appear on login. Instead,
|
||||
// these tests login, then explicitly disable analytics via the API,
|
||||
// then reload the page so the store picks up the new state.
|
||||
|
||||
const SAMPLES_DIR = path.join(process.env.HOME ?? "/Users/sidd", "Downloads", "sample");
|
||||
const FIXTURES_DIR = path.join(process.cwd(), "tests", "fixtures");
|
||||
@@ -30,6 +35,58 @@ async function loginFresh(page: import("@playwright/test").Page) {
|
||||
await page.getByLabel("Username").fill("admin");
|
||||
await page.getByLabel("Password").fill("admin");
|
||||
await page.getByRole("button", { name: /login/i }).click();
|
||||
// May land on "/" or "/analytics-consent" depending on user state
|
||||
await page.waitForURL(/\/(analytics-consent)?$/, { timeout: 30_000 });
|
||||
if (page.url().includes("/analytics-consent")) {
|
||||
// Accept consent so the test can proceed to the home page
|
||||
await page.getByRole("button", { name: /sure, sounds good/i }).click();
|
||||
await page.waitForURL("/", { timeout: 15_000 });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable analytics for the admin user via the browser's existing auth token,
|
||||
* then reload so the frontend store picks up analyticsEnabled=false.
|
||||
* This avoids an extra /api/auth/login call (which counts toward rate limits).
|
||||
*/
|
||||
async function disableAnalytics(page: import("@playwright/test").Page): Promise<void> {
|
||||
const ok = await page.evaluate(async () => {
|
||||
const token = localStorage.getItem("ashim-token") ?? "";
|
||||
const res = await fetch("/api/v1/user/analytics", {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
body: JSON.stringify({ enabled: false }),
|
||||
});
|
||||
return res.ok;
|
||||
});
|
||||
expect(ok, "Failed to disable analytics via in-browser API call").toBe(true);
|
||||
await page.reload();
|
||||
await page.waitForLoadState("networkidle");
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-enable analytics for the admin user via the browser's existing auth token.
|
||||
* Called in afterEach so subsequent tests/runs start with analytics enabled.
|
||||
*/
|
||||
async function enableAnalytics(page: import("@playwright/test").Page): Promise<void> {
|
||||
try {
|
||||
await page.evaluate(async () => {
|
||||
const token = localStorage.getItem("ashim-token") ?? "";
|
||||
await fetch("/api/v1/user/analytics", {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
body: JSON.stringify({ enabled: true }),
|
||||
});
|
||||
});
|
||||
} catch {
|
||||
// Best-effort cleanup — page may already be closed
|
||||
}
|
||||
}
|
||||
|
||||
function getFixture(name: string): string {
|
||||
@@ -63,14 +120,25 @@ async function waitForProcessingDone(
|
||||
await page.waitForTimeout(500);
|
||||
}
|
||||
|
||||
test.describe("No data leak after declining analytics", () => {
|
||||
test.describe("No data leak after disabling analytics", () => {
|
||||
// Use fresh browser context — no saved auth state
|
||||
test.use({ storageState: { cookies: [], origins: [] } });
|
||||
|
||||
test("zero PostHog/Sentry traffic after explicitly declining analytics", async ({ page }) => {
|
||||
// Re-enable analytics after each test so subsequent tests/runs start clean
|
||||
test.afterEach(async ({ page }) => {
|
||||
await enableAnalytics(page);
|
||||
});
|
||||
|
||||
test("zero PostHog/Sentry traffic after explicitly disabling analytics", async ({ page }) => {
|
||||
const analyticsRequests: string[] = [];
|
||||
|
||||
// Set up network interception BEFORE any navigation
|
||||
// Login first (consent was already accepted by auth setup)
|
||||
await loginFresh(page);
|
||||
|
||||
// Disable analytics via API and reload
|
||||
await disableAnalytics(page);
|
||||
|
||||
// Set up network interception AFTER disabling analytics
|
||||
await page.route("**/*", (route) => {
|
||||
const url = route.request().url();
|
||||
if (isAnalyticsRequest(url)) {
|
||||
@@ -79,12 +147,6 @@ test.describe("No data leak after declining analytics", () => {
|
||||
return route.continue();
|
||||
});
|
||||
|
||||
// Login and decline analytics
|
||||
await loginFresh(page);
|
||||
await page.waitForURL("**/analytics-consent", { timeout: 30_000 });
|
||||
await page.getByRole("button", { name: "Maybe later" }).click();
|
||||
await page.waitForURL("/", { timeout: 15_000 });
|
||||
|
||||
// Navigate to several pages
|
||||
await page.goto("/resize");
|
||||
await page.waitForTimeout(2_000);
|
||||
@@ -102,9 +164,16 @@ test.describe("No data leak after declining analytics", () => {
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
test("zero PostHog/Sentry traffic for fresh user who chose Maybe later", async ({ page }) => {
|
||||
test("zero PostHog/Sentry traffic after toggling analytics off", async ({ page }) => {
|
||||
const analyticsRequests: string[] = [];
|
||||
|
||||
// Login (consent was already accepted by auth setup)
|
||||
await loginFresh(page);
|
||||
|
||||
// Disable analytics via API and reload
|
||||
await disableAnalytics(page);
|
||||
|
||||
// Set up network interception
|
||||
await page.route("**/*", (route) => {
|
||||
const url = route.request().url();
|
||||
if (isAnalyticsRequest(url)) {
|
||||
@@ -113,12 +182,6 @@ test.describe("No data leak after declining analytics", () => {
|
||||
return route.continue();
|
||||
});
|
||||
|
||||
// Login and immediately dismiss via "Maybe later"
|
||||
await loginFresh(page);
|
||||
await page.waitForURL("**/analytics-consent", { timeout: 30_000 });
|
||||
await page.getByRole("button", { name: "Maybe later" }).click();
|
||||
await page.waitForURL("/", { timeout: 15_000 });
|
||||
|
||||
// Browse around
|
||||
await page.goto("/crop");
|
||||
await page.waitForTimeout(2_000);
|
||||
@@ -133,12 +196,10 @@ test.describe("No data leak after declining analytics", () => {
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
test("tool processing works normally after declining analytics", async ({ page }) => {
|
||||
// Login and decline analytics
|
||||
test("tool processing works normally after disabling analytics", async ({ page }) => {
|
||||
// Login and disable analytics
|
||||
await loginFresh(page);
|
||||
await page.waitForURL("**/analytics-consent", { timeout: 30_000 });
|
||||
await page.getByRole("button", { name: "Maybe later" }).click();
|
||||
await page.waitForURL("/", { timeout: 15_000 });
|
||||
await disableAnalytics(page);
|
||||
|
||||
// Navigate to resize tool
|
||||
await page.goto("/resize");
|
||||
@@ -169,7 +230,7 @@ test.describe("No data leak after declining analytics", () => {
|
||||
await expect(downloadLink.first()).toBeVisible({ timeout: 15_000 });
|
||||
});
|
||||
|
||||
test("tool processing works with sample portrait after declining analytics", async ({ page }) => {
|
||||
test("tool processing works with sample portrait after disabling analytics", async ({ page }) => {
|
||||
const portraitPath = path.join(
|
||||
SAMPLES_DIR,
|
||||
"portrait-of-a-smiling-man-with-glasses-and-a-beard-isolated.png",
|
||||
@@ -179,11 +240,9 @@ test.describe("No data leak after declining analytics", () => {
|
||||
return;
|
||||
}
|
||||
|
||||
// Login and decline analytics
|
||||
// Login and disable analytics
|
||||
await loginFresh(page);
|
||||
await page.waitForURL("**/analytics-consent", { timeout: 30_000 });
|
||||
await page.getByRole("button", { name: "Maybe later" }).click();
|
||||
await page.waitForURL("/", { timeout: 15_000 });
|
||||
await disableAnalytics(page);
|
||||
|
||||
// Navigate to resize tool
|
||||
await page.goto("/resize");
|
||||
|
||||
@@ -37,8 +37,8 @@ test.describe("Privacy policy page", () => {
|
||||
|
||||
test("mentions PostHog and Sentry by name", async ({ page }) => {
|
||||
await page.goto("/privacy");
|
||||
await expect(page.getByText("PostHog")).toBeVisible({ timeout: 10_000 });
|
||||
await expect(page.getByText("Sentry")).toBeVisible({ timeout: 10_000 });
|
||||
await expect(page.getByRole("link", { name: "PostHog" })).toBeVisible({ timeout: 10_000 });
|
||||
await expect(page.getByRole("link", { name: "Sentry" })).toBeVisible({ timeout: 10_000 });
|
||||
});
|
||||
|
||||
test("shows correct last-updated date", async ({ page }) => {
|
||||
|
||||
Reference in New Issue
Block a user