[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)

This commit is contained in:
siva kumar
2019-02-08 19:05:07 +05:30
committed by Maurizio Vitale
parent 2b4a69797c
commit 581a0c5177
4 changed files with 46 additions and 27 deletions

View File

@@ -15,7 +15,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { GroupModel } from '../models/group.model'; import { GroupModel, GroupRoleModel } from '../models/group.model';
export let mockGroup1 = new GroupModel({ export let mockGroup1 = new GroupModel({
id: 'mock-id-1', name: 'Mock Group 1', path: '/mock', subGroups: [] id: 'mock-id-1', name: 'Mock Group 1', path: '/mock', subGroups: []
@@ -103,8 +103,10 @@ export let applicationDetailsMockApi = {
} }
}; };
export let mockGroup = new GroupModel({ export let groupRoles = [
id: 'mock-id', name: 'Mock Group', path: '/mock', realmRoles: ['MOCK-ADMIN-ROLE', 'MOCK-USER-ROLE', 'MOCK-ROLE-1'] 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']; export let clientRoles = [ 'MOCK-ADMIN-ROLE', 'MOCK-USER-ROLE'];

View File

@@ -39,3 +39,16 @@ export class GroupModel {
export interface GroupSearchParam { export interface GroupSearchParam {
name?: string; name?: string;
} }
export class GroupRoleModel {
id?: string;
name: string;
constructor(obj?: any) {
if (obj) {
this.id = obj.id || null;
this.name = obj.name || null;
}
}
}

View File

@@ -34,10 +34,10 @@ import {
mockError, mockError,
roleMappingApi, roleMappingApi,
noRoleMappingApi, noRoleMappingApi,
mockGroup, groupRoles,
clientRoles clientRoles
} from '../mock/group-cloud.mock'; } 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 { HttpErrorResponse } from '@angular/common/http';
import { throwError, of } from 'rxjs'; import { throwError, of } from 'rxjs';
@@ -93,30 +93,32 @@ describe('GroupCloudService', () => {
}); });
}); });
it('should fetch group by userId', (done) => { it('should able to fetch group roles by groupId', (done) => {
spyOn(service, 'getGroupDetailsById').and.returnValue(of(mockGroup)); spyOn(service, 'getGroupRoles').and.returnValue(of(groupRoles));
service.getGroupDetailsById('mock-group-id').subscribe( service.getGroupRoles('mock-group-id').subscribe(
(res: GroupModel) => { (res: any) => {
expect(res).toBeDefined(); expect(res).toBeDefined();
expect(res.name).toEqual('Mock Group'); expect(res.length).toEqual(3);
expect(res.realmRoles).toEqual(mockGroup.realmRoles); 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(); 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({ const errorResponse = new HttpErrorResponse({
error: 'Mock Error', error: 'Mock Error',
status: 404, statusText: 'Not Found' 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( .subscribe(
() => { () => {
fail('expected an error, not group'); fail('expected an error, not group roles');
}, },
(error) => { (error) => {
expect(error.status).toEqual(404); expect(error.status).toEqual(404);
@@ -128,7 +130,7 @@ describe('GroupCloudService', () => {
}); });
it('should return true if group has given role', (done) => { 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( service.checkGroupHasRole('mock-group-id', ['MOCK-ADMIN-ROLE']).subscribe(
(res: boolean) => { (res: boolean) => {
expect(res).toBeDefined(); expect(res).toBeDefined();
@@ -139,7 +141,7 @@ describe('GroupCloudService', () => {
}); });
it('should return false if group does not have given role', (done) => { 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( service.checkGroupHasRole('mock-group-id', ['MOCK-ADMIN-MODELER']).subscribe(
(res: boolean) => { (res: boolean) => {
expect(res).toBeDefined(); expect(res).toBeDefined();

View File

@@ -20,7 +20,7 @@ import { from, of, Observable, throwError } from 'rxjs';
import { map, catchError } from 'rxjs/operators'; import { map, catchError } from 'rxjs/operators';
import { AlfrescoApiService, AppConfigService, LogService } from '@alfresco/adf-core'; import { AlfrescoApiService, AppConfigService, LogService } from '@alfresco/adf-core';
import { GroupSearchParam, GroupModel } from '../models/group.model'; import { GroupSearchParam, GroupRoleModel } from '../models/group.model';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@@ -60,8 +60,8 @@ export class GroupCloudService {
* @param groupId ID of the target group * @param groupId ID of the target group
* @returns Group details * @returns Group details
*/ */
getGroupDetailsById(groupId: string) { getGroupRoles(groupId: string): Observable<GroupRoleModel[]> {
const url = this.getGroupsApi() + '/' + groupId; const url = this.buildRolesUrl(groupId);
const httpMethod = 'GET', pathParams = {}, queryParams = {}, bodyParam = {}, headerParams = {}, const httpMethod = 'GET', pathParams = {}, queryParams = {}, bodyParam = {}, headerParams = {},
formParams = {}, contentTypes = ['application/json'], accepts = ['application/json']; 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 * @returns True if the group has one or more of the roles, false otherwise
*/ */
checkGroupHasRole(groupId: string, roleNames: string[]): Observable<boolean> { checkGroupHasRole(groupId: string, roleNames: string[]): Observable<boolean> {
return this.getGroupDetailsById(groupId).pipe(map((response: GroupModel) => { return this.getGroupRoles(groupId).pipe(map((groupRoles: GroupRoleModel[]) => {
let availableGroupRoles = [];
let hasRole = false; let hasRole = false;
availableGroupRoles = response.realmRoles; if (groupRoles && groupRoles.length > 0) {
if (availableGroupRoles && availableGroupRoles.length > 0) {
roleNames.forEach((roleName: string) => { roleNames.forEach((roleName: string) => {
const role = availableGroupRoles.find((availableRole) => { const role = groupRoles.find((groupRole) => {
return roleName === availableRole; return roleName === groupRole.name;
}); });
if (role) { if (role) {
hasRole = true; hasRole = true;
@@ -199,6 +197,10 @@ export class GroupCloudService {
return `${this.appConfigService.get('identityHost')}/groups`; 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 * Throw the error
* @param error * @param error