diff --git a/frontend/e2e/appointments.spec.ts b/frontend/e2e/appointments.spec.ts index a2f4f14..abf6de2 100644 --- a/frontend/e2e/appointments.spec.ts +++ b/frontend/e2e/appointments.spec.ts @@ -1,5 +1,6 @@ import { test, expect, Page } from "@playwright/test"; import { formatDateLocal, getTomorrowDateStr } from "./helpers/date"; +import { API_URL, REGULAR_USER, ADMIN_USER, clearAuth, loginUser } from "./helpers/auth"; /** * Appointments Page E2E Tests @@ -7,38 +8,6 @@ import { formatDateLocal, getTomorrowDateStr } from "./helpers/date"; * 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 async function createTestBooking(page: Page) { const dateStr = getTomorrowDateStr(); diff --git a/frontend/e2e/availability.spec.ts b/frontend/e2e/availability.spec.ts index 9cb4767..19254ad 100644 --- a/frontend/e2e/availability.spec.ts +++ b/frontend/e2e/availability.spec.ts @@ -1,5 +1,6 @@ import { test, expect, Page } from "@playwright/test"; import { formatDateLocal, getTomorrowDateStr } from "./helpers/date"; +import { API_URL, REGULAR_USER, ADMIN_USER, clearAuth, loginUser } from "./helpers/auth"; /** * Availability Page E2E Tests @@ -7,38 +8,6 @@ import { formatDateLocal, getTomorrowDateStr } from "./helpers/date"; * 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 function getTomorrowDisplay(): string { const tomorrow = new Date(); diff --git a/frontend/e2e/booking.spec.ts b/frontend/e2e/booking.spec.ts index ac7a8f4..1234af2 100644 --- a/frontend/e2e/booking.spec.ts +++ b/frontend/e2e/booking.spec.ts @@ -1,5 +1,6 @@ import { test, expect, Page } from "@playwright/test"; import { formatDateLocal, getTomorrowDateStr } from "./helpers/date"; +import { API_URL, REGULAR_USER, ADMIN_USER, clearAuth, loginUser } from "./helpers/auth"; /** * Booking Page E2E Tests @@ -7,38 +8,6 @@ import { formatDateLocal, getTomorrowDateStr } from "./helpers/date"; * 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 async function setAvailability(page: Page, dateStr: string, maxRetries = 3) { const cookies = await page.context().cookies(); diff --git a/frontend/e2e/helpers/auth.ts b/frontend/e2e/helpers/auth.ts new file mode 100644 index 0000000..89ee01b --- /dev/null +++ b/frontend/e2e/helpers/auth.ts @@ -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 }); +} +