Files
kestrelos/test/nuxt/auth-middleware.spec.js
Madison Grubb b7046dc0e6 initial commit
2026-02-10 23:32:26 -05:00

39 lines
1.7 KiB
JavaScript

import { describe, it, expect } from 'vitest'
import { mountSuspended, registerEndpoint } from '@nuxt/test-utils/runtime'
import Index from '../../app/pages/index.vue'
import Login from '../../app/pages/login.vue'
describe('auth middleware', () => {
it('allows /login without redirect when unauthenticated', async () => {
registerEndpoint('/api/me', () => null, { method: 'GET' })
const wrapper = await mountSuspended(Login)
expect(wrapper.text()).toContain('Sign in')
expect(wrapper.find('input[type="password"]').exists()).toBe(true)
})
it('redirects to /login with redirect query when unauthenticated and visiting protected route', async () => {
registerEndpoint('/api/me', () => null, { method: 'GET' })
registerEndpoint('/api/cameras', () => ({ devices: [], liveSessions: [] }), { method: 'GET' })
registerEndpoint('/api/pois', () => [], { method: 'GET' })
await mountSuspended(Index)
await new Promise(r => setTimeout(r, 200))
const router = useRouter()
await router.isReady()
expect(router.currentRoute.value.path).toBe('/login')
expect(router.currentRoute.value.query.redirect).toBe('/')
})
it('401 handler redirects to login when API returns 401', async () => {
registerEndpoint('/api/me', () => {
throw createError({ statusCode: 401 })
}, { method: 'GET' })
registerEndpoint('/api/cameras', () => ({ devices: [], liveSessions: [] }), { method: 'GET' })
registerEndpoint('/api/pois', () => [], { method: 'GET' })
await mountSuspended(Index)
await new Promise(r => setTimeout(r, 250))
const router = useRouter()
await router.isReady()
expect(router.currentRoute.value.path).toBe('/login')
})
})