-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathauth.setup.ts
More file actions
37 lines (30 loc) · 1.5 KB
/
auth.setup.ts
File metadata and controls
37 lines (30 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { test as setup, expect } from '@playwright/test'
const TEST_USERS = [
{ name: 'student', username: 'student', password: 'student' },
{ name: 'advisor', username: 'advisor', password: 'advisor' },
{ name: 'supervisor', username: 'supervisor', password: 'supervisor' },
{ name: 'admin', username: 'admin', password: 'admin' },
] as const
for (const user of TEST_USERS) {
setup(`authenticate as ${user.name}`, async ({ page }) => {
// Navigate to a protected route to trigger Keycloak login redirect
await page.goto('/dashboard')
// Wait for Keycloak login page to load
await expect(page.locator('#kc-login')).toBeVisible({ timeout: 30_000 })
// Fill in credentials on the Keycloak login form
await page.locator('#username').fill(user.username)
await page.locator('#password').fill(user.password)
await page.locator('#kc-login').click()
// Wait for redirect back to the app and the dashboard to load
await expect(page).toHaveURL(/localhost:3000/, { timeout: 30_000 })
// Wait for the app to fully initialize with the auth tokens
await page.waitForFunction(() => {
const tokens = localStorage.getItem('authentication_tokens')
if (!tokens) return false
const parsed = JSON.parse(tokens)
return !!parsed.access_token && !!parsed.refresh_token
}, { timeout: 15_000 })
// Save the authenticated state (localStorage + cookies including Keycloak session)
await page.context().storageState({ path: `e2e/.auth/${user.name}.json` })
})
}