From 76a6004370cee3ac1b48afe37e20b678c3e0a8d3 Mon Sep 17 00:00:00 2001 From: OZGCloud <ozgcloud@mgm-tp.com> Date: Wed, 6 Mar 2024 12:09:07 +0100 Subject: [PATCH] OZG-4321 simplify login function; fix test --- ...profile-button-container.component.spec.ts | 2 +- .../src/lib/authentication.service.spec.ts | 56 ++++++++----------- .../src/lib/authentication.service.ts | 12 +--- 3 files changed, 26 insertions(+), 44 deletions(-) diff --git a/alfa-client/apps/admin/src/common/user-profile-button-container/user-profile-button-container.component.spec.ts b/alfa-client/apps/admin/src/common/user-profile-button-container/user-profile-button-container.component.spec.ts index ca63742722..f96d0b911d 100644 --- a/alfa-client/apps/admin/src/common/user-profile-button-container/user-profile-button-container.component.spec.ts +++ b/alfa-client/apps/admin/src/common/user-profile-button-container/user-profile-button-container.component.spec.ts @@ -66,7 +66,7 @@ describe('UserProfileButtonContainerComponent', () => { fixture.detectChanges(); const buttonElement: HTMLElement = getElementFromFixture(fixture, dropDownButton); - expect(buttonElement.textContent).toEqual('AV'); + expect(buttonElement.textContent.trim()).toEqual('AV'); }); }); diff --git a/alfa-client/libs/authentication/src/lib/authentication.service.spec.ts b/alfa-client/libs/authentication/src/lib/authentication.service.spec.ts index 466e7d4567..16b30c377f 100644 --- a/alfa-client/libs/authentication/src/lib/authentication.service.spec.ts +++ b/alfa-client/libs/authentication/src/lib/authentication.service.spec.ts @@ -23,53 +23,43 @@ describe('AuthenticationService', () => { }); describe('login', () => { - it('should call proceedWithLogin', () => { - service.proceedWithLogin = jest.fn().mockImplementation(() => Promise.resolve()); + it('should configure service with authConfig', () => { + const authConfig: AuthConfig = createAuthConfig(); + service.buildConfiguration = jest.fn().mockReturnValue(authConfig); service.login(); - expect(service.proceedWithLogin).toHaveBeenCalled(); + expect(oAuthService.configure).toHaveBeenCalledWith(authConfig); }); - describe('proceed with login', () => { - it('should configure service with authConfig', () => { - const authConfig: AuthConfig = createAuthConfig(); - service.buildConfiguration = jest.fn().mockReturnValue(authConfig); - - service.proceedWithLogin(Promise.resolve); - - expect(oAuthService.configure).toHaveBeenCalledWith(authConfig); - }); - - it('should setup automatic silent refresh', () => { - service.proceedWithLogin(Promise.resolve); + it('should setup automatic silent refresh', () => { + service.login(); - expect(oAuthService.setupAutomaticSilentRefresh).toHaveBeenCalled(); - }); + expect(oAuthService.setupAutomaticSilentRefresh).toHaveBeenCalled(); + }); - // it('should set tokenValidator', () => { - // oAuthService.tokenValidationHandler = null; + it('should set tokenValidator', () => { + oAuthService.tokenValidationHandler = null; - // service.proceedWithLogin(Promise.resolve); + service.login(); - // expect(oAuthService.tokenValidationHandler).not.toBeNull(); - // }); + expect(oAuthService.tokenValidationHandler).not.toBeNull(); + }); - it('should load discovery document and login', () => { - service.proceedWithLogin(Promise.resolve); + it('should load discovery document and login', () => { + service.login(); - expect(oAuthService.loadDiscoveryDocumentAndLogin).toHaveBeenCalled(); - }); + expect(oAuthService.loadDiscoveryDocumentAndLogin).toHaveBeenCalled(); + }); - it('should set current user', fakeAsync(() => { - service.setCurrentUser = jest.fn(); + it('should set current user', fakeAsync(() => { + service.setCurrentUser = jest.fn(); - service.proceedWithLogin(() => Promise.resolve); - tick(); + service.login(); + tick(); - expect(service.setCurrentUser).toHaveBeenCalled(); - })); - }); + expect(service.setCurrentUser).toHaveBeenCalled(); + })); }); describe('set current user', () => { diff --git a/alfa-client/libs/authentication/src/lib/authentication.service.ts b/alfa-client/libs/authentication/src/lib/authentication.service.ts index 542131f685..0e45de7eec 100644 --- a/alfa-client/libs/authentication/src/lib/authentication.service.ts +++ b/alfa-client/libs/authentication/src/lib/authentication.service.ts @@ -15,19 +15,11 @@ export class AuthenticationService { ) {} public async login(): Promise<void> { - return new Promise<void>((resolve: (value: void | PromiseLike<void>) => void) => - this.proceedWithLogin(resolve), - ); - } - - proceedWithLogin(resolve: (value: void | PromiseLike<void>) => void): void { this.oAuthService.configure(this.buildConfiguration()); this.oAuthService.setupAutomaticSilentRefresh(); this.oAuthService.tokenValidationHandler = new JwksValidationHandler(); - this.oAuthService.loadDiscoveryDocumentAndLogin().then(() => { - this.setCurrentUser(); - resolve(); - }); + await this.oAuthService.loadDiscoveryDocumentAndLogin(); + this.setCurrentUser(); } buildConfiguration(): AuthConfig { -- GitLab