3 * Copyright (C) 2009-2023 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 { screen } from '@testing-library/react';
21 import userEvent from '@testing-library/user-event';
22 import React from 'react';
23 import AlmSettingsServiceMock from '../../../../../api/mocks/AlmSettingsServiceMock';
24 import { AvailableFeaturesContext } from '../../../../../app/components/available-features/AvailableFeaturesContext';
25 import { renderComponent } from '../../../../../helpers/testReactTestingUtils';
26 import { byRole, byText } from '../../../../../helpers/testSelector';
27 import { AlmKeys } from '../../../../../types/alm-settings';
28 import { Feature } from '../../../../../types/features';
29 import AlmIntegration from '../AlmIntegration';
31 jest.mock('../../../../../api/alm-settings');
33 let almSettings: AlmSettingsServiceMock;
36 almSettings = new AlmSettingsServiceMock();
43 describe('github tab', () => {
44 it('can create/edit/delete new configuration', async () => {
45 const { ui } = getPageObjects();
46 const { rerender } = renderAlmIntegration();
47 expect(await ui.almHeading.find()).toBeInTheDocument();
48 expect(ui.emptyIntro(AlmKeys.GitHub).get()).toBeInTheDocument();
50 // Create new configuration
51 await ui.createConfiguration('Name', {
52 'name.github': 'Name',
53 'url.github': 'https://api.github.com',
54 app_id: 'Github App ID',
55 'client_id.github': 'Github Client ID',
56 'client_secret.github': 'Client Secret',
60 await ui.editConfiguration('New Name', 'Name', 'client_secret.github', AlmKeys.GitHub);
62 await ui.checkConfiguration('New Name');
64 rerender(<AlmIntegration />);
65 expect(await screen.findByRole('heading', { name: 'New Name' })).toBeInTheDocument();
67 await ui.deleteConfiguration('New Name');
68 expect(ui.emptyIntro(AlmKeys.GitHub).get()).toBeInTheDocument();
72 describe.each([AlmKeys.GitLab, AlmKeys.Azure])(
74 (almKey: AlmKeys.Azure | AlmKeys.GitLab) => {
75 it('can create/edit/delete new configuration', async () => {
76 const { ui } = getPageObjects();
78 renderAlmIntegration();
79 expect(await ui.almHeading.find()).toBeInTheDocument();
81 await userEvent.click(ui.tab(almKey).get());
82 expect(ui.emptyIntro(almKey).get()).toBeInTheDocument();
84 // Create new configuration
85 await ui.createConfiguration('Name', {
86 [`name.${almKey}`]: 'Name',
87 [`url.${almKey}`]: 'https://api.alm.com',
88 personal_access_token: 'Access Token',
91 // Cannot create another configuration without Multiple Alm feature
92 expect(ui.createConfigurationButton.get()).toBeDisabled();
94 await ui.editConfiguration('New Name', 'Name', 'personal_access_token', almKey);
96 await ui.checkConfiguration('New Name');
98 await ui.deleteConfiguration('New Name');
99 expect(ui.emptyIntro(almKey).get()).toBeInTheDocument();
104 describe('bitbucket tab', () => {
105 it('can create/edit/delete new configuration', async () => {
106 const { ui } = getPageObjects();
107 renderAlmIntegration([Feature.MultipleAlm]);
108 expect(await ui.almHeading.find()).toBeInTheDocument();
110 await userEvent.click(ui.tab(AlmKeys.BitbucketServer).get());
111 expect(ui.emptyIntro(AlmKeys.BitbucketServer).get()).toBeInTheDocument();
113 // Create new Bitbucket Server configuration
114 await ui.createConfiguration(
117 'name.bitbucket': 'Name',
118 'url.bitbucket': 'https://api.bitbucket.com',
119 personal_access_token: 'Access Token',
121 AlmKeys.BitbucketServer
124 // Create new Bitbucket Cloud configuration
125 await ui.createConfiguration(
128 'name.bitbucket': 'Name Cloud',
129 'workspace.bitbucketcloud': 'workspace',
130 'client_id.bitbucketcloud': 'Client ID',
131 'client_secret.bitbucketcloud': 'Client Secret',
133 AlmKeys.BitbucketCloud
136 // Edit, check delete Bitbucket Server configuration
137 await ui.editConfiguration(
140 'personal_access_token',
141 AlmKeys.BitbucketServer
144 await ui.checkConfiguration('New Name');
146 await ui.deleteConfiguration('New Name');
148 // Cloud configuration still exists
149 expect(screen.getByRole('heading', { name: 'Name Cloud' })).toBeInTheDocument();
153 function getPageObjects() {
154 const user = userEvent.setup();
157 almHeading: byRole('heading', { name: 'settings.almintegration.title' }),
158 emptyIntro: (almKey: AlmKeys) => byText(`settings.almintegration.empty.${almKey}`),
159 createConfigurationButton: byRole('button', { name: 'settings.almintegration.create' }),
160 tab: (almKey: AlmKeys) =>
161 byRole('tab', { name: `${almKey} settings.almintegration.tab.${almKey}` }),
162 bitbucketConfiguration: (almKey: AlmKeys.BitbucketCloud | AlmKeys.BitbucketServer) =>
163 byRole('button', { name: `alm.${almKey}.long` }),
164 configurationInput: (id: string) =>
165 byRole('textbox', { name: `settings.almintegration.form.${id}` }),
166 updateSecretValueButton: (key: string) =>
168 name: `settings.almintegration.form.secret.update_field_x.settings.almintegration.form.${key}`,
170 saveConfigurationButton: byRole('button', { name: 'settings.almintegration.form.save' }),
171 editConfigurationButton: (key: string) =>
172 byRole('button', { name: `settings.almintegration.edit_configuration.${key}` }),
173 deleteConfigurationButton: (key: string) =>
174 byRole('button', { name: `settings.almintegration.delete_configuration.${key}` }),
175 cancelButton: byRole('button', { name: 'cancel' }),
176 confirmDelete: byRole('button', { name: 'delete' }),
177 checkConfigurationButton: (key: string) =>
178 byRole('button', { name: `settings.almintegration.check_configuration_x.${key}` }),
179 validationMessage: (text: string) => byText(text),
182 async function createConfiguration(
184 params: { [key: string]: string },
185 almKey?: AlmKeys.BitbucketCloud | AlmKeys.BitbucketServer
187 await userEvent.click(ui.createConfigurationButton.get());
188 expect(ui.saveConfigurationButton.get()).toBeDisabled();
191 await userEvent.click(ui.bitbucketConfiguration(almKey).get());
194 for (const [key, value] of Object.entries(params)) {
195 // eslint-disable-next-line no-await-in-loop
196 await userEvent.type(ui.configurationInput(key).get(), value);
198 expect(ui.saveConfigurationButton.get()).toBeEnabled();
199 await userEvent.click(ui.saveConfigurationButton.get());
201 // New configuration is created
202 expect(screen.getByRole('heading', { name })).toBeInTheDocument();
205 async function editConfiguration(
211 almSettings.setDefinitionErrorMessage('Something is wrong');
212 await userEvent.click(ui.editConfigurationButton(currentName).get());
213 expect(ui.configurationInput(secretId).query()).not.toBeInTheDocument();
214 await userEvent.click(ui.updateSecretValueButton(secretId).get());
215 await userEvent.type(ui.configurationInput(secretId).get(), 'New Secret Value');
216 await userEvent.clear(ui.configurationInput(`name.${almKey}`).get());
217 await userEvent.type(ui.configurationInput(`name.${almKey}`).get(), newName);
218 await userEvent.click(ui.saveConfigurationButton.get());
220 // Existing configuration is edited
221 expect(screen.queryByRole('heading', { name: currentName })).not.toBeInTheDocument();
222 expect(screen.getByRole('heading', { name: newName })).toBeInTheDocument();
223 expect(ui.validationMessage('Something is wrong').get()).toBeInTheDocument();
226 async function checkConfiguration(name: string) {
227 almSettings.setDefinitionErrorMessage('');
228 await userEvent.click(ui.checkConfigurationButton(name).get());
230 ui.validationMessage('settings.almintegration.configuration_valid').getAll()[0]
231 ).toBeInTheDocument();
234 async function deleteConfiguration(name: string) {
235 await userEvent.click(ui.deleteConfigurationButton(name).get());
236 await userEvent.click(ui.cancelButton.get());
237 expect(screen.getByRole('heading', { name })).toBeInTheDocument();
239 await userEvent.click(ui.deleteConfigurationButton(name).get());
240 await userEvent.click(ui.confirmDelete.get());
241 expect(screen.queryByRole('heading', { name })).not.toBeInTheDocument();
256 function renderAlmIntegration(features: Feature[] = []) {
257 return renderComponent(
258 <AvailableFeaturesContext.Provider value={features}>
260 </AvailableFeaturesContext.Provider>