mirror of
https://github.com/fawney19/Aether.git
synced 2026-10-12 04:09:48 +08:00
fix auth refresh request body
This commit is contained in:
@@ -1,12 +1,14 @@
|
|||||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
|
|
||||||
const { postMock } = vi.hoisted(() => ({
|
const { postMock, setTokenMock } = vi.hoisted(() => ({
|
||||||
postMock: vi.fn(),
|
postMock: vi.fn(),
|
||||||
|
setTokenMock: vi.fn(),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
vi.mock('@/api/client', () => ({
|
vi.mock('@/api/client', () => ({
|
||||||
default: {
|
default: {
|
||||||
post: postMock,
|
post: postMock,
|
||||||
|
setToken: setTokenMock,
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
|
|
||||||
@@ -15,6 +17,7 @@ import { authApi } from '@/api/auth'
|
|||||||
describe('authApi turnstile payloads', () => {
|
describe('authApi turnstile payloads', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
postMock.mockReset()
|
postMock.mockReset()
|
||||||
|
setTokenMock.mockReset()
|
||||||
postMock.mockResolvedValue({ data: {} })
|
postMock.mockResolvedValue({ data: {} })
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -42,4 +45,13 @@ describe('authApi turnstile payloads', () => {
|
|||||||
turnstile_token: 'turnstile-token',
|
turnstile_token: 'turnstile-token',
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('refreshes auth token without a request body', async () => {
|
||||||
|
postMock.mockResolvedValue({ data: { access_token: 'new-access-token' } })
|
||||||
|
|
||||||
|
await authApi.refreshToken()
|
||||||
|
|
||||||
|
expect(postMock).toHaveBeenCalledWith('/api/auth/refresh')
|
||||||
|
expect(setTokenMock).toHaveBeenCalledWith('new-access-token')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
|
import type { AxiosAdapter, AxiosInstance, InternalAxiosRequestConfig } from 'axios'
|
||||||
|
|
||||||
import apiClient, { AUTH_STATE_CHANGE_EVENT } from '@/api/client'
|
import apiClient, { AUTH_STATE_CHANGE_EVENT } from '@/api/client'
|
||||||
|
|
||||||
|
type TestableApiClient = typeof apiClient & {
|
||||||
|
client: AxiosInstance
|
||||||
|
}
|
||||||
|
|
||||||
describe('apiClient auth state change event', () => {
|
describe('apiClient auth state change event', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
localStorage.clear()
|
localStorage.clear()
|
||||||
@@ -28,4 +33,33 @@ describe('apiClient auth state change event', () => {
|
|||||||
|
|
||||||
window.removeEventListener(AUTH_STATE_CHANGE_EVENT, handler as EventListener)
|
window.removeEventListener(AUTH_STATE_CHANGE_EVENT, handler as EventListener)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('sends auth refresh without a request body', async () => {
|
||||||
|
const rawClient = apiClient as TestableApiClient
|
||||||
|
const previousAdapter = rawClient.client.defaults.adapter
|
||||||
|
const requests: InternalAxiosRequestConfig[] = []
|
||||||
|
|
||||||
|
rawClient.client.defaults.adapter = (async (config: InternalAxiosRequestConfig) => {
|
||||||
|
requests.push(config)
|
||||||
|
return {
|
||||||
|
data: { access_token: 'new-access-token' },
|
||||||
|
status: 200,
|
||||||
|
statusText: 'OK',
|
||||||
|
headers: {},
|
||||||
|
config,
|
||||||
|
}
|
||||||
|
}) as AxiosAdapter
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await apiClient.refreshToken()
|
||||||
|
|
||||||
|
expect(response.data.access_token).toBe('new-access-token')
|
||||||
|
expect(requests).toHaveLength(1)
|
||||||
|
expect(requests[0].url).toBe('/api/auth/refresh')
|
||||||
|
expect(requests[0].method).toBe('post')
|
||||||
|
expect(requests[0].data).toBeUndefined()
|
||||||
|
} finally {
|
||||||
|
rawClient.client.defaults.adapter = previousAdapter
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -164,7 +164,7 @@ export const authApi = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async refreshToken(): Promise<LoginResponse> {
|
async refreshToken(): Promise<LoginResponse> {
|
||||||
const response = await apiClient.post<LoginResponse>('/api/auth/refresh', {})
|
const response = await apiClient.post<LoginResponse>('/api/auth/refresh')
|
||||||
apiClient.setToken(response.data.access_token)
|
apiClient.setToken(response.data.access_token)
|
||||||
return response.data
|
return response.data
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -323,7 +323,7 @@ class ApiClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async refreshToken(): Promise<AxiosResponse> {
|
async refreshToken(): Promise<AxiosResponse> {
|
||||||
return this.client.post('/api/auth/refresh', {})
|
return this.client.post('/api/auth/refresh')
|
||||||
}
|
}
|
||||||
|
|
||||||
// 以下方法直接委托给 axios client,Demo 模式由 adapter 统一处理
|
// 以下方法直接委托给 axios client,Demo 模式由 adapter 统一处理
|
||||||
|
|||||||
Reference in New Issue
Block a user