Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Password): Allow special characters in password #1402

Merged
merged 15 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"@stomp/rx-stomp": "^1.1.4",
"@stomp/stompjs": "^5.4.4",
"@tinymce/tinymce-angular": "^4.2.4",
"@zxcvbn-ts/core": "^2.2.1",
"@zxcvbn-ts/language-en": "^2.1.0",
"angular-password-strength-meter": "^6.0.0",
"canvg": "^2.0.0",
"compute-covariance": "^1.0.1",
"core-js": "^3.22.0",
Expand Down
27 changes: 27 additions & 0 deletions src/app/common/password-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { FormGroup } from '@angular/forms';
import { NewPasswordAndConfirmComponent } from '../password/new-password-and-confirm/new-password-and-confirm.component';
import { PasswordErrors } from '../domain/password/password-errors';

export function changePasswordError(
error: PasswordErrors,
incorrectPasswordFormGroup: FormGroup,
invalidPasswordFormGroup: FormGroup,
previousPasswordFieldName: string
): void {
switch (error.messageCode) {
case 'incorrectPassword':
incorrectPasswordFormGroup
.get(previousPasswordFieldName)
.setErrors({ incorrectPassword: true });
break;
case 'invalidPassword':
injectPasswordErrors(invalidPasswordFormGroup, error);
break;
}
}

