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 package org.sonar.server.permission.ws.template;
22 import java.util.List;
23 import javax.annotation.Nullable;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.sonar.api.resources.Qualifiers;
27 import org.sonar.api.resources.ResourceTypes;
28 import org.sonar.api.server.ws.Change;
29 import org.sonar.api.server.ws.WebService.Action;
30 import org.sonar.core.permission.GlobalPermissions;
31 import org.sonar.db.component.ResourceTypesRule;
32 import org.sonar.db.permission.PermissionQuery;
33 import org.sonar.db.permission.template.PermissionTemplateDto;
34 import org.sonar.db.user.GroupDto;
35 import org.sonar.server.exceptions.BadRequestException;
36 import org.sonar.server.exceptions.ForbiddenException;
37 import org.sonar.server.exceptions.NotFoundException;
38 import org.sonar.server.exceptions.UnauthorizedException;
39 import org.sonar.server.permission.PermissionService;
40 import org.sonar.server.permission.PermissionServiceImpl;
41 import org.sonar.server.permission.ws.BasePermissionWsTest;
42 import org.sonar.server.permission.ws.WsParameters;
43 import org.sonar.server.ws.TestRequest;
45 import static org.assertj.core.api.Assertions.assertThat;
46 import static org.assertj.core.api.Assertions.assertThatThrownBy;
47 import static org.assertj.core.api.Assertions.tuple;
48 import static org.sonar.api.security.DefaultGroups.ANYONE;
49 import static org.sonar.api.web.UserRole.CODEVIEWER;
50 import static org.sonar.db.permission.GlobalPermission.SCAN;
51 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_NAME;
52 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
53 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
54 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
56 public class RemoveGroupFromTemplateActionTest extends BasePermissionWsTest<RemoveGroupFromTemplateAction> {
58 private static final String PERMISSION = CODEVIEWER;
60 private GroupDto group;
61 private PermissionTemplateDto template;
62 private ResourceTypes resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT);
63 private PermissionService permissionService = new PermissionServiceImpl(resourceTypes);
64 private WsParameters wsParameters = new WsParameters(permissionService);
67 protected RemoveGroupFromTemplateAction buildWsAction() {
68 return new RemoveGroupFromTemplateAction(db.getDbClient(), newPermissionWsSupport(), userSession, wsParameters);
75 group = db.users().insertGroup("group-name");
76 template = db.permissionTemplates().insertTemplate();
77 addGroupToTemplate(template, group.getUuid(), PERMISSION, group.getName());
81 public void verify_definition() {
82 Action wsDef = wsTester.getDef();
84 assertThat(wsDef.isInternal()).isFalse();
85 assertThat(wsDef.since()).isEqualTo("5.2");
86 assertThat(wsDef.isPost()).isTrue();
87 assertThat(wsDef.changelog()).extracting(Change::getVersion, Change::getDescription).containsOnly(
88 tuple("10.0", "Parameter 'groupId' is removed. Use 'groupName' instead."),
89 tuple("8.4", "Parameter 'groupId' is deprecated. Format changes from integer to string. Use 'groupName' instead."));
93 public void remove_group_from_template() {
94 newRequest(group.getName(), template.getUuid(), PERMISSION);
96 assertThat(getGroupNamesInTemplateAndPermission(template, PERMISSION)).isEmpty();
100 public void remove_group_from_template_by_name_case_insensitive() {
102 .setParam(PARAM_GROUP_NAME, group.getName())
103 .setParam(PARAM_PERMISSION, PERMISSION)
104 .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
107 assertThat(getGroupNamesInTemplateAndPermission(template, PERMISSION)).isEmpty();
111 public void remove_group_twice_without_error() {
112 newRequest(group.getName(), template.getUuid(), PERMISSION);
113 newRequest(group.getName(), template.getUuid(), PERMISSION);
115 assertThat(getGroupNamesInTemplateAndPermission(template, PERMISSION)).isEmpty();
119 public void remove_anyone_group_from_template() {
120 addGroupToTemplate(template, null, PERMISSION, null);
122 newRequest(ANYONE, template.getUuid(), PERMISSION);
124 assertThat(getGroupNamesInTemplateAndPermission(template, PERMISSION)).containsExactly(group.getName());
128 public void fail_if_not_a_project_permission() {
129 assertThatThrownBy(() -> newRequest(group.getName(), template.getUuid(), GlobalPermissions.PROVISIONING))
130 .isInstanceOf(IllegalArgumentException.class);
134 public void fail_if_insufficient_privileges() {
135 userSession.logIn().addPermission(SCAN);
137 assertThatThrownBy(() -> newRequest(group.getName(), template.getUuid(), PERMISSION))
138 .isInstanceOf(ForbiddenException.class);
142 public void fail_if_not_logged_in() {
143 assertThatThrownBy(() -> {
144 userSession.anonymous();
145 newRequest(group.getName(), template.getUuid(), PERMISSION);
147 .isInstanceOf(UnauthorizedException.class);
151 public void fail_if_group_params_missing() {
152 assertThatThrownBy(() -> {
153 newRequest(null, template.getUuid(), PERMISSION);
155 .isInstanceOf(IllegalArgumentException.class)
156 .hasMessage("The 'groupName' parameter is missing");
160 public void fail_if_permission_missing() {
161 assertThatThrownBy(() -> {
162 newRequest(group.getName(), template.getUuid(), null);
164 .isInstanceOf(IllegalArgumentException.class);
168 public void fail_if_template_missing() {
169 assertThatThrownBy(() -> {
170 newRequest(group.getName(), null, PERMISSION);
172 .isInstanceOf(BadRequestException.class);
176 public void fail_if_group_does_not_exist() {
177 assertThatThrownBy(() -> {
178 newRequest("unknown-group-name", template.getUuid(), PERMISSION);
180 .isInstanceOf(NotFoundException.class)
181 .hasMessage("No group with name 'unknown-group-name'");
185 public void fail_if_template_key_does_not_exist() {
186 assertThatThrownBy(() -> {
187 newRequest(group.getName(), "unknown-key", PERMISSION);
189 .isInstanceOf(NotFoundException.class)
190 .hasMessage("Permission template with id 'unknown-key' is not found");
193 private void newRequest(@Nullable String groupName, @Nullable String templateKey, @Nullable String permission) {
194 TestRequest request = newRequest();
195 if (groupName != null) {
196 request.setParam(PARAM_GROUP_NAME, groupName);
198 if (templateKey != null) {
199 request.setParam(PARAM_TEMPLATE_ID, templateKey);
201 if (permission != null) {
202 request.setParam(PARAM_PERMISSION, permission);
208 private void addGroupToTemplate(PermissionTemplateDto template, @Nullable String groupUuid, String permission, String groupName) {
209 db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), template.getUuid(), groupUuid,
210 permission, template.getName(), groupName);
214 private List<String> getGroupNamesInTemplateAndPermission(PermissionTemplateDto template, String permission) {
215 PermissionQuery permissionQuery = PermissionQuery.builder().setPermission(permission).build();
216 return db.getDbClient().permissionTemplateDao()
217 .selectGroupNamesByQueryAndTemplate(db.getSession(), permissionQuery, template.getUuid());