mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
Moved the providers inside AlfrescoSetting
This commit is contained in:
@@ -84,13 +84,18 @@ describe('AlfrescoAuthentication', () => {
|
||||
return keys[i] || null;
|
||||
});
|
||||
|
||||
service = injector.get(AlfrescoAuthenticationService);
|
||||
// service = injector.get(AlfrescoAuthenticationService);
|
||||
});
|
||||
|
||||
describe('when the setting is ECM', () => {
|
||||
|
||||
it('should create an AlfrescoAuthenticationECM instance', (done) => {
|
||||
let providers = ['ECM'];
|
||||
|
||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
||||
alfSetting.providers = providers;
|
||||
|
||||
service = injector.get(AlfrescoAuthenticationService);
|
||||
spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise').and.returnValue(fakePromiseECM);
|
||||
|
||||
service.login('fake-username', 'fake-password', providers)
|
||||
@@ -106,6 +111,10 @@ describe('AlfrescoAuthentication', () => {
|
||||
|
||||
it('should return an ECM token after the login done', (done) => {
|
||||
let providers = ['ECM'];
|
||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
||||
alfSetting.providers = providers;
|
||||
|
||||
service = injector.get(AlfrescoAuthenticationService);
|
||||
spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise').and.returnValue(fakePromiseECM);
|
||||
|
||||
service.login('fake-username', 'fake-password', providers)
|
||||
@@ -119,6 +128,10 @@ describe('AlfrescoAuthentication', () => {
|
||||
|
||||
it('should return token undefined when the credentials are wrong', (done) => {
|
||||
let providers = ['ECM'];
|
||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
||||
alfSetting.providers = providers;
|
||||
|
||||
service = injector.get(AlfrescoAuthenticationService);
|
||||
spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise')
|
||||
.and.returnValue(Promise.reject('fake invalid credentials'));
|
||||
|
||||
@@ -137,7 +150,10 @@ describe('AlfrescoAuthentication', () => {
|
||||
|
||||
it('should return an error if no provider are defined calling the login', (done) => {
|
||||
let providers = [];
|
||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
||||
alfSetting.providers = providers;
|
||||
|
||||
service = injector.get(AlfrescoAuthenticationService);
|
||||
service.login('fake-username', 'fake-password', providers)
|
||||
.subscribe(
|
||||
(res) => {
|
||||
@@ -153,7 +169,10 @@ describe('AlfrescoAuthentication', () => {
|
||||
|
||||
it('should return an error if an empty provider are defined calling the login', (done) => {
|
||||
let providers = [''];
|
||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
||||
alfSetting.providers = providers;
|
||||
|
||||
service = injector.get(AlfrescoAuthenticationService);
|
||||
service.login('fake-username', 'fake-password', providers)
|
||||
.subscribe(
|
||||
(res) => {
|
||||
@@ -161,7 +180,7 @@ describe('AlfrescoAuthentication', () => {
|
||||
},
|
||||
(err: any) => {
|
||||
expect(err).toBeDefined();
|
||||
expect(err).toEqual('No providers defined');
|
||||
expect(err.message).toEqual('Wrong provider defined');
|
||||
done();
|
||||
}
|
||||
);
|
||||
@@ -169,6 +188,10 @@ describe('AlfrescoAuthentication', () => {
|
||||
|
||||
it('should return a token undefined after logout', (done) => {
|
||||
let providers = ['ECM'];
|
||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
||||
alfSetting.providers = providers;
|
||||
|
||||
service = injector.get(AlfrescoAuthenticationService);
|
||||
localStorage.setItem('token-ECM', 'fake-post-token-ECM');
|
||||
service.createProviderInstance(providers);
|
||||
spyOn(AlfrescoAuthenticationECM.prototype, 'getDeleteTicketPromise').and.returnValue(fakePromiseECM);
|
||||
@@ -184,6 +207,11 @@ describe('AlfrescoAuthentication', () => {
|
||||
});
|
||||
|
||||
it('should return an error if no provider are defined calling the logout', (done) => {
|
||||
let providers = [];
|
||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
||||
alfSetting.providers = providers;
|
||||
|
||||
service = injector.get(AlfrescoAuthenticationService);
|
||||
service.logout()
|
||||
.subscribe(
|
||||
(res) => {
|
||||
@@ -207,6 +235,10 @@ describe('AlfrescoAuthentication', () => {
|
||||
|
||||
it('should create an AlfrescoAuthenticationBPM instance', (done) => {
|
||||
let providers = ['BPM'];
|
||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
||||
alfSetting.providers = providers;
|
||||
|
||||
service = injector.get(AlfrescoAuthenticationService);
|
||||
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(fakePromiseBPM);
|
||||
|
||||
service.login('fake-username', 'fake-password', providers)
|
||||
@@ -222,6 +254,10 @@ describe('AlfrescoAuthentication', () => {
|
||||
|
||||
it('should return an BPM token after the login done', (done) => {
|
||||
let providers = ['BPM'];
|
||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
||||
alfSetting.providers = providers;
|
||||
|
||||
service = injector.get(AlfrescoAuthenticationService);
|
||||
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(fakePromiseBPM);
|
||||
|
||||
service.login('fake-username', 'fake-password', providers)
|
||||
@@ -235,6 +271,10 @@ describe('AlfrescoAuthentication', () => {
|
||||
|
||||
it('should return token undefined when the credentials are wrong', (done) => {
|
||||
let providers = ['BPM'];
|
||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
||||
alfSetting.providers = providers;
|
||||
|
||||
service = injector.get(AlfrescoAuthenticationService);
|
||||
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(Promise.reject('fake invalid credentials'));
|
||||
|
||||
service.login('fake-wrong-username', 'fake-wrong-password', providers)
|
||||
@@ -252,6 +292,10 @@ describe('AlfrescoAuthentication', () => {
|
||||
|
||||
it('should return a token undefined after logout', (done) => {
|
||||
let providers = ['BPM'];
|
||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
||||
alfSetting.providers = providers;
|
||||
|
||||
service = injector.get(AlfrescoAuthenticationService);
|
||||
localStorage.setItem('token-BPM', 'fake-post-token-BPM');
|
||||
service.createProviderInstance(providers);
|
||||
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogout').and.returnValue(fakePromiseBPM);
|
||||
@@ -268,6 +312,10 @@ describe('AlfrescoAuthentication', () => {
|
||||
|
||||
it('should throw an error when the logout return error', (done) => {
|
||||
let providers = ['BPM'];
|
||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
||||
alfSetting.providers = providers;
|
||||
|
||||
service = injector.get(AlfrescoAuthenticationService);
|
||||
localStorage.setItem('token-BPM', 'fake-post-token-BPM');
|
||||
service.createProviderInstance(providers);
|
||||
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogout').and.returnValue(Promise.reject('fake logout error'));
|
||||
@@ -293,6 +341,10 @@ describe('AlfrescoAuthentication', () => {
|
||||
|
||||
it('should create both instances', (done) => {
|
||||
let providers = ['ECM', 'BPM'];
|
||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
||||
alfSetting.providers = providers;
|
||||
|
||||
service = injector.get(AlfrescoAuthenticationService);
|
||||
spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise').and.returnValue(fakePromiseECM);
|
||||
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(fakePromiseBPM);
|
||||
|
||||
@@ -311,6 +363,10 @@ describe('AlfrescoAuthentication', () => {
|
||||
|
||||
it('should return both ECM and BPM tokens after the login done', (done) => {
|
||||
let providers = ['ECM', 'BPM'];
|
||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
||||
alfSetting.providers = providers;
|
||||
|
||||
service = injector.get(AlfrescoAuthenticationService);
|
||||
spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise').and.returnValue(fakePromiseECM);
|
||||
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(fakePromiseBPM);
|
||||
|
||||
@@ -327,6 +383,10 @@ describe('AlfrescoAuthentication', () => {
|
||||
|
||||
it('should return token undefined when the credentials are correct for the ECM login but wrong for the BPM login', (done) => {
|
||||
let providers = ['ECM', 'BPM'];
|
||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
||||
alfSetting.providers = providers;
|
||||
|
||||
service = injector.get(AlfrescoAuthenticationService);
|
||||
spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise').and.returnValue(fakePromiseECM);
|
||||
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(Promise.reject('fake invalid credentials'));
|
||||
|
||||
@@ -347,6 +407,10 @@ describe('AlfrescoAuthentication', () => {
|
||||
|
||||
it('should return token undefined when the credentials are correct for the BPM login but wrong for the ECM login', (done) => {
|
||||
let providers = ['ECM', 'BPM'];
|
||||
let alfSetting = injector.get(AlfrescoSettingsService);
|
||||
alfSetting.providers = providers;
|
||||
|
||||
service = injector.get(AlfrescoAuthenticationService);
|
||||
spyOn(AlfrescoAuthenticationECM.prototype, 'getCreateTicketPromise')
|
||||
.and.returnValue(Promise.reject('fake invalid credentials'));
|
||||
spyOn(AlfrescoAuthenticationBPM.prototype, 'apiActivitiLogin').and.returnValue(fakePromiseBPM);
|
||||
|
@@ -40,6 +40,7 @@ export class AlfrescoAuthenticationService extends AlfrescoAuthenticationBase {
|
||||
constructor(private alfrescoSettingsService: AlfrescoSettingsService,
|
||||
private http: Http) {
|
||||
super(alfrescoSettingsService, http);
|
||||
this.createProviderInstance(this.alfrescoSettingsService.getProviders());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,8 +54,7 @@ export class AlfrescoAuthenticationService extends AlfrescoAuthenticationBase {
|
||||
if (providers.length === 0) {
|
||||
return Observable.throw('No providers defined');
|
||||
} else {
|
||||
this.createProviderInstance(providers);
|
||||
return this.performeLogin(username, password);
|
||||
return this.performeLogin(username, password, providers);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,28 +65,29 @@ export class AlfrescoAuthenticationService extends AlfrescoAuthenticationBase {
|
||||
* @param password
|
||||
* @returns {Observable<R>|Observable<T>}
|
||||
*/
|
||||
private performeLogin(username: string, password: string): Observable<any> {
|
||||
private performeLogin(username: string, password: string, providers: string []): Observable<any> {
|
||||
let observableBatch = [];
|
||||
if (this.providersInstance.length !== 0) {
|
||||
this.providersInstance.forEach((authInstance) => {
|
||||
observableBatch.push(authInstance.login(username, password));
|
||||
});
|
||||
return Observable.create(observer => {
|
||||
Observable.forkJoin(observableBatch).subscribe(
|
||||
(response: any[]) => {
|
||||
this.performeSaveToken();
|
||||
/*response.forEach((res) => {
|
||||
this.performeSaveToken(res.name, res.token);
|
||||
});*/
|
||||
observer.next(response);
|
||||
},
|
||||
(err: any) => {
|
||||
observer.error(new Error(err));
|
||||
});
|
||||
});
|
||||
} else {
|
||||
return Observable.throw('No providers defined');
|
||||
}
|
||||
providers.forEach((provider) => {
|
||||
let auth: AbstractAuthentication = this.findProviderInstance(provider);
|
||||
if (auth) {
|
||||
observableBatch.push(auth.login(username, password));
|
||||
} else {
|
||||
observableBatch.push(Observable.throw('Wrong provider defined'));
|
||||
}
|
||||
});
|
||||
return Observable.create(observer => {
|
||||
Observable.forkJoin(observableBatch).subscribe(
|
||||
(response: any[]) => {
|
||||
this.performeSaveToken();
|
||||
/*response.forEach((res) => {
|
||||
this.performeSaveToken(res.name, res.token);
|
||||
});*/
|
||||
observer.next(response);
|
||||
},
|
||||
(err: any) => {
|
||||
observer.error(new Error(err));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -28,6 +28,8 @@ export class AlfrescoSettingsService {
|
||||
private _contextPath = AlfrescoSettingsService.DEFAULT_CONTEXT_PATH;
|
||||
private _apiBasePath: string = AlfrescoSettingsService.DEFAULT_BASE_API_PATH;
|
||||
|
||||
private providers: string[] = ['ECM', 'BPM'];
|
||||
|
||||
public get host(): string {
|
||||
return this._host;
|
||||
}
|
||||
@@ -39,4 +41,8 @@ export class AlfrescoSettingsService {
|
||||
getApiBaseUrl(): string {
|
||||
return this._host + this._contextPath + this._apiBasePath;
|
||||
}
|
||||
|
||||
getProviders(): string [] {
|
||||
return this.providers;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user