Add backend and frontend tests and run them in CI
Build and Publish / Build Yale Access Frontend (pull_request) Successful in 1m17s
Build and Publish / Push Yale Access Frontend Docker Image (pull_request) Skipped
Build and Publish / Build Yale Access Backend (pull_request) Successful in 2m12s
Build and Publish / Push Yale Access Backend Docker Image (pull_request) Skipped
Build and Publish / Build Yale Access Frontend (pull_request) Successful in 1m17s
Build and Publish / Push Yale Access Frontend Docker Image (pull_request) Skipped
Build and Publish / Build Yale Access Backend (pull_request) Successful in 2m12s
Build and Publish / Push Yale Access Backend Docker Image (pull_request) Skipped
- Backend: xUnit integration tests (auth, rate limiting, people, lock codes, validation, error handling) using WebApplicationFactory with a fake lock - Frontend: Vitest + Nuxt test utils for useApi, the auth middleware, LoginForm, UserCodeList and PeopleList - Run both suites in CI before building
This commit is contained in:
1 parent
a9bef7b50d
commit
4fe6f9d7b5
15 files changed
+968
No files matched your search
@@ -0,0 +1,47 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { mockNuxtImport } from '@nuxt/test-utils/runtime'
|
||||
import middleware from '~/middleware/authentication.global'
|
||||
|
||||
// Returning false aborts the navigation. (Returning a path would make the real router follow it, and
|
||||
// since this mock also applies to the app's own startup navigation, that could redirect forever.)
|
||||
const { navigateToMock } = vi.hoisted(() => ({ navigateToMock: vi.fn(() => false) }))
|
||||
mockNuxtImport('navigateTo', () => navigateToMock)
|
||||
|
||||
// The middleware only reads `to.path`
|
||||
const run = (path: string) => (middleware as (to: unknown, from: unknown) => unknown)({ path }, { path: '/' })
|
||||
|
||||
describe('authentication middleware', () => {
|
||||
describe('when logged out', () => {
|
||||
beforeEach(() => {
|
||||
useCookie<boolean>('authenticated').value = false
|
||||
navigateToMock.mockClear()
|
||||
})
|
||||
|
||||
it.each(['/', '/people'])('redirects %s to the login page', (path) => {
|
||||
run(path)
|
||||
expect(navigateToMock).toHaveBeenCalledExactlyOnceWith('/login')
|
||||
})
|
||||
|
||||
it.each(['/login', '/health'])('allows anonymous access to %s', (path) => {
|
||||
expect(run(path)).toBeUndefined()
|
||||
expect(navigateToMock).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('when logged in', () => {
|
||||
beforeEach(() => {
|
||||
useCookie<boolean>('authenticated').value = true
|
||||
navigateToMock.mockClear()
|
||||
})
|
||||
|
||||
it.each(['/', '/people', '/health'])('allows %s', (path) => {
|
||||
expect(run(path)).toBeUndefined()
|
||||
expect(navigateToMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('skips the login page', () => {
|
||||
run('/login')
|
||||
expect(navigateToMock).toHaveBeenCalledExactlyOnceWith('/')
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user