Files
Aether/frontend/src/api/__tests__/auth-turnstile.spec.ts
T

58 lines
1.5 KiB
TypeScript
Raw Normal View History

2026-05-15 01:54:20 +08:00
import { beforeEach, describe, expect, it, vi } from 'vitest'
2026-05-27 15:06:51 +08:00
const { postMock, setTokenMock } = vi.hoisted(() => ({
2026-05-15 01:54:20 +08:00
postMock: vi.fn(),
2026-05-27 15:06:51 +08:00
setTokenMock: vi.fn(),
2026-05-15 01:54:20 +08:00
}))
vi.mock('@/api/client', () => ({
default: {
post: postMock,
2026-05-27 15:06:51 +08:00
setToken: setTokenMock,
2026-05-15 01:54:20 +08:00
},
}))
import { authApi } from '@/api/auth'
describe('authApi turnstile payloads', () => {
beforeEach(() => {
postMock.mockReset()
2026-05-27 15:06:51 +08:00
setTokenMock.mockReset()
2026-05-15 01:54:20 +08:00
postMock.mockResolvedValue({ data: {} })
})
it('includes turnstile token when sending email verification code', async () => {
await authApi.sendVerificationCode('[email protected]', 'turnstile-token')
expect(postMock).toHaveBeenCalledWith('/api/auth/send-verification-code', {
email: '[email protected]',
turnstile_token: 'turnstile-token',
})
})
it('includes turnstile token when registering', async () => {
await authApi.register({
email: '[email protected]',
username: 'alice',
password: 'secret123',
turnstile_token: 'turnstile-token',
})
expect(postMock).toHaveBeenCalledWith('/api/auth/register', {
email: '[email protected]',
username: 'alice',
password: 'secret123',
turnstile_token: 'turnstile-token',
})
})
2026-05-27 15:06:51 +08:00
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')
})
2026-05-15 01:54:20 +08:00
})