You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GroupsServiceMock.ts 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { cloneDeep } from 'lodash';
  21. import { mockGroup, mockIdentityProvider } from '../../helpers/testMocks';
  22. import { Group, IdentityProvider, Paging, Provider } from '../../types/types';
  23. import { createGroup, deleteGroup, getUsersGroups, updateGroup } from '../user_groups';
  24. jest.mock('../user_groups');
  25. export default class GroupsServiceMock {
  26. provider: Provider | undefined;
  27. groups: Group[];
  28. readOnlyGroups = [
  29. mockGroup({ name: 'managed-group', managed: true, id: '1' }),
  30. mockGroup({ name: 'local-group', managed: false, id: '2' }),
  31. ];
  32. constructor() {
  33. this.groups = cloneDeep(this.readOnlyGroups);
  34. jest.mocked(getUsersGroups).mockImplementation((p) => this.handleSearchUsersGroups(p));
  35. jest.mocked(createGroup).mockImplementation((g) => this.handleCreateGroup(g));
  36. jest.mocked(deleteGroup).mockImplementation((id) => this.handleDeleteGroup(id));
  37. jest.mocked(updateGroup).mockImplementation((id, data) => this.handleUpdateGroup(id, data));
  38. }
  39. reset() {
  40. this.groups = cloneDeep(this.readOnlyGroups);
  41. }
  42. handleCreateGroup = (group: { name: string; description?: string }): Promise<Group> => {
  43. const newGroup = mockGroup(group);
  44. this.groups.push(newGroup);
  45. return this.reply(newGroup);
  46. };
  47. handleDeleteGroup: typeof deleteGroup = (id: string) => {
  48. if (!this.groups.some((g) => g.id === id)) {
  49. return Promise.reject();
  50. }
  51. const groupToDelete = this.groups.find((g) => g.id === id);
  52. if (groupToDelete?.managed) {
  53. return Promise.reject();
  54. }
  55. this.groups = this.groups.filter((g) => g.id !== id);
  56. return this.reply(undefined);
  57. };
  58. handleUpdateGroup: typeof updateGroup = (id, data): Promise<Record<string, never>> => {
  59. const group = this.groups.find((g) => g.id === id);
  60. if (group === undefined) {
  61. return Promise.reject();
  62. }
  63. if (data.description !== undefined) {
  64. group.description = data.description;
  65. }
  66. if (data.name !== undefined) {
  67. group.name = data.name;
  68. }
  69. return this.reply({});
  70. };
  71. handleSearchUsersGroups = (
  72. params: Parameters<typeof getUsersGroups>[0],
  73. ): Promise<{ groups: Group[]; page: Paging }> => {
  74. const pageIndex = params.pageIndex ?? 1;
  75. const pageSize = params.pageSize ?? 10;
  76. const groups = this.groups
  77. .filter((g) => !params.q || g.name.includes(params.q))
  78. .filter((g) => params.managed === undefined || g.managed === params.managed);
  79. return this.reply({
  80. page: {
  81. pageIndex,
  82. pageSize,
  83. total: groups.length,
  84. },
  85. groups: groups.slice((pageIndex - 1) * pageSize, pageIndex * pageSize),
  86. });
  87. };
  88. handleGetIdentityProviders = (): Promise<{ identityProviders: IdentityProvider[] }> => {
  89. return this.reply({ identityProviders: [mockIdentityProvider()] });
  90. };
  91. reply<T>(response: T): Promise<T> {
  92. return Promise.resolve(cloneDeep(response));
  93. }
  94. }