61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import {
|
|
passwordMatchValidator,
|
|
PasswordMatchGroupLike,
|
|
} from '../../frontend/src/app/features/setup/setup-create-admin.validators';
|
|
|
|
function makeGroup(password: string | null | undefined, confirm: string | null | undefined): PasswordMatchGroupLike {
|
|
return {
|
|
get: (name) => {
|
|
if (name === 'password') return { value: password };
|
|
if (name === 'passwordConfirm') return { value: confirm };
|
|
return null;
|
|
},
|
|
};
|
|
}
|
|
|
|
describe('passwordMatchValidator', () => {
|
|
it('returns null when either field is empty (let other validators handle required)', () => {
|
|
expect(passwordMatchValidator(makeGroup('', 'x'))).toBeNull();
|
|
expect(passwordMatchValidator(makeGroup('x', ''))).toBeNull();
|
|
expect(passwordMatchValidator(makeGroup(undefined, undefined))).toBeNull();
|
|
});
|
|
|
|
it('returns null when passwords match', () => {
|
|
expect(passwordMatchValidator(makeGroup('Sup3r!', 'Sup3r!'))).toBeNull();
|
|
});
|
|
|
|
it('returns { passwordMismatch: true } when passwords differ', () => {
|
|
expect(passwordMatchValidator(makeGroup('Sup3r!', 'Sup4r!'))).toEqual({
|
|
passwordMismatch: true,
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('Username field rules (mirroring component validators)', () => {
|
|
const PATTERN = /^[a-zA-Z0-9_.-]+$/;
|
|
const MIN = 3;
|
|
const MAX = 32;
|
|
|
|
function isValidUsername(value: string): boolean {
|
|
return value.length >= MIN && value.length <= MAX && PATTERN.test(value);
|
|
}
|
|
|
|
it('accepts a 3-32 char username with allowed characters', () => {
|
|
expect(isValidUsername('first.admin_01')).toBe(true);
|
|
});
|
|
|
|
it('rejects usernames shorter than 3', () => {
|
|
expect(isValidUsername('a')).toBe(false);
|
|
});
|
|
|
|
it('rejects usernames with disallowed characters', () => {
|
|
expect(isValidUsername('bad name')).toBe(false);
|
|
expect(isValidUsername('user!')).toBe(false);
|
|
expect(isValidUsername('user@')).toBe(false);
|
|
});
|
|
|
|
it('rejects usernames longer than 32', () => {
|
|
expect(isValidUsername('a'.repeat(33))).toBe(false);
|
|
});
|
|
});
|