From 1d3cf1330d61448f33cefd67e17521dd6c85a012 Mon Sep 17 00:00:00 2001 From: mauriziovitale84 Date: Mon, 18 Jul 2016 12:44:41 +0100 Subject: [PATCH] Fix authentication unit test --- .../AlfrescoAuthenticationBPM.service.ts | 5 +-- .../AlfrescoAuthenticationECM.service.ts | 38 ++++++++++++------- ...rescoAuthenticationService.service.spec.ts | 16 ++++---- .../AlfrescoAuthenticationService.service.ts | 15 ++++++++ 4 files changed, 50 insertions(+), 24 deletions(-) diff --git a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationBPM.service.ts b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationBPM.service.ts index f379ad71fd..366c4ef918 100644 --- a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationBPM.service.ts +++ b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationBPM.service.ts @@ -40,8 +40,7 @@ export class AlfrescoAuthenticationBPM extends AlfrescoAuthenticationBase implem login(username: string, password: string): Observable { return Observable.fromPromise(this.apiActivitiLogin(username, password)) .map((response: any) => { - this.saveTicket(response.status); - return response.status; + return {type: this.TYPE, ticket: response.status}; }) .catch(this.handleError); } @@ -97,7 +96,7 @@ export class AlfrescoAuthenticationBPM extends AlfrescoAuthenticationBase implem * The method save the ticket in the localStorage * @param ticket */ - public saveTicket(ticket): void { + public saveTicket(ticket: string): void { if (ticket) { super.saveTicket(this.TYPE, ticket); } diff --git a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationECM.service.ts b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationECM.service.ts index 3ff86e8196..d1d8b21cff 100644 --- a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationECM.service.ts +++ b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationECM.service.ts @@ -54,10 +54,6 @@ export class AlfrescoAuthenticationECM extends AlfrescoAuthenticationBase implem return this.alfrescoSettingsService.host; } - getAlfrescoApi(): any { - return this.alfrescoApi; - } - /** * The method return tru if the user is logged in * @returns {boolean} @@ -73,19 +69,27 @@ export class AlfrescoAuthenticationECM extends AlfrescoAuthenticationBase implem * @returns {Observable|Observable} */ login(username: string, password: string) { + + return Observable.fromPromise(this.callApiLogin(username, password)) + .map((response: any) => { + return {type: this.TYPE, ticket: response.entry.id}; + }) + .catch(this.handleError); + } + + /** + * Initialize the alfresco Api with user and password end call the login method + * @param username + * @param password + * @returns {*|Observable} + */ + private callApiLogin(username: string, password: string) { this.alfrescoApi = new AlfrescoApi({ username: username, password: password, host: this.getBaseUrl() }); - - return Observable.fromPromise(this.alfrescoApi.login()) - .map(res => res) - .do(response => { - this.saveTicket(response); - return response; - }) - .catch(this.handleError); + return this.alfrescoApi.login(); } /** @@ -94,7 +98,7 @@ export class AlfrescoAuthenticationECM extends AlfrescoAuthenticationBase implem * @returns {Observable|Observable} */ public logout() { - return Observable.fromPromise(this.alfrescoApi.logout()) + return Observable.fromPromise(this.callApiLogout()) .map(res => res) .do(response => { this.removeTicket(this.TYPE); @@ -103,6 +107,14 @@ export class AlfrescoAuthenticationECM extends AlfrescoAuthenticationBase implem .catch(this.handleError); } + /** + * + * @returns {*|Observable|Observable|Promise} + */ + private callApiLogout():Promise { + return this.alfrescoApi.logout(); + } + /** * The method return the ticket stored in the localStorage diff --git a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationService.service.spec.ts b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationService.service.spec.ts index ea417dcca2..32903dbc43 100644 --- a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationService.service.spec.ts +++ b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationService.service.spec.ts @@ -96,7 +96,7 @@ describe('AlfrescoAuthentication', () => { alfSetting.providers = providers; service = injector.get(AlfrescoAuthenticationService); - spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise').and.returnValue(fakePromiseECM); + spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogin').and.returnValue(fakePromiseECM); service.login('fake-username', 'fake-password', providers) .subscribe(() => { @@ -115,7 +115,7 @@ describe('AlfrescoAuthentication', () => { alfSetting.providers = providers; service = injector.get(AlfrescoAuthenticationService); - spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise').and.returnValue(fakePromiseECM); + spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogin').and.returnValue(fakePromiseECM); service.login('fake-username', 'fake-password', providers) .subscribe(() => { @@ -132,7 +132,7 @@ describe('AlfrescoAuthentication', () => { alfSetting.providers = providers; service = injector.get(AlfrescoAuthenticationService); - spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise') + spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogin') .and.returnValue(Promise.reject('fake invalid credentials')); service.login('fake-wrong-username', 'fake-wrong-password', providers) @@ -194,7 +194,7 @@ describe('AlfrescoAuthentication', () => { service = injector.get(AlfrescoAuthenticationService); localStorage.setItem('ticket-ECM', 'fake-post-ticket-ECM'); service.createProviderInstance(providers); - spyOn(AlfrescoAuthenticationECM.prototype, 'getDeleteTicketPromise').and.returnValue(fakePromiseECM); + spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogout').and.returnValue(fakePromiseECM); service.logout() .subscribe(() => { @@ -345,7 +345,7 @@ describe('AlfrescoAuthentication', () => { alfSetting.providers = providers; service = injector.get(AlfrescoAuthenticationService); - spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise').and.returnValue(fakePromiseECM); + spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogin').and.returnValue(fakePromiseECM); spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(fakePromiseBPM); service.login('fake-username', 'fake-password', providers) @@ -367,7 +367,7 @@ describe('AlfrescoAuthentication', () => { alfSetting.providers = providers; service = injector.get(AlfrescoAuthenticationService); - spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise').and.returnValue(fakePromiseECM); + spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogin').and.returnValue(fakePromiseECM); spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(fakePromiseBPM); service.login('fake-username', 'fake-password', providers) @@ -387,7 +387,7 @@ describe('AlfrescoAuthentication', () => { alfSetting.providers = providers; service = injector.get(AlfrescoAuthenticationService); - spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise').and.returnValue(fakePromiseECM); + spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogin').and.returnValue(fakePromiseECM); spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(Promise.reject('fake invalid credentials')); service.login('fake-username', 'fake-password', providers) @@ -411,7 +411,7 @@ describe('AlfrescoAuthentication', () => { alfSetting.providers = providers; service = injector.get(AlfrescoAuthenticationService); - spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise') + spyOn(AlfrescoAuthenticationECM.prototype, 'callApiLogin') .and.returnValue(Promise.reject('fake invalid credentials')); spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(fakePromiseBPM); diff --git a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationService.service.ts b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationService.service.ts index 520542949c..d427bcfc7c 100644 --- a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationService.service.ts +++ b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationService.service.ts @@ -81,6 +81,9 @@ export class AlfrescoAuthenticationService extends AlfrescoAuthenticationBase { return Observable.create(observer => { Observable.forkJoin(observableBatch).subscribe( (response: any[]) => { + response.forEach((res) => { + this.performeSaveToken(res.type, res.ticket); + }); observer.next(response); }, (err: any) => { @@ -117,6 +120,18 @@ export class AlfrescoAuthenticationService extends AlfrescoAuthenticationBase { return ''; } + /** + * Save the token calling the method of the specific provider type + * @param type - providerName + * @param ticket + */ + private performeSaveToken(type: string, ticket: string) { + let auth: AbstractAuthentication = this.findProviderInstance(type); + if (auth) { + auth.saveTicket(ticket); + } + } + /** * The method remove the ticket from the local storage * @returns {Observable}