|
| 1 | +import { type AxiosError, type AxiosRequestConfig } from 'axios'; |
| 2 | + |
| 3 | +import useAuthStore from '@/stores/auth/store'; |
| 4 | + |
| 5 | +jest.mock('@/lib/logging', () => ({ |
| 6 | + logger: { error: jest.fn(), warn: jest.fn(), info: jest.fn(), debug: jest.fn() }, |
| 7 | +})); |
| 8 | + |
| 9 | +jest.mock('@/lib/storage/app', () => ({ |
| 10 | + getBaseApiUrl: () => 'https://example.test/api/v4', |
| 11 | +})); |
| 12 | + |
| 13 | +jest.mock('@/stores/auth/store', () => ({ |
| 14 | + __esModule: true, |
| 15 | + default: { |
| 16 | + getState: jest.fn(), |
| 17 | + }, |
| 18 | +})); |
| 19 | + |
| 20 | +const mockRefreshAccessToken = jest.fn(); |
| 21 | +const getState = (useAuthStore as unknown as { getState: jest.Mock }).getState; |
| 22 | + |
| 23 | +// eslint-disable-next-line @typescript-eslint/no-require-imports |
| 24 | +const { api } = require('../client') as typeof import('../client'); |
| 25 | + |
| 26 | +const unauthorized = (config: AxiosRequestConfig): AxiosError => { |
| 27 | + const error = new Error('Request failed with status code 401') as AxiosError; |
| 28 | + error.isAxiosError = true; |
| 29 | + error.config = config as AxiosError['config']; |
| 30 | + error.response = { status: 401, statusText: 'Unauthorized', data: {}, headers: {}, config: config as never }; |
| 31 | + return error; |
| 32 | +}; |
| 33 | + |
| 34 | +describe('api client 401 handling', () => { |
| 35 | + let adapterCalls: number; |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + adapterCalls = 0; |
| 39 | + mockRefreshAccessToken.mockReset(); |
| 40 | + getState.mockReset(); |
| 41 | + api.defaults.adapter = async (config) => { |
| 42 | + adapterCalls += 1; |
| 43 | + throw unauthorized(config); |
| 44 | + }; |
| 45 | + }); |
| 46 | + |
| 47 | + it('does not attempt a refresh once the refresh token is gone', async () => { |
| 48 | + getState.mockReturnValue({ |
| 49 | + accessToken: null, |
| 50 | + refreshToken: null, |
| 51 | + refreshAccessToken: mockRefreshAccessToken, |
| 52 | + }); |
| 53 | + |
| 54 | + await expect(api.get('/Calls/GetActiveCalls')).rejects.toMatchObject({ response: { status: 401 } }); |
| 55 | + |
| 56 | + expect(mockRefreshAccessToken).not.toHaveBeenCalled(); |
| 57 | + // The original request only -- no retry, no doomed refresh round-trip. |
| 58 | + expect(adapterCalls).toBe(1); |
| 59 | + }); |
| 60 | + |
| 61 | + it('collapses a serial burst of 401s into a single refresh attempt', async () => { |
| 62 | + // Mirrors the startup chain: each store awaits the previous one, so the in-flight |
| 63 | + // dedupe never sees them overlap. The first 401 spends the refresh token; the rest |
| 64 | + // must fail fast rather than replay the refresh once each. |
| 65 | + let refreshToken: string | null = 'refresh-token'; |
| 66 | + mockRefreshAccessToken.mockImplementation(async () => { |
| 67 | + refreshToken = null; // refresh failed -> store cleared the session |
| 68 | + }); |
| 69 | + getState.mockImplementation(() => ({ |
| 70 | + accessToken: null, |
| 71 | + refreshToken, |
| 72 | + refreshAccessToken: mockRefreshAccessToken, |
| 73 | + })); |
| 74 | + |
| 75 | + const endpoints = ['/Calls/GetActiveCalls', '/Security/GetCurrentUsersRights', '/WeatherAlerts/GetSettings', '/WeatherAlerts/GetActiveAlerts']; |
| 76 | + for (const endpoint of endpoints) { |
| 77 | + await expect(api.get(endpoint)).rejects.toBeDefined(); |
| 78 | + } |
| 79 | + |
| 80 | + expect(mockRefreshAccessToken).toHaveBeenCalledTimes(1); |
| 81 | + expect(adapterCalls).toBe(4); |
| 82 | + }); |
| 83 | + |
| 84 | + it('refreshes and retries when a token is available', async () => { |
| 85 | + getState.mockReturnValue({ |
| 86 | + accessToken: 'stale-token', |
| 87 | + refreshToken: 'refresh-token', |
| 88 | + refreshAccessToken: mockRefreshAccessToken, |
| 89 | + }); |
| 90 | + mockRefreshAccessToken.mockImplementation(async () => { |
| 91 | + getState.mockReturnValue({ |
| 92 | + accessToken: 'fresh-token', |
| 93 | + refreshToken: 'refresh-token', |
| 94 | + refreshAccessToken: mockRefreshAccessToken, |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + api.defaults.adapter = async (config) => { |
| 99 | + adapterCalls += 1; |
| 100 | + if (adapterCalls === 1) { |
| 101 | + throw unauthorized(config); |
| 102 | + } |
| 103 | + return { data: { ok: true }, status: 200, statusText: 'OK', headers: {}, config } as never; |
| 104 | + }; |
| 105 | + |
| 106 | + await expect(api.get('/Calls/GetActiveCalls')).resolves.toMatchObject({ status: 200 }); |
| 107 | + |
| 108 | + expect(mockRefreshAccessToken).toHaveBeenCalledTimes(1); |
| 109 | + expect(adapterCalls).toBe(2); |
| 110 | + }); |
| 111 | +}); |
0 commit comments