3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 import userEvent from '@testing-library/user-event';
21 import React from 'react';
22 import { byRole, byTestId, byText } from '~sonar-aligned/helpers/testSelector';
23 import SettingsServiceMock from '../../../../../api/mocks/SettingsServiceMock';
24 import { AvailableFeaturesContext } from '../../../../../app/components/available-features/AvailableFeaturesContext';
25 import { definitions } from '../../../../../helpers/mocks/definitions-list';
26 import { renderComponent } from '../../../../../helpers/testReactTestingUtils';
27 import { AlmKeys } from '../../../../../types/alm-settings';
28 import { Feature } from '../../../../../types/features';
29 import Authentication from '../Authentication';
31 let settingsHandler: SettingsServiceMock;
34 settingsHandler = new SettingsServiceMock();
35 settingsHandler.setDefinitions(definitions);
39 settingsHandler.reset();
42 const enabledDefinition = byTestId('sonar.auth.bitbucket.enabled');
43 const consumerKeyDefinition = byTestId('sonar.auth.bitbucket.clientId.secured');
44 const consumerSecretDefinition = byTestId('sonar.auth.bitbucket.clientSecret.secured');
45 const allowUsersToSignUpDefinition = byTestId('sonar.auth.bitbucket.allowUsersToSignUp');
46 const workspacesDefinition = byTestId('sonar.auth.bitbucket.workspaces');
49 save: byRole('button', { name: 'save' }),
50 cancel: byRole('button', { name: 'cancel' }),
51 reset: byRole('button', { name: /settings.definition.reset/ }),
52 confirmReset: byRole('dialog').byRole('button', { name: 'reset_verb' }),
53 change: byRole('button', { name: 'change_verb' }),
55 enabled: enabledDefinition.byRole('switch'),
56 consumerKeyDefinition,
57 consumerKey: consumerKeyDefinition.byRole('textbox'),
58 consumerSecretDefinition,
59 consumerSecret: consumerSecretDefinition.byRole('textbox'),
60 allowUsersToSignUpDefinition,
61 allowUsersToSignUp: allowUsersToSignUpDefinition.byRole('switch'),
63 workspaces: workspacesDefinition.byRole('textbox'),
64 workspacesDelete: workspacesDefinition.byRole('button', {
65 name: /settings.definition.delete_value/,
67 insecureWarning: byText(/settings.authentication.gitlab.configuration.insecure/),
70 it('should show warning if sign up is enabled and there are no workspaces', async () => {
71 renderAuthentication();
72 const user = userEvent.setup();
74 expect(await ui.allowUsersToSignUpDefinition.find()).toBeInTheDocument();
75 expect(ui.allowUsersToSignUp.get()).toBeChecked();
76 expect(ui.workspaces.get()).toHaveValue('');
77 expect(ui.insecureWarning.get()).toBeInTheDocument();
79 await user.click(ui.allowUsersToSignUp.get());
80 await user.click(ui.allowUsersToSignUpDefinition.by(ui.save).get());
81 expect(ui.allowUsersToSignUp.get()).not.toBeChecked();
82 expect(ui.insecureWarning.query()).not.toBeInTheDocument();
84 await user.click(ui.allowUsersToSignUp.get());
85 await user.click(ui.allowUsersToSignUpDefinition.by(ui.save).get());
86 expect(ui.allowUsersToSignUp.get()).toBeChecked();
87 expect(await ui.insecureWarning.find()).toBeInTheDocument();
89 await user.type(ui.workspaces.get(), 'test');
90 await user.click(ui.workspacesDefinition.by(ui.save).get());
91 expect(ui.insecureWarning.query()).not.toBeInTheDocument();
93 await user.click(ui.workspacesDefinition.by(ui.reset).get());
94 await user.click(ui.confirmReset.get());
95 expect(await ui.insecureWarning.find()).toBeInTheDocument();
98 function renderAuthentication(features: Feature[] = []) {
100 <AvailableFeaturesContext.Provider value={features}>
101 <Authentication definitions={definitions} />
102 </AvailableFeaturesContext.Provider>,
103 `?tab=${AlmKeys.BitbucketServer}`,