From 581a0c51770bab152d3b287c6ccfa1bdf7dffed8 Mon Sep 17 00:00:00 2001 From: siva kumar Date: Fri, 8 Feb 2019 19:05:07 +0530 Subject: [PATCH] [ADF-4054] GroupCloudService - Use composite api to fetch group roles* Used composite api to fetch group roles.* Updated unit test to the recent changes. (#4285) --- .../src/lib/group/mock/group-cloud.mock.ts | 10 ++++--- .../src/lib/group/models/group.model.ts | 13 ++++++++ .../services/group-cloud.service.spec.ts | 30 ++++++++++--------- .../lib/group/services/group-cloud.service.ts | 20 +++++++------ 4 files changed, 46 insertions(+), 27 deletions(-) diff --git a/lib/process-services-cloud/src/lib/group/mock/group-cloud.mock.ts b/lib/process-services-cloud/src/lib/group/mock/group-cloud.mock.ts index f41acdce1e..3068df1086 100644 --- a/lib/process-services-cloud/src/lib/group/mock/group-cloud.mock.ts +++ b/lib/process-services-cloud/src/lib/group/mock/group-cloud.mock.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { GroupModel } from '../models/group.model'; +import { GroupModel, GroupRoleModel } from '../models/group.model'; export let mockGroup1 = new GroupModel({ id: 'mock-id-1', name: 'Mock Group 1', path: '/mock', subGroups: [] @@ -103,8 +103,10 @@ export let applicationDetailsMockApi = { } }; -export let mockGroup = new GroupModel({ - id: 'mock-id', name: 'Mock Group', path: '/mock', realmRoles: ['MOCK-ADMIN-ROLE', 'MOCK-USER-ROLE', 'MOCK-ROLE-1'] -}); +export let groupRoles = [ + new GroupRoleModel({id: 'mock-id', name: 'MOCK-ADMIN-ROLE'}), + new GroupRoleModel({id: 'mock-id', name: 'MOCK-USER-ROLE'}), + new GroupRoleModel({id: 'mock-id', name: 'MOCK-ROLE-1'}) +]; export let clientRoles = [ 'MOCK-ADMIN-ROLE', 'MOCK-USER-ROLE']; diff --git a/lib/process-services-cloud/src/lib/group/models/group.model.ts b/lib/process-services-cloud/src/lib/group/models/group.model.ts index 4ed0304bf0..215185cde2 100644 --- a/lib/process-services-cloud/src/lib/group/models/group.model.ts +++ b/lib/process-services-cloud/src/lib/group/models/group.model.ts @@ -39,3 +39,16 @@ export class GroupModel { export interface GroupSearchParam { name?: string; } + +export class GroupRoleModel { + + id?: string; + name: string; + + constructor(obj?: any) { + if (obj) { + this.id = obj.id || null; + this.name = obj.name || null; + } + } +} diff --git a/lib/process-services-cloud/src/lib/group/services/group-cloud.service.spec.ts b/lib/process-services-cloud/src/lib/group/services/group-cloud.service.spec.ts index f0310b2034..68e4acd1ee 100644 --- a/lib/process-services-cloud/src/lib/group/services/group-cloud.service.spec.ts +++ b/lib/process-services-cloud/src/lib/group/services/group-cloud.service.spec.ts @@ -34,10 +34,10 @@ import { mockError, roleMappingApi, noRoleMappingApi, - mockGroup, + groupRoles, clientRoles } from '../mock/group-cloud.mock'; -import { GroupSearchParam, GroupModel } from '../models/group.model'; +import { GroupSearchParam } from '../models/group.model'; import { HttpErrorResponse } from '@angular/common/http'; import { throwError, of } from 'rxjs'; @@ -93,30 +93,32 @@ describe('GroupCloudService', () => { }); }); - it('should fetch group by userId', (done) => { - spyOn(service, 'getGroupDetailsById').and.returnValue(of(mockGroup)); - service.getGroupDetailsById('mock-group-id').subscribe( - (res: GroupModel) => { + it('should able to fetch group roles by groupId', (done) => { + spyOn(service, 'getGroupRoles').and.returnValue(of(groupRoles)); + service.getGroupRoles('mock-group-id').subscribe( + (res: any) => { expect(res).toBeDefined(); - expect(res.name).toEqual('Mock Group'); - expect(res.realmRoles).toEqual(mockGroup.realmRoles); + expect(res.length).toEqual(3); + expect(res[0].name).toEqual('MOCK-ADMIN-ROLE'); + expect(res[1].name).toEqual('MOCK-USER-ROLE'); + expect(res[2].name).toEqual('MOCK-ROLE-1'); done(); } ); }); - it('Should not fetch group if error occurred', (done) => { + it('Should not able to fetch group roles if error occurred', (done) => { const errorResponse = new HttpErrorResponse({ error: 'Mock Error', status: 404, statusText: 'Not Found' }); - spyOn(service, 'getGroupDetailsById').and.returnValue(throwError(errorResponse)); + spyOn(service, 'getGroupRoles').and.returnValue(throwError(errorResponse)); - service.getGroupDetailsById('mock-group-id') + service.getGroupRoles('mock-group-id') .subscribe( () => { - fail('expected an error, not group'); + fail('expected an error, not group roles'); }, (error) => { expect(error.status).toEqual(404); @@ -128,7 +130,7 @@ describe('GroupCloudService', () => { }); it('should return true if group has given role', (done) => { - spyOn(service, 'getGroupDetailsById').and.returnValue(of(mockGroup)); + spyOn(service, 'getGroupRoles').and.returnValue(of(groupRoles)); service.checkGroupHasRole('mock-group-id', ['MOCK-ADMIN-ROLE']).subscribe( (res: boolean) => { expect(res).toBeDefined(); @@ -139,7 +141,7 @@ describe('GroupCloudService', () => { }); it('should return false if group does not have given role', (done) => { - spyOn(service, 'getGroupDetailsById').and.returnValue(of(mockGroup)); + spyOn(service, 'getGroupRoles').and.returnValue(of(groupRoles)); service.checkGroupHasRole('mock-group-id', ['MOCK-ADMIN-MODELER']).subscribe( (res: boolean) => { expect(res).toBeDefined(); diff --git a/lib/process-services-cloud/src/lib/group/services/group-cloud.service.ts b/lib/process-services-cloud/src/lib/group/services/group-cloud.service.ts index 5c63807ef4..de679a206a 100644 --- a/lib/process-services-cloud/src/lib/group/services/group-cloud.service.ts +++ b/lib/process-services-cloud/src/lib/group/services/group-cloud.service.ts @@ -20,7 +20,7 @@ import { from, of, Observable, throwError } from 'rxjs'; import { map, catchError } from 'rxjs/operators'; import { AlfrescoApiService, AppConfigService, LogService } from '@alfresco/adf-core'; -import { GroupSearchParam, GroupModel } from '../models/group.model'; +import { GroupSearchParam, GroupRoleModel } from '../models/group.model'; @Injectable({ providedIn: 'root' @@ -60,8 +60,8 @@ export class GroupCloudService { * @param groupId ID of the target group * @returns Group details */ - getGroupDetailsById(groupId: string) { - const url = this.getGroupsApi() + '/' + groupId; + getGroupRoles(groupId: string): Observable { + const url = this.buildRolesUrl(groupId); const httpMethod = 'GET', pathParams = {}, queryParams = {}, bodyParam = {}, headerParams = {}, formParams = {}, contentTypes = ['application/json'], accepts = ['application/json']; @@ -81,14 +81,12 @@ export class GroupCloudService { * @returns True if the group has one or more of the roles, false otherwise */ checkGroupHasRole(groupId: string, roleNames: string[]): Observable { - return this.getGroupDetailsById(groupId).pipe(map((response: GroupModel) => { - let availableGroupRoles = []; + return this.getGroupRoles(groupId).pipe(map((groupRoles: GroupRoleModel[]) => { let hasRole = false; - availableGroupRoles = response.realmRoles; - if (availableGroupRoles && availableGroupRoles.length > 0) { + if (groupRoles && groupRoles.length > 0) { roleNames.forEach((roleName: string) => { - const role = availableGroupRoles.find((availableRole) => { - return roleName === availableRole; + const role = groupRoles.find((groupRole) => { + return roleName === groupRole.name; }); if (role) { hasRole = true; @@ -199,6 +197,10 @@ export class GroupCloudService { return `${this.appConfigService.get('identityHost')}/groups`; } + private buildRolesUrl(groupId: string): any { + return `${this.appConfigService.get('identityHost')}/groups/${groupId}/role-mappings/realm/composite`; + } + /** * Throw the error * @param error