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.core.permission.GlobalPermissions;
29 import org.sonar.db.component.ResourceTypesRule;
30 import org.sonar.db.permission.PermissionQuery;
31 import org.sonar.db.permission.template.PermissionTemplateDto;
32 import org.sonar.db.user.UserDto;
33 import org.sonar.server.exceptions.BadRequestException;
34 import org.sonar.server.exceptions.ForbiddenException;
35 import org.sonar.server.exceptions.NotFoundException;
36 import org.sonar.server.permission.PermissionService;
37 import org.sonar.server.permission.PermissionServiceImpl;
38 import org.sonar.server.permission.ws.BasePermissionWsTest;
39 import org.sonar.server.permission.ws.WsParameters;
40 import org.sonar.server.ws.TestRequest;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.assertj.core.api.Assertions.assertThatThrownBy;
44 import static org.sonar.api.web.UserRole.CODEVIEWER;
45 import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
46 import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES;
47 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
48 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
49 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
50 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN;
52 public class AddUserToTemplateActionTest extends BasePermissionWsTest<AddUserToTemplateAction> {
55 private PermissionTemplateDto permissionTemplate;
56 private ResourceTypes resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT);
57 private PermissionService permissionService = new PermissionServiceImpl(resourceTypes);
58 private WsParameters wsParameters = new WsParameters(permissionService);
61 protected AddUserToTemplateAction buildWsAction() {
62 return new AddUserToTemplateAction(db.getDbClient(), newPermissionWsSupport(), userSession, wsParameters);
67 user = db.users().insertUser("user-login");
68 permissionTemplate = db.permissionTemplates().insertTemplate();
72 public void add_user_to_template() {
75 newRequest(user.getLogin(), permissionTemplate.getUuid(), CODEVIEWER);
77 assertThat(getLoginsInTemplateAndPermission(permissionTemplate, CODEVIEWER)).containsExactly(user.getLogin());
81 public void add_user_to_template_by_name() {
85 .setParam(PARAM_USER_LOGIN, user.getLogin())
86 .setParam(PARAM_PERMISSION, CODEVIEWER)
87 .setParam(PARAM_TEMPLATE_NAME, permissionTemplate.getName().toUpperCase())
90 assertThat(getLoginsInTemplateAndPermission(permissionTemplate, CODEVIEWER)).containsExactly(user.getLogin());
94 public void does_not_add_a_user_twice() {
97 newRequest(user.getLogin(), permissionTemplate.getUuid(), ISSUE_ADMIN);
98 newRequest(user.getLogin(), permissionTemplate.getUuid(), ISSUE_ADMIN);
100 assertThat(getLoginsInTemplateAndPermission(permissionTemplate, ISSUE_ADMIN)).containsExactly(user.getLogin());
104 public void fail_if_not_a_project_permission() {
107 assertThatThrownBy(() -> {
108 newRequest(user.getLogin(), permissionTemplate.getUuid(), GlobalPermissions.PROVISIONING);
110 .isInstanceOf(IllegalArgumentException.class);
114 public void fail_if_not_admin() {
115 userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
117 assertThatThrownBy(() -> {
118 newRequest(user.getLogin(), permissionTemplate.getUuid(), CODEVIEWER);
120 .isInstanceOf(ForbiddenException.class);
124 public void fail_if_user_missing() {
127 assertThatThrownBy(() -> {
128 newRequest(null, permissionTemplate.getUuid(), CODEVIEWER);
130 .isInstanceOf(IllegalArgumentException.class);
134 public void fail_if_permission_missing() {
137 assertThatThrownBy(() -> {
138 newRequest(user.getLogin(), permissionTemplate.getUuid(), null);
140 .isInstanceOf(IllegalArgumentException.class);
144 public void fail_if_template_uuid_and_name_are_missing() {
147 assertThatThrownBy(() -> {
148 newRequest(user.getLogin(), null, CODEVIEWER);
150 .isInstanceOf(BadRequestException.class);
154 public void fail_if_user_does_not_exist() {
157 assertThatThrownBy(() -> newRequest("unknown-login", permissionTemplate.getUuid(), CODEVIEWER))
158 .isInstanceOf(NotFoundException.class)
159 .hasMessageContaining("User with login 'unknown-login' is not found");
164 public void fail_if_template_key_does_not_exist() {
167 assertThatThrownBy(() -> {
168 newRequest(user.getLogin(), "unknown-key", CODEVIEWER);
170 .isInstanceOf(NotFoundException.class)
171 .hasMessage("Permission template with id 'unknown-key' is not found");
174 private void newRequest(@Nullable String userLogin, @Nullable String templateKey, @Nullable String permission) {
175 TestRequest request = newRequest();
176 if (userLogin != null) {
177 request.setParam(PARAM_USER_LOGIN, userLogin);
179 if (templateKey != null) {
180 request.setParam(PARAM_TEMPLATE_ID, templateKey);
182 if (permission != null) {
183 request.setParam(PARAM_PERMISSION, permission);
189 private List<String> getLoginsInTemplateAndPermission(PermissionTemplateDto template, String permission) {
190 PermissionQuery permissionQuery = PermissionQuery.builder().setPermission(permission).build();
191 return db.getDbClient().permissionTemplateDao()
192 .selectUserLoginsByQueryAndTemplate(db.getSession(), permissionQuery, template.getUuid());