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 userEvent from '@testing-library/user-event';
21 import React from 'react';
22 import { byRole } from 'testing-library-selector';
23 import PermissionsServiceMock from '../../../../api/mocks/PermissionsServiceMock';
24 import { mockAppState } from '../../../../helpers/testMocks';
25 import { renderApp } from '../../../../helpers/testReactTestingUtils';
26 import { ComponentQualifier } from '../../../../types/component';
27 import { Permissions } from '../../../../types/permissions';
28 import PermissionTemplatesApp from '../PermissionTemplatesApp';
30 const serviceMock = new PermissionsServiceMock();
37 templateLink1: byRole('link', { name: 'Permission Template 1' }),
38 permissionCheckbox: (target: string, permission: Permissions) =>
40 name: `permission.assign_x_to_y.projects_role.${permission}.${target}`,
42 showMoreButton: byRole('button', { name: 'show_more' }),
45 it('grants/revokes permission from users or groups', async () => {
46 const user = userEvent.setup();
47 renderPermissionTemplatesApp();
49 await user.click(await ui.templateLink1.find());
52 expect(ui.permissionCheckbox('Admin Admin', Permissions.Browse).get()).not.toBeChecked();
53 await user.click(ui.permissionCheckbox('Admin Admin', Permissions.Browse).get());
54 expect(ui.permissionCheckbox('Admin Admin', Permissions.Browse).get()).toBeChecked();
56 expect(ui.permissionCheckbox('Admin Admin', Permissions.Admin).get()).toBeChecked();
57 await user.click(ui.permissionCheckbox('Admin Admin', Permissions.Admin).get());
58 expect(ui.permissionCheckbox('Admin Admin', Permissions.Admin).get()).not.toBeChecked();
61 expect(ui.permissionCheckbox('Anyone', Permissions.Browse).get()).not.toBeChecked();
62 await user.click(ui.permissionCheckbox('Anyone', Permissions.Browse).get());
63 expect(ui.permissionCheckbox('Anyone', Permissions.Browse).get()).toBeChecked();
65 expect(ui.permissionCheckbox('Anyone', Permissions.CodeViewer).get()).toBeChecked();
66 await user.click(ui.permissionCheckbox('Anyone', Permissions.CodeViewer).get());
67 expect(ui.permissionCheckbox('Anyone', Permissions.CodeViewer).get()).not.toBeChecked();
69 // Handles error on permission change
70 serviceMock.setIsAllowedToChangePermissions(false);
71 await user.click(ui.permissionCheckbox('Admin Admin', Permissions.Browse).get());
72 expect(ui.permissionCheckbox('Admin Admin', Permissions.Browse).get()).toBeChecked();
74 await user.click(ui.permissionCheckbox('Anyone', Permissions.CodeViewer).get());
75 expect(ui.permissionCheckbox('Anyone', Permissions.CodeViewer).get()).not.toBeChecked();
77 await user.click(ui.permissionCheckbox('Admin Admin', Permissions.Admin).get());
78 expect(ui.permissionCheckbox('Admin Admin', Permissions.Admin).get()).not.toBeChecked();
81 it('loads more items on Show More', async () => {
82 const user = userEvent.setup();
83 renderPermissionTemplatesApp();
85 await user.click(await ui.templateLink1.find());
87 expect(ui.permissionCheckbox('White', Permissions.Browse).query()).not.toBeInTheDocument();
88 await user.click(ui.showMoreButton.get());
89 expect(ui.permissionCheckbox('White', Permissions.Browse).get()).toBeInTheDocument();
92 function renderPermissionTemplatesApp() {
93 renderApp('admin/permission_templates', <PermissionTemplatesApp />, {
94 appState: mockAppState({ qualifiers: [ComponentQualifier.Project] }),