Files
yale-user-access/packages/frontend/test/LoginForm.test.ts
T
LiamPietralla 4fe6f9d7b5
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
Add backend and frontend tests and run them in CI
- 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
2026-11-16 12:23:32 +11:00

67 lines
2.5 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
import { flushPromises } from '@vue/test-utils'
import { mockNuxtImport, mountSuspended } from '@nuxt/test-utils/runtime'
import LoginForm from '~/components/LoginForm.vue'
const { api, navigateToMock } = vi.hoisted(() => ({
api: { get: vi.fn(), post: vi.fn(), delete: vi.fn() },
navigateToMock: vi.fn()
}))
mockNuxtImport('useApi', () => () => api)
mockNuxtImport('navigateTo', () => navigateToMock)
describe('LoginForm', () => {
beforeEach(() => {
api.post.mockReset()
navigateToMock.mockReset()
useCookie<boolean>('authenticated').value = false
})
it('disables the login button until a password is entered', async () => {
const wrapper = await mountSuspended(LoginForm)
const button = wrapper.find('button[type="submit"]')
expect(button.attributes('disabled')).toBeDefined()
await wrapper.find('input').setValue('secret')
expect(button.attributes('disabled')).toBeUndefined()
})
it('logs in with the password, sets the cookie and goes home', async () => {
api.post.mockResolvedValue({ success: true, data: true })
const wrapper = await mountSuspended(LoginForm)
await wrapper.find('input').setValue('pa"ss')
await wrapper.find('form').trigger('submit')
await flushPromises()
expect(api.post).toHaveBeenCalledWith('/api/Authentication/login', 'pa"ss')
expect(useCookie<boolean>('authenticated').value).toBe(true)
expect(navigateToMock).toHaveBeenCalledWith('/')
})
it('shows the error and stays on the page when the login fails', async () => {
api.post.mockResolvedValue({ success: false, error: 'Incorrect password.' })
const wrapper = await mountSuspended(LoginForm)
await wrapper.find('input').setValue('wrong')
await wrapper.find('form').trigger('submit')
await flushPromises()
expect(wrapper.text()).toContain('Incorrect password.')
expect(useCookie<boolean>('authenticated').value).toBe(false)
expect(navigateToMock).not.toHaveBeenCalled()
})
it('can reveal and hide the password', async () => {
const wrapper = await mountSuspended(LoginForm)
const input = wrapper.find('input')
expect(input.attributes('type')).toBe('password')
await wrapper.find('button[aria-label="Show password"]').trigger('click')
expect(input.attributes('type')).toBe('text')
await wrapper.find('button[aria-label="Hide password"]').trigger('click')
expect(input.attributes('type')).toBe('password')
})
})