69432: Fix being able to send invalid password form + 6 character password validator

This commit is contained in:
Kristof De Langhe
2020-03-18 11:43:56 +01:00
parent 33e395a7f6
commit a8636b0e5c
4 changed files with 28 additions and 8 deletions

View File

@@ -5,4 +5,5 @@
[formGroup]="formGroup"
[displaySubmit]="false">
</ds-form>
<div class="container-fluid text-danger" *ngIf="formGroup.hasError('notLongEnough')">{{'profile.security.form.error.password-length' | translate}}</div>
<div class="container-fluid text-danger" *ngIf="formGroup.hasError('notSame')">{{'profile.security.form.error.matching-passwords' | translate}}</div>

View File

@@ -80,14 +80,14 @@ describe('ProfilePageSecurityFormComponent', () => {
});
});
describe('when both password fields are filled in and equal', () => {
describe('when both password fields are filled in, long enough and equal', () => {
let result;
let operations;
beforeEach(() => {
setModelValue('password', 'test');
setModelValue('passwordrepeat', 'test');
operations = [{ op: 'replace', path: '/password', value: 'test' }];
setModelValue('password', 'testest');
setModelValue('passwordrepeat', 'testest');
operations = [{ op: 'replace', path: '/password', value: 'testest' }];
result = component.updateSecurity();
});

View File

@@ -64,7 +64,7 @@ export class ProfilePageSecurityFormComponent implements OnInit {
}
ngOnInit(): void {
this.formGroup = this.formService.createFormGroup(this.formModel, { validators: this.checkPasswords });
this.formGroup = this.formService.createFormGroup(this.formModel, { validators: [this.checkPasswordsEqual, this.checkPasswordLength] });
this.updateFieldTranslations();
this.translate.onLangChange
.subscribe(() => {
@@ -87,11 +87,21 @@ export class ProfilePageSecurityFormComponent implements OnInit {
* Check if both password fields are filled in and equal
* @param group The FormGroup to validate
*/
checkPasswords(group: FormGroup) {
checkPasswordsEqual(group: FormGroup) {
const pass = group.get('password').value;
const repeatPass = group.get('passwordrepeat').value;
return isEmpty(repeatPass) || pass === repeatPass ? null : { notSame: true };
return pass === repeatPass ? null : { notSame: true };
}
/**
* Check if the password is at least 6 characters long
* @param group The FormGroup to validate
*/
checkPasswordLength(group: FormGroup) {
const pass = group.get('password').value;
return isEmpty(pass) || pass.length >= 6 ? null : { notLongEnough: true };
}
/**
@@ -109,7 +119,12 @@ export class ProfilePageSecurityFormComponent implements OnInit {
const passEntered = isNotEmpty(pass);
if (!this.formGroup.valid) {
if (passEntered) {
this.notificationsService.error(this.translate.instant(this.NOTIFICATIONS_PREFIX + 'error.not-same'));
if (this.checkPasswordsEqual(this.formGroup) != null) {
this.notificationsService.error(this.translate.instant(this.NOTIFICATIONS_PREFIX + 'error.not-same'));
}
if (this.checkPasswordLength(this.formGroup) != null) {
this.notificationsService.error(this.translate.instant(this.NOTIFICATIONS_PREFIX + 'error.not-long-enough'));
}
return true;
}
return false;