change ticket storing and remove is login check in the login

This commit is contained in:
Eugenio Romano
2016-08-18 16:50:40 +01:00
parent 6ef1fccf80
commit d9d7b27c64
3 changed files with 38 additions and 57 deletions

View File

@@ -23,22 +23,11 @@ import {AlfrescoAuthenticationService} from './AlfrescoAuthentication.service';
declare var AlfrescoApi: any; declare var AlfrescoApi: any;
describe('AlfrescoAuthentication', () => { 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( resolve(
'fake-post-ticket-ECM' 'fake-post-ticket'
);
reject({
response: {
error: 'fake-error'
}
});
});
fakePromiseBPM = new Promise(function (resolve, reject) {
resolve(
'fake-post-ticket-BPM'
); );
reject({ reject({
response: { response: {
@@ -91,11 +80,11 @@ describe('AlfrescoAuthentication', () => {
}); });
it('should return an ECM ticket after the login done', (done) => { 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(() => { authService.login('fake-username', 'fake-password').subscribe(() => {
expect(authService.isLoggedIn()).toBe(true); expect(authService.isLoggedIn()).toBe(true);
expect(authService.getTicket()).toEqual('fake-post-ticket-ECM'); expect(authService.getTicket()).toEqual('fake-post-ticket');
done(); done();
}); });
}); });
@@ -115,7 +104,7 @@ describe('AlfrescoAuthentication', () => {
}); });
it('should login in the ECM if no provider are defined calling the login', (done) => { 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(() => { authService.login('fake-username', 'fake-password').subscribe(() => {
done(); done();
@@ -123,28 +112,28 @@ describe('AlfrescoAuthentication', () => {
}); });
it('should return a ticket undefined after logout', (done) => { it('should return a ticket undefined after logout', (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.logout().subscribe(() => { authService.logout().subscribe(() => {
expect(authService.isLoggedIn()).toBe(false); expect(authService.isLoggedIn()).toBe(false);
expect(authService.getTicket()).toBeUndefined(); expect(authService.getTicket()).toBeUndefined();
expect(localStorage.getItem('ticket-ECM')).toBeUndefined(); expect(localStorage.getItem('ticket')).toBeUndefined();
done(); done();
}); });
}); });
it('should logout only if the provider is already logged in', (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(() => { authService.logout().subscribe(() => {
expect(authService.isLoggedIn()).toBe(false); expect(authService.isLoggedIn()).toBe(false);
expect(authService.getTicket()).toBeUndefined(); expect(authService.getTicket()).toBeUndefined();
expect(localStorage.getItem('ticket-ECM')).toBeUndefined(); expect(localStorage.getItem('ticket')).toBeUndefined();
done(); done();
}); });
}); });
@@ -163,11 +152,11 @@ describe('AlfrescoAuthentication', () => {
it('should return an BPM ticket after the login done', (done) => { 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(() => { authService.login('fake-username', 'fake-password').subscribe(() => {
expect(authService.isLoggedIn()).toBe(true); expect(authService.isLoggedIn()).toBe(true);
expect(authService.getTicket()).toEqual('fake-post-ticket-BPM'); expect(authService.getTicket()).toEqual('fake-post-ticket');
done(); done();
}); });
}); });
@@ -186,22 +175,22 @@ describe('AlfrescoAuthentication', () => {
}); });
it('should return a ticket undefined after logout', (done) => { 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(() => { authService.login('fake-username', 'fake-password').subscribe(() => {
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(fakePromiseBPM); spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(fakePromise);
authService.logout().subscribe(() => { authService.logout().subscribe(() => {
expect(authService.isLoggedIn()).toBe(false); expect(authService.isLoggedIn()).toBe(false);
expect(authService.getTicket()).toBeUndefined(); expect(authService.getTicket()).toBeUndefined();
expect(localStorage.getItem('ticket-BPM')).toBeUndefined(); expect(localStorage.getItem('ticket')).toBeUndefined();
done(); done();
}); });
}); });
}); });
it('should return an error when the logout return error', (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')); spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(Promise.reject('fake logout error'));
authService.logout().subscribe( authService.logout().subscribe(
@@ -209,7 +198,7 @@ describe('AlfrescoAuthentication', () => {
}, },
(err: any) => { (err: any) => {
expect(err).toBeDefined(); expect(err).toBeDefined();
expect(localStorage.getItem('ticket-BPM')).toEqual('fake-post-ticket-BPM'); expect(localStorage.getItem('ticket')).toEqual('fake-post-ticket');
done(); done();
}); });
}); });
@@ -220,7 +209,7 @@ describe('AlfrescoAuthentication', () => {
beforeEach(() => { beforeEach(() => {
authService = injector.get(AlfrescoAuthenticationService); authService = injector.get(AlfrescoAuthenticationService);
authService.alfrescoSetting.setProviders('ALL'); 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) => { it('should host ecm url change be reflected in the api configuration', (done) => {

View File

@@ -69,20 +69,12 @@ export class AlfrescoAuthenticationService {
* @returns {Observable<R>|Observable<T>} * @returns {Observable<R>|Observable<T>}
*/ */
login(username: string, password: string) { login(username: string, password: string) {
return Observable.fromPromise(this.callApiLogin(username, password))
if (this.isLoggedIn()) { .map((response: any) => {
return Observable.create((observer) => { this.saveTicket(response);
observer.next({type: this.alfrescoSetting.getProviders(), ticket: this.getTicket()}); return {type: this.alfrescoSetting.getProviders(), ticket: response};
observer.complete(); })
}).catch(this.handleError); .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);
}
} }
/** /**
@@ -110,13 +102,6 @@ export class AlfrescoAuthenticationService {
.catch(this.handleError); .catch(this.handleError);
} }
/**
* Remove the login ticket from localStorage
*/
public removeTicket(): void {
localStorage.removeItem(`ticket-${this.alfrescoSetting.getProviders()}`);
}
/** /**
* *
* @returns {*|Observable<string>|Observable<any>|Promise<T>} * @returns {*|Observable<string>|Observable<any>|Promise<T>}
@@ -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 * The method return the ticket stored in the localStorage
* @returns ticket * @returns ticket
*/ */
public getTicket(): string { 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 { public saveTicket(ticket): void {
if (ticket) { if (ticket) {
localStorage.setItem(`ticket-${this.alfrescoSetting.getProviders()}`, ticket); localStorage.setItem('ticket', ticket);
} }
} }

View File

@@ -49,13 +49,13 @@ describe('AlfrescoContentService', () => {
}); });
it('should return a valid content URL', () => { 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'); '-default-/public/alfresco/versions/1/nodes/fake-node-id/content?attachment=false&alf_ticket=myTicket');
}); });
it('should return a valid thumbnail URL', () => { it('should return a valid thumbnail URL', () => {
expect(contentService.getDocumentThumbnailUrl(node)) 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'); '/versions/1/nodes/fake-node-id/renditions/doclib/content?attachment=false&alf_ticket=myTicket');
}); });
}); });