From bcaf069f5bb7ffdc50c885ed810112f5fbe9c1c6 Mon Sep 17 00:00:00 2001 From: Mathieu Suen Date: Tue, 23 May 2023 18:08:03 +0200 Subject: [PATCH] SONAR-19336 Move github organizations setting in the common configuration --- .../js/api/mocks/AuthenticationServiceMock.ts | 16 +++++++++++-- .../authentication/ConfigurationForm.tsx | 9 +++++-- .../GithubAuthenticationTab.tsx | 1 - .../__tests__/Authentication-it.tsx | 24 ++++++++++++++++++- .../hook/useGithubConfiguration.ts | 6 +++-- 5 files changed, 48 insertions(+), 8 deletions(-) diff --git a/server/sonar-web/src/main/js/api/mocks/AuthenticationServiceMock.ts b/server/sonar-web/src/main/js/api/mocks/AuthenticationServiceMock.ts index a248742588c..ec685999c0e 100644 --- a/server/sonar-web/src/main/js/api/mocks/AuthenticationServiceMock.ts +++ b/server/sonar-web/src/main/js/api/mocks/AuthenticationServiceMock.ts @@ -120,7 +120,7 @@ export default class AuthenticationServiceMock { return Promise.resolve(this.settingValues); }; - handleSetValue = (definition: SettingDefinition, value: string | boolean) => { + handleSetValue = (definition: SettingDefinition, value: string | boolean | string[]) => { if (value === 'error') { const res = new Response('', { status: 400, @@ -130,8 +130,19 @@ export default class AuthenticationServiceMock { return Promise.reject(res); } const updatedSettingValue = this.settingValues.find((set) => set.key === definition.key); + if (updatedSettingValue) { - updatedSettingValue.value = String(value); + if (definition.multiValues) { + updatedSettingValue.values = value as string[]; + } else { + updatedSettingValue.value = String(value); + } + } else if (definition.multiValues) { + this.settingValues.push({ + key: definition.key, + values: value as string[], + inherited: false, + }); } else { this.settingValues.push({ key: definition.key, value: String(value), inherited: false }); } @@ -143,6 +154,7 @@ export default class AuthenticationServiceMock { this.settingValues.forEach((set) => { if (data.keys.includes(set.key)) { set.value = ''; + set.values = []; } return set; }); diff --git a/server/sonar-web/src/main/js/apps/settings/components/authentication/ConfigurationForm.tsx b/server/sonar-web/src/main/js/apps/settings/components/authentication/ConfigurationForm.tsx index e07e859adc6..f6888c38ed4 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/authentication/ConfigurationForm.tsx +++ b/server/sonar-web/src/main/js/apps/settings/components/authentication/ConfigurationForm.tsx @@ -17,10 +17,11 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +import { isEmptyArray } from 'formik'; import { isEmpty, keyBy } from 'lodash'; import * as React from 'react'; import { FormattedMessage } from 'react-intl'; -import { setSettingValue } from '../../../../api/settings'; +import { resetSettingValue, setSettingValue } from '../../../../api/settings'; import DocLink from '../../../../components/common/DocLink'; import Modal from '../../../../components/controls/Modal'; import { ResetButtonLink, SubmitButton } from '../../../../components/controls/buttons'; @@ -74,7 +75,11 @@ export default function ConfigurationForm(props: Props) { .filter((v) => v.newValue !== undefined) .map(async ({ key, newValue, definition }) => { try { - await setSettingValue(definition, newValue); + if (isEmptyArray(newValue)) { + await resetSettingValue({ keys: definition.key }); + } else { + await setSettingValue(definition, newValue); + } return { key, success: true }; } catch (error) { return { key, success: false }; diff --git a/server/sonar-web/src/main/js/apps/settings/components/authentication/GithubAuthenticationTab.tsx b/server/sonar-web/src/main/js/apps/settings/components/authentication/GithubAuthenticationTab.tsx index 44f704a755b..5df3bcdc37d 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/authentication/GithubAuthenticationTab.tsx +++ b/server/sonar-web/src/main/js/apps/settings/components/authentication/GithubAuthenticationTab.tsx @@ -46,7 +46,6 @@ const GITHUB_EXCLUDED_FIELD = [ 'sonar.auth.github.enabled', 'sonar.auth.github.groupsSync', 'sonar.auth.github.allowUsersToSignUp', - 'sonar.auth.github.organizations', ]; export default function GithubAuthenticationTab(props: GithubAuthenticationProps) { diff --git a/server/sonar-web/src/main/js/apps/settings/components/authentication/__tests__/Authentication-it.tsx b/server/sonar-web/src/main/js/apps/settings/components/authentication/__tests__/Authentication-it.tsx index 26e4242cd89..883a9111f3f 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/authentication/__tests__/Authentication-it.tsx +++ b/server/sonar-web/src/main/js/apps/settings/components/authentication/__tests__/Authentication-it.tsx @@ -121,6 +121,10 @@ const ui = { enableConfigButton: byRole('button', { name: 'settings.authentication.form.enable' }), disableConfigButton: byRole('button', { name: 'settings.authentication.form.disable' }), editConfigButton: byRole('button', { name: 'settings.authentication.form.edit' }), + deleteOrg: (org: string) => + byRole('button', { + name: `settings.definition.delete_value.property.sonar.auth.github.organizations.name.${org}`, + }), enableFirstMessage: byText('settings.authentication.github.enable_first'), jitProvisioningButton: byRole('radio', { name: 'settings.authentication.form.provisioning_at_login', @@ -135,6 +139,7 @@ const ui = { await user.type(github.clientSecret.get(), 'Client shut'); await user.type(github.githubApiUrl.get(), 'API Url'); await user.type(github.githubWebUrl.get(), 'WEb Url'); + await user.type(github.organizations.get(), 'organization1'); }); }, createConfiguration: async (user: UserEvent) => { @@ -289,6 +294,24 @@ describe('Github tab', () => { expect(await github.editConfigButton.find()).toBeInTheDocument(); }); + it('should be able to edit configuration', async () => { + const { github } = ui; + const user = userEvent.setup(); + renderAuthentication(); + await user.click(await github.tab.find()); + + await github.createConfiguration(user); + + await user.click(github.editConfigButton.get()); + await user.click(github.deleteOrg('organization1').get()); + + await user.click(github.saveConfigButton.get()); + + await user.click(await github.editConfigButton.find()); + + expect(github.organizations.get()).toHaveValue(''); + }); + it('should be able to enable/disable configuration', async () => { const { github } = ui; const user = userEvent.setup(); @@ -336,7 +359,6 @@ describe('Github tab', () => { expect(github.saveGithubProvisioning.get()).toBeDisabled(); await user.click(github.allowUserToSignUp.get()); await user.click(github.syncGroupsAsTeams.get()); - await user.type(github.organizations.get(), 'organization1, organization2'); expect(github.saveGithubProvisioning.get()).toBeEnabled(); await user.click(github.saveGithubProvisioning.get()); diff --git a/server/sonar-web/src/main/js/apps/settings/components/authentication/hook/useGithubConfiguration.ts b/server/sonar-web/src/main/js/apps/settings/components/authentication/hook/useGithubConfiguration.ts index 8c9c255467b..d8efbe74788 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/authentication/hook/useGithubConfiguration.ts +++ b/server/sonar-web/src/main/js/apps/settings/components/authentication/hook/useGithubConfiguration.ts @@ -34,12 +34,14 @@ export const GITHUB_APP_ID_FIELD = 'sonar.auth.github.appId'; export const GITHUB_API_URL_FIELD = 'sonar.auth.github.apiUrl'; export const GITHUB_CLIENT_ID_FIELD = 'sonar.auth.github.clientId.secured'; export const GITHUB_JIT_FIELDS = [ - 'sonar.auth.github.organizations', 'sonar.auth.github.allowUsersToSignUp', 'sonar.auth.github.groupsSync', +]; +export const OPTIONAL_FIELDS = [ + GITHUB_ENABLED_FIELD, + ...GITHUB_JIT_FIELDS, 'sonar.auth.github.organizations', ]; -export const OPTIONAL_FIELDS = [GITHUB_ENABLED_FIELD, ...GITHUB_JIT_FIELDS]; export interface SamlSettingValue { key: string; -- 2.39.5