This commit is contained in:
@@ -3,6 +3,13 @@ import { mountSuspended, registerEndpoint } from '@nuxt/test-utils/runtime'
|
||||
import Index from '../../app/pages/index.vue'
|
||||
import Login from '../../app/pages/login.vue'
|
||||
|
||||
const wait = (ms = 200) => new Promise(r => setTimeout(r, ms))
|
||||
|
||||
const setupProtectedEndpoints = () => {
|
||||
registerEndpoint('/api/cameras', () => ({ devices: [], liveSessions: [] }), { method: 'GET' })
|
||||
registerEndpoint('/api/pois', () => [], { method: 'GET' })
|
||||
}
|
||||
|
||||
describe('auth middleware', () => {
|
||||
it('allows /login without redirect when unauthenticated', async () => {
|
||||
registerEndpoint('/api/me', () => null, { method: 'GET' })
|
||||
@@ -11,28 +18,25 @@ describe('auth middleware', () => {
|
||||
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' })
|
||||
it.each([
|
||||
[() => null, '/login', { redirect: '/' }],
|
||||
[
|
||||
() => {
|
||||
throw createError({ statusCode: 401 })
|
||||
},
|
||||
'/login',
|
||||
undefined,
|
||||
],
|
||||
])('redirects to /login when unauthenticated: %s', async (meResponse, expectedPath, expectedQuery) => {
|
||||
registerEndpoint('/api/me', meResponse, { method: 'GET' })
|
||||
setupProtectedEndpoints()
|
||||
await mountSuspended(Index)
|
||||
await new Promise(r => setTimeout(r, 200))
|
||||
await wait(meResponse.toString().includes('401') ? 250 : 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')
|
||||
expect(router.currentRoute.value.path).toBe(expectedPath)
|
||||
if (expectedQuery) {
|
||||
expect(router.currentRoute.value.query).toMatchObject(expectedQuery)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user