From 2532e370109178557b070aafeaf09bdc958c7b54 Mon Sep 17 00:00:00 2001 From: Sufiyan Shaikh Date: Mon, 13 Jun 2022 18:43:50 +0530 Subject: [PATCH] [UXP-10] Test cases --- .../eperson-form.component.spec.ts | 2 +- .../data/eperson-registration.service.spec.ts | 2 +- .../register-email-form.component.spec.ts | 51 +++++++++++++++++-- .../register-email-form.component.ts | 10 ++-- src/environments/environment.test.ts | 2 + 5 files changed, 56 insertions(+), 11 deletions(-) diff --git a/src/app/access-control/epeople-registry/eperson-form/eperson-form.component.spec.ts b/src/app/access-control/epeople-registry/eperson-form/eperson-form.component.spec.ts index 4957958658..e2bae9a820 100644 --- a/src/app/access-control/epeople-registry/eperson-form/eperson-form.component.spec.ts +++ b/src/app/access-control/epeople-registry/eperson-form/eperson-form.component.spec.ts @@ -537,7 +537,7 @@ describe('EPersonFormComponent', () => { }); it('should call epersonRegistrationService.registerEmail', () => { - expect(epersonRegistrationService.registerEmail).toHaveBeenCalledWith(ePersonEmail); + expect(epersonRegistrationService.registerEmail).toHaveBeenCalledWith(ePersonEmail, null); }); }); }); diff --git a/src/app/core/data/eperson-registration.service.spec.ts b/src/app/core/data/eperson-registration.service.spec.ts index dc13fff3a0..48f5d3cfe5 100644 --- a/src/app/core/data/eperson-registration.service.spec.ts +++ b/src/app/core/data/eperson-registration.service.spec.ts @@ -78,7 +78,7 @@ describe('EpersonRegistrationService', () => { describe('registerEmail', () => { it('should send an email registration', () => { - const expected = service.registerEmail('test@mail.org'); + const expected = service.registerEmail('test@mail.org', null); expect(requestService.send).toHaveBeenCalledWith(new PostRequest('request-id', 'rest-url/registrations', registration)); expect(expected).toBeObservable(cold('(a|)', { a: rd })); diff --git a/src/app/register-email-form/register-email-form.component.spec.ts b/src/app/register-email-form/register-email-form.component.spec.ts index a415ef4808..040af04f94 100644 --- a/src/app/register-email-form/register-email-form.component.spec.ts +++ b/src/app/register-email-form/register-email-form.component.spec.ts @@ -14,6 +14,8 @@ import { RouterStub } from '../shared/testing/router.stub'; import { NotificationsServiceStub } from '../shared/testing/notifications-service.stub'; import { RegisterEmailFormComponent } from './register-email-form.component'; import { createSuccessfulRemoteDataObject$ } from '../shared/remote-data.utils'; +import { ConfigurationDataService } from '../core/data/configuration-data.service'; +import { GoogleRecaptchaService } from '../core/data/google-recaptcha.service'; describe('RegisterEmailComponent', () => { @@ -24,6 +26,17 @@ describe('RegisterEmailComponent', () => { let epersonRegistrationService: EpersonRegistrationService; let notificationsService; + const configurationDataService = jasmine.createSpyObj('configurationDataService', { + findByPropertyName: jasmine.createSpy('findByPropertyName') + }); + + const googleRecaptchaService = jasmine.createSpyObj('googleRecaptchaService', { + getRecaptchaToken: jasmine.createSpy('getRecaptchaToken') + }); + + const confResponse$ = createSuccessfulRemoteDataObject$({ values: ['true'] }); + const confResponseDisabled$ = createSuccessfulRemoteDataObject$({ values: ['false'] }); + beforeEach(waitForAsync(() => { router = new RouterStub(); @@ -39,8 +52,10 @@ describe('RegisterEmailComponent', () => { providers: [ {provide: Router, useValue: router}, {provide: EpersonRegistrationService, useValue: epersonRegistrationService}, + {provide: ConfigurationDataService, useValue: configurationDataService}, {provide: FormBuilder, useValue: new FormBuilder()}, {provide: NotificationsService, useValue: notificationsService}, + {provide: GoogleRecaptchaService, useValue: googleRecaptchaService}, ], schemas: [CUSTOM_ELEMENTS_SCHEMA] }).compileComponents(); @@ -48,6 +63,8 @@ describe('RegisterEmailComponent', () => { beforeEach(() => { fixture = TestBed.createComponent(RegisterEmailFormComponent); comp = fixture.componentInstance; + configurationDataService.findByPropertyName.and.returnValues(confResponse$, confResponse$, confResponse$, confResponse$, confResponse$, confResponse$, confResponse$, confResponse$, confResponse$, confResponse$); + googleRecaptchaService.getRecaptchaToken.and.returnValue(observableOf('googleRecaptchaToken')); fixture.detectChanges(); }); @@ -71,21 +88,47 @@ describe('RegisterEmailComponent', () => { }); }); describe('register', () => { - it('should send a registration to the service and on success display a message and return to home', () => { + it('should send a registration to the service with google recaptcha and on success display a message and return to home', () => { comp.form.patchValue({email: 'valid@email.org'}); comp.register(); - expect(epersonRegistrationService.registerEmail).toHaveBeenCalledWith('valid@email.org'); + expect(epersonRegistrationService.registerEmail).toHaveBeenCalledWith('valid@email.org', 'googleRecaptchaToken'); expect(notificationsService.success).toHaveBeenCalled(); expect(router.navigate).toHaveBeenCalledWith(['/home']); }); - it('should send a registration to the service and on error display a message', () => { + it('should send a registration to the service with google recaptcha and on error display a message', () => { (epersonRegistrationService.registerEmail as jasmine.Spy).and.returnValue(observableOf(new RestResponse(false, 400, 'Bad Request'))); comp.form.patchValue({email: 'valid@email.org'}); comp.register(); - expect(epersonRegistrationService.registerEmail).toHaveBeenCalledWith('valid@email.org'); + expect(epersonRegistrationService.registerEmail).toHaveBeenCalledWith('valid@email.org', 'googleRecaptchaToken'); + expect(notificationsService.error).toHaveBeenCalled(); + expect(router.navigate).not.toHaveBeenCalled(); + }); + }); + describe('register', () => { + beforeEach(waitForAsync(() => { + configurationDataService.findByPropertyName.and.returnValues(confResponseDisabled$, confResponseDisabled$, confResponseDisabled$, confResponseDisabled$, confResponseDisabled$, confResponseDisabled$, confResponseDisabled$, confResponseDisabled$, confResponseDisabled$, confResponseDisabled$); + comp.ngOnInit(); + fixture.detectChanges(); + })); + + it('should send a registration to the service without google recaptcha and on success display a message and return to home', () => { + comp.form.patchValue({email: 'valid@email.org'}); + + comp.register(); + expect(epersonRegistrationService.registerEmail).toHaveBeenCalledWith('valid@email.org', null); + expect(notificationsService.success).toHaveBeenCalled(); + expect(router.navigate).toHaveBeenCalledWith(['/home']); + }); + it('should send a registration to the service without google recaptcha and on error display a message', () => { + (epersonRegistrationService.registerEmail as jasmine.Spy).and.returnValue(observableOf(new RestResponse(false, 400, 'Bad Request'))); + + comp.form.patchValue({email: 'valid@email.org'}); + + comp.register(); + expect(epersonRegistrationService.registerEmail).toHaveBeenCalledWith('valid@email.org', null); expect(notificationsService.error).toHaveBeenCalled(); expect(router.navigate).not.toHaveBeenCalled(); }); diff --git a/src/app/register-email-form/register-email-form.component.ts b/src/app/register-email-form/register-email-form.component.ts index 4fcc5e6650..b8f8a57353 100644 --- a/src/app/register-email-form/register-email-form.component.ts +++ b/src/app/register-email-form/register-email-form.component.ts @@ -74,9 +74,9 @@ export class RegisterEmailFormComponent implements OnInit { register() { if (!this.form.invalid) { if (this.registrationVerification) { - this.googleRecaptchaService.getRecaptchaToken('register_email').subscribe(res => { - if (isNotEmpty(res)) { - this.registeration(res); + this.googleRecaptchaService.getRecaptchaToken('register_email').subscribe(captcha => { + if (isNotEmpty(captcha)) { + this.registeration(captcha); } else { this.notificationService.error(this.translateService.get(`${this.MESSAGE_PREFIX}.error.head`), this.translateService.get(`${this.MESSAGE_PREFIX}.error.recaptcha`, {email: this.email.value})); @@ -91,7 +91,7 @@ export class RegisterEmailFormComponent implements OnInit { /** * Register an email address */ - registeration(captchaToken) { + registeration(captchaToken) { this.epersonRegistrationService.registerEmail(this.email.value, captchaToken).subscribe((response: RemoteData) => { if (response.hasSucceeded) { this.notificationService.success(this.translateService.get(`${this.MESSAGE_PREFIX}.success.head`), @@ -102,7 +102,7 @@ export class RegisterEmailFormComponent implements OnInit { this.translateService.get(`${this.MESSAGE_PREFIX}.error.content`, {email: this.email.value})); } }); - } + } get email() { return this.form.get('email'); diff --git a/src/environments/environment.test.ts b/src/environments/environment.test.ts index 4d466bd37b..d605320af9 100644 --- a/src/environments/environment.test.ts +++ b/src/environments/environment.test.ts @@ -13,6 +13,8 @@ export const environment: BuildConfig = { time: false }, + recaptchaSiteKey: '6LfmfEsgAAAAACNqQ0aHqJa0HOHcUsvv2OCiEbV4', + // Angular Universal server settings. ui: { ssl: false,