统一甲屿云命名体系
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { InternalAxiosRequestConfig } from 'axios'
|
||||
import { attachRequestSignature } from '@/utils/request-signature'
|
||||
|
||||
const buildConfig = (): InternalAxiosRequestConfig => ({
|
||||
headers: {},
|
||||
method: 'post',
|
||||
baseURL: '/admin/api',
|
||||
url: '/manager/planning/appointments',
|
||||
params: { b: 2, a: 1 },
|
||||
data: { name: 'test' }
|
||||
} as InternalAxiosRequestConfig)
|
||||
|
||||
describe('request signature', () => {
|
||||
beforeEach(() => {
|
||||
window.localStorage.clear()
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
it('adds device and signature headers for protected unsafe requests', async () => {
|
||||
vi.spyOn(Date, 'now').mockReturnValue(1_789_999_999_000)
|
||||
vi.spyOn(crypto, 'randomUUID').mockReturnValue('00000000-0000-4000-8000-000000000001')
|
||||
vi.spyOn(crypto, 'getRandomValues').mockImplementation(array => {
|
||||
const values = array as Uint32Array
|
||||
values[0] = 10
|
||||
values[1] = 20
|
||||
return array
|
||||
})
|
||||
|
||||
const config = await attachRequestSignature(buildConfig(), 'token-secret', { authFree: false })
|
||||
|
||||
expect(config.headers['X-Device-Id']).toBe('00000000-0000-4000-8000-000000000001')
|
||||
expect(config.headers['X-Timestamp']).toBe('1789999999000')
|
||||
expect(config.headers['X-Nonce']).toBe('1789999999000-00000000-0000-4000-8000-000000000001-ak')
|
||||
expect(config.headers['X-Sign-Version']).toBe('v1')
|
||||
expect(String(config.headers['X-Signature'])).toHaveLength(64)
|
||||
})
|
||||
|
||||
it('skips signature for auth-free requests but still attaches device id', async () => {
|
||||
vi.spyOn(crypto, 'randomUUID').mockReturnValue('00000000-0000-4000-8000-000000000001')
|
||||
|
||||
const config = await attachRequestSignature(buildConfig(), 'token-secret', { authFree: true })
|
||||
|
||||
expect(config.headers['X-Device-Id']).toBe('00000000-0000-4000-8000-000000000001')
|
||||
expect(config.headers['X-Signature']).toBeUndefined()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user