rename two times service class just once

move responsability providers configurration in setting service
refactoring
This commit is contained in:
Eugenio Romano 2016-08-16 17:46:34 +01:00
parent 560bef7c1d
commit dc19e45e7b
18 changed files with 183 additions and 178 deletions

View File

@ -126,6 +126,9 @@ class MyDemoApp {
constructor(public alfrescoAuthenticationService: AlfrescoAuthenticationService,
private alfrescoSettingsService: AlfrescoSettingsService) {
alfrescoSettingsService.ecmHost = this.ecmHost;
alfrescoSettingsService.setProviders('ECM');
}
ngOnInit() {
@ -133,7 +136,7 @@ class MyDemoApp {
}
login() {
this.alfrescoAuthenticationService.login('admin', 'admin', 'ECM').subscribe(
this.alfrescoAuthenticationService.login('admin', 'admin').subscribe(
token => {
this.token = token.ticket;
this.authenticated = true;

View File

@ -17,15 +17,15 @@
import {it, describe} from '@angular/core/testing';
import {ReflectiveInjector, provide} from '@angular/core';
import {AlfrescoSettingsService} from './AlfrescoSettingsService.service';
import {AlfrescoAuthenticationService} from './AlfrescoAuthenticationService.service';
import {AlfrescoSettingsService} from './AlfrescoSettings.service';
import {AlfrescoAuthenticationService} from './AlfrescoAuthentication.service';
import {XHRBackend, HTTP_PROVIDERS} from '@angular/http';
import {MockBackend} from '@angular/http/testing';
declare var AlfrescoApi: any;
describe('AlfrescoAuthentication', () => {
let injector, fakePromiseECM, fakePromiseBPM, service, fakePromiseBPMECM;
let injector, fakePromiseECM, fakePromiseBPM, authService, fakePromiseBPMECM;
fakePromiseECM = new Promise(function (resolve, reject) {
resolve(
@ -90,16 +90,15 @@ describe('AlfrescoAuthentication', () => {
describe('when the setting is ECM', () => {
beforeEach(() => {
this.providers = 'ECM';
service = injector.get(AlfrescoAuthenticationService);
authService = injector.get(AlfrescoAuthenticationService);
});
it('should return an ECM ticket after the login done', (done) => {
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseECM);
service.login('fake-username', 'fake-password', this.providers).subscribe(() => {
expect(service.isLoggedIn()).toBe(true);
expect(service.getTicket()).toEqual('fake-post-ticket-ECM');
authService.login('fake-username', 'fake-password').subscribe(() => {
expect(authService.isLoggedIn()).toBe(true);
expect(authService.getTicket()).toEqual('fake-post-ticket-ECM');
done();
});
});
@ -108,12 +107,12 @@ describe('AlfrescoAuthentication', () => {
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin')
.and.returnValue(Promise.reject('fake invalid credentials'));
service.login('fake-wrong-username', 'fake-wrong-password', this.providers).subscribe(
authService.login('fake-wrong-username', 'fake-wrong-password').subscribe(
(res) => {
},
(err: any) => {
expect(service.isLoggedIn()).toBe(false);
expect(service.getTicket()).toBeUndefined();
expect(authService.isLoggedIn()).toBe(false);
expect(authService.getTicket()).toBeUndefined();
done();
});
});
@ -121,8 +120,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);
service.login('fake-username', 'fake-password').subscribe(() => {
service.TYPE = 'ECM';
authService.login('fake-username', 'fake-password').subscribe(() => {
done();
});
});
@ -131,9 +129,9 @@ describe('AlfrescoAuthentication', () => {
localStorage.setItem('ticket-ECM', 'fake-post-ticket-ECM');
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(fakePromiseECM);
service.logout().subscribe(() => {
expect(service.isLoggedIn()).toBe(false);
expect(service.getTicket()).toBeUndefined();
authService.logout().subscribe(() => {
expect(authService.isLoggedIn()).toBe(false);
expect(authService.getTicket()).toBeUndefined();
expect(localStorage.getItem('ticket-ECM')).toBeUndefined();
done();
});
@ -141,35 +139,38 @@ describe('AlfrescoAuthentication', () => {
it('should logout only if the provider is already logged in', (done) => {
localStorage.setItem('ticket-ECM', 'fake-post-ticket-ECM');
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(fakePromiseECM);
service.saveTicket('fake-ticket-ECM');
service.logout().subscribe(() => {
expect(service.isLoggedIn()).toBe(false);
expect(service.getTicket()).toBeUndefined();
authService.saveTicket('fake-ticket-ECM');
authService.logout().subscribe(() => {
expect(authService.isLoggedIn()).toBe(false);
expect(authService.getTicket()).toBeUndefined();
expect(localStorage.getItem('ticket-ECM')).toBeUndefined();
done();
});
});
it('should return false if the user is not logged in', () => {
expect(service.isLoggedIn()).toBe(false);
expect(authService.isLoggedIn()).toBe(false);
});
});
describe('when the setting is BPM', () => {
beforeEach(() => {
this.providers = 'BPM';
service = injector.get(AlfrescoAuthenticationService);
authService = injector.get(AlfrescoAuthenticationService);
authService.providers = 'BPM';
});
it('should return an BPM ticket after the login done', (done) => {
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseBPM);
service.login('fake-username', 'fake-password', this.providers).subscribe(() => {
expect(service.isLoggedIn()).toBe(true);
expect(service.getTicket()).toEqual('fake-post-ticket-BPM');
authService.login('fake-username', 'fake-password').subscribe(() => {
expect(authService.isLoggedIn()).toBe(true);
expect(authService.getTicket()).toEqual('fake-post-ticket-BPM');
done();
});
});
@ -177,12 +178,12 @@ describe('AlfrescoAuthentication', () => {
it('should return ticket undefined when the credentials are wrong', (done) => {
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(Promise.reject('fake invalid credentials'));
service.login('fake-wrong-username', 'fake-wrong-password', this.providers).subscribe(
authService.login('fake-wrong-username', 'fake-wrong-password').subscribe(
(res) => {
},
(err: any) => {
expect(service.isLoggedIn()).toBe(false);
expect(service.getTicket()).toBeUndefined();
expect(authService.isLoggedIn()).toBe(false);
expect(authService.getTicket()).toBeUndefined();
done();
});
});
@ -190,12 +191,12 @@ describe('AlfrescoAuthentication', () => {
it('should return a ticket undefined after logout', (done) => {
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseBPM);
service.login('fake-username', 'fake-password', this.providers).subscribe(() => {
authService.login('fake-username', 'fake-password').subscribe(() => {
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(fakePromiseBPM);
service.logout().subscribe(() => {
expect(service.isLoggedIn()).toBe(false);
expect(service.getTicket()).toBeUndefined();
authService.logout().subscribe(() => {
expect(authService.isLoggedIn()).toBe(false);
expect(authService.getTicket()).toBeUndefined();
expect(localStorage.getItem('ticket-BPM')).toBeUndefined();
done();
});
@ -206,7 +207,7 @@ describe('AlfrescoAuthentication', () => {
localStorage.setItem('ticket-BPM', 'fake-post-ticket-BPM');
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(Promise.reject('fake logout error'));
service.logout().subscribe(
authService.logout().subscribe(
(res) => {
},
(err: any) => {
@ -220,26 +221,16 @@ describe('AlfrescoAuthentication', () => {
describe('when the setting is both ECM and BPM ', () => {
beforeEach(() => {
this.providers = 'ALL';
service = injector.get(AlfrescoAuthenticationService);
});
it('should create both instances', (done) => {
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseBPMECM);
service.login('fake-username', 'fake-password', this.providers).subscribe(() => {
expect(service.isLoggedIn()).toBe(true);
expect(service.TYPE).toEqual(this.providers);
done();
});
authService = injector.get(AlfrescoAuthenticationService);
authService.providers = 'ALL';
});
it('should return both ECM and BPM tickets after the login done', (done) => {
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseBPMECM);
service.login('fake-username', 'fake-password', this.providers).subscribe(() => {
expect(service.isLoggedIn()).toBe(true);
expect(service.getTicket()).toEqual('fake-post-ticket-ECM,fake-post-ticket-BPM');
authService.login('fake-username', 'fake-password').subscribe(() => {
expect(authService.isLoggedIn()).toBe(true);
expect(authService.getTicket()).toEqual('fake-post-ticket-ECM,fake-post-ticket-BPM');
done();
});
});
@ -247,12 +238,12 @@ describe('AlfrescoAuthentication', () => {
it('should return ticket undefined when the credentials are wrong', (done) => {
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(Promise.reject('fake invalid credentials'));
service.login('fake-username', 'fake-password', this.providers).subscribe(
authService.login('fake-username', 'fake-password').subscribe(
(res) => {
},
(err: any) => {
expect(service.isLoggedIn()).toBe(false);
expect(service.getTicket()).toBeUndefined();
expect(authService.isLoggedIn()).toBe(false);
expect(authService.getTicket()).toBeUndefined();
done();
});
});

View File

@ -17,7 +17,7 @@
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Rx';
import {AlfrescoSettingsService} from './AlfrescoSettingsService.service';
import {AlfrescoSettingsService} from './AlfrescoSettings.service';
declare let AlfrescoApi: any;
@ -29,26 +29,11 @@ export class AlfrescoAuthenticationService {
alfrescoApi: any;
TYPE: string = 'ECM';
/**A
* Constructor
* @param alfrescoSetting
*/
constructor(public alfrescoSetting: AlfrescoSettingsService) {
if (!this.isLoggedIn()) {
this.alfrescoApi = new AlfrescoApi({
host: alfrescoSetting.ecmHost,
hostActiviti: alfrescoSetting.bpmHost
});
} else {
this.alfrescoApi = new AlfrescoApi({
ticket: this.getTicket(),
host: alfrescoSetting.ecmHost,
hostActiviti: alfrescoSetting.bpmHost
});
}
}
/**
@ -63,29 +48,43 @@ export class AlfrescoAuthenticationService {
* Method to delegate to POST login
* @param username
* @param password
* @param provider
* @returns {Observable<R>|Observable<T>}
*/
login(username: string, password: string, provider: string) {
login(username: string, password: string) {
this.TYPE = provider || this.TYPE;
return Observable.fromPromise(this.callApiLogin(username, password, provider))
if (this.isLoggedIn()) {
this.alfrescoApi = new AlfrescoApi({
provider: this.alfrescoSetting.getProviders(),
ticket: this.getTicket(),
host: this.alfrescoSetting.ecmHost,
hostActiviti: this.alfrescoSetting.bpmHost
});
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: provider, ticket: response};
return {type: this.alfrescoSetting.getProviders(), ticket: response};
})
.catch(this.handleError);
}
}
/**
* Initialize the alfresco Api with user and password end call the login method
* @param username
* @param password
* @param provider
* @returns {*|Observable<any>}
*/
private callApiLogin(username: string, password: string, provider: string) {
private callApiLogin(username: string, password: string) {
this.alfrescoApi = new AlfrescoApi({
provider: this.alfrescoSetting.getProviders(),
username: username,
password: password,
host: this.alfrescoSetting.ecmHost,
@ -114,7 +113,7 @@ export class AlfrescoAuthenticationService {
* Remove the login ticket from localStorage
*/
public removeTicket(): void {
localStorage.removeItem(`ticket-${this.TYPE}`);
localStorage.removeItem(`ticket-${this.alfrescoSetting.getProviders()}`);
}
/**
@ -122,16 +121,17 @@ export class AlfrescoAuthenticationService {
* @returns {*|Observable<string>|Observable<any>|Promise<T>}
*/
private callApiLogout(): Promise<any> {
if (this.alfrescoApi) {
return this.alfrescoApi.logout();
}
}
/**
* The method return the ticket stored in the localStorage
* @returns ticket
*/
public getTicket(): string {
return localStorage.getItem(`ticket-${this.TYPE}`);
return localStorage.getItem(`ticket-${this.alfrescoSetting.getProviders()}`);
}
/**
@ -140,7 +140,7 @@ export class AlfrescoAuthenticationService {
*/
public saveTicket(ticket): void {
if (ticket) {
localStorage.setItem(`ticket-${this.TYPE}`, ticket);
localStorage.setItem(`ticket-${this.alfrescoSetting.getProviders()}`, ticket);
}
}

View File

@ -17,7 +17,7 @@
import { Injectable } from '@angular/core';
import { AlfrescoAuthenticationService } from './AlfrescoAuthenticationService.service';
import { AlfrescoAuthenticationService } from './AlfrescoAuthentication.service';
@Injectable()
export class AlfrescoContentService {

View File

@ -0,0 +1,63 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {describe, it, beforeEach} from '@angular/core/testing';
import {ReflectiveInjector} from '@angular/core';
import {AlfrescoSettingsService} from './AlfrescoSettings.service';
import {AlfrescoAuthenticationService} from './AlfrescoAuthentication.service';
import {AlfrescoContentService} from './AlfrescoContent.service';
import {HTTP_PROVIDERS} from '@angular/http';
describe('AlfrescoContentService', () => {
let injector, contentService: AlfrescoContentService, authService: AlfrescoAuthenticationService, node;
const nodeId = 'fake-node-id';
beforeEach(() => {
injector = ReflectiveInjector.resolveAndCreate([
HTTP_PROVIDERS,
AlfrescoContentService,
AlfrescoAuthenticationService,
AlfrescoSettingsService
]);
spyOn(localStorage, 'getItem').and.callFake(function (key) {
return 'myTicket';
});
contentService = injector.get(AlfrescoContentService);
authService = injector.get(AlfrescoAuthenticationService);
authService.login('fake-username', 'fake-password');
node = {
entry: {
id: nodeId
}
};
});
it('should return a valid content URL', () => {
expect(contentService.getContentUrl(node)).toBe('http://127.0.0.1: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' +
'/versions/1/nodes/fake-node-id/renditions/doclib/content?attachment=false&alf_ticket=myTicket');
});
});

View File

@ -1,69 +0,0 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { describe, it, beforeEach } from '@angular/core/testing';
import { ReflectiveInjector } from '@angular/core';
import { AlfrescoSettingsService } from './AlfrescoSettingsService.service';
import { AlfrescoAuthenticationService } from './AlfrescoAuthenticationService.service';
import { AlfrescoContentService } from './AlfrescoContentService.service';
import { HTTP_PROVIDERS } from '@angular/http';
describe('AlfrescoContentService', () => {
let injector, service: AlfrescoContentService, authService: AlfrescoAuthenticationService;
const nodeId = 'blah';
let DEFAULT_CONTEXT_PATH: string = '/alfresco';
let DEFAULT_BASE_API_PATH: string = '/api/-default-/public/alfresco/versions/1';
beforeEach(() => {
injector = ReflectiveInjector.resolveAndCreate([
HTTP_PROVIDERS,
AlfrescoContentService,
AlfrescoAuthenticationService,
AlfrescoSettingsService
]);
spyOn(localStorage, 'getItem').and.callFake(function (key) {
return 'myTicket';
});
service = injector.get(AlfrescoContentService);
authService = injector.get(AlfrescoAuthenticationService);
});
it('should return a valid content URL', () => {
expect(service.getContentUrl({
entry: {
id: nodeId
}
})).toBe(
AlfrescoSettingsService.DEFAULT_ECM_ADDRESS + DEFAULT_CONTEXT_PATH +
DEFAULT_BASE_API_PATH + '/nodes/' + nodeId + '/content' +
'?attachment=false&alf_ticket=' + authService.getTicket()
);
});
it('should return a valid thumbnail URL', () => {
expect(service.getDocumentThumbnailUrl({
entry: {
id: nodeId
}
})).toBe(
AlfrescoSettingsService.DEFAULT_ECM_ADDRESS + DEFAULT_CONTEXT_PATH +
DEFAULT_BASE_API_PATH + '/nodes/' + nodeId + '/renditions/doclib/content' +
'?attachment=false&alf_ticket=' + authService.getTicket()
);
});
});

View File

@ -17,7 +17,7 @@
import { Injectable, ChangeDetectorRef, Pipe } from '@angular/core';
import { TranslatePipe } from 'ng2-translate/ng2-translate';
import { AlfrescoTranslationService } from './AlfrescoTranslationService.service';
import { AlfrescoTranslationService } from './AlfrescoTranslation.service';
@Injectable()
@Pipe({

View File

@ -30,7 +30,7 @@ export class AlfrescoSettingsService {
private _bpmContextPath = AlfrescoSettingsService.DEFAULT_BPM_CONTEXT_PATH;
private providers: string[] = 'ALL';
private providers: string = 'ECM'; // ECM, BPM , ALL
public get ecmHost(): string {
return this._ecmHost;
@ -52,4 +52,12 @@ export class AlfrescoSettingsService {
return this._bpmHost + this._bpmContextPath;
}
public getProviders(): string {
return this.providers;
}
public setProviders(providers: string) {
this.providers = providers;
}
}

View File

@ -16,7 +16,7 @@
*/
import { describe, it, beforeEach } from '@angular/core/testing';
import { AlfrescoSettingsService } from './AlfrescoSettingsService.service';
import { AlfrescoSettingsService } from './AlfrescoSettings.service';
describe('AlfrescoSettingsService', () => {

View File

@ -15,9 +15,9 @@
* limitations under the License.
*/
export * from './AlfrescoSettingsService.service';
export * from './AlfrescoSettings.service';
export * from './AlfrescoTranslationLoader.service';
export * from './AlfrescoTranslationService.service';
export * from './AlfrescoTranslation.service';
export * from './AlfrescoPipeTranslate.service';
export * from './AlfrescoAuthenticationService.service';
export * from './AlfrescoContentService.service';
export * from './AlfrescoAuthentication.service';
export * from './AlfrescoContent.service';

View File

@ -189,7 +189,7 @@ class DocumentListDemo implements OnInit {
}
login() {
this.authService.login('admin', 'admin', 'ECM').subscribe(
this.authService.login('admin', 'admin').subscribe(
token => {
console.log(token);
this.token = token;

View File

@ -23,7 +23,7 @@ import {
beforeEach,
beforeEachProviders
} from '@angular/core/testing';
import { AlfrescoAuthenticationService } from 'ng2-alfresco-core';
import { AlfrescoAuthenticationService, AlfrescoSettingsService } from 'ng2-alfresco-core';
import { TestComponentBuilder } from '@angular/compiler/testing';
import { AlfrescoTranslationService } from 'ng2-alfresco-core';
import { AlfrescoLoginComponent } from './alfresco-login.component';
@ -38,6 +38,7 @@ describe('AlfrescoLogin', () => {
beforeEachProviders(() => {
return [
{ provide: AlfrescoAuthenticationService, useClass: AuthenticationMock },
{ provide: AlfrescoSettingsService, useClass: AlfrescoSettingsService },
{ provide: AlfrescoTranslationService, useClass: TranslationMock }
];
});

View File

@ -20,7 +20,8 @@ import { FORM_DIRECTIVES, ControlGroup, FormBuilder, Validators } from '@angular
import {
AlfrescoTranslationService,
AlfrescoPipeTranslate,
AlfrescoAuthenticationService
AlfrescoAuthenticationService,
AlfrescoSettingsService
} from 'ng2-alfresco-core';
declare let componentHandler: any;
@ -67,11 +68,13 @@ export class AlfrescoLoginComponent {
/**
* Constructor
* @param _fb
* @param auth
* @param authService
* @param settingService
* @param translate
*/
constructor(private _fb: FormBuilder,
public auth: AlfrescoAuthenticationService,
public authService: AlfrescoAuthenticationService,
public settingService: AlfrescoSettingsService,
private translate: AlfrescoTranslationService) {
this.formError = {
@ -104,12 +107,15 @@ export class AlfrescoLoginComponent {
* @param value
* @param event
*/
onSubmit(value: any, event) {
onSubmit(value: any, event: any) {
this.error = false;
if (event) {
event.preventDefault();
}
this.auth.login(value.username, value.password, this.providers)
this.settingService.setProviders(this.providers);
this.authService.login(value.username, value.password)
.subscribe(
(token: any) => {
this.success = true;

View File

@ -81,7 +81,7 @@ class SearchDemo implements OnInit {
}
login() {
this.authService.login('admin', 'admin', 'ECM').subscribe(
this.authService.login('admin', 'admin').subscribe(
token => {
console.log(token);
this.token = token;

View File

@ -106,7 +106,7 @@ export class MyDemoApp implements OnInit {
}
login() {
this.authService.login('admin', 'admin', 'ECM').subscribe(
this.authService.login('admin', 'admin').subscribe(
token => {
console.log(token);
this.token = token;

View File

@ -79,7 +79,7 @@ class MyDemoApp {
}
login() {
this.authService.login('admin', 'admin', 'ECM').subscribe(
this.authService.login('admin', 'admin').subscribe(
token => {
console.log(token);
this.token = token;

View File

@ -79,6 +79,8 @@ class WebscriptDemo implements OnInit {
private alfrescoSettingsService: AlfrescoSettingsService) {
alfrescoSettingsService.ecmHost = this.ecmHost;
alfrescoSettingsService.setProviders('ECM');
if (this.authService.getTicket()) {
this.token = this.authService.getTicket();
}
@ -98,7 +100,7 @@ class WebscriptDemo implements OnInit {
}
login() {
this.authService.login('admin', 'admin', ['ECM']).subscribe(
this.authService.login('admin', 'admin').subscribe(
token => {
console.log(token);
this.token = token;