mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-05-19 17:14:57 +00:00
Added unit tests for services
This commit is contained in:
parent
336ffe75d8
commit
0b7f3cc9a6
@ -14,35 +14,133 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
// import { UserInfoComponent } from '../src/userinfo.component';
|
|
||||||
|
|
||||||
/*
|
import { BpmUserService } from '../services/bpm-user.service';
|
||||||
describe('Bpm User Service', () => {
|
import { AlfrescoAuthenticationService } from 'ng2-alfresco-core';
|
||||||
|
import { TestBed, async, inject } from '@angular/core/testing';
|
||||||
|
import { BpmUserModel } from '../models/bpm-user.model';
|
||||||
|
|
||||||
beforeEachProviders(() => {
|
export var fakeBpmUser: BpmUserModel = {
|
||||||
return [
|
apps: {},
|
||||||
HTTP_PROVIDERS,
|
capabilities: 'fake-capability',
|
||||||
AlfrescoSettingsService,
|
company: 'fake-company',
|
||||||
AlfrescoAuthenticationService,
|
created: 'fake-create-date',
|
||||||
WidgetVisibilityService
|
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'
|
||||||
|
};
|
||||||
|
|
||||||
beforeEach(
|
class StubAuthentication {
|
||||||
inject([WidgetVisibilityService], (activitiService: WidgetVisibilityService) => {
|
isEcmConnected: boolean;
|
||||||
jasmine.Ajax.install();
|
isBpmConnected: boolean;
|
||||||
service = activitiService;
|
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); };
|
||||||
|
};
|
||||||
|
|
||||||
afterEach(() => {
|
describe('Bpm User service', () => {
|
||||||
jasmine.Ajax.uninstall();
|
|
||||||
});
|
|
||||||
|
|
||||||
|
beforeEach( async(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
providers: [ BpmUserService,
|
||||||
|
{ provide: AlfrescoAuthenticationService, useClass: StubAuthentication }
|
||||||
|
]
|
||||||
|
})
|
||||||
|
.compileComponents();
|
||||||
|
}));
|
||||||
|
|
||||||
it('should be able to get current user information', () => {
|
it('can instantiate service when inject service',
|
||||||
|
inject([BpmUserService], (service: BpmUserService) => {
|
||||||
|
expect(service instanceof BpmUserService).toBe(true);
|
||||||
|
}));
|
||||||
|
|
||||||
});
|
it('can instantiate service with authorization', inject([AlfrescoAuthenticationService],
|
||||||
|
(auth: AlfrescoAuthenticationService) => {
|
||||||
|
expect(auth).not.toBeNull('authorization should be provided');
|
||||||
|
let service = new BpmUserService(auth, null);
|
||||||
|
expect(service 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).not.toBeUndefined();
|
||||||
|
expect(user.firstName).toEqual('fake-first-name');
|
||||||
|
expect(user.lastName).toEqual('fake-last-name');
|
||||||
|
expect(user.email).toEqual('fakeBpm@fake.com');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should retrieve current logged user information via js api', () => {
|
||||||
|
spyOn(service, 'callApiGetProfile');
|
||||||
|
service.getCurrentUserInfo();
|
||||||
|
expect(service.callApiGetProfile).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
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).not.toBeUndefined();
|
||||||
|
expect(path).toEqual('fake/img/path');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('should not retrieve the user information', () => {
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
*/
|
|
||||||
|
@ -59,14 +59,14 @@ export class BpmUserService {
|
|||||||
/**
|
/**
|
||||||
* Call js api to get current user profile picture
|
* Call js api to get current user profile picture
|
||||||
*/
|
*/
|
||||||
private callApiGetProfilePicture() {
|
callApiGetProfilePicture() {
|
||||||
return this.authService.getAlfrescoApi().activiti.profileApi.getProfile();
|
return this.authService.getAlfrescoApi().activiti.profileApi.getProfile();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Call js api to get current user information
|
* Call js api to get current user information
|
||||||
*/
|
*/
|
||||||
private callApiGetProfile() {
|
callApiGetProfile() {
|
||||||
return this.authService.getAlfrescoApi().activiti.profileApi.getProfile();
|
return this.authService.getAlfrescoApi().activiti.profileApi.getProfile();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,56 +14,166 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
/*
|
|
||||||
// import { AlfrescoAuthenticationService, AlfrescoContentService } from 'ng2-alfresco-core';
|
|
||||||
import { EcmUserService } from './ecm-user.service';
|
|
||||||
import { ReflectiveInjector } from '@angular/core';
|
|
||||||
import { EcmUserModel } from '../models/ecm-user.model';
|
|
||||||
|
|
||||||
declare var AlfrescoApi: any;
|
import { EcmUserService } from '../services/ecm-user.service';
|
||||||
declare let jasmine: any;
|
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';
|
||||||
|
|
||||||
|
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 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'; } ;
|
||||||
|
}
|
||||||
|
|
||||||
describe('Ecm User service', () => {
|
describe('Ecm User service', () => {
|
||||||
|
|
||||||
let injector;
|
beforeEach( async(() => {
|
||||||
let ecmUserService: EcmUserService;
|
TestBed.configureTestingModule({
|
||||||
// let contentService: AlfrescoContentService;
|
providers: [ EcmUserService,
|
||||||
// let authService: AlfrescoAuthenticationService;
|
{ provide: AlfrescoAuthenticationService, useClass: StubAuthentication },
|
||||||
|
{ provide: AlfrescoContentService, useClass: StubAlfrescoContentService }
|
||||||
|
]
|
||||||
|
})
|
||||||
|
.compileComponents();
|
||||||
|
}));
|
||||||
|
|
||||||
beforeEach(() => {
|
it('can instantiate service when inject service',
|
||||||
|
inject([EcmUserService], (service: EcmUserService) => {
|
||||||
|
expect(service instanceof EcmUserService).toBe(true);
|
||||||
|
}));
|
||||||
|
|
||||||
injector = ReflectiveInjector.resolveAndCreate([
|
it('can instantiate service with authorization', inject([AlfrescoAuthenticationService],
|
||||||
EcmUserService
|
(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');
|
||||||
|
}));
|
||||||
|
|
||||||
// contentService = injector.get(AlfrescoContentService);
|
it('can instantiate service with content service', inject([AlfrescoContentService],
|
||||||
// authService = injector.get(AlfrescoAuthenticationService);
|
(content: AlfrescoContentService) => {
|
||||||
ecmUserService = injector.get(EcmUserService);
|
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');
|
||||||
|
}));
|
||||||
|
|
||||||
jasmine.Ajax.install();
|
describe('when user is logged in', () => {
|
||||||
});
|
let service: EcmUserService;
|
||||||
|
let authServiceForTest: AlfrescoAuthenticationService;
|
||||||
|
let contentServiceForTest: AlfrescoContentService;
|
||||||
|
|
||||||
afterEach(() => {
|
beforeEach(
|
||||||
jasmine.Ajax.uninstall();
|
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', (done) => {
|
it('should be able to retrieve current user info', (done) => {
|
||||||
let authService = new AlfrescoAuthenticationService();
|
let userJsApiResponse = {entry: fakeEcmUser};
|
||||||
spyOn(authService, )
|
spyOn(service, 'callApiGetPersonInfo').and.returnValue(Promise.resolve(userJsApiResponse));
|
||||||
ecmUserService.getUserInfo('fake-user').subscribe((res) => {
|
service.getCurrentUserInfo().subscribe(
|
||||||
expect(res).not.toBeUndefined();
|
(user) => {
|
||||||
expect(res).toEqual(jasmine.any(EcmUserModel));
|
expect(user).not.toBeUndefined();
|
||||||
expect(res.firstName).toEqual('fake-user-response');
|
expect(user.firstName).toEqual('fake-first-name');
|
||||||
expect(res.email).toEqual('fake@email.com');
|
expect(user.lastName).toEqual('fake-last-name');
|
||||||
done();
|
expect(user.email).toEqual('fakeEcm@ecmUser.com');
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
jasmine.Ajax.requests.mostRecent().respondWith({
|
it('should retrieve current logged user information', () => {
|
||||||
'status': 201,
|
spyOn(service, 'getUserInfo');
|
||||||
contentType: 'application/json',
|
spyOn(service, 'callApiGetPersonInfo').and.callThrough();
|
||||||
responseText: JSON.stringify({'entry': {'firstName': 'fake-user-response', 'id': 'fake@email.com'}})
|
service.getCurrentUserInfo();
|
||||||
|
expect(service.getUserInfo).toHaveBeenCalledWith('-me-');
|
||||||
|
});
|
||||||
|
|
||||||
|
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');
|
||||||
|
|
||||||
|
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();
|
||||||
|
expect(urlRs).toBeUndefined();
|
||||||
|
expect(contentServiceForTest.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).not.toBeUndefined();
|
||||||
|
expect(contentServiceForTest.getContentUrl).toHaveBeenCalledWith( {entry: {id: '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();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
*/
|
|
||||||
|
@ -50,7 +50,7 @@ export class EcmUserService {
|
|||||||
return this.getUserInfo('-me-');
|
return this.getUserInfo('-me-');
|
||||||
}
|
}
|
||||||
|
|
||||||
private callApiGetPersonInfo(userName: string, opts?: any) {
|
callApiGetPersonInfo(userName: string, opts?: any) {
|
||||||
return this.authService.getAlfrescoApi().core.peopleApi.getPerson(userName, opts);
|
return this.authService.getAlfrescoApi().core.peopleApi.getPerson(userName, opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user