|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | + |
| 3 | +import { appConfigs } from '../presets'; |
| 4 | +import type { FakeUser } from '../testUtils'; |
| 5 | +import { createTestUtils, testAgainstRunningApps } from '../testUtils'; |
| 6 | + |
| 7 | +testAgainstRunningApps({ withEnv: [appConfigs.envs.withEmailCodes] })( |
| 8 | + 'offline session persistence @generic', |
| 9 | + ({ app }) => { |
| 10 | + test.describe.configure({ mode: 'serial' }); |
| 11 | + |
| 12 | + let fakeUser: FakeUser; |
| 13 | + |
| 14 | + test.beforeAll(async () => { |
| 15 | + const u = createTestUtils({ app }); |
| 16 | + fakeUser = u.services.users.createFakeUser(); |
| 17 | + await u.services.users.createBapiUser(fakeUser); |
| 18 | + }); |
| 19 | + |
| 20 | + test.afterAll(async () => { |
| 21 | + await fakeUser.deleteIfExists(); |
| 22 | + await app.teardown(); |
| 23 | + }); |
| 24 | + |
| 25 | + test('user remains signed in after token endpoint outage and recovery', async ({ page, context }) => { |
| 26 | + const u = createTestUtils({ app, page, context }); |
| 27 | + |
| 28 | + await u.po.signIn.goTo(); |
| 29 | + await u.po.signIn.signInWithEmailAndInstantPassword({ |
| 30 | + email: fakeUser.email, |
| 31 | + password: fakeUser.password, |
| 32 | + }); |
| 33 | + await u.po.expect.toBeSignedIn(); |
| 34 | + |
| 35 | + const initialToken = await page.evaluate(() => window.Clerk?.session?.getToken()); |
| 36 | + expect(initialToken).toBeTruthy(); |
| 37 | + |
| 38 | + // Simulate token endpoint outage — requests will fail with network error |
| 39 | + await page.route('**/v1/client/sessions/*/tokens**', route => route.abort('failed')); |
| 40 | + |
| 41 | + // Clear token cache so any subsequent internal refresh hits the failing endpoint |
| 42 | + await page.evaluate(() => window.Clerk?.session?.clearCache()); |
| 43 | + |
| 44 | + // eslint-disable-next-line playwright/no-wait-for-timeout |
| 45 | + await page.waitForTimeout(3_000); |
| 46 | + |
| 47 | + // Restore network |
| 48 | + await page.unrouteAll(); |
| 49 | + |
| 50 | + // The session cookie must NOT have been removed during the outage. |
| 51 | + // Before the fix, empty tokens would be dispatched to AuthCookieService, |
| 52 | + // which interpreted them as sign-out and removed the __session cookie. |
| 53 | + await u.po.expect.toBeSignedIn(); |
| 54 | + |
| 55 | + // Verify recovery: a fresh token can still be obtained |
| 56 | + const recoveredToken = await page.evaluate(() => window.Clerk?.session?.getToken()); |
| 57 | + expect(recoveredToken).toBeTruthy(); |
| 58 | + }); |
| 59 | + |
| 60 | + test('session survives page reload after token endpoint outage', async ({ page, context }) => { |
| 61 | + const u = createTestUtils({ app, page, context }); |
| 62 | + |
| 63 | + await u.po.signIn.goTo(); |
| 64 | + await u.po.signIn.signInWithEmailAndInstantPassword({ |
| 65 | + email: fakeUser.email, |
| 66 | + password: fakeUser.password, |
| 67 | + }); |
| 68 | + await u.po.expect.toBeSignedIn(); |
| 69 | + |
| 70 | + // Fail all token refresh requests |
| 71 | + await page.route('**/v1/client/sessions/*/tokens**', route => route.abort('failed')); |
| 72 | + |
| 73 | + // Force a refresh attempt that will fail |
| 74 | + await page.evaluate(() => window.Clerk?.session?.clearCache()); |
| 75 | + |
| 76 | + // eslint-disable-next-line playwright/no-wait-for-timeout |
| 77 | + await page.waitForTimeout(2_000); |
| 78 | + |
| 79 | + // Restore network before reload |
| 80 | + await page.unrouteAll(); |
| 81 | + |
| 82 | + // Reload the page — if the __session cookie was removed during the outage, |
| 83 | + // the server would treat this as an unauthenticated request |
| 84 | + await page.reload(); |
| 85 | + await u.po.clerk.toBeLoaded(); |
| 86 | + |
| 87 | + await u.po.expect.toBeSignedIn(); |
| 88 | + }); |
| 89 | + |
| 90 | + test('session cookie persists when browser goes fully offline and recovers', async ({ page, context }) => { |
| 91 | + const u = createTestUtils({ app, page, context }); |
| 92 | + |
| 93 | + await u.po.signIn.goTo(); |
| 94 | + await u.po.signIn.signInWithEmailAndInstantPassword({ |
| 95 | + email: fakeUser.email, |
| 96 | + password: fakeUser.password, |
| 97 | + }); |
| 98 | + await u.po.expect.toBeSignedIn(); |
| 99 | + |
| 100 | + // Go fully offline — sets navigator.onLine to false, |
| 101 | + // which triggers the isBrowserOnline() guard in _getToken |
| 102 | + await context.setOffline(true); |
| 103 | + |
| 104 | + // Clear token cache while offline |
| 105 | + await page.evaluate(() => window.Clerk?.session?.clearCache()); |
| 106 | + |
| 107 | + // eslint-disable-next-line playwright/no-wait-for-timeout |
| 108 | + await page.waitForTimeout(2_000); |
| 109 | + |
| 110 | + // Come back online |
| 111 | + await context.setOffline(false); |
| 112 | + |
| 113 | + // Reload — session cookie must still be intact |
| 114 | + await page.reload(); |
| 115 | + await u.po.clerk.toBeLoaded(); |
| 116 | + |
| 117 | + await u.po.expect.toBeSignedIn(); |
| 118 | + |
| 119 | + // Confirm a fresh token can be obtained after recovery |
| 120 | + const token = await page.evaluate(() => window.Clerk?.session?.getToken()); |
| 121 | + expect(token).toBeTruthy(); |
| 122 | + }); |
| 123 | + }, |
| 124 | +); |
0 commit comments