Files
shared/packages/web-ui/tests/AuthPage.test.ts
T

166 lines
7.4 KiB
TypeScript
Raw Normal View History

import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/svelte';
import { afterEach, describe, expect, it, vi } from 'vitest';
import AuthPage from '../src/lib/components/AuthPage.svelte';
afterEach(cleanup);
const input = (name: string): HTMLInputElement => {
const element = document.querySelector<HTMLInputElement>(`input[name="${name}"]`);
if (!element) throw new Error(`no input named ${name}`);
return element;
};
async function fill(name: string, value: string): Promise<void> {
await fireEvent.input(input(name), { target: { value } });
}
async function submit(): Promise<void> {
const form = document.querySelector('form');
if (!form) throw new Error('no form');
await fireEvent.submit(form);
}
describe('AuthPage login', () => {
it('shows the login form and passes the trimmed credentials to onLogin', async () => {
const onLogin = vi.fn().mockResolvedValue(undefined);
render(AuthPage, { props: { brand: 'Demo', onLogin } });
expect(screen.getByRole('heading', { name: 'Welcome back' })).toBeTruthy();
expect(document.querySelector('input[name="confirmPassword"]')).toBeNull();
await fill('identifier', 'alice@example.com');
await fill('password', 'correct horse');
await submit();
await waitFor(() => expect(onLogin).toHaveBeenCalledTimes(1));
expect(onLogin).toHaveBeenCalledWith({
identifier: 'alice@example.com',
password: 'correct horse',
totpToken: '',
});
});
it('shows the error from a failed login and lets the user retry', async () => {
const onLogin = vi
.fn()
.mockRejectedValueOnce(new Error('Invalid username or password.'))
.mockResolvedValue(undefined);
render(AuthPage, { props: { onLogin } });
await fill('identifier', 'alice');
await fill('password', 'wrong password');
await submit();
await waitFor(() => expect(screen.getByText('Invalid username or password.')).toBeTruthy());
await submit();
await waitFor(() => expect(onLogin).toHaveBeenCalledTimes(2));
await waitFor(() => expect(screen.queryByText('Invalid username or password.')).toBeNull());
});
it('asks for the authenticator code after a two-factor challenge and sends it on the next try', async () => {
const onLogin = vi
.fn()
.mockRejectedValueOnce(new Error('Two-factor authentication code required.'))
.mockResolvedValue(undefined);
render(AuthPage, { props: { onLogin } });
await fill('identifier', 'alice');
await fill('password', 'correct horse');
expect(document.querySelector('input[name="totpToken"]')).toBeNull();
await submit();
await waitFor(() => expect(input('totpToken')).toBeTruthy());
expect(screen.getByText('Enter the six-digit authenticator code to continue.')).toBeTruthy();
await submit(); // an empty code is not sent
expect(onLogin).toHaveBeenCalledTimes(1);
await fill('totpToken', ' 123456 ');
await submit();
await waitFor(() => expect(onLogin).toHaveBeenCalledTimes(2));
expect(onLogin).toHaveBeenLastCalledWith({ identifier: 'alice', password: 'correct horse', totpToken: '123456' });
});
it('does not offer the authenticator field when showTotp is off', async () => {
const onLogin = vi.fn().mockRejectedValue(new Error('Two-factor authentication code required.'));
render(AuthPage, { props: { onLogin, showTotp: false } });
await fill('identifier', 'alice');
await submit();
await waitFor(() => expect(screen.getByText('Two-factor authentication code required.')).toBeTruthy());
expect(document.querySelector('input[name="totpToken"]')).toBeNull();
});
it('reports a missing login handler instead of failing silently', async () => {
render(AuthPage);
await submit();
await waitFor(() => expect(screen.getByText('The application has not configured its login handler.')).toBeTruthy());
});
it('shows only the loading heading while loading', () => {
render(AuthPage, { props: { loading: true } });
expect(screen.getByRole('heading', { name: 'Loading...' })).toBeTruthy();
expect(document.querySelector('form')).toBeNull();
});
it('falls back to the login view when the status check failed, whatever setupRequired says', () => {
render(AuthPage, { props: { setupRequired: true, error: 'Could not reach the server.' } });
expect(screen.getByRole('heading', { name: 'Welcome back' })).toBeTruthy();
expect(screen.getByText('Could not reach the server.')).toBeTruthy();
});
});
describe('AuthPage setup', () => {
it('refuses mismatched passwords without calling onSetup', async () => {
const onSetup = vi.fn();
render(AuthPage, { props: { setupRequired: true, onSetup } });
expect(screen.getByRole('heading', { name: 'Create your account' })).toBeTruthy();
await fill('username', 'root');
await fill('email', 'root@example.com');
await fill('password', 'password one');
await fill('confirmPassword', 'password two');
await submit();
await waitFor(() => expect(screen.getByText('Passwords do not match.')).toBeTruthy());
expect(onSetup).not.toHaveBeenCalled();
});
it('passes the new account to onSetup when the passwords match', async () => {
const onSetup = vi.fn().mockResolvedValue(undefined);
render(AuthPage, { props: { setupRequired: true, onSetup } });
await fill('username', 'root');
await fill('email', 'root@example.com');
await fill('password', 'password one');
await fill('confirmPassword', 'password one');
await submit();
await waitFor(() =>
expect(onSetup).toHaveBeenCalledWith({ username: 'root', email: 'root@example.com', password: 'password one' }),
);
});
});
describe('AuthPage account reset', () => {
it('offers the reset link only when both handlers are given', () => {
render(AuthPage, { props: { onRequestReset: vi.fn() } });
expect(screen.queryByText('Reset admin account')).toBeNull();
cleanup();
render(AuthPage, { props: { onRequestReset: vi.fn(), onConfirmReset: vi.fn() } });
expect(screen.getByText('Reset admin account')).toBeTruthy();
});
it('requests a code, then confirms the trimmed code the user types', async () => {
const onRequestReset = vi.fn().mockResolvedValue(undefined);
const onConfirmReset = vi.fn().mockResolvedValue(undefined);
render(AuthPage, { props: { onRequestReset, onConfirmReset } });
await fireEvent.click(screen.getByText('Reset admin account'));
await waitFor(() => expect(onRequestReset).toHaveBeenCalledTimes(1));
expect(screen.getByText('A reset code has been printed to the server console.')).toBeTruthy();
await fireEvent.click(screen.getByText('Confirm reset'));
await waitFor(() => expect(screen.getByText('Enter the code printed in the server console.')).toBeTruthy());
expect(onConfirmReset).not.toHaveBeenCalled();
await fireEvent.input(screen.getByLabelText('Reset code'), { target: { value: ' ABCD1234 ' } });
await fireEvent.click(screen.getByText('Confirm reset'));
await waitFor(() => expect(onConfirmReset).toHaveBeenCalledWith('ABCD1234'));
});
it('shows the error when the reset request fails', async () => {
const onRequestReset = vi.fn().mockRejectedValue(new Error('AuthGuard is not reachable.'));
render(AuthPage, { props: { onRequestReset, onConfirmReset: vi.fn() } });
await fireEvent.click(screen.getByText('Reset admin account'));
await waitFor(() => expect(screen.getByText('AuthGuard is not reachable.')).toBeTruthy());
});
});