Fix authentication unit test

This commit is contained in:
mauriziovitale84 2016-07-18 12:44:41 +01:00
parent 0bda181b94
commit 1d3cf1330d
4 changed files with 50 additions and 24 deletions

View File

@ -40,8 +40,7 @@ export class AlfrescoAuthenticationBPM extends AlfrescoAuthenticationBase implem
login(username: string, password: string): Observable<any> {
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);
}

View File

@ -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<R>|Observable<T>}
*/
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<any>}
*/
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 => <any> 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<R>|Observable<T>}
*/
public logout() {
return Observable.fromPromise(this.alfrescoApi.logout())
return Observable.fromPromise(this.callApiLogout())
.map(res => <any> res)
.do(response => {
this.removeTicket(this.TYPE);
@ -103,6 +107,14 @@ export class AlfrescoAuthenticationECM extends AlfrescoAuthenticationBase implem
.catch(this.handleError);
}
/**
*
* @returns {*|Observable<string>|Observable<any>|Promise<T>}
*/
private callApiLogout():Promise<any> {
return this.alfrescoApi.logout();
}
/**
* The method return the ticket stored in the localStorage

View File

@ -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);

View File

@ -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<T>}