munch-ease-frontend/tests/auth.api.test.ts

43 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-11-01 02:44:58 +00:00
import { describe, it, expect } from 'vitest'
import { server, http, HttpResponse } from './test-setup'
import { currentUser, logout, loginWithPassword, createAccount, handleGoogleLogin } from '@/api/auth'
describe('auth api (multitenant prep)', () => {
it('loginWithPassword proxies to /auth/login with username=email', async () => {
server.use(
http.post('*/api/v1/auth/login', async ({ request }) => {
const body = await request.json()
expect(body).toEqual({ username: 'test@example.com' })
return HttpResponse.json({ id: 123, name: 'Test User' })
})
)
const person = await loginWithPassword('test@example.com', 'secret')
expect(person.id).toBe(123)
expect(person.name).toBe('Test User')
})
it('logout clears cached user; subsequent currentUser returns null', async () => {
// First refresh returns a user
server.use(
http.post('*/api/v1/auth/refresh', () => {
return HttpResponse.json({ id: 1, name: 'Ada' })
})
)
const first = await currentUser()
expect(first?.name).toBe('Ada')
// After logout, next refresh returns 401 and currentUser should resolve to null
await logout()
server.use(
http.post('*/api/v1/auth/refresh', () => new HttpResponse(null, { status: 401 }))
)
const second = await currentUser()
expect(second).toBeNull()
})
it('createAccount and handleGoogleLogin exist but are not implemented yet', async () => {
await expect(createAccount('new@example.com', 'New User', 'pw')).rejects.toBeInstanceOf(Error)
await expect(handleGoogleLogin('token-123')).rejects.toBeInstanceOf(Error)
})
})