initial commit
This commit is contained in:
38
test/nuxt/auth-middleware.spec.js
Normal file
38
test/nuxt/auth-middleware.spec.js
Normal file
@@ -0,0 +1,38 @@
|
||||
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')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user