auth.spec.ts
1,945 bytes
| 1 | import { test, expect } from '@playwright/test' |
|---|---|
| 2 | |
| 3 | /** |
| 4 | * Smoke auth flow against a live backend on http://localhost:90 with seed data. |
| 5 | * Uses the seed user alice@taltech.ee / Kala.12345. |
| 6 | * |
| 7 | * These tests intentionally avoid creating new users so the seed stays stable. |
| 8 | */ |
| 9 | |
| 10 | test.describe('authentication', () => { |
| 11 | test('login as seed user alice, then logout', async ({ page }) => { |
| 12 | await page.goto('/login') |
| 13 | |
| 14 | await page.getByLabel(/email/i).fill('alice@taltech.ee') |
| 15 | await page.getByLabel(/password/i).fill('Kala.12345') |
| 16 | await page.getByRole('button', { name: /sign in|log ?in/i }).click() |
| 17 | |
| 18 | // After login, the navbar shows a button with the user name. |
| 19 | const userButton = page.getByRole('button', { name: /Alice/i }) |
| 20 | await expect(userButton).toBeVisible({ timeout: 10_000 }) |
| 21 | |
| 22 | // The /trips area is reachable via the navbar link. |
| 23 | await page.getByRole('link', { name: /trips/i }).first().click() |
| 24 | await expect(page).toHaveURL(/\/trips$/) |
| 25 | |
| 26 | // Logout via the avatar button. |
| 27 | await userButton.click() |
| 28 | |
| 29 | // The user button is gone — logout succeeded. |
| 30 | await expect(userButton).toBeHidden() |
| 31 | // And we landed on the home page (not /trips anymore). |
| 32 | await expect(page).not.toHaveURL(/\/trips/) |
| 33 | }) |
| 34 | |
| 35 | test('login with wrong password shows an error and stays on the login page', async ({ page }) => { |
| 36 | await page.goto('/login') |
| 37 | await page.getByLabel(/email/i).fill('alice@taltech.ee') |
| 38 | await page.getByLabel(/password/i).fill('wrong-password-12345') |
| 39 | await page.getByRole('button', { name: /sign in|log ?in/i }).click() |
| 40 | |
| 41 | // Still on login page (URL hasn't changed to /) |
| 42 | await expect(page).toHaveURL(/\/login/) |
| 43 | }) |
| 44 | |
| 45 | test('protected route redirects to login when not authenticated', async ({ page, context }) => { |
| 46 | // Fresh context = no localStorage. |
| 47 | await context.clearCookies() |
| 48 | await page.goto('/trips') |
| 49 | await expect(page).toHaveURL(/\/login/) |
| 50 | }) |
| 51 | }) |
| 52 | |