From d9d7b27c6469ddf26e1cfd8fec31dd3b776d6784 Mon Sep 17 00:00:00 2001 From: Eugenio Romano Date: Thu, 18 Aug 2016 16:50:40 +0100 Subject: [PATCH] change ticket storing and remove is login check in the login --- .../AlfrescoAuthentication.service.spec.ts | 53 ++++++++----------- .../AlfrescoAuthentication.service.ts | 38 ++++++------- .../src/services/AlfrescoContent.spec.ts | 4 +- 3 files changed, 38 insertions(+), 57 deletions(-) diff --git a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthentication.service.spec.ts b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthentication.service.spec.ts index 8806839543..097d2f0157 100644 --- a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthentication.service.spec.ts +++ b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthentication.service.spec.ts @@ -23,22 +23,11 @@ import {AlfrescoAuthenticationService} from './AlfrescoAuthentication.service'; declare var AlfrescoApi: any; describe('AlfrescoAuthentication', () => { - let injector, fakePromiseECM, fakePromiseBPM, authService, fakePromiseBPMECM; + let injector, fakePromise, fakePromiseBPMECM, authService; - fakePromiseECM = new Promise(function (resolve, reject) { + fakePromise = new Promise(function (resolve, reject) { resolve( - 'fake-post-ticket-ECM' - ); - reject({ - response: { - error: 'fake-error' - } - }); - }); - - fakePromiseBPM = new Promise(function (resolve, reject) { - resolve( - 'fake-post-ticket-BPM' + 'fake-post-ticket' ); reject({ response: { @@ -91,11 +80,11 @@ describe('AlfrescoAuthentication', () => { }); it('should return an ECM ticket after the login done', (done) => { - spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseECM); + spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromise); authService.login('fake-username', 'fake-password').subscribe(() => { expect(authService.isLoggedIn()).toBe(true); - expect(authService.getTicket()).toEqual('fake-post-ticket-ECM'); + expect(authService.getTicket()).toEqual('fake-post-ticket'); done(); }); }); @@ -115,7 +104,7 @@ describe('AlfrescoAuthentication', () => { }); it('should login in the ECM if no provider are defined calling the login', (done) => { - spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseECM); + spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromise); authService.login('fake-username', 'fake-password').subscribe(() => { done(); @@ -123,28 +112,28 @@ describe('AlfrescoAuthentication', () => { }); it('should return a ticket undefined after logout', (done) => { - localStorage.setItem('ticket-ECM', 'fake-post-ticket-ECM'); - spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(fakePromiseECM); + localStorage.setItem('ticket', 'fake-post-ticket'); + spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(fakePromise); authService.logout().subscribe(() => { expect(authService.isLoggedIn()).toBe(false); expect(authService.getTicket()).toBeUndefined(); - expect(localStorage.getItem('ticket-ECM')).toBeUndefined(); + expect(localStorage.getItem('ticket')).toBeUndefined(); done(); }); }); it('should logout only if the provider is already logged in', (done) => { - localStorage.setItem('ticket-ECM', 'fake-post-ticket-ECM'); + localStorage.setItem('ticket', 'fake-post-ticket'); - spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(fakePromiseECM); + spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(fakePromise); - authService.saveTicket('fake-ticket-ECM'); + authService.saveTicket('fake-ticket'); authService.logout().subscribe(() => { expect(authService.isLoggedIn()).toBe(false); expect(authService.getTicket()).toBeUndefined(); - expect(localStorage.getItem('ticket-ECM')).toBeUndefined(); + expect(localStorage.getItem('ticket')).toBeUndefined(); done(); }); }); @@ -163,11 +152,11 @@ describe('AlfrescoAuthentication', () => { it('should return an BPM ticket after the login done', (done) => { - spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseBPM); + spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromise); authService.login('fake-username', 'fake-password').subscribe(() => { expect(authService.isLoggedIn()).toBe(true); - expect(authService.getTicket()).toEqual('fake-post-ticket-BPM'); + expect(authService.getTicket()).toEqual('fake-post-ticket'); done(); }); }); @@ -186,22 +175,22 @@ describe('AlfrescoAuthentication', () => { }); it('should return a ticket undefined after logout', (done) => { - spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseBPM); + spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromise); authService.login('fake-username', 'fake-password').subscribe(() => { - spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(fakePromiseBPM); + spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(fakePromise); authService.logout().subscribe(() => { expect(authService.isLoggedIn()).toBe(false); expect(authService.getTicket()).toBeUndefined(); - expect(localStorage.getItem('ticket-BPM')).toBeUndefined(); + expect(localStorage.getItem('ticket')).toBeUndefined(); done(); }); }); }); it('should return an error when the logout return error', (done) => { - localStorage.setItem('ticket-BPM', 'fake-post-ticket-BPM'); + localStorage.setItem('ticket', 'fake-post-ticket'); spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(Promise.reject('fake logout error')); authService.logout().subscribe( @@ -209,7 +198,7 @@ describe('AlfrescoAuthentication', () => { }, (err: any) => { expect(err).toBeDefined(); - expect(localStorage.getItem('ticket-BPM')).toEqual('fake-post-ticket-BPM'); + expect(localStorage.getItem('ticket')).toEqual('fake-post-ticket'); done(); }); }); @@ -220,7 +209,7 @@ describe('AlfrescoAuthentication', () => { beforeEach(() => { authService = injector.get(AlfrescoAuthenticationService); authService.alfrescoSetting.setProviders('ALL'); - spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseBPMECM); + spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromise); }); it('should host ecm url change be reflected in the api configuration', (done) => { diff --git a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthentication.service.ts b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthentication.service.ts index cc7e1d84c1..9742e6179e 100644 --- a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthentication.service.ts +++ b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthentication.service.ts @@ -69,20 +69,12 @@ export class AlfrescoAuthenticationService { * @returns {Observable|Observable} */ login(username: string, password: string) { - - if (this.isLoggedIn()) { - return Observable.create((observer) => { - observer.next({type: this.alfrescoSetting.getProviders(), ticket: this.getTicket()}); - observer.complete(); - }).catch(this.handleError); - } else { - return Observable.fromPromise(this.callApiLogin(username, password)) - .map((response: any) => { - this.saveTicket(response); - return {type: this.alfrescoSetting.getProviders(), ticket: response}; - }) - .catch(this.handleError); - } + return Observable.fromPromise(this.callApiLogin(username, password)) + .map((response: any) => { + this.saveTicket(response); + return {type: this.alfrescoSetting.getProviders(), ticket: response}; + }) + .catch(this.handleError); } /** @@ -110,13 +102,6 @@ export class AlfrescoAuthenticationService { .catch(this.handleError); } - /** - * Remove the login ticket from localStorage - */ - public removeTicket(): void { - localStorage.removeItem(`ticket-${this.alfrescoSetting.getProviders()}`); - } - /** * * @returns {*|Observable|Observable|Promise} @@ -127,12 +112,19 @@ export class AlfrescoAuthenticationService { } } + /** + * Remove the login ticket from localStorage + */ + public removeTicket(): void { + localStorage.removeItem('ticket'); + } + /** * The method return the ticket stored in the localStorage * @returns ticket */ public getTicket(): string { - return localStorage.getItem(`ticket-${this.alfrescoSetting.getProviders()}`); + return localStorage.getItem('ticket'); } /** @@ -141,7 +133,7 @@ export class AlfrescoAuthenticationService { */ public saveTicket(ticket): void { if (ticket) { - localStorage.setItem(`ticket-${this.alfrescoSetting.getProviders()}`, ticket); + localStorage.setItem('ticket', ticket); } } diff --git a/ng2-components/ng2-alfresco-core/src/services/AlfrescoContent.spec.ts b/ng2-components/ng2-alfresco-core/src/services/AlfrescoContent.spec.ts index 8731e8430f..922f976d63 100644 --- a/ng2-components/ng2-alfresco-core/src/services/AlfrescoContent.spec.ts +++ b/ng2-components/ng2-alfresco-core/src/services/AlfrescoContent.spec.ts @@ -49,13 +49,13 @@ describe('AlfrescoContentService', () => { }); it('should return a valid content URL', () => { - expect(contentService.getContentUrl(node)).toBe('http://127.0.0.1:8080/alfresco/api/' + + expect(contentService.getContentUrl(node)).toBe('http://localhost:8080/alfresco/api/' + '-default-/public/alfresco/versions/1/nodes/fake-node-id/content?attachment=false&alf_ticket=myTicket'); }); it('should return a valid thumbnail URL', () => { expect(contentService.getDocumentThumbnailUrl(node)) - .toBe('http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco' + + .toBe('http://localhost:8080/alfresco/api/-default-/public/alfresco' + '/versions/1/nodes/fake-node-id/renditions/doclib/content?attachment=false&alf_ticket=myTicket'); }); });