arbret/frontend/e2e/helpers/auth.ts
counterweight 3d83472b97
Extract duplicate test helper functions to shared module
- Created frontend/e2e/helpers/auth.ts with shared auth utilities
- Extracted getRequiredEnv, REGULAR_USER, ADMIN_USER, clearAuth, loginUser
- Updated all three e2e test files to use shared helpers
- Reduced code duplication across test files
2025-12-21 17:59:48 +01:00

40 lines
1 KiB
TypeScript

import { Page } from "@playwright/test";
/**
* Auth helpers for e2e tests.
*/
const API_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000";
export { API_URL };
export function getRequiredEnv(name: string): string {
const value = process.env[name];
if (!value) {
throw new Error(`Required environment variable ${name} is not set.`);
}
return value;
}
export const REGULAR_USER = {
email: getRequiredEnv("DEV_USER_EMAIL"),
password: getRequiredEnv("DEV_USER_PASSWORD"),
};
export const ADMIN_USER = {
email: getRequiredEnv("DEV_ADMIN_EMAIL"),
password: getRequiredEnv("DEV_ADMIN_PASSWORD"),
};
export async function clearAuth(page: Page) {
await page.context().clearCookies();
}
export async function loginUser(page: Page, email: string, password: string) {
await page.goto("/login");
await page.fill('input[type="email"]', email);
await page.fill('input[type="password"]', password);
await page.click('button[type="submit"]');
await page.waitForURL((url) => !url.pathname.includes("/login"), { timeout: 10000 });
}