docs: record auth hash alignment and validation results
This commit is contained in:
1 parent
22887fef03
commit
f7819904b9
51 files changed
+1792
-542
No files matched your search
@@ -0,0 +1,68 @@
|
||||
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/svelte';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import ProfileDialog from '../src/lib/components/ProfileDialog.svelte';
|
||||
|
||||
afterEach(cleanup);
|
||||
|
||||
const fieldLabels: Record<string, string> = {
|
||||
username: 'Username',
|
||||
email: 'Email',
|
||||
password: 'New password',
|
||||
confirmPassword: 'Confirm new password',
|
||||
currentPassword: 'Current password',
|
||||
totpToken: 'Current authenticator code',
|
||||
};
|
||||
|
||||
const field = (name: string): HTMLInputElement => screen.getByLabelText(fieldLabels[name]) as HTMLInputElement;
|
||||
|
||||
async function fill(name: string, value: string): Promise<void> {
|
||||
await fireEvent.input(field(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('ProfileDialog password changes', () => {
|
||||
it('requires current password and a current TOTP code when enabled', async () => {
|
||||
const onSave = vi.fn().mockResolvedValue(undefined);
|
||||
render(ProfileDialog, { props: { open: true, onSave, totpEnabled: true } });
|
||||
await fill('username', 'alice');
|
||||
await fill('email', 'alice@example.com');
|
||||
await fill('password', 'new-password');
|
||||
await fill('confirmPassword', 'new-password');
|
||||
|
||||
await submit();
|
||||
expect(onSave).not.toHaveBeenCalled();
|
||||
expect(screen.getByText('Enter your current password to change it.')).toBeTruthy();
|
||||
|
||||
await fill('currentPassword', 'old-password');
|
||||
await submit();
|
||||
expect(onSave).not.toHaveBeenCalled();
|
||||
expect(screen.getByText('Enter a current authenticator code to change your password.')).toBeTruthy();
|
||||
|
||||
await fill('totpToken', '123456');
|
||||
await submit();
|
||||
await waitFor(() => expect(onSave).toHaveBeenCalledTimes(1));
|
||||
expect(onSave).toHaveBeenCalledWith({
|
||||
username: 'alice',
|
||||
email: 'alice@example.com',
|
||||
password: 'new-password',
|
||||
currentPassword: 'old-password',
|
||||
totpToken: '123456',
|
||||
});
|
||||
});
|
||||
|
||||
it('does not ask for password proof when only profile fields change', async () => {
|
||||
const onSave = vi.fn().mockResolvedValue(undefined);
|
||||
render(ProfileDialog, { props: { open: true, onSave, totpEnabled: true } });
|
||||
await fill('username', 'alice');
|
||||
await fill('email', 'alice@example.com');
|
||||
await submit();
|
||||
await waitFor(() => expect(onSave).toHaveBeenCalledTimes(1));
|
||||
expect(onSave).toHaveBeenCalledWith({ username: 'alice', email: 'alice@example.com' });
|
||||
expect(screen.queryByLabelText('Current password')).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user