|
| 1 | +import { renderHook, waitFor } from '@testing-library/react'; |
| 2 | + |
| 3 | +import { storage } from 'src/utilities/storage'; |
| 4 | + |
| 5 | +import { useInitialRequests } from './useInitialRequests'; |
| 6 | + |
| 7 | +vi.stubEnv('REACT_APP_CLIENT_ID', 'test-client-id'); |
| 8 | +vi.stubEnv('REACT_APP_LOGIN_ROOT', 'https://login.test'); |
| 9 | + |
| 10 | +const queryClientMock = { |
| 11 | + prefetchQuery: vi.fn().mockResolvedValue(undefined), |
| 12 | +}; |
| 13 | + |
| 14 | +vi.mock('@tanstack/react-query', async (importOriginal) => { |
| 15 | + const actual = await importOriginal(); |
| 16 | + return { |
| 17 | + ...actual, |
| 18 | + useQueryClient: () => queryClientMock, |
| 19 | + }; |
| 20 | +}); |
| 21 | + |
| 22 | +const oauthMocks = vi.hoisted(() => ({ |
| 23 | + validateTokenAndSession: vi.fn(), |
| 24 | +})); |
| 25 | + |
| 26 | +vi.mock('src/OAuth/oauth', async () => { |
| 27 | + const actual = await vi.importActual('src/OAuth/oauth'); |
| 28 | + return { |
| 29 | + ...actual, |
| 30 | + validateTokenAndSession: oauthMocks.validateTokenAndSession, |
| 31 | + }; |
| 32 | +}); |
| 33 | + |
| 34 | +describe('OAuth token verification and initial data fetch', () => { |
| 35 | + beforeEach(() => { |
| 36 | + vi.resetAllMocks(); |
| 37 | + storage.authentication.token.clear(); |
| 38 | + vi.mocked(require('react-redux')).useSelector = vi.fn().mockReturnValue(false); |
| 39 | + }); |
| 40 | + |
| 41 | + it('redirects to logout when login server reports the token does not match the session', async () => { |
| 42 | + storage.authentication.token.set('Bearer faketoken'); |
| 43 | + |
| 44 | + global.fetch = vi.fn().mockResolvedValue({ |
| 45 | + ok: true, |
| 46 | + json: async () => ({ match: false }), |
| 47 | + } as any); |
| 48 | + |
| 49 | + const { result } = renderHook(() => useInitialRequests()); |
| 50 | + |
| 51 | + await waitFor(() => expect(oauthMocks.validateTokenAndSession).toHaveBeenCalled()); |
| 52 | + }); |
| 53 | + |
| 54 | + it('runs initial requests when login server confirms the token belongs to the session (USER_MATCH)', async () => { |
| 55 | + storage.authentication.token.set('Bearer faketoken'); |
| 56 | + |
| 57 | + global.fetch = vi.fn().mockResolvedValue({ |
| 58 | + ok: true, |
| 59 | + json: async () => ({ match: true }), |
| 60 | + } as any); |
| 61 | + |
| 62 | + const { result } = renderHook(() => useInitialRequests()); |
| 63 | + |
| 64 | + await waitFor(() => expect(queryClientMock.prefetchQuery).toHaveBeenCalled()); |
| 65 | + await waitFor(() => expect(result.current.isLoading).toBe(false)); |
| 66 | + |
| 67 | + expect(oauthMocks.validateTokenAndSession).not.toHaveBeenCalled(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('falls back to running initial requests if token verification fetch fails (network/error)', async () => { |
| 71 | + storage.authentication.token.set('Bearer faketoken'); |
| 72 | + |
| 73 | + global.fetch = vi.fn().mockRejectedValue(new Error('network')); |
| 74 | + |
| 75 | + const { result } = renderHook(() => useInitialRequests()); |
| 76 | + |
| 77 | + await waitFor(() => expect(queryClientMock.prefetchQuery).toHaveBeenCalled()); |
| 78 | + await waitFor(() => expect(result.current.isLoading).toBe(false)); |
| 79 | + |
| 80 | + expect(oauthMocks.validateTokenAndSession).not.toHaveBeenCalled(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('does not call the login server verify endpoint for Admin tokens', async () => { |
| 84 | + storage.authentication.token.set('Admin admintoken'); |
| 85 | + |
| 86 | + global.fetch = vi.fn().mockRejectedValue(new Error('should-not-be-called')); |
| 87 | + |
| 88 | + const { result } = renderHook(() => useInitialRequests()); |
| 89 | + |
| 90 | + await waitFor(() => expect(result.current.isLoading).toBe(false)); |
| 91 | + |
| 92 | + expect(global.fetch).not.toHaveBeenCalled(); |
| 93 | + }); |
| 94 | +}); |
0 commit comments