Skip to content

Commit

Permalink
[AAE-6389] Remove duplicated identity user service mock (#7373)
Browse files Browse the repository at this point in the history
* [AAE-6389] removed duplicate services and refactored existing

* [AAE-6389] improved and moved mocks to proper files

* [AAE-6389] fixed circular dependency
  • Loading branch information
tomgny committed Nov 24, 2021
1 parent 5618ed7 commit a40de80
Show file tree
Hide file tree
Showing 14 changed files with 657 additions and 637 deletions.
76 changes: 76 additions & 0 deletions lib/core/mock/identity-group.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { IdentityGroupModel, IdentityGroupCountModel } from '../models/identity-group.model';
import { IdentityRoleModel } from '../models/identity-role.model';
import { IdentityJoinGroupRequestModel } from '../services/identity-user.service.interface';

export const mockIdentityGroup1 = <IdentityGroupModel> {
id: 'mock-group-id-1', name: 'Mock Group 1', path: '/mock', subGroups: []
};

export const mockIdentityGroup2 = <IdentityGroupModel> {
id: 'mock-group-id-2', name: 'Mock Group 2', path: '', subGroups: []
};

export const mockIdentityGroup3 = <IdentityGroupModel> {
id: 'mock-group-id-3', name: 'Mock Group 3', path: '', subGroups: []
};

export const mockIdentityGroup4 = <IdentityGroupModel> {
id: 'mock-group-id-4', name: 'Mock Group 4', path: '', subGroups: []
};

export const mockIdentityGroup5 = <IdentityGroupModel> {
id: 'mock-group-id-5', name: 'Mock Group 5', path: '', subGroups: []
};

export const mockIdentityGroupsCount = <IdentityGroupCountModel> { count: 10 };

export const mockIdentityGroups = [
mockIdentityGroup1, mockIdentityGroup2, mockIdentityGroup3, mockIdentityGroup4, mockIdentityGroup5
];

export const roleMappingMock = [
{ id: 'role-id-1', name: 'role-name-1' }, { id: 'role-id-2', name: 'role-name-2' }
];

export const mockIdentityRoles = [
new IdentityRoleModel({id: 'mock-role-id', name: 'MOCK-ADMIN-ROLE'}),
new IdentityRoleModel({id: 'mock-role-id', name: 'MOCK-USER-ROLE'}),
new IdentityRoleModel({id: 'mock-role-id', name: 'MOCK-ROLE-1'})
];

export const clientRoles: IdentityRoleModel[] = [
new IdentityRoleModel({ name: 'MOCK-ADMIN-ROLE' }),
new IdentityRoleModel({ name: 'MOCK-USER-ROLE' })
];

export const mockJoinGroupRequest: IdentityJoinGroupRequestModel = {userId: 'mock-hser-id', groupId: 'mock-group-id', realm: 'mock-realm-name'};

export const mockGroup1 = <IdentityGroupModel> {
id: 'mock-group-id-1', name: 'Mock Group 1', path: '/mock', subGroups: []
};

export const mockGroup2 = <IdentityGroupModel> {
id: 'mock-group-id-2', name: 'Mock Group 2', path: '', subGroups: []
};

export const mockGroups = [
<IdentityGroupModel> { id: 'mock-group-id-1', name: 'Mock Group 1', path: '/mock', subGroups: [] },
<IdentityGroupModel> { id: 'mock-group-id-2', name: 'Mock Group 2', path: '', subGroups: [] }
];
173 changes: 100 additions & 73 deletions lib/core/mock/identity-group.service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,104 +15,131 @@
* limitations under the License.
*/

import { IdentityGroupModel, IdentityGroupCountModel } from '../models/identity-group.model';
import { Injectable } from '@angular/core';
import { mockIdentityGroups, mockIdentityGroupsCount, mockIdentityRoles } from './identity-group.mock';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { IdentityGroupServiceInterface } from '../services/identity-group.interface';
import {
IdentityGroupModel,
IdentityGroupQueryResponse,
IdentityGroupQueryCloudRequestModel,
IdentityGroupSearchParam,
IdentityGroupCountModel
} from '../models/identity-group.model';
import { IdentityRoleModel } from '../models/identity-role.model';

export const mockIdentityGroup1 = <IdentityGroupModel> {
id: 'mock-group-id-1', name: 'Mock Group 1', path: '/mock', subGroups: []
};
Injectable({ providedIn: 'root' });
export class IdentityGroupServiceMock implements IdentityGroupServiceInterface {

export const mockIdentityGroup2 = <IdentityGroupModel> {
id: 'mock-group-id-2', name: 'Mock Group 2', path: '', subGroups: []
};
getGroups(): Observable<IdentityGroupModel[]> {
return of(mockIdentityGroups);
}

export const mockIdentityGroup3 = <IdentityGroupModel> {
id: 'mock-group-id-3', name: 'Mock Group 3', path: '', subGroups: []
};
getAvailableRoles(_groupId: string): Observable<IdentityRoleModel[]> {
return of(mockIdentityRoles);
}

export const mockIdentityGroup4 = <IdentityGroupModel> {
id: 'mock-group-id-4', name: 'Mock Group 4', path: '', subGroups: []
};
getAssignedRoles(_groupId: string): Observable<IdentityRoleModel[]> {
return of(mockIdentityRoles);
}

export const mockIdentityGroup5 = <IdentityGroupModel> {
id: 'mock-group-id-5', name: 'Mock Group 5', path: '', subGroups: []
};
assignRoles(_groupId: string, _roles: IdentityRoleModel[]): Observable<any> {
return of();
}

export const mockIdentityGroupsCount = <IdentityGroupCountModel> { count: 10 };
removeRoles(_groupId: string, _roles: IdentityRoleModel[]): Observable<any> {
return of();
}

export const mockIdentityGroups = [
mockIdentityGroup1, mockIdentityGroup2, mockIdentityGroup3, mockIdentityGroup4, mockIdentityGroup5
];
getEffectiveRoles(_groupId: string): Observable<IdentityRoleModel[]> {
return of(mockIdentityRoles);
}

export const mockApplicationDetails = {id: 'mock-app-id', name: 'mock-app-name'};
queryGroups(_requestQuery: IdentityGroupQueryCloudRequestModel): Observable<IdentityGroupQueryResponse> {
return of();
}

export const roleMappingMock = [
{ id: 'role-id-1', name: 'role-name-1' }, { id: 'role-id-2', name: 'role-name-2' }
];
getTotalGroupsCount(): Observable<IdentityGroupCountModel> {
return of(mockIdentityGroupsCount);
}

export const roleMappingApi: any = {
oauth2Auth: {
callCustomApi: () => {
return Promise.resolve(roleMappingMock);
}
createGroup(_newGroup: IdentityGroupModel): Observable<any> {
return of();
}
};

export const noRoleMappingApi: any = {
oauth2Auth: {
callCustomApi: () => {
return Promise.resolve([]);
}
updateGroup(_groupId: string, _updatedGroup: IdentityGroupModel): Observable<any> {
return of();
}
};

export const groupsMockApi: any = {
oauth2Auth: {
callCustomApi: () => {
return Promise.resolve(mockIdentityGroups);
}
deleteGroup(_groupId: string): Observable<any> {
return of();
}
};

export const createGroupMappingApi: any = {
oauth2Auth: {
callCustomApi: () => {
return Promise.resolve();
findGroupsByName(searchParams: IdentityGroupSearchParam): Observable<IdentityGroupModel[]> {
if (searchParams.name === '') {
return of([]);
}

return of(mockIdentityGroups.filter(group =>
group.name.toUpperCase().includes(searchParams.name.toUpperCase())
));
}
};

export const updateGroupMappingApi: any = {
oauth2Auth: {
callCustomApi: () => {
return Promise.resolve();
}
getGroupRoles(_groupId: string): Observable<IdentityRoleModel[]> {
return of(mockIdentityRoles);
}
};

export const deleteGroupMappingApi: any = {
oauth2Auth: {
callCustomApi: () => {
return Promise.resolve();
}
checkGroupHasRole(groupId: string, roleNames: string[]): Observable<boolean> {
return this.getGroupRoles(groupId).pipe(map((groupRoles) => {
let hasRole = false;
if (groupRoles?.length > 0) {
roleNames.forEach((roleName: string) => {
const role = groupRoles.find(({ name }) => roleName === name);
if (role) {
hasRole = true;
return;
}
});
}
return hasRole;
}));
}
};

export const applicationDetailsMockApi: any = {
oauth2Auth: {
callCustomApi: () => {
return Promise.resolve([mockApplicationDetails]);
getClientIdByApplicationName(_applicationName: string): Observable<string> {
return of('fake-client-id');
}

getClientRoles(groupId: string, _clientId: string): Observable<IdentityRoleModel[]> {
if (['mock-group-id-1', 'mock-group-id-2'].includes(groupId)) {
return of([{ id: 'mock-role-id', name: 'MOCK-ADMIN-ROLE' }]);
}

return of([{ id: 'mock-role-id', name: 'MOCK-USER-ROLE' }]);
}
};

export const mockIdentityRoles = [
new IdentityRoleModel({id: 'mock-role-id', name: 'MOCK-ADMIN-ROLE'}),
new IdentityRoleModel({id: 'mock-role-id', name: 'MOCK-USER-ROLE'}),
new IdentityRoleModel({id: 'mock-role-id', name: 'MOCK-ROLE-1'})
];
checkGroupHasClientApp(groupId: string, clientId: string): Observable<boolean> {
return this.getClientRoles(groupId, clientId).pipe(
map((response) => response && response.length > 0)
);
}

export const clientRoles: IdentityRoleModel[] = [
new IdentityRoleModel({ name: 'MOCK-ADMIN-ROLE' }),
new IdentityRoleModel({ name: 'MOCK-USER-ROLE' })
];
checkGroupHasAnyClientAppRole(groupId: string, clientId: string, roleNames: string[]): Observable<boolean> {
return this.getClientRoles(groupId, clientId).pipe(
map((clientRoles: any[]) => {
let hasRole = false;
if (clientRoles.length > 0) {
roleNames.forEach((roleName) => {
const role = clientRoles.find(({ name }) => name === roleName);

if (role) {
hasRole = true;
return;
}
});
}
return hasRole;
})
);
}
}
54 changes: 54 additions & 0 deletions lib/core/mock/identity-user.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { IdentityUserModel } from '../models/identity-user.model';
import { IdentityRoleModel } from '../models/identity-role.model';

export const mockIdentityUser1: IdentityUserModel = { id: 'mock-user-id-1', username: 'userName1', firstName: 'first-name-1', lastName: 'last-name-1', email: 'abc@xyz.com' };
export const mockIdentityUser2: IdentityUserModel = { id: 'mock-user-id-2', username: 'userName2', firstName: 'first-name-2', lastName: 'last-name-2', email: 'abcd@xyz.com'};
export const mockIdentityUser3: IdentityUserModel = { id: 'mock-user-id-3', username: 'userName3', firstName: 'first-name-3', lastName: 'last-name-3', email: 'abcde@xyz.com' };
export const mockIdentityUser4: IdentityUserModel = { id: 'mock-user-id-4', username: 'userName4', firstName: 'first-name-4', lastName: 'last-name-4', email: 'abcde@xyz.com' };
export let mockIdentityUser5: IdentityUserModel = { id: 'mock-user-id-5', username: 'userName5', firstName: 'first-name-5', lastName: 'last-name-5', email: 'abcde@xyz.com' };

export const mockIdentityUsers: IdentityUserModel[] = [
mockIdentityUser1,
mockIdentityUser2,
mockIdentityUser3,
mockIdentityUser4,
mockIdentityUser5
];

export const mockIdentityRole = new IdentityRoleModel({ id: 'id-1', name: 'MOCK-ADMIN-ROLE'});

export const mockAvailableRoles = [
new IdentityRoleModel({ id: 'mock-role-id-1', name: 'MOCK-ADMIN-ROLE'}),
new IdentityRoleModel({ id: 'mock-role-id-2', name: 'MOCK-USER-ROLE'}),
new IdentityRoleModel({ id: 'mock-role-id-3', name: 'MOCK_MODELER-ROLE' }),
new IdentityRoleModel({ id: 'mock-role-id-5', name: 'MOCK-ROLE-2'})
];

export const mockAssignedRoles = [
new IdentityRoleModel({ id: 'mock-role-id-1', name: 'MOCK-ADMIN-ROLE'}),
new IdentityRoleModel({ id: 'mock-role-id-2', name: 'MOCK_MODELER-ROLE' }),
new IdentityRoleModel({ id: 'mock-role-id-3', name: 'MOCK-ROLE-1' })
];

export const mockEffectiveRoles = [
new IdentityRoleModel({id: 'mock-role-id-1', name: 'MOCK-ACTIVE-ADMIN-ROLE'}),
new IdentityRoleModel({id: 'mock-role-id-2', name: 'MOCK-ACTIVE-USER-ROLE'}),
new IdentityRoleModel({id: 'mock-role-id-3', name: 'MOCK-ROLE-1'})
];

0 comments on commit a40de80

Please sign in to comment.