Files
HIPCTF2/tests/frontend/change-password-modal.spec.ts
T

72 lines
3.0 KiB
TypeScript

import {
passwordMatchValidator,
buildPasswordPolicyMessage,
} from '../../frontend/src/app/features/shell/change-password/change-password-modal.validators';
import { formatChangePasswordError } from '../../frontend/src/app/features/shell/change-password/password-feedback';
describe('passwordMatchValidator', () => {
const grp = (newP: string, conf: string) => ({
get: (k: string) =>
k === 'newPassword' ? { value: newP } : k === 'confirmNewPassword' ? { value: conf } : { value: '' },
});
it('returns null when both inputs are equal', () => {
expect(passwordMatchValidator(grp('Abc123!Xyz', 'Abc123!Xyz'))).toBeNull();
});
it('returns {passwordMismatch:true} when inputs differ', () => {
expect(passwordMatchValidator(grp('Abc123!Xyz', 'Other1!Pass'))).toEqual({ passwordMismatch: true });
});
it('returns null when either input is empty (handled by required validators)', () => {
expect(passwordMatchValidator(grp('', ''))).toBeNull();
expect(passwordMatchValidator(grp('Abc', ''))).toBeNull();
});
});
describe('buildPasswordPolicyMessage', () => {
it('returns the policy description verbatim', () => {
expect(buildPasswordPolicyMessage({ minLength: 12, requireMixed: true, description: 'foo bar' })).toBe('foo bar');
});
});
describe('formatChangePasswordError', () => {
it('maps INVALID_OLD_PASSWORD to a clear message', () => {
expect(formatChangePasswordError({ code: 'INVALID_OLD_PASSWORD' })).toBe('Old password is incorrect');
});
it('maps PASSWORDS_DO_NOT_MATCH', () => {
expect(formatChangePasswordError({ code: 'PASSWORDS_DO_NOT_MATCH' })).toBe('New password and confirmation do not match');
});
it('maps PASSWORD_POLICY and surfaces server message if present', () => {
expect(formatChangePasswordError({ code: 'PASSWORD_POLICY', message: 'too short' })).toBe('too short');
expect(formatChangePasswordError({ code: 'PASSWORD_POLICY' })).toBe('New password does not meet the policy requirements');
});
it('maps UNAUTHORIZED', () => {
expect(formatChangePasswordError({ code: 'UNAUTHORIZED' })).toBe('You must be signed in to change your password');
});
it('falls back to message or default for unknown codes', () => {
expect(formatChangePasswordError({ code: 'EXOTIC', message: 'Quux' })).toBe('Quux');
expect(formatChangePasswordError({ code: 'EXOTIC' })).toBe('Failed to change password');
});
it('handles null/undefined code and message', () => {
expect(formatChangePasswordError({ code: null, message: null })).toBe('Failed to change password');
});
});
describe('ChangePasswordModalComponent admin-reset field errors', () => {
it('maps a fieldErrors record to per-path messages', () => {
const errors: Record<string, string> = {
newPassword: 'Password too short',
confirmPassword: 'Confirmation does not match',
};
expect(errors['newPassword']).toBe('Password too short');
expect(errors['confirmPassword']).toBe('Confirmation does not match');
expect(errors['confirmNewPassword']).toBeUndefined();
});
});