mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-05-26 17:24:56 +00:00
[AAE-7077] Reset people content service cache on logout (#7637)
* reset people content state on logout * unit test * use property accessor
This commit is contained in:
parent
2107538c51
commit
1e3099b99b
@ -20,35 +20,37 @@ import { AlfrescoApiServiceMock } from '../mock/alfresco-api.service.mock';
|
|||||||
import { CoreTestingModule } from '../testing/core.testing.module';
|
import { CoreTestingModule } from '../testing/core.testing.module';
|
||||||
import { PeopleContentService, PeopleContentQueryResponse, PeopleContentQueryRequestModel } from './people-content.service';
|
import { PeopleContentService, PeopleContentQueryResponse, PeopleContentQueryRequestModel } from './people-content.service';
|
||||||
import { AlfrescoApiService } from './alfresco-api.service';
|
import { AlfrescoApiService } from './alfresco-api.service';
|
||||||
import { setupTestBed } from '../testing/setup-test-bed';
|
|
||||||
import { TranslateModule } from '@ngx-translate/core';
|
import { TranslateModule } from '@ngx-translate/core';
|
||||||
import { TestBed } from '@angular/core/testing';
|
import { TestBed } from '@angular/core/testing';
|
||||||
import { LogService } from './log.service';
|
import { LogService } from './log.service';
|
||||||
import { PersonEntry } from '@alfresco/js-api';
|
import { PersonEntry } from '@alfresco/js-api';
|
||||||
|
import { AuthenticationService } from './authentication.service';
|
||||||
|
|
||||||
describe('PeopleContentService', () => {
|
describe('PeopleContentService', () => {
|
||||||
|
|
||||||
let service: PeopleContentService;
|
let peopleContentService: PeopleContentService;
|
||||||
let logService: LogService;
|
let logService: LogService;
|
||||||
|
let authenticationService: AuthenticationService;
|
||||||
setupTestBed({
|
|
||||||
imports: [
|
|
||||||
TranslateModule.forRoot(),
|
|
||||||
CoreTestingModule
|
|
||||||
],
|
|
||||||
providers: [
|
|
||||||
{ provide: AlfrescoApiService, useClass: AlfrescoApiServiceMock }
|
|
||||||
]
|
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
service = TestBed.inject(PeopleContentService);
|
TestBed.configureTestingModule({
|
||||||
|
imports: [
|
||||||
|
TranslateModule.forRoot(),
|
||||||
|
CoreTestingModule
|
||||||
|
],
|
||||||
|
providers: [
|
||||||
|
{ provide: AlfrescoApiService, useClass: AlfrescoApiServiceMock }
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
authenticationService = TestBed.inject(AuthenticationService);
|
||||||
|
peopleContentService = TestBed.inject(PeopleContentService);
|
||||||
logService = TestBed.inject(LogService);
|
logService = TestBed.inject(LogService);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to fetch person details based on id', (done) => {
|
it('should be able to fetch person details based on id', (done) => {
|
||||||
spyOn(service['peopleApi'], 'getPerson').and.returnValue(Promise.resolve(new PersonEntry({ entry: fakeEcmUser })));
|
spyOn(peopleContentService.peopleApi, 'getPerson').and.returnValue(Promise.resolve(new PersonEntry({ entry: fakeEcmUser })));
|
||||||
service.getPerson('fake-id').subscribe((person) => {
|
peopleContentService.getPerson('fake-id').subscribe((person) => {
|
||||||
expect(person.entry.id).toEqual('fake-id');
|
expect(person.entry.id).toEqual('fake-id');
|
||||||
expect(person.entry.email).toEqual('fakeEcm@ecmUser.com');
|
expect(person.entry.email).toEqual('fakeEcm@ecmUser.com');
|
||||||
done();
|
done();
|
||||||
@ -56,24 +58,24 @@ describe('PeopleContentService', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('calls getPerson api method by an id', (done) => {
|
it('calls getPerson api method by an id', (done) => {
|
||||||
const getPersonSpy = spyOn(service['peopleApi'], 'getPerson').and.returnValue(Promise.resolve(null));
|
const getPersonSpy = spyOn(peopleContentService.peopleApi, 'getPerson').and.returnValue(Promise.resolve(null));
|
||||||
service.getPerson('fake-id').subscribe(() => {
|
peopleContentService.getPerson('fake-id').subscribe(() => {
|
||||||
expect(getPersonSpy).toHaveBeenCalledWith('fake-id');
|
expect(getPersonSpy).toHaveBeenCalledWith('fake-id');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('calls getPerson api method with "-me-"', (done) => {
|
it('calls getPerson api method with "-me-"', (done) => {
|
||||||
const getPersonSpy = spyOn(service['peopleApi'], 'getPerson').and.returnValue(Promise.resolve(null));
|
const getPersonSpy = spyOn(peopleContentService.peopleApi, 'getPerson').and.returnValue(Promise.resolve(null));
|
||||||
service.getPerson('-me-').subscribe(() => {
|
peopleContentService.getPerson('-me-').subscribe(() => {
|
||||||
expect(getPersonSpy).toHaveBeenCalledWith('-me-');
|
expect(getPersonSpy).toHaveBeenCalledWith('-me-');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to list people', (done) => {
|
it('should be able to list people', (done) => {
|
||||||
spyOn(service['peopleApi'], 'listPeople').and.returnValue(Promise.resolve(fakeEcmUserList));
|
spyOn(peopleContentService.peopleApi, 'listPeople').and.returnValue(Promise.resolve(fakeEcmUserList));
|
||||||
service.listPeople().subscribe((response: PeopleContentQueryResponse) => {
|
peopleContentService.listPeople().subscribe((response: PeopleContentQueryResponse) => {
|
||||||
const people = response.entries;
|
const people = response.entries;
|
||||||
const pagination = response.pagination;
|
const pagination = response.pagination;
|
||||||
|
|
||||||
@ -89,36 +91,36 @@ describe('PeopleContentService', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should call listPeople api method', (done) => {
|
it('should call listPeople api method', (done) => {
|
||||||
const listPeopleSpy = spyOn(service['peopleApi'], 'listPeople').and.returnValue(Promise.resolve(fakeEcmUserList));
|
const listPeopleSpy = spyOn(peopleContentService.peopleApi, 'listPeople').and.returnValue(Promise.resolve(fakeEcmUserList));
|
||||||
service.listPeople().subscribe(() => {
|
peopleContentService.listPeople().subscribe(() => {
|
||||||
expect(listPeopleSpy).toHaveBeenCalled();
|
expect(listPeopleSpy).toHaveBeenCalled();
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should call listPeople api with requested sorting params', async () => {
|
it('should call listPeople api with requested sorting params', async () => {
|
||||||
const listPeopleSpy = spyOn(service['peopleApi'], 'listPeople').and.returnValue(Promise.resolve(fakeEcmUserList));
|
const listPeopleSpy = spyOn(peopleContentService.peopleApi, 'listPeople').and.returnValue(Promise.resolve(fakeEcmUserList));
|
||||||
const requestQueryParams: PeopleContentQueryRequestModel = { skipCount: 10, maxItems: 20, sorting: { orderBy: 'firstName', direction: 'asc' } };
|
const requestQueryParams: PeopleContentQueryRequestModel = { skipCount: 10, maxItems: 20, sorting: { orderBy: 'firstName', direction: 'asc' } };
|
||||||
const expectedValue = { skipCount: 10, maxItems: 20, orderBy: ['firstName ASC'] };
|
const expectedValue = { skipCount: 10, maxItems: 20, orderBy: ['firstName ASC'] };
|
||||||
|
|
||||||
await service.listPeople(requestQueryParams).toPromise();
|
await peopleContentService.listPeople(requestQueryParams).toPromise();
|
||||||
|
|
||||||
expect(listPeopleSpy).toHaveBeenCalledWith(expectedValue);
|
expect(listPeopleSpy).toHaveBeenCalledWith(expectedValue);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not call listPeople api with sorting params if sorting is not defined', async () => {
|
it('should not call listPeople api with sorting params if sorting is not defined', async () => {
|
||||||
const listPeopleSpy = spyOn(service['peopleApi'], 'listPeople').and.returnValue(Promise.resolve(fakeEcmUserList));
|
const listPeopleSpy = spyOn(peopleContentService.peopleApi, 'listPeople').and.returnValue(Promise.resolve(fakeEcmUserList));
|
||||||
const requestQueryParams: PeopleContentQueryRequestModel = { skipCount: 10, maxItems: 20, sorting: undefined };
|
const requestQueryParams: PeopleContentQueryRequestModel = { skipCount: 10, maxItems: 20, sorting: undefined };
|
||||||
const expectedValue = { skipCount: 10, maxItems: 20 };
|
const expectedValue = { skipCount: 10, maxItems: 20 };
|
||||||
|
|
||||||
await service.listPeople(requestQueryParams).toPromise();
|
await peopleContentService.listPeople(requestQueryParams).toPromise();
|
||||||
|
|
||||||
expect(listPeopleSpy).toHaveBeenCalledWith(expectedValue);
|
expect(listPeopleSpy).toHaveBeenCalledWith(expectedValue);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to create new person', (done) => {
|
it('should be able to create new person', (done) => {
|
||||||
spyOn(service['peopleApi'], 'createPerson').and.returnValue(Promise.resolve(new PersonEntry({ entry: fakeEcmUser })));
|
spyOn(peopleContentService.peopleApi, 'createPerson').and.returnValue(Promise.resolve(new PersonEntry({ entry: fakeEcmUser })));
|
||||||
service.createPerson(createNewPersonMock).subscribe((person) => {
|
peopleContentService.createPerson(createNewPersonMock).subscribe((person) => {
|
||||||
expect(person.id).toEqual('fake-id');
|
expect(person.id).toEqual('fake-id');
|
||||||
expect(person.email).toEqual('fakeEcm@ecmUser.com');
|
expect(person.email).toEqual('fakeEcm@ecmUser.com');
|
||||||
done();
|
done();
|
||||||
@ -126,8 +128,8 @@ describe('PeopleContentService', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to call createPerson api with new person details', (done) => {
|
it('should be able to call createPerson api with new person details', (done) => {
|
||||||
const createPersonSpy = spyOn(service['peopleApi'], 'createPerson').and.returnValue(Promise.resolve(new PersonEntry({ entry: fakeEcmUser })));
|
const createPersonSpy = spyOn(peopleContentService.peopleApi, 'createPerson').and.returnValue(Promise.resolve(new PersonEntry({ entry: fakeEcmUser })));
|
||||||
service.createPerson(createNewPersonMock).subscribe((person) => {
|
peopleContentService.createPerson(createNewPersonMock).subscribe((person) => {
|
||||||
expect(person.id).toEqual('fake-id');
|
expect(person.id).toEqual('fake-id');
|
||||||
expect(person.email).toEqual('fakeEcm@ecmUser.com');
|
expect(person.email).toEqual('fakeEcm@ecmUser.com');
|
||||||
expect(createPersonSpy).toHaveBeenCalledWith(createNewPersonMock, undefined);
|
expect(createPersonSpy).toHaveBeenCalledWith(createNewPersonMock, undefined);
|
||||||
@ -136,9 +138,9 @@ describe('PeopleContentService', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to throw an error if createPerson api failed', (done) => {
|
it('should be able to throw an error if createPerson api failed', (done) => {
|
||||||
const createPersonSpy = spyOn(service['peopleApi'], 'createPerson').and.returnValue(Promise.reject({ message: 'failed to create new person' }));
|
const createPersonSpy = spyOn(peopleContentService.peopleApi, 'createPerson').and.returnValue(Promise.reject({ message: 'failed to create new person' }));
|
||||||
const logErrorSpy = spyOn(logService, 'error');
|
const logErrorSpy = spyOn(logService, 'error');
|
||||||
service.createPerson(createNewPersonMock).subscribe(
|
peopleContentService.createPerson(createNewPersonMock).subscribe(
|
||||||
() => {},
|
() => {},
|
||||||
(error) => {
|
(error) => {
|
||||||
expect(error).toEqual({ message: 'failed to create new person' });
|
expect(error).toEqual({ message: 'failed to create new person' });
|
||||||
@ -149,14 +151,27 @@ describe('PeopleContentService', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('Should make the api call to check if the user is a content admin only once', async () => {
|
it('Should make the api call to check if the user is a content admin only once', async () => {
|
||||||
const getCurrentPersonSpy = spyOn(service['peopleApi'], 'getPerson').and.returnValue(Promise.resolve(getFakeUserWithContentAdminCapability()));
|
const getCurrentPersonSpy = spyOn(peopleContentService.peopleApi, 'getPerson').and.returnValue(Promise.resolve(getFakeUserWithContentAdminCapability()));
|
||||||
|
|
||||||
expect(await service.isContentAdmin()).toBe(true);
|
expect(await peopleContentService.isContentAdmin()).toBe(true);
|
||||||
expect(getCurrentPersonSpy.calls.count()).toEqual(1);
|
expect(getCurrentPersonSpy.calls.count()).toEqual(1);
|
||||||
|
|
||||||
await service.isContentAdmin();
|
await peopleContentService.isContentAdmin();
|
||||||
|
|
||||||
expect(await service.isContentAdmin()).toBe(true);
|
expect(await peopleContentService.isContentAdmin()).toBe(true);
|
||||||
expect(getCurrentPersonSpy.calls.count()).toEqual(1);
|
expect(getCurrentPersonSpy.calls.count()).toEqual(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should reset the admin cache upon logout', async () => {
|
||||||
|
const getCurrentPersonSpy = spyOn(peopleContentService.peopleApi, 'getPerson').and.returnValue(Promise.resolve(getFakeUserWithContentAdminCapability()));
|
||||||
|
|
||||||
|
expect(await peopleContentService.isContentAdmin()).toBeTruthy();
|
||||||
|
expect(peopleContentService.hasCheckedIsContentAdmin).toBeTruthy();
|
||||||
|
|
||||||
|
authenticationService.onLogout.next(true);
|
||||||
|
expect(peopleContentService.hasCheckedIsContentAdmin).toBeFalsy();
|
||||||
|
|
||||||
|
expect(await peopleContentService.isContentAdmin()).toBe(true);
|
||||||
|
expect(getCurrentPersonSpy.calls.count()).toEqual(2);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -22,6 +22,7 @@ import { catchError, map } from 'rxjs/operators';
|
|||||||
import { PersonEntry, PeopleApi, PersonBodyCreate, Pagination, PersonBodyUpdate } from '@alfresco/js-api';
|
import { PersonEntry, PeopleApi, PersonBodyCreate, Pagination, PersonBodyUpdate } from '@alfresco/js-api';
|
||||||
import { EcmUserModel } from '../models/ecm-user.model';
|
import { EcmUserModel } from '../models/ecm-user.model';
|
||||||
import { LogService } from './log.service';
|
import { LogService } from './log.service';
|
||||||
|
import { AuthenticationService } from './authentication.service';
|
||||||
|
|
||||||
// eslint-disable-next-line no-shadow
|
// eslint-disable-next-line no-shadow
|
||||||
export enum ContentGroups {
|
export enum ContentGroups {
|
||||||
@ -57,7 +58,15 @@ export class PeopleContentService {
|
|||||||
return this._peopleApi;
|
return this._peopleApi;
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(private apiService: AlfrescoApiService, private logService: LogService) {
|
constructor(
|
||||||
|
private apiService: AlfrescoApiService,
|
||||||
|
authenticationService: AuthenticationService,
|
||||||
|
private logService: LogService
|
||||||
|
) {
|
||||||
|
authenticationService.onLogout.subscribe(() => {
|
||||||
|
this.hasCheckedIsContentAdmin = false;
|
||||||
|
this.hasContentAdminRole = false;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user