From 90332daa64d6b8a1c059fc566fa28090abbbd45e Mon Sep 17 00:00:00 2001 From: Vito Albano Date: Wed, 12 Oct 2016 17:12:00 +0100 Subject: [PATCH] Improved test readibility and code --- .../ng2-alfresco-userinfo/karma.conf.js | 2 + .../fake-bpm-user.service.ts | 2 +- .../src/assets/fake-ecm-user.service.ts | 107 +++++++++ .../src/components/user-info.component.css | 2 + .../src/components/user-info.component.html | 6 +- .../components/user-info.component.spec.ts | 12 +- .../src/components/user-info.component.ts | 41 ++-- .../src/services/bpm-user.service.spec.ts | 174 ++++++--------- .../src/services/bpm-user.service.ts | 25 +-- .../src/services/ecm-user.service.spec.ts | 205 ++++++------------ .../src/services/ecm-user.service.ts | 6 +- .../src/testing/fake-ecm-user.service.ts | 107 --------- 12 files changed, 289 insertions(+), 400 deletions(-) rename ng2-components/ng2-alfresco-userinfo/src/{testing => assets}/fake-bpm-user.service.ts (97%) create mode 100644 ng2-components/ng2-alfresco-userinfo/src/assets/fake-ecm-user.service.ts delete mode 100644 ng2-components/ng2-alfresco-userinfo/src/testing/fake-ecm-user.service.ts diff --git a/ng2-components/ng2-alfresco-userinfo/karma.conf.js b/ng2-components/ng2-alfresco-userinfo/karma.conf.js index 8f3adc54b4..4dfc103d4d 100644 --- a/ng2-components/ng2-alfresco-userinfo/karma.conf.js +++ b/ng2-components/ng2-alfresco-userinfo/karma.conf.js @@ -49,6 +49,8 @@ module.exports = function (config) { // paths to support debugging with source maps in dev tools {pattern: 'src/**/*.ts', included: false, watched: false}, {pattern: 'dist/**/*.js.map', included: false, watched: false} + // fake file path + { pattern: '/base/dist/src/img/anonymous.gif', included: false, watched: true, served: true } ], exclude: [ diff --git a/ng2-components/ng2-alfresco-userinfo/src/testing/fake-bpm-user.service.ts b/ng2-components/ng2-alfresco-userinfo/src/assets/fake-bpm-user.service.ts similarity index 97% rename from ng2-components/ng2-alfresco-userinfo/src/testing/fake-bpm-user.service.ts rename to ng2-components/ng2-alfresco-userinfo/src/assets/fake-bpm-user.service.ts index c8eaeb2ab1..ffddf85e68 100644 --- a/ng2-components/ng2-alfresco-userinfo/src/testing/fake-bpm-user.service.ts +++ b/ng2-components/ng2-alfresco-userinfo/src/assets/fake-bpm-user.service.ts @@ -83,7 +83,7 @@ export class FakeBpmUserService { }; getCurrentUserProfileImage() { - return this.usersList[this.userNeeded].pictureId; + return Observable.of(this.usersList[this.userNeeded].pictureId); }; respondWithTheUserWithoutImage() { diff --git a/ng2-components/ng2-alfresco-userinfo/src/assets/fake-ecm-user.service.ts b/ng2-components/ng2-alfresco-userinfo/src/assets/fake-ecm-user.service.ts new file mode 100644 index 0000000000..9024896f7b --- /dev/null +++ b/ng2-components/ng2-alfresco-userinfo/src/assets/fake-ecm-user.service.ts @@ -0,0 +1,107 @@ +/*! + * @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. + */ + +// re-export for tester convenience +export { EcmUserModel } from '../models/ecm-user.model'; +export { EcmUserService } from '../services/ecm-user.service'; + +import { EcmUserModel } from '../models/ecm-user.model'; +import { EcmCompanyModel } from '../models/ecm-company.model'; +import { Observable } from 'rxjs/Rx'; + +export var fakeEcmCompany: EcmCompanyModel = { + organization: 'company-fake-name', + address1: 'fake-address-1', + address2: 'fake-address-2', + address3: 'fake-address-3', + postcode: 'fAk1', + telephone: '00000000', + fax: '11111111', + email: 'fakeCompany@fake.com' +}; + +export var fakeEcmUserNoImage: EcmUserModel = { + id: 'fake-id', + firstName: 'fake-first-name', + lastName: 'fake-last-name', + description: 'i am a fake user for test', + avatarId: undefined, + email: 'fakeEcm@ecmUser.com', + skypeId: 'fake-skype-id', + googleId: 'fake-googleId-id', + instantMessageId: 'fake-instantMessageId-id', + company: fakeEcmCompany, + jobTitle: 'test job', + location: 'fake location', + mobile: '000000000', + telephone: '11111111', + statusUpdatedAt: 'fake-date', + userStatus: 'active', + enabled: true, + emailNotificationsEnabled: true +}; + +export var fakeEcmUser: EcmUserModel = { + id: 'fake-id', + firstName: 'fake-first-name', + lastName: 'fake-last-name', + description: 'i am a fake user for test', + avatarId: 'fake-avatar-id', + email: 'fakeEcm@ecmUser.com', + skypeId: 'fake-skype-id', + googleId: 'fake-googleId-id', + instantMessageId: 'fake-instantMessageId-id', + company: fakeEcmCompany, + jobTitle: 'test job', + location: 'fake location', + mobile: '000000000', + telephone: '11111111', + statusUpdatedAt: 'fake-date', + userStatus: 'active', + enabled: true, + emailNotificationsEnabled: true +}; + +export class FakeEcmUserService { + + lastPromise: Observable; + public userNeeded = 0; + usersList = [fakeEcmUser, fakeEcmUserNoImage]; + + getUserInfo(userName: string) { + return this.lastPromise = Observable.of(this.usersList[this.userNeeded]); + }; + + getCurrentUserInfo() { + return this.getUserInfo('fake-id'); + }; + + getUserProfileImage(avatarId: string) { + if (avatarId) { + return 'fake/url/image/for/ecm/user'; + } + }; + + respondWithTheUserWithoutImage() { + this.userNeeded = 1; + }; + + respondWithTheUserWithImage() { + this.userNeeded = 0; + }; + +} diff --git a/ng2-components/ng2-alfresco-userinfo/src/components/user-info.component.css b/ng2-components/ng2-alfresco-userinfo/src/components/user-info.component.css index 93254d00fa..ddda950182 100644 --- a/ng2-components/ng2-alfresco-userinfo/src/components/user-info.component.css +++ b/ng2-components/ng2-alfresco-userinfo/src/components/user-info.component.css @@ -2,6 +2,7 @@ text-align: center; border-radius: 90%; width: 40px; + height: 40px; margin-right: 0%; cursor: pointer; border: 1px solid #999999; @@ -16,6 +17,7 @@ .detail-user-profile-list-mdl{ margin-right: 10px; + padding: 0px 0; } .user-profile-list-mdl{ diff --git a/ng2-components/ng2-alfresco-userinfo/src/components/user-info.component.html b/ng2-components/ng2-alfresco-userinfo/src/components/user-info.component.html index 5a8b50db9c..04b7db4e95 100644 --- a/ng2-components/ng2-alfresco-userinfo/src/components/user-info.component.html +++ b/ng2-components/ng2-alfresco-userinfo/src/components/user-info.component.html @@ -22,7 +22,7 @@ {{ecmUser.firstName}} {{ecmUser.lastName}} @@ -33,8 +33,8 @@ {{ ecmUser.jobTitle ? ecmUser.jobTitle : 'N/A' }} +
-

BPM @@ -42,7 +42,7 @@ diff --git a/ng2-components/ng2-alfresco-userinfo/src/components/user-info.component.spec.ts b/ng2-components/ng2-alfresco-userinfo/src/components/user-info.component.spec.ts index 70ef809084..bb86e401f0 100644 --- a/ng2-components/ng2-alfresco-userinfo/src/components/user-info.component.spec.ts +++ b/ng2-components/ng2-alfresco-userinfo/src/components/user-info.component.spec.ts @@ -18,8 +18,8 @@ import { UserInfoComponent } from './user-info.component'; import { EcmUserService } from '../services/ecm-user.service'; import { BpmUserService } from '../services/bpm-user.service'; -import { FakeEcmUserService } from '../testing/fake-ecm-user.service'; -import { FakeBpmUserService } from '../testing/fake-bpm-user.service'; +import { FakeEcmUserService } from '../assets/fake-ecm-user.service'; +import { FakeBpmUserService } from '../assets/fake-bpm-user.service'; import { AlfrescoAuthenticationService, AlfrescoContentService } from 'ng2-alfresco-core'; import { ComponentFixture, TestBed, async } from '@angular/core/testing'; @@ -107,7 +107,7 @@ describe('User info component', () => { fixture.whenStable() .then( () => { fixture.detectChanges(); - let res = userInfoComp.getEcmUserDetailAvatarUrl(); + let res = userInfoComp.getEcmUserAvatar(); expect(userInfoComp.ecmUserImage).toBeUndefined(); expect(res).toEqual(userInfoComp.anonymouseImageUrl); }); @@ -146,7 +146,7 @@ describe('User info component', () => { fixture.whenStable() .then( () => { fixture.detectChanges(); - let res = userInfoComp.getBpmUserDetailAvatarUrl(); + let res = userInfoComp.getBpmUserAvatar(); expect(userInfoComp.bpmUserImage).toBeUndefined(); expect(res).toEqual(userInfoComp.anonymouseImageUrl); }); @@ -194,10 +194,10 @@ describe('User info component', () => { fixture.whenStable() .then( () => { fixture.detectChanges(); - let resBpm = userInfoComp.getBpmUserDetailAvatarUrl(); + let resBpm = userInfoComp.getBpmUserAvatar(); expect(userInfoComp.bpmUserImage).toBeUndefined(); expect(resBpm).toEqual(userInfoComp.anonymouseImageUrl); - let resEcm = userInfoComp.getEcmUserDetailAvatarUrl(); + let resEcm = userInfoComp.getEcmUserAvatar(); expect(userInfoComp.ecmUserImage).toBeUndefined(); expect(resEcm).toEqual(userInfoComp.anonymouseImageUrl); }); diff --git a/ng2-components/ng2-alfresco-userinfo/src/components/user-info.component.ts b/ng2-components/ng2-alfresco-userinfo/src/components/user-info.component.ts index 4f8ce3aa3c..063dd42517 100644 --- a/ng2-components/ng2-alfresco-userinfo/src/components/user-info.component.ts +++ b/ng2-components/ng2-alfresco-userinfo/src/components/user-info.component.ts @@ -45,43 +45,46 @@ export class UserInfoComponent implements OnInit { } ngOnInit() { - if ( this.authService.isEcmLoggedIn() ) { + + if (this.authService.isEcmLoggedIn()) { this.ecmUserService.getCurrentUserInfo() .subscribe( (res) => { - this.ecmUser = res; - this.getEcmUserProfileImage(); - } - ); + this.ecmUser = res; + this.getEcmAvatar(); + } + ); } - if ( this.authService.isBpmLoggedIn() ) { + + if (this.authService.isBpmLoggedIn()) { this.bpmUserService.getCurrentUserInfo() .subscribe( (res) => { - this.bpmUser = res; - this.getBpmUserProfileImage(); - } - ); + this.bpmUser = res; + } + ); + this.bpmUserService.getCurrentUserProfileImage() + .subscribe( + (res) => { + this.bpmUserImage = res; + } + ); } } - private getBpmUserProfileImage() { - this.bpmUserImage = this.bpmUserService.getCurrentUserProfileImage(); - } - - private getEcmUserProfileImage() { - this.ecmUserImage = this.ecmUserService.getCurrentUserProfileImageUrl(this.ecmUser.avatarId); + private getEcmAvatar() { + this.ecmUserImage = this.ecmUserService.getUserProfileImage(this.ecmUser.avatarId); } public getUserAvatar() { - return this.ecmUserImage || this.bpmUserImage || this.anonymouseImageUrl; + return this.ecmUserImage || this.bpmUserImage || this.anonymouseImageUrl; } - public getBpmUserDetailAvatarUrl() { + public getBpmUserAvatar() { return this.bpmUserImage || this.anonymouseImageUrl; } - public getEcmUserDetailAvatarUrl() { + public getEcmUserAvatar() { return this.ecmUserImage || this.anonymouseImageUrl; } diff --git a/ng2-components/ng2-alfresco-userinfo/src/services/bpm-user.service.spec.ts b/ng2-components/ng2-alfresco-userinfo/src/services/bpm-user.service.spec.ts index 8899f70daf..422ebbba39 100644 --- a/ng2-components/ng2-alfresco-userinfo/src/services/bpm-user.service.spec.ts +++ b/ng2-components/ng2-alfresco-userinfo/src/services/bpm-user.service.spec.ts @@ -15,132 +15,100 @@ * limitations under the License. */ +import { ReflectiveInjector } from '@angular/core'; import { BpmUserService } from '../services/bpm-user.service'; -import { AlfrescoAuthenticationService } from 'ng2-alfresco-core'; -import { TestBed, async, inject } from '@angular/core/testing'; -import { BpmUserModel } from '../models/bpm-user.model'; +import { AlfrescoAuthenticationService, AlfrescoApiService, AlfrescoSettingsService } from 'ng2-alfresco-core'; +import { fakeBpmUser } from '../assets/fake-bpm-user.service'; -export var fakeBpmUser: BpmUserModel = { - apps: {}, - capabilities: 'fake-capability', - company: 'fake-company', - created: 'fake-create-date', - email: 'fakeBpm@fake.com', - externalId: 'fake-external-id', - firstName: 'fake-first-name', - lastName: 'fake-last-name', - fullname: 'fake-full-name', - groups: {}, - id: 'fake-id', - lastUpdate: 'fake-update-date', - latestSyncTimeStamp: 'fake-timestamp', - password: 'fake-password', - pictureId: 'fake-picture-id', - status: 'fake-status', - tenantId: 'fake-tenant-id', - tenantName: 'fake-tenant-name', - tenantPictureId: 'fake-tenant-picture-id', - type: 'fake-type' -}; - -class StubAuthentication { - isEcmConnected: boolean; - isBpmConnected: boolean; - setIsEcmLoggedIn(logged: boolean) { this.isEcmConnected = logged; }; - setIsBpmLoggedIn(logged: boolean) { this.isBpmConnected = logged; }; - isEcmLoggedIn() { return this.isEcmConnected; }; - isBpmLoggedIn() { return this.isBpmConnected; }; - callApiGetPersonInfo() { return Promise.resolve(fakeBpmUser); }; -}; +declare let jasmine: any; describe('Bpm User service', () => { - beforeEach( async(() => { - TestBed.configureTestingModule({ - providers: [ BpmUserService, - { provide: AlfrescoAuthenticationService, useClass: StubAuthentication } - ] - }) - .compileComponents(); - })); + let service, injector, authService; - it('can instantiate service when inject service', - inject([BpmUserService], (service: BpmUserService) => { - expect(service instanceof BpmUserService).toBe(true); - })); + beforeEach(() => { + injector = ReflectiveInjector.resolveAndCreate([ + AlfrescoSettingsService, + AlfrescoApiService, + AlfrescoAuthenticationService, + BpmUserService + ]); + }); - it('can instantiate service with authorization', inject([AlfrescoAuthenticationService], - (auth: AlfrescoAuthenticationService) => { - expect(auth).not.toBeNull('authorization should be provided'); - let service = new BpmUserService(auth); - expect(service instanceof BpmUserService).toBe(true, 'new service should be ok'); - })); + beforeEach(() => { + service = injector.get(BpmUserService); + authService = injector.get(AlfrescoAuthenticationService); + }); + + beforeEach(() => { + jasmine.Ajax.install(); + }); + + afterEach(() => { + jasmine.Ajax.uninstall(); + }); + + it('can instantiate service with authorization', () => { + let serviceTest = new BpmUserService(authService); + + expect(serviceTest instanceof BpmUserService).toBe(true, 'new service should be ok'); + }); describe('when user is logged in', () => { - let service: BpmUserService; - let authServiceForTest: AlfrescoAuthenticationService; - beforeEach( - inject( - [AlfrescoAuthenticationService ], - ( authService: AlfrescoAuthenticationService ) => { - authServiceForTest = authService; - service = new BpmUserService(authService); - spyOn(authServiceForTest, 'isBpmLoggedIn').and.returnValue(true); - })); - - it('should be able to retrieve current user info', (done) => { - spyOn(service, 'callApiGetProfile').and.returnValue(Promise.resolve(fakeBpmUser)); - service.getCurrentUserInfo().subscribe( - (user) => { - expect(user).toBeDefined(); - expect(user.firstName).toEqual('fake-first-name'); - expect(user.lastName).toEqual('fake-last-name'); - expect(user.email).toEqual('fakeBpm@fake.com'); - done(); - }); + beforeEach(() => { + spyOn(authService, 'isBpmLoggedIn').and.returnValue(true); }); - it('should retrieve current logged user information via js api', () => { - spyOn(service, 'callApiGetProfile'); - service.getCurrentUserInfo(); - expect(service.callApiGetProfile).toHaveBeenCalled(); + it('should be able to retrieve current user info', (done) => { + spyOn(service, 'getCurrentUserProfileImage').and.callThrough(); + service.getCurrentUserInfo().subscribe( + (user) => { + expect(user).toBeDefined(); + expect(user.firstName).toEqual('fake-first-name'); + expect(user.lastName).toEqual('fake-last-name'); + expect(user.email).toEqual('fakeBpm@fake.com'); + done(); + }); + + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200, + contentType: 'json', + responseText: fakeBpmUser + }); }); it('should retrieve avatar url for current user', (done) => { - spyOn(service, 'callApiGetProfilePicture').and.returnValue(Promise.resolve('fake/img/path')); - service.getCurrentUserProfileImage().subscribe( - (path) => { - expect(path).toBeDefined(); - expect(path).toEqual('fake/img/path'); - done(); - }); + service.getCurrentUserProfileImage().subscribe( + (path) => { + expect(path).toBeDefined(); + expect(path).toEqual('fake/img/path'); + done(); + }); + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200, + contentType: 'json', + responseText: 'fake/img/path' + }); }); }); - +/* describe('when user is not logged in', () => { - let service: BpmUserService; - let authServiceForTest: AlfrescoAuthenticationService; - beforeEach( - inject( - [AlfrescoAuthenticationService], - (authService: AlfrescoAuthenticationService) => { - authServiceForTest = authService; - service = new BpmUserService(authService); - spyOn(authServiceForTest, 'isBpmLoggedIn').and.returnValue(false); - })); + beforeEach(() => { + spyOn(authService, 'isBpmLoggedIn').and.returnValue(false); + }); it('should not retrieve the user information', () => { - spyOn(service, 'callApiGetProfile'); - service.getCurrentUserInfo(); - expect(service.callApiGetProfile).not.toHaveBeenCalled(); + spyOn(service, 'callApiGetProfile'); + service.getCurrentUserInfo(); + expect(service.callApiGetProfile).not.toHaveBeenCalled(); }); it('should not retrieve the user avatar', () => { - spyOn(service, 'callApiGetProfilePicture'); - service.getCurrentUserInfo(); - expect(service.callApiGetProfilePicture).not.toHaveBeenCalled(); + spyOn(service, 'callApiGetProfilePicture'); + service.getCurrentUserInfo(); + expect(service.callApiGetProfilePicture).not.toHaveBeenCalled(); }); - }); + });*/ }); diff --git a/ng2-components/ng2-alfresco-userinfo/src/services/bpm-user.service.ts b/ng2-components/ng2-alfresco-userinfo/src/services/bpm-user.service.ts index b2cd97fadb..b20b1362e4 100644 --- a/ng2-components/ng2-alfresco-userinfo/src/services/bpm-user.service.ts +++ b/ng2-components/ng2-alfresco-userinfo/src/services/bpm-user.service.ts @@ -38,7 +38,7 @@ export class BpmUserService { */ getCurrentUserInfo(): Observable { if ( this.authService.isBpmLoggedIn() ) { - return Observable.fromPromise(this.callApiGetProfile()) + return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.profileApi.getProfilePicture()) .map( (data) => data ) @@ -46,9 +46,9 @@ export class BpmUserService { } } - getCurrentUserProfileImage(): any { + getCurrentUserProfileImage(): Observable { if ( this.authService.isBpmLoggedIn() ) { - return Observable.fromPromise(this.callApiGetProfilePicture()) + return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.profileApi.getProfile()) .map( (data) => data ) @@ -56,25 +56,6 @@ export class BpmUserService { } } - /** - * Call js api to get current user profile picture - */ - callApiGetProfilePicture() { - try { - return this.authService.getAlfrescoApi().activiti.profileApi.getProfilePicture(); - } catch (exc) { - console.error(exc); - return null; - } - } - - /** - * Call js api to get current user information - */ - callApiGetProfile() { - return this.authService.getAlfrescoApi().activiti.profileApi.getProfile(); - } - /** * Throw the error * @param error diff --git a/ng2-components/ng2-alfresco-userinfo/src/services/ecm-user.service.spec.ts b/ng2-components/ng2-alfresco-userinfo/src/services/ecm-user.service.spec.ts index 4d88d66fe9..1f82d78995 100644 --- a/ng2-components/ng2-alfresco-userinfo/src/services/ecm-user.service.spec.ts +++ b/ng2-components/ng2-alfresco-userinfo/src/services/ecm-user.service.spec.ts @@ -15,165 +15,100 @@ * limitations under the License. */ +import { ReflectiveInjector } from '@angular/core'; +import { + AlfrescoAuthenticationService, + AlfrescoApiService, + AlfrescoSettingsService, + AlfrescoContentService +} from 'ng2-alfresco-core'; import { EcmUserService } from '../services/ecm-user.service'; -import { AlfrescoAuthenticationService, AlfrescoContentService } from 'ng2-alfresco-core'; -import { TestBed, async, inject } from '@angular/core/testing'; -import { EcmUserModel } from '../models/ecm-user.model'; -import { EcmCompanyModel } from '../models/ecm-company.model'; +import { fakeEcmUser } from '../assets/fake-ecm-user.service'; -export var fakeEcmCompany: EcmCompanyModel = { - organization: 'company-fake-name', - address1: 'fake-address-1', - address2: 'fake-address-2', - address3: 'fake-address-3', - postcode: 'fAk1', - telephone: '00000000', - fax: '=1111111', - email: 'fakeCompany@fake.com' -}; - -export var fakeEcmUser: EcmUserModel = { - id: 'fake-id', - firstName: 'fake-first-name', - lastName: 'fake-last-name', - description: 'i am a fake user for test', - avatarId: 'fake-avatar-id', - email: 'fakeEcm@ecmUser.com', - skypeId: 'fake-skype-id', - googleId: 'fake-googleId-id', - instantMessageId: 'fake-instantMessageId-id', - company: fakeEcmCompany, - jobTitle: 'test job', - location: 'fake location', - mobile: '000000000', - telephone: '11111111', - statusUpdatedAt: 'fake-date', - userStatus: 'active', - enabled: true, - emailNotificationsEnabled: true -}; - -class StubAuthentication { - isEcmConnected: boolean; - isBpmConnected: boolean; - setIsEcmLoggedIn(logged: boolean) { this.isEcmConnected = logged; }; - setIsBpmLoggedIn(logged: boolean) { this.isBpmConnected = logged; }; - isEcmLoggedIn() { return this.isEcmConnected; }; - isBpmLoggedIn() { return this.isBpmConnected; }; - callApiGetPersonInfo() { return Promise.resolve(fakeEcmUser); }; -}; - -class StubAlfrescoContentService { - getContentUrl() { return 'fake/url/image/for/ecm/user'; } ; -} +declare let jasmine: any; describe('Ecm User service', () => { - beforeEach( async(() => { - TestBed.configureTestingModule({ - providers: [ EcmUserService, - { provide: AlfrescoAuthenticationService, useClass: StubAuthentication }, - { provide: AlfrescoContentService, useClass: StubAlfrescoContentService } - ] - }) - .compileComponents(); - })); + let service, injector, authService, contentService; - it('can instantiate service when inject service', - inject([EcmUserService], (service: EcmUserService) => { - expect(service instanceof EcmUserService).toBe(true); - })); + beforeEach(() => { + injector = ReflectiveInjector.resolveAndCreate([ + AlfrescoSettingsService, + AlfrescoApiService, + AlfrescoAuthenticationService, + AlfrescoContentService, + EcmUserService + ]); + }); - it('can instantiate service with authorization', inject([AlfrescoAuthenticationService], - (auth: AlfrescoAuthenticationService) => { - expect(auth).not.toBeNull('authorization should be provided'); - let service = new EcmUserService(auth, null); - expect(service instanceof EcmUserService).toBe(true, 'new service should be ok'); - })); + beforeEach(() => { + service = injector.get(EcmUserService); + authService = injector.get(AlfrescoAuthenticationService); + contentService = injector.get(AlfrescoContentService); + }); - it('can instantiate service with content service', inject([AlfrescoContentService], - (content: AlfrescoContentService) => { - expect(content).not.toBeNull('contentService should be provided'); - let service = new EcmUserService(null, content); - expect(service instanceof EcmUserService).toBe(true, 'new service should be ok'); - })); + it('can instantiate service with authorization', () => { + expect(authService).not.toBeNull('authorization should be provided'); + let serviceAuth = new EcmUserService(authService, null); + + expect(serviceAuth instanceof EcmUserService).toBe(true, 'new service should be ok'); + }); + + it('can instantiate service with content service', () => { + expect(contentService).not.toBeNull('contentService should be provided'); + let serviceContent = new EcmUserService(null, contentService); + + expect(serviceContent instanceof EcmUserService).toBe(true, 'new service should be ok'); + }); describe('when user is logged in', () => { - let service: EcmUserService; - let authServiceForTest: AlfrescoAuthenticationService; - let contentServiceForTest: AlfrescoContentService; - beforeEach( - inject( - [AlfrescoAuthenticationService, AlfrescoContentService], - (authService: AlfrescoAuthenticationService, content: AlfrescoContentService) => { - authServiceForTest = authService; - contentServiceForTest = content; - service = new EcmUserService(authService, content); - spyOn(authServiceForTest, 'isEcmLoggedIn').and.returnValue(true); - })); - - it('should be able to retrieve current user info', (done) => { - let userJsApiResponse = {entry: fakeEcmUser}; - spyOn(service, 'callApiGetPersonInfo').and.returnValue(Promise.resolve(userJsApiResponse)); - service.getCurrentUserInfo().subscribe( - (user) => { - expect(user).toBeDefined(); - expect(user.firstName).toEqual('fake-first-name'); - expect(user.lastName).toEqual('fake-last-name'); - expect(user.email).toEqual('fakeEcm@ecmUser.com'); - done(); - }); + beforeEach(() => { + spyOn(authService, 'isEcmLoggedIn').and.returnValue(true); + jasmine.Ajax.install(); }); - it('should retrieve current logged user information', () => { - spyOn(service, 'getUserInfo'); - spyOn(service, 'callApiGetPersonInfo').and.callThrough(); - service.getCurrentUserInfo(); - expect(service.getUserInfo).toHaveBeenCalledWith('-me-'); + afterEach(() => { + jasmine.Ajax.uninstall(); + }); + + it('should be able to retrieve current user info', (done) => { + service.getCurrentUserInfo().subscribe( + (user) => { + expect(user).toBeDefined(); + expect(user.firstName).toEqual('fake-first-name'); + expect(user.lastName).toEqual('fake-last-name'); + expect(user.email).toEqual('fakeEcm@ecmUser.com'); + done(); + }); + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200, + contentType: 'json', + responseText: {entry: fakeEcmUser} + }); }); it('should retrieve avatar url for current user', () => { - spyOn(contentServiceForTest, 'getContentUrl').and.returnValue('fake/url/image/for/ecm/user'); - let urlRs = service.getCurrentUserProfileImageUrl('fake-avatar-id'); + spyOn(contentService, 'getContentUrl').and.returnValue('fake/url/image/for/ecm/user'); + let urlRs = service.getUserProfileImage('fake-avatar-id'); - expect(urlRs).toEqual('fake/url/image/for/ecm/user'); + expect(urlRs).toEqual('fake/url/image/for/ecm/user'); }); it('should not call content service without avatar id', () => { - spyOn(contentServiceForTest, 'getContentUrl').and.callThrough(); - let urlRs = service.getCurrentUserProfileImageUrl(undefined); - expect(urlRs).toBeUndefined(); - expect(contentServiceForTest.getContentUrl).not.toHaveBeenCalled(); + spyOn(contentService, 'getContentUrl').and.callThrough(); + let urlRs = service.getUserProfileImage(undefined); + + expect(urlRs).toBeUndefined(); + expect(contentService.getContentUrl).not.toHaveBeenCalled(); }); it('should build the body for the content service', () => { - spyOn(contentServiceForTest, 'getContentUrl').and.callThrough(); - let urlRs = service.getCurrentUserProfileImageUrl('fake-avatar-id'); - expect(urlRs).toBeDefined(); - expect(contentServiceForTest.getContentUrl).toHaveBeenCalledWith( {entry: {id: 'fake-avatar-id'} }); - }); - }); + spyOn(contentService, 'getContentUrl').and.callThrough(); + let urlRs = service.getUserProfileImage('fake-avatar-id'); - describe('when user is not logged in', () => { - let service: EcmUserService; - let authServiceForTest: AlfrescoAuthenticationService; - let contentServiceForTest: AlfrescoContentService; - - beforeEach( - inject( - [AlfrescoAuthenticationService, AlfrescoContentService], - (authService: AlfrescoAuthenticationService, content: AlfrescoContentService) => { - authServiceForTest = authService; - contentServiceForTest = content; - service = new EcmUserService(authService, content); - spyOn(authServiceForTest, 'isEcmLoggedIn').and.returnValue(false); - })); - - it('should not retrieve the user information', () => { - spyOn(service, 'callApiGetPersonInfo'); - service.getCurrentUserInfo(); - expect(service.callApiGetPersonInfo).not.toHaveBeenCalled(); + expect(urlRs).toBeDefined(); + expect(contentService.getContentUrl).toHaveBeenCalledWith({entry: {id: 'fake-avatar-id'}}); }); }); }); diff --git a/ng2-components/ng2-alfresco-userinfo/src/services/ecm-user.service.ts b/ng2-components/ng2-alfresco-userinfo/src/services/ecm-user.service.ts index 1175af0f1a..7ff3643ef5 100644 --- a/ng2-components/ng2-alfresco-userinfo/src/services/ecm-user.service.ts +++ b/ng2-components/ng2-alfresco-userinfo/src/services/ecm-user.service.ts @@ -37,24 +37,22 @@ export class EcmUserService { * @param userName - the user name */ getUserInfo(userName: string): Observable { - if ( this.authService.isEcmLoggedIn() ) { return Observable.fromPromise(this.callApiGetPersonInfo(userName)) .map( (data) => data['entry'] ) .catch(this.handleError); - } } getCurrentUserInfo() { return this.getUserInfo('-me-'); } - callApiGetPersonInfo(userName: string, opts?: any) { + private callApiGetPersonInfo(userName: string, opts?: any) { return this.authService.getAlfrescoApi().core.peopleApi.getPerson(userName, opts); } - getCurrentUserProfileImageUrl(avatarId: string) { + getUserProfileImage(avatarId: string) { if ( avatarId ) { let nodeObj = {entry: {id: avatarId}}; return this.contentService.getContentUrl(nodeObj); diff --git a/ng2-components/ng2-alfresco-userinfo/src/testing/fake-ecm-user.service.ts b/ng2-components/ng2-alfresco-userinfo/src/testing/fake-ecm-user.service.ts deleted file mode 100644 index 2133e780b1..0000000000 --- a/ng2-components/ng2-alfresco-userinfo/src/testing/fake-ecm-user.service.ts +++ /dev/null @@ -1,107 +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. - */ - -// re-export for tester convenience -export { EcmUserModel } from '../models/ecm-user.model'; -export { EcmUserService } from '../services/ecm-user.service'; - -import { EcmUserModel } from '../models/ecm-user.model'; -import { EcmCompanyModel } from '../models/ecm-company.model'; -import { Observable } from 'rxjs/Rx'; - -export var fakeEcmCompany: EcmCompanyModel = { - organization: 'company-fake-name', - address1: 'fake-address-1', - address2: 'fake-address-2', - address3: 'fake-address-3', - postcode: 'fAk1', - telephone: '00000000', - fax: '11111111', - email: 'fakeCompany@fake.com' -}; - -export var fakeEcmUserNoImage: EcmUserModel = { - id: 'fake-id', - firstName: 'fake-first-name', - lastName: 'fake-last-name', - description: 'i am a fake user for test', - avatarId: undefined, - email: 'fakeEcm@ecmUser.com', - skypeId: 'fake-skype-id', - googleId: 'fake-googleId-id', - instantMessageId: 'fake-instantMessageId-id', - company: fakeEcmCompany, - jobTitle: 'test job', - location: 'fake location', - mobile: '000000000', - telephone: '11111111', - statusUpdatedAt: 'fake-date', - userStatus: 'active', - enabled: true, - emailNotificationsEnabled: true -}; - -export var fakeEcmUser: EcmUserModel = { - id: 'fake-id', - firstName: 'fake-first-name', - lastName: 'fake-last-name', - description: 'i am a fake user for test', - avatarId: 'fake-avatar-id', - email: 'fakeEcm@ecmUser.com', - skypeId: 'fake-skype-id', - googleId: 'fake-googleId-id', - instantMessageId: 'fake-instantMessageId-id', - company: fakeEcmCompany, - jobTitle: 'test job', - location: 'fake location', - mobile: '000000000', - telephone: '11111111', - statusUpdatedAt: 'fake-date', - userStatus: 'active', - enabled: true, - emailNotificationsEnabled: true -}; - -export class FakeEcmUserService { - - lastPromise: Observable; - public userNeeded = 0; - usersList = [fakeEcmUser, fakeEcmUserNoImage]; - - getUserInfo(userName: string) { - return this.lastPromise = Observable.of(this.usersList[this.userNeeded]); - }; - - getCurrentUserInfo() { - return this.getUserInfo('fake-id'); - }; - - getCurrentUserProfileImageUrl(avatarId: string) { - if ( avatarId ) { - return 'fake/url/image/for/ecm/user'; - } - }; - - respondWithTheUserWithoutImage() { - this.userNeeded = 1; - }; - - respondWithTheUserWithImage() { - this.userNeeded = 0; - }; - -}