Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

AuthenticationServiceMock.ts 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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 { mockTask } from '../../helpers/mocks/tasks';
  22. import { GitHubConfigurationStatus, GitHubProvisioningStatus } from '../../types/provisioning';
  23. import { Task, TaskStatuses, TaskTypes } from '../../types/tasks';
  24. import {
  25. activateGithubProvisioning,
  26. activateScim,
  27. checkConfigurationValidity,
  28. deactivateGithubProvisioning,
  29. deactivateScim,
  30. fetchGithubProvisioningStatus,
  31. fetchIsScimEnabled,
  32. } from '../provisioning';
  33. jest.mock('../provisioning');
  34. const defaultConfigurationStatus: GitHubConfigurationStatus = {
  35. application: {
  36. jit: {
  37. status: GitHubProvisioningStatus.Success,
  38. },
  39. autoProvisioning: {
  40. status: GitHubProvisioningStatus.Success,
  41. },
  42. },
  43. installations: [
  44. {
  45. organization: 'testOrg',
  46. autoProvisioning: {
  47. status: GitHubProvisioningStatus.Success,
  48. },
  49. jit: {
  50. status: GitHubProvisioningStatus.Success,
  51. },
  52. },
  53. ],
  54. };
  55. export default class AuthenticationServiceMock {
  56. scimStatus: boolean;
  57. githubProvisioningStatus: boolean;
  58. githubConfigurationStatus: GitHubConfigurationStatus;
  59. tasks: Task[];
  60. constructor() {
  61. this.scimStatus = false;
  62. this.githubProvisioningStatus = false;
  63. this.githubConfigurationStatus = cloneDeep(defaultConfigurationStatus);
  64. this.tasks = [];
  65. jest.mocked(activateScim).mockImplementation(this.handleActivateScim);
  66. jest.mocked(deactivateScim).mockImplementation(this.handleDeactivateScim);
  67. jest.mocked(fetchIsScimEnabled).mockImplementation(this.handleFetchIsScimEnabled);
  68. jest
  69. .mocked(activateGithubProvisioning)
  70. .mockImplementation(this.handleActivateGithubProvisioning);
  71. jest
  72. .mocked(deactivateGithubProvisioning)
  73. .mockImplementation(this.handleDeactivateGithubProvisioning);
  74. jest
  75. .mocked(fetchGithubProvisioningStatus)
  76. .mockImplementation(this.handleFetchGithubProvisioningStatus);
  77. jest
  78. .mocked(checkConfigurationValidity)
  79. .mockImplementation(this.handleCheckConfigurationValidity);
  80. }
  81. addProvisioningTask = (overrides: Partial<Omit<Task, 'type'>> = {}) => {
  82. this.tasks.push(
  83. mockTask({
  84. id: Math.random().toString(),
  85. type: TaskTypes.GithubProvisioning,
  86. ...overrides,
  87. })
  88. );
  89. };
  90. setConfigurationValidity = (overrides: Partial<GitHubConfigurationStatus> = {}) => {
  91. this.githubConfigurationStatus = {
  92. ...this.githubConfigurationStatus,
  93. ...overrides,
  94. };
  95. };
  96. enableGithubProvisioning = () => {
  97. this.scimStatus = false;
  98. this.githubProvisioningStatus = true;
  99. };
  100. handleActivateScim = () => {
  101. this.scimStatus = true;
  102. return Promise.resolve();
  103. };
  104. handleDeactivateScim = () => {
  105. this.scimStatus = false;
  106. return Promise.resolve();
  107. };
  108. handleFetchIsScimEnabled = () => {
  109. return Promise.resolve(this.scimStatus);
  110. };
  111. handleActivateGithubProvisioning = () => {
  112. this.githubProvisioningStatus = true;
  113. return Promise.resolve();
  114. };
  115. handleDeactivateGithubProvisioning = () => {
  116. this.githubProvisioningStatus = false;
  117. return Promise.resolve();
  118. };
  119. handleFetchGithubProvisioningStatus = () => {
  120. if (!this.githubProvisioningStatus) {
  121. return Promise.resolve({ enabled: false });
  122. }
  123. const nextSync = this.tasks.find((t: Task) =>
  124. [TaskStatuses.InProgress, TaskStatuses.Pending].includes(t.status)
  125. );
  126. const lastSync = this.tasks.find(
  127. (t: Task) => ![TaskStatuses.InProgress, TaskStatuses.Pending].includes(t.status)
  128. );
  129. return Promise.resolve({
  130. enabled: true,
  131. nextSync: nextSync ? { status: nextSync.status } : undefined,
  132. lastSync: lastSync
  133. ? {
  134. status: lastSync.status,
  135. finishedAt: lastSync.executedAt,
  136. startedAt: lastSync.startedAt,
  137. executionTimeMs: lastSync.executionTimeMs,
  138. summary: lastSync.status === TaskStatuses.Success ? 'Test summary' : undefined,
  139. errorMessage: lastSync.errorMessage,
  140. warningMessage: lastSync.warnings?.join() ?? undefined,
  141. }
  142. : undefined,
  143. });
  144. };
  145. handleCheckConfigurationValidity = () => {
  146. return Promise.resolve(this.githubConfigurationStatus);
  147. };
  148. reset = () => {
  149. this.scimStatus = false;
  150. this.githubProvisioningStatus = false;
  151. this.githubConfigurationStatus = cloneDeep(defaultConfigurationStatus);
  152. this.tasks = [];
  153. };
  154. }