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
This commit is contained in:
counterweight 2025-12-21 17:59:48 +01:00
parent 3369a71271
commit 3d83472b97
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
4 changed files with 43 additions and 96 deletions

View file

@ -1,5 +1,6 @@
import { test, expect, Page } from "@playwright/test"; import { test, expect, Page } from "@playwright/test";
import { formatDateLocal, getTomorrowDateStr } from "./helpers/date"; import { formatDateLocal, getTomorrowDateStr } from "./helpers/date";
import { API_URL, REGULAR_USER, ADMIN_USER, clearAuth, loginUser } from "./helpers/auth";
/** /**
* Appointments Page E2E Tests * Appointments Page E2E Tests
@ -7,38 +8,6 @@ import { formatDateLocal, getTomorrowDateStr } from "./helpers/date";
* Tests for viewing and cancelling user appointments. * Tests for viewing and cancelling user appointments.
*/ */
const API_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000";
function getRequiredEnv(name: string): string {
const value = process.env[name];
if (!value) {
throw new Error(`Required environment variable ${name} is not set.`);
}
return value;
}
const REGULAR_USER = {
email: getRequiredEnv("DEV_USER_EMAIL"),
password: getRequiredEnv("DEV_USER_PASSWORD"),
};
const ADMIN_USER = {
email: getRequiredEnv("DEV_ADMIN_EMAIL"),
password: getRequiredEnv("DEV_ADMIN_PASSWORD"),
};
async function clearAuth(page: Page) {
await page.context().clearCookies();
}
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 });
}
// Set up availability and create a booking // Set up availability and create a booking
async function createTestBooking(page: Page) { async function createTestBooking(page: Page) {
const dateStr = getTomorrowDateStr(); const dateStr = getTomorrowDateStr();

View file

@ -1,5 +1,6 @@
import { test, expect, Page } from "@playwright/test"; import { test, expect, Page } from "@playwright/test";
import { formatDateLocal, getTomorrowDateStr } from "./helpers/date"; import { formatDateLocal, getTomorrowDateStr } from "./helpers/date";
import { API_URL, REGULAR_USER, ADMIN_USER, clearAuth, loginUser } from "./helpers/auth";
/** /**
* Availability Page E2E Tests * Availability Page E2E Tests
@ -7,38 +8,6 @@ import { formatDateLocal, getTomorrowDateStr } from "./helpers/date";
* Tests for the admin availability management page. * Tests for the admin availability management page.
*/ */
const API_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000";
function getRequiredEnv(name: string): string {
const value = process.env[name];
if (!value) {
throw new Error(`Required environment variable ${name} is not set.`);
}
return value;
}
const REGULAR_USER = {
email: getRequiredEnv("DEV_USER_EMAIL"),
password: getRequiredEnv("DEV_USER_PASSWORD"),
};
const ADMIN_USER = {
email: getRequiredEnv("DEV_ADMIN_EMAIL"),
password: getRequiredEnv("DEV_ADMIN_PASSWORD"),
};
async function clearAuth(page: Page) {
await page.context().clearCookies();
}
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 });
}
// Helper to get tomorrow's date in the format displayed on the page // Helper to get tomorrow's date in the format displayed on the page
function getTomorrowDisplay(): string { function getTomorrowDisplay(): string {
const tomorrow = new Date(); const tomorrow = new Date();

View file

@ -1,5 +1,6 @@
import { test, expect, Page } from "@playwright/test"; import { test, expect, Page } from "@playwright/test";
import { formatDateLocal, getTomorrowDateStr } from "./helpers/date"; import { formatDateLocal, getTomorrowDateStr } from "./helpers/date";
import { API_URL, REGULAR_USER, ADMIN_USER, clearAuth, loginUser } from "./helpers/auth";
/** /**
* Booking Page E2E Tests * Booking Page E2E Tests
@ -7,38 +8,6 @@ import { formatDateLocal, getTomorrowDateStr } from "./helpers/date";
* Tests for the user booking page. * Tests for the user booking page.
*/ */
const API_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000";
function getRequiredEnv(name: string): string {
const value = process.env[name];
if (!value) {
throw new Error(`Required environment variable ${name} is not set.`);
}
return value;
}
const REGULAR_USER = {
email: getRequiredEnv("DEV_USER_EMAIL"),
password: getRequiredEnv("DEV_USER_PASSWORD"),
};
const ADMIN_USER = {
email: getRequiredEnv("DEV_ADMIN_EMAIL"),
password: getRequiredEnv("DEV_ADMIN_PASSWORD"),
};
async function clearAuth(page: Page) {
await page.context().clearCookies();
}
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 });
}
// Set up availability for a date using the API with retry logic // Set up availability for a date using the API with retry logic
async function setAvailability(page: Page, dateStr: string, maxRetries = 3) { async function setAvailability(page: Page, dateStr: string, maxRetries = 3) {
const cookies = await page.context().cookies(); const cookies = await page.context().cookies();

View file

@ -0,0 +1,40 @@
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 });
}