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.api.web.UserRole;
31 import org.sonar.core.permission.GlobalPermissions;
32 import org.sonar.db.component.ResourceTypesRule;
33 import org.sonar.db.permission.PermissionQuery;
34 import org.sonar.db.permission.template.PermissionTemplateDto;
35 import org.sonar.db.user.GroupDto;
36 import org.sonar.server.exceptions.BadRequestException;
37 import org.sonar.server.exceptions.ForbiddenException;
38 import org.sonar.server.exceptions.NotFoundException;
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.ADMIN;
50 import static org.sonar.api.web.UserRole.CODEVIEWER;
51 import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
52 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_NAME;
53 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
54 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
55 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
57 public class AddGroupToTemplateActionTest extends BasePermissionWsTest<AddGroupToTemplateAction> {
59 private PermissionTemplateDto template;
60 private GroupDto group;
61 private ResourceTypes resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT);
62 private PermissionService permissionService = new PermissionServiceImpl(resourceTypes);
63 private WsParameters wsParameters = new WsParameters(permissionService);
66 protected AddGroupToTemplateAction buildWsAction() {
67 return new AddGroupToTemplateAction(db.getDbClient(), newPermissionWsSupport(), userSession, wsParameters);
72 template = db.permissionTemplates().insertTemplate();
73 group = db.users().insertGroup("group-name");
77 public void verify_definition() {
78 Action wsDef = wsTester.getDef();
80 assertThat(wsDef.isInternal()).isFalse();
81 assertThat(wsDef.since()).isEqualTo("5.2");
82 assertThat(wsDef.isPost()).isTrue();
83 assertThat(wsDef.changelog()).extracting(Change::getVersion, Change::getDescription).containsOnly(
84 tuple("8.4", "Parameter 'groupId' is deprecated. Format changes from integer to string. Use 'groupName' instead."),
85 tuple("10.0", "Parameter 'groupId' is removed. Use 'groupName' instead."));
89 public void add_group_to_template() {
92 newRequest(group.getName(), template.getUuid(), CODEVIEWER);
94 assertThat(getGroupNamesInTemplateAndPermission(template, CODEVIEWER)).containsExactly(group.getName());
98 public void add_group_to_template_by_name() {
102 .setParam(PARAM_GROUP_NAME, group.getName())
103 .setParam(PARAM_PERMISSION, CODEVIEWER)
104 .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
107 assertThat(getGroupNamesInTemplateAndPermission(template, CODEVIEWER)).containsExactly(group.getName());
111 public void does_not_add_a_group_twice() {
114 newRequest(group.getName(), template.getUuid(), ISSUE_ADMIN);
115 newRequest(group.getName(), template.getUuid(), ISSUE_ADMIN);
117 assertThat(getGroupNamesInTemplateAndPermission(template, ISSUE_ADMIN)).containsExactly(group.getName());
121 public void add_anyone_group_to_template() {
124 newRequest(ANYONE, template.getUuid(), CODEVIEWER);
126 assertThat(getGroupNamesInTemplateAndPermission(template, CODEVIEWER)).containsExactly(ANYONE);
130 public void fail_if_add_anyone_group_to_admin_permission() {
133 assertThatThrownBy(() -> newRequest(ANYONE, template.getUuid(), ADMIN))
134 .isInstanceOf(BadRequestException.class)
135 .hasMessage(String.format("It is not possible to add the '%s' permission to the group 'Anyone'.", UserRole.ADMIN));
139 public void fail_if_not_a_project_permission() {
142 assertThatThrownBy(() -> newRequest(group.getName(), template.getUuid(), GlobalPermissions.PROVISIONING))
143 .isInstanceOf(IllegalArgumentException.class);
147 public void fail_if_not_admin() {
150 assertThatThrownBy(() -> newRequest(group.getName(), template.getUuid(), CODEVIEWER))
151 .isInstanceOf(ForbiddenException.class);
155 public void fail_if_group_params_missing() {
158 assertThatThrownBy(() -> newRequest(null, template.getUuid(), CODEVIEWER))
159 .isInstanceOf(IllegalArgumentException.class);
163 public void fail_if_permission_missing() {
166 assertThatThrownBy(() -> newRequest(group.getName(), template.getUuid(), null))
167 .isInstanceOf(IllegalArgumentException.class);
171 public void fail_if_template_uuid_and_name_missing() {
174 assertThatThrownBy(() -> newRequest(group.getName(), null, CODEVIEWER))
175 .isInstanceOf(BadRequestException.class);
179 public void fail_if_group_does_not_exist() {
182 assertThatThrownBy(() -> newRequest("unknown-group-name", template.getUuid(), CODEVIEWER))
183 .isInstanceOf(NotFoundException.class)
184 .hasMessage("No group with name 'unknown-group-name'");
188 public void fail_if_template_key_does_not_exist() {
191 assertThatThrownBy(() -> newRequest(group.getName(), "unknown-key", CODEVIEWER))
192 .isInstanceOf(NotFoundException.class)
193 .hasMessage("Permission template with id 'unknown-key' is not found");
196 private void newRequest(@Nullable String groupName, @Nullable String templateKey, @Nullable String permission) {
197 TestRequest request = newRequest();
198 if (groupName != null) {
199 request.setParam(PARAM_GROUP_NAME, groupName);
201 if (templateKey != null) {
202 request.setParam(PARAM_TEMPLATE_ID, templateKey);
204 if (permission != null) {
205 request.setParam(PARAM_PERMISSION, permission);
211 private List<String> getGroupNamesInTemplateAndPermission(PermissionTemplateDto template, String permission) {
212 PermissionQuery query = PermissionQuery.builder().setPermission(permission).build();
213 return db.getDbClient().permissionTemplateDao()
214 .selectGroupNamesByQueryAndTemplate(db.getSession(), query, template.getUuid());