[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.
*/
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'];

View File

@@ -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;
}
}
}

View File

@@ -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();

View File

@@ -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<GroupRoleModel[]> {
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<boolean> {
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