[AAE-6389] Remove duplicated identity user service mock (#7373)

* [AAE-6389] removed duplicate services and refactored existing

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

* [AAE-6389] fixed circular dependency
This commit is contained in:
tomgny
2021-11-24 11:18:20 +01:00
committed by GitHub
parent 5618ed7000
commit a40de80658
14 changed files with 672 additions and 652 deletions

View File

@@ -16,10 +16,9 @@
*/
import { Meta, moduleMetadata, Story } from '@storybook/angular';
import { IdentityGroupService, mockIdentityGroups } from '@alfresco/adf-core';
import { IdentityGroupService, mockIdentityGroups, IdentityGroupServiceMock } from '@alfresco/adf-core';
import { GroupCloudModule } from '../group-cloud.module';
import { GroupCloudComponent } from './group-cloud.component';
import { IdentityGroupServiceMock } from '../mock/identity-group.service.mock';
import { ProcessServicesCloudStoryModule } from '../../testing/process-services-cloud-story.module';
export default {

View File

@@ -1,146 +0,0 @@
/*!
* @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 { Injectable } from '@angular/core';
import {
IdentityGroupCountModel,
IdentityGroupModel,
IdentityGroupQueryCloudRequestModel,
IdentityGroupQueryResponse,
IdentityGroupSearchParam,
IdentityRoleModel,
mockIdentityGroups,
mockIdentityRoles,
IdentityGroupServiceInterface
} from '@alfresco/adf-core';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
Injectable({ providedIn: 'root' });
export class IdentityGroupServiceMock implements IdentityGroupServiceInterface {
getGroups(): Observable<IdentityGroupModel[]> {
return of(mockIdentityGroups);
}
getAvailableRoles(_groupId: string): Observable<IdentityRoleModel[]> {
return of(mockIdentityRoles);
}
getAssignedRoles(_groupId: string): Observable<IdentityRoleModel[]> {
return of(mockIdentityRoles);
}
assignRoles(_groupId: string, _roles: IdentityRoleModel[]): Observable<any> {
return of();
}
removeRoles(_groupId: string, _roles: IdentityRoleModel[]): Observable<any> {
return of();
}
getEffectiveRoles(_groupId: string): Observable<IdentityRoleModel[]> {
return of(mockIdentityRoles);
}
queryGroups(_requestQuery: IdentityGroupQueryCloudRequestModel): Observable<IdentityGroupQueryResponse> {
return of();
}
getTotalGroupsCount(): Observable<IdentityGroupCountModel> {
return of({ count: mockIdentityGroups.length });
}
createGroup(_newGroup: IdentityGroupModel): Observable<any> {
return of();
}
updateGroup(_groupId: string, _updatedGroup: IdentityGroupModel): Observable<any> {
return of();
}
deleteGroup(_groupId: string): Observable<any> {
return of();
}
findGroupsByName(searchParams: IdentityGroupSearchParam): Observable<IdentityGroupModel[]> {
if (searchParams.name === '') {
return of([]);
}
return of(mockIdentityGroups.filter(group =>
group.name.toUpperCase().includes(searchParams.name.toUpperCase())
));
}
getGroupRoles(_groupId: string): Observable<IdentityRoleModel[]> {
return of(mockIdentityRoles);
}
checkGroupHasRole(groupId: string, roleNames: string[]): Observable<boolean> {
return this.getGroupRoles(groupId).pipe(map((groupRoles) => {
let hasRole = false;
if (groupRoles && groupRoles.length > 0) {
roleNames.forEach((roleName: string) => {
const role = groupRoles.find(({ name }) => roleName === name);
if (role) {
hasRole = true;
return;
}
});
}
return hasRole;
}));
}
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' }]);
}
checkGroupHasClientApp(groupId: string, clientId: string): Observable<boolean> {
return this.getClientRoles(groupId, clientId).pipe(
map((response) => response && response.length > 0)
);
}
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;
})
);
}
}

View File

@@ -16,11 +16,9 @@
*/
import { Meta, moduleMetadata, Story } from '@storybook/angular';
import { IdentityUserService } from '@alfresco/adf-core';
import { IdentityUserService, IdentityUserServiceMock, mockIdentityUsers } from '@alfresco/adf-core';
import { PeopleCloudComponent } from './people-cloud.component';
import { PeopleCloudModule } from '../people-cloud.module';
import { IdentityUserServiceMock } from '../mock/identity-user.service.mock';
import { mockUsers } from '../mock/user-cloud.mock';
import { ProcessServicesCloudStoryModule } from '../../testing/process-services-cloud-story.module';
export default {
@@ -73,7 +71,7 @@ validPreselectedUsers.args = {
...primary.args,
validate: true,
mode: 'multiple',
preSelectUsers: mockUsers
preSelectUsers: mockIdentityUsers
};
export const mandatoryPreselectedUsers = template.bind({});
@@ -81,8 +79,8 @@ mandatoryPreselectedUsers.args = {
...primary.args,
validate: true,
mode: 'multiple',
preSelectUsers: [{ id: 'fake-id-1', username: 'first-name-1 last-name-1', firstName: 'first-name-1', lastName: 'last-name-1', email: 'abc@xyz.com', readonly: true },
{ id: 'fake-id-2', username: 'first-name-2 last-name-2', firstName: 'first-name-2', lastName: 'last-name-2', email: 'abcd@xyz.com' }]
preSelectUsers: [{ id: 'mock-user-id-1', username: 'userName1', firstName: 'first-name-1', lastName: 'last-name-1', email: 'abc@xyz.com', readonly: true },
{ id: 'mock-user-id-2', username: 'userName2', firstName: 'first-name-2', lastName: 'last-name-2', email: 'abcd@xyz.com' }]
};
export const invalidPreselectedUsers = template.bind({});
@@ -97,8 +95,8 @@ export const excludedUsers = template.bind({});
excludedUsers.args = {
...primary.args,
excludedUsers: [
{ id: 'fake-id-2' },
{ id: 'fake-id-3' }
{ id: 'mock-user-id-2' },
{ id: 'mock-user-id-3' }
]
};
@@ -111,7 +109,7 @@ adminRoleUser.args = {
export const noUsers = template.bind({});
noUsers.args = {
...primary.args,
excludedUsers: mockUsers
excludedUsers: mockIdentityUsers
};
export const invalidOrEmptyAppName = template.bind({});

View File

@@ -1,265 +0,0 @@
/*!
* @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 { Injectable } from '@angular/core';
import {
IdentityGroupModel,
IdentityRoleModel,
IdentityUserModel,
mockIdentityGroups,
IdentityJoinGroupRequestModel,
IdentityUserServiceInterface,
IdentityUserPasswordModel,
IdentityUserQueryCloudRequestModel,
IdentityUserQueryResponse
} from '@alfresco/adf-core';
import { Observable, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { cloudMockUser, mockRoles, mockUsers } from './user-cloud.mock';
@Injectable({
providedIn: 'root'
})
export class IdentityUserServiceMock implements IdentityUserServiceInterface {
getCurrentUserInfo(): IdentityUserModel {
return cloudMockUser;
}
findUsersByName(search: string): Observable<IdentityUserModel[]> {
if (search === '') {
return of([]);
}
return of(mockUsers.filter(user =>
user.username.toUpperCase().includes(search.toUpperCase())
));
}
findUserByUsername(username: string): Observable<IdentityUserModel[]> {
if (username === '') {
return of([]);
}
return of(mockUsers.filter(user => user.username === username));
}
findUserByEmail(email: string): Observable<IdentityUserModel[]> {
if (email === '') {
return of([]);
}
return of(mockUsers.filter(user => user.email === email));
}
findUserById(id: string): Observable<any> {
if (id === '') {
return of([]);
}
return of(mockUsers.find(user => user.id === id));
}
getClientRoles(userId: string, _clientId: string): Observable<any[]> {
if (userId === 'fake-id-1') {
return of([{ id: 'id-1', name: 'MOCK-ADMIN-ROLE' }]);
}
return of([{ id: 'id-2', name: 'MOCK-USER-ROLE' }]);
}
checkUserHasClientApp(userId: string, clientId: string): Observable<boolean> {
return this.getClientRoles(userId, clientId).pipe(
map((clientRoles) => clientRoles.length > 0)
);
}
checkUserHasAnyClientAppRole(userId: string, clientId: string, roleNames: string[]): Observable<boolean> {
return this.getClientRoles(userId, 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;
})
);
}
getClientIdByApplicationName(_applicationName: string): Observable<string> {
return of('fake-id-1');
}
checkUserHasApplicationAccess(userId: string, applicationName: string): Observable<boolean> {
return this.getClientIdByApplicationName(applicationName).pipe(
switchMap((clientId: string) => {
return this.checkUserHasClientApp(userId, clientId);
})
);
}
checkUserHasAnyApplicationRole(userId: string, applicationName: string, roleNames: string[]): Observable<boolean> {
return this.getClientIdByApplicationName(applicationName).pipe(
switchMap((clientId: string) => {
return this.checkUserHasAnyClientAppRole(userId, clientId, roleNames);
})
);
}
getUsers(): Observable<IdentityUserModel[]> {
return of(mockUsers);
}
getUserRoles(_userId: string): Observable<IdentityRoleModel[]> {
return of(mockRoles);
}
async getUsersByRolesWithCurrentUser(roleNames: string[]): Promise<IdentityUserModel[]> {
const filteredUsers: IdentityUserModel[] = [];
if (roleNames && roleNames.length > 0) {
const users = await this.getUsers().toPromise();
for (let i = 0; i < users.length; i++) {
const hasAnyRole = await this.userHasAnyRole(users[i].id, roleNames);
if (hasAnyRole) {
filteredUsers.push(users[i]);
}
}
}
return filteredUsers;
}
async getUsersByRolesWithoutCurrentUser(roleNames: string[]): Promise<IdentityUserModel[]> {
const filteredUsers: IdentityUserModel[] = [];
if (roleNames && roleNames.length > 0) {
const currentUser = this.getCurrentUserInfo();
let users = await this.getUsers().toPromise();
users = users.filter(({ username }) => username !== currentUser.username);
for (let i = 0; i < users.length; i++) {
const hasAnyRole = await this.userHasAnyRole(users[i].id, roleNames);
if (hasAnyRole) {
filteredUsers.push(users[i]);
}
}
}
return filteredUsers;
}
private async userHasAnyRole(userId: string, roleNames: string[]): Promise<boolean> {
const userRoles = await this.getUserRoles(userId).toPromise();
const hasAnyRole = roleNames.some((roleName) => {
const filteredRoles = userRoles.filter((userRole) => {
return userRole.name.toLocaleLowerCase() === roleName.toLocaleLowerCase();
});
return filteredRoles.length > 0;
});
return hasAnyRole;
}
checkUserHasRole(userId: string, roleNames: string[]): Observable<boolean> {
return this.getUserRoles(userId).pipe(map((userRoles: IdentityRoleModel[]) => {
let hasRole = false;
if (userRoles && userRoles.length > 0) {
roleNames.forEach((roleName: string) => {
const role = userRoles.find(({ name }) => roleName === name);
if (role) {
hasRole = true;
return;
}
});
}
return hasRole;
}));
}
queryUsers(_requestQuery: IdentityUserQueryCloudRequestModel): Observable<IdentityUserQueryResponse> {
return of();
}
getTotalUsersCount(): Observable<number> {
return of(mockUsers.length);
}
createUser(newUser: IdentityUserModel): Observable<any> {
window.alert(`Create new user: ${newUser}`);
return of([]);
}
updateUser(userId: string, updatedUser: IdentityUserModel): Observable<any> {
window.alert(`Update user: ${updatedUser} with ID: ${userId}`);
return of([]);
}
deleteUser(userId: string): Observable<any> {
window.alert(`Delete user with ID: ${userId}`);
return of([]);
}
changePassword(userId: string, newPassword: IdentityUserPasswordModel): Observable<any> {
window.alert(`New password: ${newPassword} for user with ID: ${userId}`);
return of([]);
}
getInvolvedGroups(_userId: string): Observable<IdentityGroupModel[]> {
return of(mockIdentityGroups);
}
joinGroup(joinGroupRequest: IdentityJoinGroupRequestModel): Observable<any> {
window.alert(`Join group request: ${joinGroupRequest}`);
return of([]);
}
leaveGroup(userId: any, groupId: string): Observable<any> {
window.alert(`Leave group: ${groupId} for user with ID: ${userId}`);
return of([]);
}
getAvailableRoles(_userId: string): Observable<IdentityRoleModel[]> {
return of(mockRoles);
}
getAssignedRoles(_userId: string): Observable<IdentityRoleModel[]> {
return of(mockRoles);
}
getEffectiveRoles(_userId: string): Observable<IdentityRoleModel[]> {
return of(mockRoles);
}
assignRoles(userId: string, roles: IdentityRoleModel[]): Observable<any> {
window.alert(`Assign roles: ${roles} for user with ID: ${userId}`);
return of([]);
}
removeRoles(userId: string, removedRoles: IdentityRoleModel[]): Observable<any> {
window.alert(`Remove roles: ${removedRoles} for user with ID: ${userId}`);
return of([]);
}
}