[AAE-8061] Fix the group restriction for people widget (#7601)

* FIx the restrinction

* Fix unit test about restricted groups

* Add a small sleep to make it green

* Add sleep to other checks

* Remove sleeps, improve assertions

Co-authored-by: MichalFidor <michal.fidor@hyland.com>
This commit is contained in:
Maurizio Vitale
2022-04-26 15:29:06 +01:00
committed by GitHub
parent 3f7316f393
commit 9ff1f65841
7 changed files with 81 additions and 54 deletions

View File

@@ -824,6 +824,8 @@ describe('PeopleCloudComponent', () => {
});
describe('Groups restriction', () => {
const GROUP_1 = 0;
const GROUP_2 = 1;
beforeEach(() => {
fixture.detectChanges();
@@ -831,8 +833,8 @@ describe('PeopleCloudComponent', () => {
});
it('Shoud display all users if groups restriction is empty', async () => {
getInvolvedGroupsSpy = spyOn(identityService, 'getInvolvedGroups').and.returnValue(of(mockInvolvedGroups));
component.groupsRestriction = [];
getInvolvedGroupsSpy = spyOn(identityService, 'getInvolvedGroups').and.returnValue(of({name: 'fire'}));
typeInputValue('M');
await fixture.whenStable();
@@ -842,21 +844,41 @@ describe('PeopleCloudComponent', () => {
expect(fixture.debugElement.queryAll(By.css('[data-automation-id="adf-people-cloud-row"]')).length).toEqual(mockUsers.length);
});
it('Should display users that belongs to restricted groups', async () => {
getInvolvedGroupsSpy = spyOn(identityService, 'getInvolvedGroups').and.returnValue(of(mockInvolvedGroups));
component.groupsRestriction = [mockInvolvedGroups[0].name, mockInvolvedGroups[1].name];
it('Should display users that belongs to every restricted groups', async () => {
component.groupsRestriction = ['water', 'fire', 'air'];
getInvolvedGroupsSpy = spyOn(identityService, 'getInvolvedGroups').and.returnValue(of([{name: 'fire'}, {name: 'air'}, {name: 'water'}]));
typeInputValue('M');
await fixture.whenStable();
fixture.detectChanges();
expect(getInvolvedGroupsSpy).toHaveBeenCalledTimes(3);
expect(fixture.debugElement.queryAll(By.css('[data-automation-id="adf-people-cloud-row"]')).length).toEqual(mockUsers.length);
const userList = fixture.debugElement.queryAll(By.css('[data-automation-id="adf-people-cloud-row"]'));
expect(userList.length).toEqual(3);
});
it('Should not display users that not belongs to restricted groups', async () => {
getInvolvedGroupsSpy = spyOn(identityService, 'getInvolvedGroups').and.returnValue(of([mockInvolvedGroups[0]]));
component.groupsRestriction = [mockInvolvedGroups[0].name, mockInvolvedGroups[1].name];
it('Should not display user if belong to partial restricted groups', async () => {
component.groupsRestriction = ['water', 'fire', 'air'];
getInvolvedGroupsSpy = spyOn(identityService, 'getInvolvedGroups').and.returnValue(of([{name: 'fire'}, {name: 'water'}]));
typeInputValue('M');
await fixture.whenStable();
fixture.detectChanges();
const userList = fixture.debugElement.queryAll(By.css('[data-automation-id="adf-people-cloud-row"]'));
expect(userList.length).toEqual(0);
});
it('Should not display user if belong to none of the restricted groups', async () => {
component.groupsRestriction = ['water', 'fire', 'air'];
getInvolvedGroupsSpy = spyOn(identityService, 'getInvolvedGroups').and.returnValue(of([]));
typeInputValue('M');
await fixture.whenStable();
fixture.detectChanges();
expect(fixture.debugElement.queryAll(By.css('[data-automation-id="adf-people-cloud-row"]')).length).toEqual(0);
});
it('Should not be able to see a user that does not belong to the restricted group', async () => {
component.groupsRestriction = ['water'];
getInvolvedGroupsSpy = spyOn(identityService, 'getInvolvedGroups').and.returnValue(of([{name: 'fire'}]));
typeInputValue('M');
await fixture.whenStable();
@@ -867,7 +889,7 @@ describe('PeopleCloudComponent', () => {
it('Should mark as invalid preselected user if is not belongs to restricted groups', (done) => {
spyOn(identityService, 'findUserById').and.returnValue(of(mockPreselectedUsers[0]));
getInvolvedGroupsSpy = spyOn(identityService, 'getInvolvedGroups').and.returnValue(of([mockInvolvedGroups[0]]));
getInvolvedGroupsSpy = spyOn(identityService, 'getInvolvedGroups').and.returnValue(of([mockInvolvedGroups[GROUP_1]]));
const expectedWarning = {
message: 'INVALID_PRESELECTED_USERS',
@@ -882,7 +904,7 @@ describe('PeopleCloudComponent', () => {
done();
});
component.groupsRestriction = [mockInvolvedGroups[0].name, mockInvolvedGroups[1].name];
component.groupsRestriction = [mockInvolvedGroups[GROUP_1].name, mockInvolvedGroups[GROUP_2].name];
component.mode = 'single';
component.validate = true;
component.preSelectUsers = [mockPreselectedUsers[0]];

View File

@@ -33,7 +33,6 @@ import { Observable, of, BehaviorSubject, Subject } from 'rxjs';
import { switchMap, debounceTime, distinctUntilChanged, mergeMap, tap, filter, map, takeUntil } from 'rxjs/operators';
import {
FullNamePipe,
IdentityGroupModel,
IdentityUserModel,
IdentityUserService,
LogService
@@ -563,15 +562,13 @@ export class PeopleCloudComponent implements OnInit, OnChanges, OnDestroy {
private isUserPartOfAllRestrictedGroups(user: IdentityUserModel): Observable<boolean> {
return this.getUserGroups(user.id).pipe(
map(userGroups => userGroups.filter(
restrictedGroup => userGroups.includes(restrictedGroup)
).length >= this.groupsRestriction.length)
map(userGroups => this.groupsRestriction.every(restricted => userGroups.includes(restricted)))
);
}
private getUserGroups(userId: string): Observable<IdentityGroupModel[]> {
private getUserGroups(userId: string): Observable<string[]> {
return this.identityUserService.getInvolvedGroups(userId).pipe(
map(groups => groups.map(({id, name}) => ({id, name})))
map(groups => groups.map((group) => group.name))
);
}

View File

@@ -15,8 +15,6 @@
* limitations under the License.
*/
import { IdentityGroupModel } from '@alfresco/adf-core';
export const mockUsers = [
{ id: 'fake-id-1', username: 'first-name-1 last-name-1', firstName: 'first-name-1', lastName: 'last-name-1', email: 'abc@xyz.com' },
{ id: 'fake-id-2', username: 'first-name-2 last-name-2', firstName: 'first-name-2', lastName: 'last-name-2', email: 'abcd@xyz.com'},
@@ -49,6 +47,6 @@ export const mockPreselectedUsers = [
];
export const mockInvolvedGroups = [
{ id: 'mock-group-id-1', name: 'Mock Group 1', path: '/mock', subGroups: [] } as IdentityGroupModel,
{ id: 'mock-group-id-2', name: 'Mock Group 2', path: '', subGroups: [] } as IdentityGroupModel
{ id: 'mock-group-id-1', name: 'Mock Group 1', path: '/mock', subGroups: [] },
{ id: 'mock-group-id-2', name: 'Mock Group 2', path: '', subGroups: [] }
];