diff --git a/e2e/fixtures.ts b/e2e/fixtures.ts index 07e12e6..921d9a4 100644 --- a/e2e/fixtures.ts +++ b/e2e/fixtures.ts @@ -1,4 +1,4 @@ -import { test as base, expect } from "@playwright/test"; +import { test as base, expect, type Page } from "@playwright/test"; import AxeBuilder from "@axe-core/playwright"; export const test = base.extend<{ axeCheck: void }>({ @@ -10,3 +10,96 @@ export const test = base.extend<{ axeCheck: void }>({ }); export { expect } from "@playwright/test"; + +const MOCK_USER_ID = "mock_user_123"; +const MOCK_SESSION_ID = "mock_session_456"; + +function mockAuthRoutes(page: Page, authenticated = false) { + page.route(/\/auth\/register/, async (route) => { + await route.fulfill({ + status: 200, + contentType: "application/json", + body: JSON.stringify({ + user: { + id: MOCK_USER_ID, + email: "mock@cartsnitch.test", + name: "Mock User", + emailVerified: true, + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + }, + session: { + id: MOCK_SESSION_ID, + userId: MOCK_USER_ID, + expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toISOString(), + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + ipAddress: null, + userAgent: null, + }, + }), + }); + }); + + page.route(/\/auth\/sign-in\/email/, async (route) => { + await route.fulfill({ + status: 200, + contentType: "application/json", + body: JSON.stringify({ + user: { + id: MOCK_USER_ID, + email: "mock@cartsnitch.test", + name: "Mock User", + emailVerified: true, + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + }, + session: { + id: MOCK_SESSION_ID, + userId: MOCK_USER_ID, + expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toISOString(), + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + ipAddress: null, + userAgent: null, + }, + }), + }); + }); + + page.route(/\/auth\/session/, async (route) => { + if (authenticated) { + await route.fulfill({ + status: 200, + contentType: "application/json", + body: JSON.stringify({ + session: { + id: MOCK_SESSION_ID, + userId: MOCK_USER_ID, + expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toISOString(), + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + ipAddress: null, + userAgent: null, + }, + user: { + id: MOCK_USER_ID, + email: "mock@cartsnitch.test", + name: "Mock User", + emailVerified: true, + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + }, + }), + }); + } else { + await route.fulfill({ + status: 401, + contentType: "application/json", + body: JSON.stringify({ error: "Unauthorized" }), + }); + } + }); +} + +export { mockAuthRoutes }; diff --git a/e2e/journeys/j1-registration-login.spec.ts b/e2e/journeys/j1-registration-login.spec.ts index ec116ab..e860f4f 100644 --- a/e2e/journeys/j1-registration-login.spec.ts +++ b/e2e/journeys/j1-registration-login.spec.ts @@ -1,16 +1,17 @@ import { test, expect } from '@playwright/test'; +import { mockAuthRoutes } from '../fixtures'; const uniqueEmail = () => `betty+e2e-${Date.now()}@cartsnitch.test`; test.describe('J1: Registration and Login', () => { test('can register a new account and lands on dashboard', async ({ page }) => { + mockAuthRoutes(page, true); await page.goto('/register'); await page.fill('[placeholder="Full Name"]', 'Betty Tester'); await page.fill('[placeholder="Email"]', uniqueEmail()); await page.fill('[placeholder="Password (min. 8 characters)"]', 'TestPass123!'); await page.click('button[type="submit"]'); - // With VITE_MOCK_AUTH=true the app navigates to "/" on success await expect(page).toHaveURL('http://localhost:5173/'); await expect(page.getByRole('heading', { name: /cart/i })).toBeVisible(); }); @@ -31,8 +32,8 @@ test.describe('J1: Registration and Login', () => { }); test('can sign in with credentials and land on dashboard', async ({ page }) => { - // Register first so we have a real account const email = uniqueEmail(); + mockAuthRoutes(page, true); await page.goto('/register'); await page.fill('[placeholder="Full Name"]', 'Login Betty'); await page.fill('[placeholder="Email"]', email); @@ -40,11 +41,9 @@ test.describe('J1: Registration and Login', () => { await page.click('button[type="submit"]'); await expect(page).toHaveURL('http://localhost:5173/'); - // Sign out by clearing the mock session (reload with no session) await page.goto('/'); await page.reload(); - // Now sign in await page.goto('/login'); await page.fill('[placeholder="Email"]', email); await page.fill('[placeholder="Password"]', 'TestPass123!'); diff --git a/playwright.config.ts b/playwright.config.ts index b22d74a..a2d7b0b 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -9,7 +9,7 @@ export default defineConfig({ }, ], webServer: { - command: 'VITE_MOCK_AUTH=true npm run dev', + command: 'npm run dev', url: 'http://localhost:5173', reuseExistingServer: !process.env.CI, },