feat(ui): add password confirmations (#2291)
* feat(ui): add password confirmation to setup screen * feat(ui): add password confirmation to create user dialog * fix(tests): force frontend tests to use jsdom, and run tests in isolated environment. Also add debug statements to further diagnose failing test if needed --------- Co-authored-by: ACX <8075870+acx10@users.noreply.github.com>
This commit is contained in:
co-authored by
ACX
parent
dfd757fe36
commit
4253322d2c
@@ -111,6 +111,36 @@
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="form-field">
|
||||
<label for="confirmPassword" class="form-label">
|
||||
<i class="pi pi-lock"></i>
|
||||
<span>Confirm Password</span>
|
||||
</label>
|
||||
<input
|
||||
pInputText
|
||||
id="confirmPassword"
|
||||
name="confirmPassword"
|
||||
type="password"
|
||||
required
|
||||
formControlName="confirmPassword"
|
||||
placeholder="Re-enter your password"
|
||||
class="form-input"
|
||||
autocomplete="new-password"
|
||||
/>
|
||||
@if (setupForm.get('confirmPassword')?.invalid && setupForm.get('confirmPassword')?.touched) {
|
||||
<small class="field-error">
|
||||
<i class="pi pi-exclamation-circle"></i>
|
||||
Password confirmation is required
|
||||
</small>
|
||||
}
|
||||
@if (setupForm.get('confirmPassword')?.touched && setupForm.hasError('passwordMismatch')) {
|
||||
<small class="field-error">
|
||||
<i class="pi pi-exclamation-circle"></i>
|
||||
Passwords do not match
|
||||
</small>
|
||||
}
|
||||
</div>
|
||||
|
||||
<p-button
|
||||
fluid
|
||||
type="submit"
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
import {ComponentFixture, TestBed} from '@angular/core/testing';
|
||||
import {beforeEach, describe, expect, it, vi} from 'vitest';
|
||||
import {Router} from '@angular/router';
|
||||
import {of} from 'rxjs';
|
||||
|
||||
import {SetupComponent} from './setup.component';
|
||||
import {SetupService} from './setup.service';
|
||||
|
||||
describe('SetupComponent confirm password', () => {
|
||||
let fixture: ComponentFixture<SetupComponent>;
|
||||
let component: SetupComponent;
|
||||
let setupServiceMock: { createAdmin: ReturnType<typeof vi.fn> };
|
||||
|
||||
beforeEach(async () => {
|
||||
setupServiceMock = {
|
||||
createAdmin: vi.fn().mockReturnValue(of(void 0))
|
||||
};
|
||||
const routerMock = {
|
||||
navigate: vi.fn()
|
||||
};
|
||||
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [SetupComponent],
|
||||
providers: [
|
||||
{provide: SetupService, useValue: setupServiceMock},
|
||||
{provide: Router, useValue: routerMock}
|
||||
]
|
||||
}).compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(SetupComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
const getErrorTexts = () =>
|
||||
Array.from(
|
||||
(fixture.nativeElement as HTMLElement).querySelectorAll('.field-error')
|
||||
)
|
||||
.map(el => el.textContent?.trim())
|
||||
.filter((text): text is string => Boolean(text));
|
||||
|
||||
|
||||
it('shows required error when confirm password is empty', () => {
|
||||
const confirmControl = component.setupForm.get('confirmPassword');
|
||||
confirmControl?.setValue('');
|
||||
confirmControl?.markAsTouched();
|
||||
|
||||
fixture.detectChanges();
|
||||
expect(getErrorTexts()).toContain('Password confirmation is required');
|
||||
});
|
||||
|
||||
it('shows mismatch error when confirm password does not match', () => {
|
||||
component.setupForm.get('password')?.setValue('abcdef');
|
||||
const confirmControl = component.setupForm.get('confirmPassword');
|
||||
confirmControl?.setValue('abcdeg');
|
||||
confirmControl?.markAsTouched();
|
||||
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(getErrorTexts()).toContain('Passwords do not match');
|
||||
});
|
||||
|
||||
it('hides mismatch error when confirm password matches', () => {
|
||||
component.setupForm.get('password')?.setValue('abcdef');
|
||||
const confirmControl = component.setupForm.get('confirmPassword');
|
||||
confirmControl?.setValue('abcdef');
|
||||
confirmControl?.markAsTouched();
|
||||
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(getErrorTexts()).not.toContain('Passwords do not match');
|
||||
});
|
||||
|
||||
it('does not submit when confirm password is missing', () => {
|
||||
component.setupForm.patchValue({
|
||||
username: 'admin',
|
||||
name: 'Admin User',
|
||||
email: 'admin@example.com',
|
||||
password: '123456',
|
||||
confirmPassword: ''
|
||||
});
|
||||
|
||||
component.onSubmit();
|
||||
|
||||
expect(component.setupForm.invalid).toBe(true);
|
||||
expect(setupServiceMock.createAdmin).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not submit when confirm password does not match', () => {
|
||||
component.setupForm.patchValue({
|
||||
username: 'admin',
|
||||
name: 'Admin User',
|
||||
email: 'admin@example.com',
|
||||
password: '123456',
|
||||
confirmPassword: '123457'
|
||||
});
|
||||
|
||||
component.onSubmit();
|
||||
|
||||
expect(component.setupForm.invalid).toBe(true);
|
||||
expect(setupServiceMock.createAdmin).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('submits when form is valid and excludes confirmPassword from payload', () => {
|
||||
component.setupForm.patchValue({
|
||||
username: 'admin',
|
||||
name: 'Admin User',
|
||||
email: 'admin@example.com',
|
||||
password: '123456',
|
||||
confirmPassword: '123456'
|
||||
});
|
||||
|
||||
component.onSubmit();
|
||||
|
||||
expect(component.setupForm.valid).toBe(true);
|
||||
expect(setupServiceMock.createAdmin).toHaveBeenCalledWith({
|
||||
username: 'admin',
|
||||
name: 'Admin User',
|
||||
email: 'admin@example.com',
|
||||
password: '123456'
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -5,6 +5,7 @@ import {SetupService} from './setup.service';
|
||||
import {InputText} from 'primeng/inputtext';
|
||||
import {Button} from 'primeng/button';
|
||||
import {Message} from 'primeng/message';
|
||||
import {passwordMatchValidator} from '../../validators/password-match.validator';
|
||||
|
||||
@Component({
|
||||
selector: 'app-setup',
|
||||
@@ -34,7 +35,8 @@ export class SetupComponent {
|
||||
username: ['', [Validators.required]],
|
||||
email: ['', [Validators.required, Validators.email]],
|
||||
password: ['', [Validators.required, Validators.minLength(6)]],
|
||||
});
|
||||
confirmPassword: ['', [Validators.required]],
|
||||
}, {validators: [passwordMatchValidator('password', 'confirmPassword')]});
|
||||
}
|
||||
|
||||
onSubmit(): void {
|
||||
@@ -42,8 +44,10 @@ export class SetupComponent {
|
||||
|
||||
this.loading = true;
|
||||
this.error = null;
|
||||
|
||||
this.setupService.createAdmin(this.setupForm.value).subscribe({
|
||||
// Remove confirm password from the payload, as it does not need to be sent to backend
|
||||
const { confirmPassword, ...payload } = this.setupForm.value;
|
||||
void confirmPassword;
|
||||
this.setupService.createAdmin(payload).subscribe({
|
||||
next: () => {
|
||||
this.success = true;
|
||||
setTimeout(() => this.router.navigate(['/login']), 1500);
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import {AbstractControl, ValidationErrors, ValidatorFn} from '@angular/forms';
|
||||
|
||||
export const passwordMatchValidator = (
|
||||
passwordControlName: string,
|
||||
confirmPasswordControlName: string
|
||||
): ValidatorFn => (control: AbstractControl): ValidationErrors | null => {
|
||||
const password = control.get(passwordControlName)?.value;
|
||||
const confirmPassword = control.get(confirmPasswordControlName)?.value;
|
||||
|
||||
if (!password || !confirmPassword) return null;
|
||||
|
||||
return password === confirmPassword ? null : {passwordMismatch: true};
|
||||
};
|
||||
Reference in New Issue
Block a user