113 lines
3.2 KiB
TypeScript
113 lines
3.2 KiB
TypeScript
// @vitest-environment jsdom
|
|
|
|
import { AxiosError, type AxiosAdapter, type AxiosResponse, type InternalAxiosRequestConfig } from 'axios'
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
const messageError = vi.hoisted(() => vi.fn())
|
|
|
|
vi.mock('ant-design-vue', () => ({
|
|
message: {
|
|
error: messageError
|
|
}
|
|
}))
|
|
|
|
import adminApi from '../../src/api/admin'
|
|
|
|
function createTokenExpiredError(config: InternalAxiosRequestConfig) {
|
|
const response = {
|
|
data: {
|
|
code: 401,
|
|
message: 'Token expired'
|
|
},
|
|
status: 401,
|
|
statusText: 'Unauthorized',
|
|
headers: {},
|
|
config,
|
|
request: {}
|
|
} as AxiosResponse
|
|
|
|
return new AxiosError(
|
|
'Request failed with status code 401',
|
|
AxiosError.ERR_BAD_REQUEST,
|
|
config,
|
|
{},
|
|
response
|
|
)
|
|
}
|
|
|
|
describe('admin api stale token handling', () => {
|
|
const originalAdapter = adminApi.defaults.adapter
|
|
|
|
beforeEach(() => {
|
|
vi.useFakeTimers()
|
|
messageError.mockClear()
|
|
window.history.replaceState(null, '', '/admin')
|
|
localStorage.clear()
|
|
sessionStorage.clear()
|
|
})
|
|
|
|
afterEach(() => {
|
|
adminApi.defaults.adapter = originalAdapter
|
|
localStorage.clear()
|
|
sessionStorage.clear()
|
|
vi.useRealTimers()
|
|
})
|
|
|
|
it('keeps a newer admin session when an old-token request returns 401', async () => {
|
|
localStorage.setItem('adminAccessToken', 'old-admin-token')
|
|
localStorage.setItem('adminRefreshToken', 'old-admin-refresh')
|
|
sessionStorage.setItem('adminAccessToken', 'old-admin-token')
|
|
|
|
const requests: Array<{ url?: string, authorization?: string }> = []
|
|
let markRequestStarted!: () => void
|
|
let releaseStale401!: () => void
|
|
const requestStarted = new Promise<void>(resolve => {
|
|
markRequestStarted = resolve
|
|
})
|
|
|
|
adminApi.defaults.adapter = ((config: InternalAxiosRequestConfig) => {
|
|
if (config.url !== '/user/info') {
|
|
return Promise.reject(new Error(`Unexpected request: ${config.url}`))
|
|
}
|
|
requests.push({
|
|
url: config.url,
|
|
authorization: String(config.headers.Authorization || '')
|
|
})
|
|
markRequestStarted()
|
|
return new Promise((_resolve, reject) => {
|
|
releaseStale401 = () => reject(createTokenExpiredError(config))
|
|
})
|
|
}) as AxiosAdapter
|
|
|
|
const staleRequest = adminApi.get('/user/info')
|
|
|
|
await requestStarted
|
|
expect(requests).toEqual([
|
|
{
|
|
url: '/user/info',
|
|
authorization: 'Bearer old-admin-token'
|
|
}
|
|
])
|
|
|
|
localStorage.setItem('adminAccessToken', 'new-admin-token')
|
|
localStorage.setItem('adminRefreshToken', 'new-admin-refresh')
|
|
sessionStorage.setItem('adminAccessToken', 'new-admin-token')
|
|
const hrefBefore401 = window.location.href
|
|
|
|
releaseStale401()
|
|
|
|
await expect(staleRequest).rejects.toMatchObject({
|
|
response: {
|
|
status: 401
|
|
}
|
|
})
|
|
await vi.runAllTimersAsync()
|
|
|
|
expect(localStorage.getItem('adminAccessToken')).toBe('new-admin-token')
|
|
expect(localStorage.getItem('adminRefreshToken')).toBe('new-admin-refresh')
|
|
expect(sessionStorage.getItem('adminAccessToken')).toBe('new-admin-token')
|
|
expect(window.location.href).toBe(hrefBefore401)
|
|
expect(messageError).not.toHaveBeenCalled()
|
|
})
|
|
})
|