mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
use people api and new js api where possible (#5888)
* use people api and new js api where possible * fix code and tests * cleanup tests
This commit is contained in:
@@ -18,41 +18,30 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { fakeEcmUser } from '../mock/ecm-user.service.mock';
|
||||
import { EcmUserService } from '../services/ecm-user.service';
|
||||
import { setupTestBed } from '../testing/setup-test-bed';
|
||||
import { CoreTestingModule } from '../testing/core.testing.module';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
import { AuthenticationService } from './authentication.service';
|
||||
import { ContentService } from './content.service';
|
||||
|
||||
declare let jasmine: any;
|
||||
|
||||
describe('EcmUserService', () => {
|
||||
|
||||
let service: EcmUserService;
|
||||
let authService: AuthenticationService;
|
||||
let contentService: ContentService;
|
||||
|
||||
setupTestBed({
|
||||
imports: [
|
||||
TranslateModule.forRoot(),
|
||||
CoreTestingModule
|
||||
]
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [
|
||||
TranslateModule.forRoot(),
|
||||
CoreTestingModule
|
||||
]
|
||||
});
|
||||
|
||||
service = TestBed.inject(EcmUserService);
|
||||
authService = TestBed.inject(AuthenticationService);
|
||||
contentService = TestBed.inject(ContentService);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
jasmine.Ajax.install();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jasmine.Ajax.uninstall();
|
||||
});
|
||||
|
||||
describe('when user is logged in', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -60,6 +49,7 @@ describe('EcmUserService', () => {
|
||||
});
|
||||
|
||||
it('should be able to retrieve current user info', (done) => {
|
||||
spyOn(service.peopleApi, 'getPerson').and.returnValue(Promise.resolve({ entry: fakeEcmUser }));
|
||||
service.getCurrentUserInfo().subscribe(
|
||||
(user) => {
|
||||
expect(user).toBeDefined();
|
||||
@@ -67,23 +57,8 @@ describe('EcmUserService', () => {
|
||||
expect(user.lastName).toEqual('fake-ecm-last-name');
|
||||
expect(user.email).toEqual('fakeEcm@ecmUser.com');
|
||||
done();
|
||||
});
|
||||
jasmine.Ajax.requests.mostRecent().respondWith({
|
||||
status: 200,
|
||||
contentType: 'application/json',
|
||||
responseText: JSON.stringify({entry: fakeEcmUser})
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to log errors on call', (done) => {
|
||||
service.getCurrentUserInfo().subscribe(() => {
|
||||
}, () => {
|
||||
done();
|
||||
});
|
||||
|
||||
jasmine.Ajax.requests.mostRecent().respondWith({
|
||||
status: 403
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it('should retrieve avatar url for current user', () => {
|
||||
@@ -92,13 +67,5 @@ describe('EcmUserService', () => {
|
||||
|
||||
expect(urlRs).toEqual('fake/url/image/for/ecm/user');
|
||||
});
|
||||
|
||||
it('should not call content service without avatar id', () => {
|
||||
spyOn(contentService, 'getContentUrl').and.callThrough();
|
||||
const urlRs = service.getUserProfileImage(undefined);
|
||||
|
||||
expect(urlRs).toBeNull();
|
||||
expect(contentService.getContentUrl).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -16,22 +16,26 @@
|
||||
*/
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable, from, throwError } from 'rxjs';
|
||||
import { map, catchError } from 'rxjs/operators';
|
||||
import { Observable, from } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ContentService } from './content.service';
|
||||
import { AlfrescoApiService } from './alfresco-api.service';
|
||||
import { LogService } from './log.service';
|
||||
import { EcmUserModel } from '../models/ecm-user.model';
|
||||
import { PersonEntry } from '@alfresco/js-api';
|
||||
import { PeopleApi } from '@alfresco/js-api';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class EcmUserService {
|
||||
|
||||
private _peopleApi: PeopleApi;
|
||||
|
||||
constructor(private apiService: AlfrescoApiService,
|
||||
private contentService: ContentService,
|
||||
private logService: LogService) {
|
||||
private contentService: ContentService) {
|
||||
}
|
||||
|
||||
get peopleApi(): PeopleApi {
|
||||
return this._peopleApi || (this._peopleApi = new PeopleApi(this.apiService.getInstance()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,12 +44,9 @@ export class EcmUserService {
|
||||
* @returns User information
|
||||
*/
|
||||
getUserInfo(userName: string): Observable<EcmUserModel> {
|
||||
return from(this.apiService.getInstance().core.peopleApi.getPerson(userName))
|
||||
return from(this.peopleApi.getPerson(userName))
|
||||
.pipe(
|
||||
map((personEntry: PersonEntry) => {
|
||||
return new EcmUserModel(personEntry.entry);
|
||||
}),
|
||||
catchError((err) => this.handleError(err))
|
||||
map((personEntry) => new EcmUserModel(personEntry.entry))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -63,19 +64,6 @@ export class EcmUserService {
|
||||
* @returns Image URL
|
||||
*/
|
||||
getUserProfileImage(avatarId: string): string {
|
||||
if (avatarId) {
|
||||
return this.contentService.getContentUrl(avatarId);
|
||||
}
|
||||
return null;
|
||||
return this.contentService.getContentUrl(avatarId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Throw the error
|
||||
* @param error
|
||||
*/
|
||||
private handleError(error: any) {
|
||||
this.logService.error(error);
|
||||
return throwError(error || 'Server error');
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user