export function injectPasswordErrors(formGroup: FormGroup, passwordErrors: PasswordErrors): void {
formGroup
.get(NewPasswordAndConfirmComponent.NEW_PASSWORD_FORM_CONTROL_NAME)
.setErrors(passwordErrors);
}
12 changes: 12 additions & 0 deletions src/app/domain/password/password-errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export class PasswordErrors {
messageCode: string = 'invalidPassword';
missingLetter: boolean;
missingNumber: boolean;
tooShort: boolean;

constructor(missingLetter: boolean, missingNumber: boolean, tooShort: boolean) {
this.missingLetter = missingLetter;
this.missingNumber = missingNumber;
this.tooShort = tooShort;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { StudentService } from '../../../student/student.service';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { PasswordModule } from '../../../password/password.module';
import { PasswordRequirementComponent } from '../../../password/password-requirement/password-requirement.component';

export class MockStudentService {
changePassword(
Expand All @@ -26,8 +27,6 @@ export class MockStudentService {
}
}

const PASSWORD = 'Abcd1234';

describe('ForgotStudentPasswordChangeComponent', () => {
let component: ForgotStudentPasswordChangeComponent;
let fixture: ComponentFixture<ForgotStudentPasswordChangeComponent>;
Expand Down Expand Up @@ -65,8 +64,9 @@ describe('ForgotStudentPasswordChangeComponent', () => {
});

it('should enable the submit button when the password fields are filled in', () => {
component.changePasswordFormGroup.controls['newPassword'].setValue(PASSWORD);
component.changePasswordFormGroup.controls['confirmNewPassword'].setValue(PASSWORD);
const password = PasswordRequirementComponent.VALID_PASSWORD;
component.changePasswordFormGroup.controls['newPassword'].setValue(password);
component.changePasswordFormGroup.controls['confirmNewPassword'].setValue(password);
fixture.detectChanges();
const submitButton = getSubmitButton();
expect(submitButton.disabled).toBe(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { ActivatedRoute, Router } from '@angular/router';
import { StudentService } from '../../../student/student.service';
import { finalize } from 'rxjs/operators';
import { NewPasswordAndConfirmComponent } from '../../../password/new-password-and-confirm/new-password-and-confirm.component';
import { injectPasswordErrors } from '../../../common/password-helper';
import { PasswordErrors } from '../../../domain/password/password-errors';

@Component({
selector: 'forgot-student-password-change',
Expand Down Expand Up @@ -62,20 +64,10 @@ export class ForgotStudentPasswordChangeComponent implements OnInit {
this.goToSuccessPage();
}

private changePasswordError(error: any): void {
const formError: any = {};
private changePasswordError(error: PasswordErrors): void {
switch (error.messageCode) {
case 'invalidPasswordLength':
formError.minlength = true;
this.changePasswordFormGroup
.get(NewPasswordAndConfirmComponent.NEW_PASSWORD_FORM_CONTROL_NAME)
.setErrors(formError);
break;
case 'invalidPasswordPattern':
formError.pattern = true;
this.changePasswordFormGroup
.get(NewPasswordAndConfirmComponent.NEW_PASSWORD_FORM_CONTROL_NAME)
.setErrors(formError);
case 'invalidPassword':
injectPasswordErrors(this.changePasswordFormGroup, error);
break;
default:
this.setErrorOccurredMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PasswordModule } from '../../../password/password.module';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatCardModule } from '@angular/material/card';
import { MatDividerModule } from '@angular/material/divider';
import { PasswordRequirementComponent } from '../../../password/password-requirement/password-requirement.component';

export class MockTeacherService {
changePassword(
Expand Down Expand Up @@ -50,8 +51,8 @@ describe('ForgotTeacherPasswordChangeComponent', () => {
MatCardModule,
MatDividerModule,
PasswordModule,
RouterTestingModule,
ReactiveFormsModule
ReactiveFormsModule,
RouterTestingModule
],
providers: [{ provide: TeacherService, useClass: MockTeacherService }],
schemas: []
Expand Down Expand Up @@ -100,7 +101,7 @@ describe('ForgotTeacherPasswordChangeComponent', () => {
const navigateSpy = spyOn(router, 'navigate');
component.username = 'SpongebobSquarepants';
component.verificationCode = '123456';
const newPassword = 'Abcd1234';
const newPassword = PasswordRequirementComponent.VALID_PASSWORD;
component.changePasswordFormGroup.controls['newPassword'].setValue(newPassword);
component.changePasswordFormGroup.controls['confirmNewPassword'].setValue(newPassword);
component.submit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { AbstractControl, FormBuilder, FormGroup } from '@angular/forms';
import { TeacherService } from '../../../teacher/teacher.service';
import { finalize } from 'rxjs/operators';
import { NewPasswordAndConfirmComponent } from '../../../password/new-password-and-confirm/new-password-and-confirm.component';
import { injectPasswordErrors } from '../../../common/password-helper';
import { PasswordErrors } from '../../../domain/password/password-errors';

@Component({
selector: 'app-forgot-teacher-password-change',
Expand Down Expand Up @@ -67,8 +69,7 @@ export class ForgotTeacherPasswordChangeComponent implements OnInit {
this.goToSuccessPage();
}

private changePasswordError(error: any): void {
const formError: any = {};
private changePasswordError(error: PasswordErrors): void {
switch (error.messageCode) {
case 'tooManyVerificationCodeAttempts':
this.setTooManyVerificationCodeAttemptsMessage();
Expand All @@ -85,23 +86,13 @@ export class ForgotTeacherPasswordChangeComponent implements OnInit {
case 'verificationCodeIncorrect':
this.setVerificationCodeIncorrectMessage();
break;
case 'invalidPasswordLength':
formError.minlength = true;
this.changePasswordFormGroup
.get(NewPasswordAndConfirmComponent.NEW_PASSWORD_FORM_CONTROL_NAME)
.setErrors(formError);
break;
case 'invalidPasswordPattern':
formError.pattern = true;
this.changePasswordFormGroup
.get(NewPasswordAndConfirmComponent.NEW_PASSWORD_FORM_CONTROL_NAME)
.setErrors(formError);
case 'invalidPassword':
injectPasswordErrors(this.changePasswordFormGroup, error);
break;
case 'passwordDoesNotMatch':
formError.passwordDoesNotMatch = true;
this.changePasswordFormGroup
.get(NewPasswordAndConfirmComponent.CONFIRM_NEW_PASSWORD_FORM_CONTROL_NAME)
.setErrors(formError);
.setErrors({ passwordDoesNotMatch: true });
break;
default:
this.setErrorOccurredMessage();
Expand Down
Loading
Loading