40 lines
1.5 KiB
TypeScript
40 lines
1.5 KiB
TypeScript
|
|
import { describe, it, expect } from 'vitest'
|
||
|
|
import { server, http, HttpResponse } from './test-setup'
|
||
|
|
import { handleGoogleLogin, completeGoogleLogin } from '@/api/auth'
|
||
|
|
import { api } from '@/api/client'
|
||
|
|
|
||
|
|
describe('auth google oauth', () => {
|
||
|
|
it('handleGoogleLogin returns start URL', async () => {
|
||
|
|
server.use(
|
||
|
|
http.get('*/api/v1/auth/google/start', () => {
|
||
|
|
return HttpResponse.json({ url: 'https://accounts.google.com/o/oauth2/v2/auth?x=y' })
|
||
|
|
})
|
||
|
|
)
|
||
|
|
const url = await handleGoogleLogin()
|
||
|
|
expect(url).toContain('https://accounts.google.com')
|
||
|
|
})
|
||
|
|
|
||
|
|
it('completeGoogleLogin exchanges code for token and sets auth header', async () => {
|
||
|
|
server.use(
|
||
|
|
http.post('*/api/v1/auth/google/callback', async ({ request }) => {
|
||
|
|
const body = await request.json()
|
||
|
|
expect(body).toEqual({ code: 'abc', state: 'state-1' })
|
||
|
|
return HttpResponse.json({ accessToken: 'goog-123', tokenType: 'bearer', user: { id: 42, email: 'g@example.com', displayName: 'G User' } })
|
||
|
|
})
|
||
|
|
)
|
||
|
|
const user = await completeGoogleLogin('abc', 'state-1')
|
||
|
|
expect(user.id).toBe(42)
|
||
|
|
|
||
|
|
// subsequent requests include bearer
|
||
|
|
server.use(
|
||
|
|
http.get('*/api/v1/users/me/households', ({ request }) => {
|
||
|
|
const auth = request.headers.get('authorization')
|
||
|
|
expect(auth?.toLowerCase()).toBe('bearer goog-123')
|
||
|
|
return HttpResponse.json([])
|
||
|
|
})
|
||
|
|
)
|
||
|
|
const res = await api.GET('/api/v1/users/me/households', { params: {} })
|
||
|
|
expect(res.response.ok).toBe(true)
|
||
|
|
})
|
||
|
|
})
|