Added tests for user-info component

This commit is contained in:
Vito Albano
2016-09-30 00:54:23 +01:00
parent d946532f14
commit 336ffe75d8
8 changed files with 452 additions and 31 deletions

View File

@@ -74,6 +74,7 @@ describe('AlfrescoAuthentication', () => {
authService.login('fake-username', 'fake-password').subscribe(() => {
expect(authService.isLoggedIn()).toBe(true);
expect(authService.getTicketEcm()).toEqual('fake-post-ticket');
expect(authService.isEcmLoggedIn()).toBe(true);
done();
});
@@ -106,6 +107,7 @@ describe('AlfrescoAuthentication', () => {
(err: any) => {
expect(authService.isLoggedIn()).toBe(false);
expect(authService.getTicketEcm()).toBe(null);
expect(authService.isEcmLoggedIn()).toBe(false);
done();
});
@@ -141,6 +143,7 @@ describe('AlfrescoAuthentication', () => {
authService.logout().subscribe(() => {
expect(authService.isLoggedIn()).toBe(false);
expect(authService.getTicketEcm()).toBe(null);
expect(authService.isEcmLoggedIn()).toBe(false);
done();
});
@@ -158,6 +161,7 @@ describe('AlfrescoAuthentication', () => {
it('should return false if the user is not logged in', () => {
expect(authService.isLoggedIn()).toBe(false);
expect(authService.isEcmLoggedIn()).toBe(false);
});
});
@@ -171,6 +175,7 @@ describe('AlfrescoAuthentication', () => {
authService.login('fake-username', 'fake-password').subscribe(() => {
expect(authService.isLoggedIn()).toBe(true);
expect(authService.getTicketBpm()).toEqual('Basic ZmFrZS11c2VybmFtZTpmYWtlLXBhc3N3b3Jk');
expect(authService.isBpmLoggedIn()).toBe(true);
done();
});
@@ -201,6 +206,7 @@ describe('AlfrescoAuthentication', () => {
(err: any) => {
expect(authService.isLoggedIn()).toBe(false);
expect(authService.getTicketBpm()).toBe(null);
expect(authService.isBpmLoggedIn()).toBe(false);
done();
});
@@ -214,6 +220,7 @@ describe('AlfrescoAuthentication', () => {
authService.logout().subscribe(() => {
expect(authService.isLoggedIn()).toBe(false);
expect(authService.getTicketBpm()).toBe(null);
expect(authService.isBpmLoggedIn()).toBe(false);
done();
});
@@ -280,6 +287,8 @@ describe('AlfrescoAuthentication', () => {
expect(authService.isLoggedIn()).toBe(true);
expect(authService.getTicketEcm()).toEqual('fake-post-ticket');
expect(authService.getTicketBpm()).toEqual('Basic ZmFrZS11c2VybmFtZTpmYWtlLXBhc3N3b3Jk');
expect(authService.isBpmLoggedIn()).toBe(true);
expect(authService.isEcmLoggedIn()).toBe(true);
done();
});
@@ -302,6 +311,7 @@ describe('AlfrescoAuthentication', () => {
expect(authService.isLoggedIn()).toBe(false);
expect(authService.getTicketEcm()).toBe(null);
expect(authService.getTicketBpm()).toBe(null);
expect(authService.isEcmLoggedIn()).toBe(false);
done();
});
@@ -322,6 +332,7 @@ describe('AlfrescoAuthentication', () => {
expect(authService.isLoggedIn()).toBe(false);
expect(authService.getTicketEcm()).toBe(null);
expect(authService.getTicketBpm()).toBe(null);
expect(authService.isBpmLoggedIn()).toBe(false);
done();
});
@@ -344,6 +355,8 @@ describe('AlfrescoAuthentication', () => {
expect(authService.isLoggedIn()).toBe(false);
expect(authService.getTicketEcm()).toBe(null);
expect(authService.getTicketBpm()).toBe(null);
expect(authService.isBpmLoggedIn()).toBe(false);
expect(authService.isEcmLoggedIn()).toBe(false);
done();
});

View File

@@ -82,6 +82,7 @@ export class AlfrescoAuthenticationService {
* @returns {Observable<R>|Observable<T>}
*/
login(username: string, password: string): Observable<{ type: string, ticket: any }> {
this.removeTicket();
return Observable.fromPromise(this.callApiLogin(username, password))
.map((response: any) => {
this.saveTickets();
@@ -191,6 +192,20 @@ export class AlfrescoAuthenticationService {
}
}
/**
* The method return true if user is logged in on ecm provider
*/
public isEcmLoggedIn() {
return this.alfrescoApi.ecmAuth && !!this.alfrescoApi.ecmAuth.isLoggedIn();
}
/**
* The method return true if user is logged in on bpm provider
*/
public isBpmLoggedIn() {
return this.alfrescoApi.bpmAuth && !!this.alfrescoApi.bpmAuth.isLoggedIn();
}
/**
* The method write the error in the console browser
* @param error

View File

@@ -18,33 +18,233 @@
import { UserInfoComponent } from './user-info.component';
import { EcmUserService } from '../services/ecm-user.service';
import { BpmUserService } from '../services/bpm-user.service';
import { AlfrescoAuthenticationService,
AlfrescoApiService,
AlfrescoSettingsService } from 'ng2-alfresco-core';
import { FakeEcmUserService } from '../testing/fake-ecm-user.service';
import { FakeBpmUserService } from '../testing/fake-bpm-user.service';
import { AlfrescoAuthenticationService, AlfrescoContentService } from 'ng2-alfresco-core';
import { ComponentFixture, TestBed, async } from '@angular/core/testing';
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; };
}
class StubAlfrescoContentService {
getContentUrl() { return 'fake/url/image/for/ecm/user'; } ;
}
describe('User info component', () => {
let userInfoComp: UserInfoComponent;
let ecmUserService = new EcmUserService(null, null);
let bpmUserService = new BpmUserService(null);
let authService = new AlfrescoAuthenticationService(new AlfrescoSettingsService() ,
new AlfrescoApiService());
let fixture: ComponentFixture<UserInfoComponent>;
let authStub: StubAuthentication;
let fakeEcmService: FakeEcmUserService;
let fakeBpmService: FakeBpmUserService;
beforeEach(() => {
userInfoComp = new UserInfoComponent(ecmUserService, bpmUserService, authService);
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ UserInfoComponent ],
providers: [{ provide: EcmUserService, useClass: FakeEcmUserService},
{ provide: BpmUserService, useClass: FakeBpmUserService},
{ provide: AlfrescoAuthenticationService, useClass: StubAuthentication },
{ provide: AlfrescoContentService, useClass: StubAlfrescoContentService }
]
}).compileComponents().then(() => {
fixture = TestBed.createComponent(UserInfoComponent);
userInfoComp = fixture.componentInstance;
});
}));
it('should NOT have users before ngOnInit only anonymous image', () => {
expect(userInfoComp.ecmUser).toBeUndefined();
expect(userInfoComp.ecmUserImage).toBeUndefined();
expect(userInfoComp.bpmUser).toBeUndefined();
expect(userInfoComp.bpmUserImage).toBeUndefined();
expect(userInfoComp.anonymouseImageUrl).not.toBeUndefined();
});
it('should get the ecm user informations when is logged in', () => {
spyOn(ecmUserService, 'getUserInfo');
spyOn(bpmUserService, 'getCurrentUserInfo');
spyOn(authService, 'getAlfrescoApi').and.callThrough();
// spyOn(authService.getAlfrescoApi(), 'ecmAuth').and.callThrough();
spyOn(authService, 'getAlfrescoApi().ecmAuth.isLoggedIn').and.returnValue(true);
userInfoComp.ngOnInit();
expect(ecmUserService.getUserInfo).toHaveBeenCalledWith('-me-');
expect(bpmUserService.getCurrentUserInfo).not.toHaveBeenCalled();
it('should NOT have users immediately after ngOnInit', () => {
fixture.detectChanges();
expect(userInfoComp.ecmUser).toBeUndefined();
expect(userInfoComp.ecmUserImage).toBeUndefined();
expect(userInfoComp.bpmUser).toBeUndefined();
expect(userInfoComp.bpmUserImage).toBeUndefined();
expect(userInfoComp.anonymouseImageUrl).not.toBeUndefined();
});
describe('when user is logged on ecm', () => {
beforeEach( async(() => {
authStub = fixture.debugElement.injector.get(AlfrescoAuthenticationService);
fakeEcmService = fixture.debugElement.injector.get(EcmUserService);
authStub.setIsEcmLoggedIn(true);
fixture.detectChanges(); // runs ngOnInit -> getUsers
fixture.whenStable()
.then( () => {
fixture.detectChanges();
} );
}));
it('should get the ecm current user image from the service', () => {
expect(userInfoComp.ecmUser).not.toBeUndefined();
expect(userInfoComp.ecmUserImage).not.toBeUndefined();
expect(userInfoComp.ecmUserImage).toEqual('fake/url/image/for/ecm/user');
});
it('should get the ecm user informations from the service', () => {
expect(userInfoComp.ecmUser).not.toBeUndefined();
expect(userInfoComp.ecmUser.firstName).toEqual('fake-first-name');
expect(userInfoComp.ecmUser.lastName).toEqual('fake-last-name');
});
it('should return the anonynous user avatar image url when user does not have avatarId', async(() => {
fakeEcmService.userNeeded = 1;
userInfoComp.ngOnInit();
fixture.whenStable()
.then( () => {
fixture.detectChanges();
let res = userInfoComp.getEcmUserDetailAvatarUrl();
expect(userInfoComp.ecmUserImage).toBeUndefined();
expect(res).toEqual(userInfoComp.anonymouseImageUrl);
});
}));
});
describe('when user is logged on bpm', () => {
beforeEach( async(() => {
authStub = fixture.debugElement.injector.get(AlfrescoAuthenticationService);
fakeBpmService = fixture.debugElement.injector.get(BpmUserService);
authStub.setIsBpmLoggedIn(true);
fixture.detectChanges(); // runs ngOnInit -> getUsers
fixture.whenStable()
.then( () => {
fixture.detectChanges();
} );
}));
it('should get the bpm current user image from the service', () => {
expect(userInfoComp.bpmUser).not.toBeUndefined();
expect(userInfoComp.bpmUserImage).not.toBeUndefined();
expect(userInfoComp.bpmUserImage).toEqual('fake-picture-id');
});
it('should get the bpm user informations from the service', () => {
expect(userInfoComp.bpmUser).not.toBeUndefined();
expect(userInfoComp.bpmUser.firstName).toEqual('fake-first-name');
expect(userInfoComp.bpmUser.lastName).toEqual('fake-last-name');
});
it('should return the anonynous user avatar image url when user does not have avatarId', async(() => {
fakeBpmService.userNeeded = 1;
userInfoComp.ngOnInit();
fixture.whenStable()
.then( () => {
fixture.detectChanges();
let res = userInfoComp.getBpmUserDetailAvatarUrl();
expect(userInfoComp.bpmUserImage).toBeUndefined();
expect(res).toEqual(userInfoComp.anonymouseImageUrl);
});
}));
});
describe('when user is logged on bpm and ecm', () => {
beforeEach( async(() => {
authStub = fixture.debugElement.injector.get(AlfrescoAuthenticationService);
fakeBpmService = fixture.debugElement.injector.get(BpmUserService);
fakeEcmService = fixture.debugElement.injector.get(EcmUserService);
authStub.setIsBpmLoggedIn(true);
authStub.setIsEcmLoggedIn(true);
fixture.detectChanges(); // runs ngOnInit -> getUsers
fixture.whenStable()
.then( () => {
fixture.detectChanges();
} );
}));
it('should get the bpm current user image from the service', () => {
expect(userInfoComp.bpmUser).not.toBeUndefined();
expect(userInfoComp.bpmUserImage).not.toBeUndefined();
expect(userInfoComp.bpmUserImage).toEqual('fake-picture-id');
expect(userInfoComp.ecmUser).not.toBeUndefined();
expect(userInfoComp.ecmUserImage).not.toBeUndefined();
expect(userInfoComp.ecmUserImage).toEqual('fake/url/image/for/ecm/user');
});
it('should get the bpm user informations from the service', () => {
expect(userInfoComp.bpmUser).not.toBeUndefined();
expect(userInfoComp.bpmUser.firstName).toEqual('fake-first-name');
expect(userInfoComp.bpmUser.lastName).toEqual('fake-last-name');
expect(userInfoComp.ecmUser).not.toBeUndefined();
expect(userInfoComp.ecmUser.firstName).toEqual('fake-first-name');
expect(userInfoComp.ecmUser.lastName).toEqual('fake-last-name');
});
it('should return the anonynous user avatar image url when user does not have avatarId', async(() => {
fakeBpmService.userNeeded = 1;
fakeEcmService.userNeeded = 1;
userInfoComp.ngOnInit();
fixture.whenStable()
.then( () => {
fixture.detectChanges();
let resBpm = userInfoComp.getBpmUserDetailAvatarUrl();
expect(userInfoComp.bpmUserImage).toBeUndefined();
expect(resBpm).toEqual(userInfoComp.anonymouseImageUrl);
let resEcm = userInfoComp.getEcmUserDetailAvatarUrl();
expect(userInfoComp.ecmUserImage).toBeUndefined();
expect(resEcm).toEqual(userInfoComp.anonymouseImageUrl);
});
}));
it('should return the ecm image if exists', async(() => {
fakeBpmService.userNeeded = 0;
fakeEcmService.userNeeded = 0;
userInfoComp.ngOnInit();
fixture.whenStable()
.then( () => {
fixture.detectChanges();
let res = userInfoComp.getUserAvatar();
expect(userInfoComp.bpmUserImage).not.toBeUndefined();
expect(userInfoComp.ecmUserImage).not.toBeUndefined();
expect(res).toEqual(userInfoComp.ecmUserImage);
});
}));
it('should return the bpm image if ecm does not have it', async(() => {
fakeBpmService.userNeeded = 0;
fakeEcmService.userNeeded = 1;
userInfoComp.ngOnInit();
fixture.whenStable()
.then( () => {
fixture.detectChanges();
let res = userInfoComp.getUserAvatar();
expect(userInfoComp.bpmUserImage).not.toBeUndefined();
expect(userInfoComp.ecmUserImage).toBeUndefined();
expect(res).toEqual(userInfoComp.bpmUserImage);
});
}));
it('should return the anonynous avatar if no user has it', async(() => {
fakeBpmService.userNeeded = 1;
fakeEcmService.userNeeded = 1;
userInfoComp.ngOnInit();
fixture.whenStable()
.then( () => {
fixture.detectChanges();
let res = userInfoComp.getUserAvatar();
expect(userInfoComp.bpmUserImage).toBeUndefined();
expect(userInfoComp.ecmUserImage).toBeUndefined();
expect(res).toEqual(userInfoComp.anonymouseImageUrl);
});
}));
});
});

View File

@@ -33,12 +33,13 @@ declare let __moduleName: string;
export class UserInfoComponent implements OnInit {
private ecmUser: EcmUserModel;
private bpmUser: BpmUserModel;
private baseComponentPath = __moduleName.replace('components/user-info.component.js', '');
private anonymouseImageUrl: string = this.baseComponentPath + 'img/anonymous.gif';
public bpmUserImage: any;
public ecmUserImage: any;
ecmUser: EcmUserModel;
bpmUser: BpmUserModel;
anonymouseImageUrl: string = this.baseComponentPath + 'img/anonymous.gif';
bpmUserImage: any;
ecmUserImage: any;
constructor(private ecmUserService: EcmUserService,
private bpmUserService: BpmUserService,
@@ -46,8 +47,8 @@ export class UserInfoComponent implements OnInit {
}
ngOnInit() {
if (this.authService.getAlfrescoApi().ecmAuth.isLoggedIn()) {
this.ecmUserService.getUserInfo('-me-')
if ( this.authService.isEcmLoggedIn() ) {
this.ecmUserService.getCurrentUserInfo()
.subscribe(
(res) => {
this.ecmUser = <EcmUserModel> res;
@@ -55,7 +56,7 @@ export class UserInfoComponent implements OnInit {
}
);
}
if (this.authService.getAlfrescoApi().bpmAuth.isLoggedIn()) {
if ( this.authService.isBpmLoggedIn() ) {
this.bpmUserService.getCurrentUserInfo()
.subscribe(
(res) => {

View File

@@ -37,7 +37,7 @@ export class BpmUserService {
* @param userName - the user name
*/
getCurrentUserInfo(): Observable<BpmUserModel> {
if ( this.authService.getAlfrescoApi().bpmAuth.isLoggedIn() ) {
if ( this.authService.isBpmLoggedIn() ) {
return Observable.fromPromise(this.callApiGetProfile())
.map(
(data) => <BpmUserModel> data
@@ -47,7 +47,7 @@ export class BpmUserService {
}
getCurrentUserProfileImage(): any {
if ( this.authService.getAlfrescoApi().bpmAuth.isLoggedIn() ) {
if ( this.authService.isBpmLoggedIn() ) {
return Observable.fromPromise(this.callApiGetProfilePicture())
.map(
(data) => data

View File

@@ -37,7 +37,7 @@ export class EcmUserService {
* @param userName - the user name
*/
getUserInfo(userName: string): Observable<EcmUserModel> {
if ( this.authService.getAlfrescoApi().ecmAuth.isLoggedIn() ) {
if ( this.authService.isEcmLoggedIn() ) {
return Observable.fromPromise(this.callApiGetPersonInfo(userName))
.map(
(data) => <EcmUserModel> data['entry']
@@ -46,6 +46,10 @@ export class EcmUserService {
}
}
getCurrentUserInfo() {
return this.getUserInfo('-me-');
}
private callApiGetPersonInfo(userName: string, opts?: any) {
return this.authService.getAlfrescoApi().core.peopleApi.getPerson(userName, opts);
}

View File

@@ -0,0 +1,89 @@
/*!
* @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 { BpmUserModel } from '../models/bpm-user.model';
export { BpmUserService } from '../services/bpm-user.service';
import { BpmUserModel } from '../models/bpm-user.model';
import { Observable } from 'rxjs/Rx';
export var fakeBpmUserNoImage: 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: undefined,
status: 'fake-status',
tenantId: 'fake-tenant-id',
tenantName: 'fake-tenant-name',
tenantPictureId: 'fake-tenant-picture-id',
type: 'fake-type'
};
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'
};
export class FakeBpmUserService {
lastPromise: Observable<BpmUserModel>;
public userNeeded = 0;
usersList = [fakeBpmUser, fakeBpmUserNoImage];
getUserInfo(userName: string) {
return this.lastPromise = Observable.of(this.usersList[this.userNeeded]);
};
getCurrentUserInfo() {
return this.getUserInfo('fake-id');
};
getCurrentUserProfileImage() {
return this.usersList[this.userNeeded].pictureId;
};
}

View File

@@ -0,0 +1,99 @@
/*!
* @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<EcmUserModel>;
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';
}
};
}