|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { eventBus, events } from '../../events'; |
| 4 | + |
| 5 | +const mocks = vi.hoisted(() => ({ |
| 6 | + sessionCookie: { set: vi.fn(), remove: vi.fn(), get: vi.fn() }, |
| 7 | + clientUatCookie: { set: vi.fn(), remove: vi.fn(), get: vi.fn(() => 0) }, |
| 8 | + activeContextCookie: { set: vi.fn(), remove: vi.fn(), get: vi.fn<() => string | undefined>(() => undefined) }, |
| 9 | + inCrossOriginIframe: vi.fn(() => false), |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock('../cookies/session', () => ({ createSessionCookie: () => mocks.sessionCookie })); |
| 13 | +vi.mock('../cookies/clientUat', () => ({ createClientUatCookie: () => mocks.clientUatCookie })); |
| 14 | +vi.mock('../cookies/activeContext', () => ({ createActiveContextCookie: () => mocks.activeContextCookie })); |
| 15 | +vi.mock('../cookieSuffix', () => ({ getCookieSuffix: vi.fn(() => Promise.resolve('suffix')) })); |
| 16 | +vi.mock('../devBrowser', () => ({ |
| 17 | + createDevBrowser: () => ({ |
| 18 | + clear: vi.fn(), |
| 19 | + setup: vi.fn(() => Promise.resolve()), |
| 20 | + getDevBrowser: vi.fn(() => 'deadbeef'), |
| 21 | + refreshCookies: vi.fn(), |
| 22 | + }), |
| 23 | +})); |
| 24 | +vi.mock('@clerk/shared/internal/clerk-js/runtime', async importOriginal => { |
| 25 | + const actual = await importOriginal<Record<string, unknown>>(); |
| 26 | + return { ...actual, inCrossOriginIframe: () => mocks.inCrossOriginIframe() }; |
| 27 | +}); |
| 28 | + |
| 29 | +import { AuthCookieService } from '../AuthCookieService'; |
| 30 | + |
| 31 | +const setFocus = (hasFocus: boolean) => |
| 32 | + Object.defineProperty(document, 'hasFocus', { value: () => hasFocus, configurable: true }); |
| 33 | +const setVisibility = (state: DocumentVisibilityState) => |
| 34 | + Object.defineProperty(document, 'visibilityState', { value: state, configurable: true }); |
| 35 | + |
| 36 | +describe('AuthCookieService session cookie refresh', () => { |
| 37 | + const getToken = vi.fn(() => Promise.resolve('fresh-jwt')); |
| 38 | + const clerkStub = { |
| 39 | + publishableKey: 'pk_test_Y2xlcmsuYWJjZWYuMTIzNDUuZGV2LmxjbGNsZXJrLmNvbSQ', |
| 40 | + frontendApi: 'clerk.abcef.12345.dev.lclclerk.com', |
| 41 | + loaded: true, |
| 42 | + session: { id: 'sess_active', getToken }, |
| 43 | + organization: null, |
| 44 | + user: {}, |
| 45 | + client: {}, |
| 46 | + handleUnauthenticated: vi.fn(), |
| 47 | + } as any; |
| 48 | + const clerkEventBusStub = { emit: vi.fn(), on: vi.fn(), prioritizedOn: vi.fn() } as any; |
| 49 | + |
| 50 | + const createService = () => AuthCookieService.create(clerkStub, {} as any, 'production', clerkEventBusStub); |
| 51 | + const emitTokenUpdate = (raw: string) => |
| 52 | + eventBus.emit(events.TokenUpdate, { token: { getRawString: () => raw } as any }); |
| 53 | + |
| 54 | + let service: Awaited<ReturnType<typeof createService>> | undefined; |
| 55 | + |
| 56 | + beforeEach(() => { |
| 57 | + vi.clearAllMocks(); |
| 58 | + mocks.inCrossOriginIframe.mockReturnValue(false); |
| 59 | + mocks.activeContextCookie.get.mockReturnValue(undefined); |
| 60 | + getToken.mockResolvedValue('fresh-jwt'); |
| 61 | + setFocus(true); |
| 62 | + setVisibility('visible'); |
| 63 | + }); |
| 64 | + |
| 65 | + afterEach(() => { |
| 66 | + service?.stopPollingForToken(); |
| 67 | + service = undefined; |
| 68 | + // The service registers listeners on the shared event bus on construction. |
| 69 | + eventBus.off(events.TokenUpdate); |
| 70 | + eventBus.off(events.UserSignOut); |
| 71 | + eventBus.off(events.EnvironmentUpdate); |
| 72 | + }); |
| 73 | + |
| 74 | + it('registers both focus and visibilitychange listeners', async () => { |
| 75 | + const windowSpy = vi.spyOn(window, 'addEventListener'); |
| 76 | + const documentSpy = vi.spyOn(document, 'addEventListener'); |
| 77 | + |
| 78 | + service = await createService(); |
| 79 | + |
| 80 | + expect(windowSpy).toHaveBeenCalledWith('focus', expect.any(Function)); |
| 81 | + expect(documentSpy).toHaveBeenCalledWith('visibilitychange', expect.any(Function)); |
| 82 | + }); |
| 83 | + |
| 84 | + it('writes the session cookie on token:update when the tab is focused', async () => { |
| 85 | + service = await createService(); |
| 86 | + |
| 87 | + emitTokenUpdate('jwt-focused'); |
| 88 | + |
| 89 | + expect(mocks.sessionCookie.set).toHaveBeenCalledWith('jwt-focused'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('does not write when unfocused, outside an iframe, and the active context does not match', async () => { |
| 93 | + mocks.activeContextCookie.get.mockReturnValue('sess_other:'); |
| 94 | + service = await createService(); |
| 95 | + setFocus(false); |
| 96 | + |
| 97 | + emitTokenUpdate('jwt-blocked'); |
| 98 | + |
| 99 | + expect(mocks.sessionCookie.set).not.toHaveBeenCalledWith('jwt-blocked'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('writes when unfocused but in a visible cross-origin iframe, even if the active context does not match', async () => { |
| 103 | + mocks.activeContextCookie.get.mockReturnValue('sess_other:'); |
| 104 | + mocks.inCrossOriginIframe.mockReturnValue(true); |
| 105 | + service = await createService(); |
| 106 | + setFocus(false); |
| 107 | + setVisibility('visible'); |
| 108 | + |
| 109 | + emitTokenUpdate('jwt-iframe'); |
| 110 | + |
| 111 | + expect(mocks.sessionCookie.set).toHaveBeenCalledWith('jwt-iframe'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('does not write in a cross-origin iframe while it is hidden', async () => { |
| 115 | + mocks.activeContextCookie.get.mockReturnValue('sess_other:'); |
| 116 | + mocks.inCrossOriginIframe.mockReturnValue(true); |
| 117 | + service = await createService(); |
| 118 | + setFocus(false); |
| 119 | + setVisibility('hidden'); |
| 120 | + |
| 121 | + emitTokenUpdate('jwt-hidden'); |
| 122 | + |
| 123 | + expect(mocks.sessionCookie.set).not.toHaveBeenCalledWith('jwt-hidden'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('refreshes the session cookie when an unfocused iframe becomes visible (visibilitychange)', async () => { |
| 127 | + mocks.inCrossOriginIframe.mockReturnValue(true); |
| 128 | + service = await createService(); |
| 129 | + setFocus(false); |
| 130 | + setVisibility('visible'); |
| 131 | + getToken.mockResolvedValue('jwt-on-visible'); |
| 132 | + mocks.sessionCookie.set.mockClear(); |
| 133 | + |
| 134 | + document.dispatchEvent(new Event('visibilitychange')); |
| 135 | + await vi.waitFor(() => expect(mocks.sessionCookie.set).toHaveBeenCalledWith('jwt-on-visible')); |
| 136 | + |
| 137 | + expect(getToken).toHaveBeenCalled(); |
| 138 | + }); |
| 139 | +}); |
0 commit comments