|
| 1 | +import { test, expect, type BrowserContext, type Page } from '@playwright/test' |
| 2 | +import { loginAndSetCookie, makeExpiredJwt } from '@/utils/helpers' |
| 3 | + |
| 4 | +const COOKIE_PREFIX = 'localhost_' |
| 5 | +const HP_COOKIE = `${COOKIE_PREFIX}jwt_hp` |
| 6 | + |
| 7 | +test.describe('auth token handling', () => { |
| 8 | + test('expired jwt is refreshed and the protected page loads', async ({ |
| 9 | + page, |
| 10 | + context, |
| 11 | + }) => { |
| 12 | + await loginAndSetCookie(page, context, 'test@example.com') |
| 13 | + |
| 14 | + // Replace the readable jwt_hp part with an expired token; the real refresh_token stays |
| 15 | + await context.addCookies([ |
| 16 | + { name: HP_COOKIE, value: makeExpiredJwt(), domain: 'localhost', path: '/' }, |
| 17 | + ]) |
| 18 | + |
| 19 | + await page.goto('/camps') |
| 20 | + await expect(page).toHaveURL(/\/camps/, { timeout: 15000 }) |
| 21 | + }) |
| 22 | + |
| 23 | + test('redirects to login when the refresh fails', async ({ page }) => { |
| 24 | + await page.route(/\/token\/refresh$/, (route) => |
| 25 | + route.fulfill({ status: 401, body: '' }) |
| 26 | + ) |
| 27 | + |
| 28 | + await page.goto('/camps') |
| 29 | + await expect(page).toHaveURL(/\/login/) |
| 30 | + }) |
| 31 | + |
| 32 | + test('skips refresh and redirects to login when refresh token is known expired', async ({ |
| 33 | + page, |
| 34 | + }) => { |
| 35 | + await page.addInitScript(() => { |
| 36 | + localStorage.setItem('refreshTokenExpiresAt', '1') |
| 37 | + }) |
| 38 | + |
| 39 | + await page.goto('/camps') |
| 40 | + await expect(page).toHaveURL(/\/login/) |
| 41 | + }) |
| 42 | + |
| 43 | + test('mid-session 401 triggers transparent refresh and page stays loaded', async ({ |
| 44 | + page, |
| 45 | + context, |
| 46 | + }) => { |
| 47 | + await loginAndSetCookie(page, context, 'test@example.com') |
| 48 | + |
| 49 | + // Make the first camps request fail with 401; no anchor so query params are ignored |
| 50 | + let failed = false |
| 51 | + await page.route(/\/api\/camps/, async (route) => { |
| 52 | + if (!failed) { |
| 53 | + failed = true |
| 54 | + await route.fulfill({ status: 401, body: '' }) |
| 55 | + } else { |
| 56 | + await route.continue() |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + await page.reload() |
| 61 | + await expect(page).toHaveURL(/\/camps/, { timeout: 15000 }) |
| 62 | + }) |
| 63 | + |
| 64 | + test('mid-session 401 with failed refresh redirects to login', async ({ |
| 65 | + page, |
| 66 | + context, |
| 67 | + }) => { |
| 68 | + await loginAndSetCookie(page, context, 'test@example.com') |
| 69 | + |
| 70 | + await page.route(/\/api\/camps/, (route) => route.fulfill({ status: 401, body: '' })) |
| 71 | + await page.route(/\/token\/refresh$/, (route) => |
| 72 | + route.fulfill({ status: 401, body: '' }) |
| 73 | + ) |
| 74 | + |
| 75 | + await page.reload() |
| 76 | + await expect(page).toHaveURL(/\/login/, { timeout: 15000 }) |
| 77 | + }) |
| 78 | + |
| 79 | + test('token refreshed in one tab keeps another tab authenticated', async ({ |
| 80 | + browser, |
| 81 | + }) => { |
| 82 | + const context: BrowserContext = await browser.newContext() |
| 83 | + |
| 84 | + try { |
| 85 | + const tabA: Page = await context.newPage() |
| 86 | + const tabB: Page = await context.newPage() |
| 87 | + |
| 88 | + await loginAndSetCookie(tabA, context, 'test@example.com') |
| 89 | + await tabB.goto('/camps') |
| 90 | + await expect(tabB).toHaveURL(/\/camps/) |
| 91 | + |
| 92 | + // Make the first camps request in tab A return 401, triggering a real token refresh |
| 93 | + let failed = false |
| 94 | + await context.route(/\/api\/camps/, async (route) => { |
| 95 | + if (!failed) { |
| 96 | + failed = true |
| 97 | + await route.fulfill({ status: 401, body: '' }) |
| 98 | + } else { |
| 99 | + await route.continue() |
| 100 | + } |
| 101 | + }) |
| 102 | + |
| 103 | + await tabA.goto('/camps') |
| 104 | + await expect(tabA).toHaveURL(/\/camps/, { timeout: 15000 }) |
| 105 | + |
| 106 | + // After the token rotated in tab A, tab B must still work |
| 107 | + await tabB.reload() |
| 108 | + await expect(tabB).toHaveURL(/\/camps/, { timeout: 15000 }) |
| 109 | + } finally { |
| 110 | + await context.close() |
| 111 | + } |
| 112 | + }) |
| 113 | +}) |
0 commit comments