3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.core.permission.GlobalPermissions;
27 import org.sonar.db.permission.PermissionQuery;
28 import org.sonar.db.permission.template.PermissionTemplateDto;
29 import org.sonar.db.user.UserDto;
30 import org.sonar.server.exceptions.BadRequestException;
31 import org.sonar.server.exceptions.ForbiddenException;
32 import org.sonar.server.exceptions.NotFoundException;
33 import org.sonar.server.exceptions.UnauthorizedException;
34 import org.sonar.server.permission.ws.BasePermissionWsTest;
35 import org.sonar.server.ws.WsTester;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.sonar.api.web.UserRole.CODEVIEWER;
39 import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
40 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.CONTROLLER;
41 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
42 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
43 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN;
45 public class RemoveUserFromTemplateActionTest extends BasePermissionWsTest<RemoveUserFromTemplateAction> {
47 private static final String DEFAULT_PERMISSION = CODEVIEWER;
48 private static final String ACTION = "remove_user_from_template";
51 private PermissionTemplateDto template;
54 protected RemoveUserFromTemplateAction buildWsAction() {
55 return new RemoveUserFromTemplateAction(db.getDbClient(), newPermissionWsSupport(), userSession);
60 user = db.users().insertUser("user-login");
61 template = insertTemplate();
62 addUserToTemplate(user, template, DEFAULT_PERMISSION);
66 public void remove_user_from_template() throws Exception {
67 loginAsAdminOnDefaultOrganization();
68 newRequest(user.getLogin(), template.getUuid(), DEFAULT_PERMISSION);
70 assertThat(getLoginsInTemplateAndPermission(template.getId(), DEFAULT_PERMISSION)).isEmpty();
74 public void remove_user_from_template_by_name_case_insensitive() throws Exception {
75 loginAsAdminOnDefaultOrganization();
76 wsTester.newPostRequest(CONTROLLER, ACTION)
77 .setParam(PARAM_USER_LOGIN, user.getLogin())
78 .setParam(PARAM_PERMISSION, DEFAULT_PERMISSION)
79 .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
82 assertThat(getLoginsInTemplateAndPermission(template.getId(), DEFAULT_PERMISSION)).isEmpty();
86 public void remove_user_from_template_twice_without_failing() throws Exception {
87 loginAsAdminOnDefaultOrganization();
88 newRequest(user.getLogin(), template.getUuid(), DEFAULT_PERMISSION);
89 newRequest(user.getLogin(), template.getUuid(), DEFAULT_PERMISSION);
91 assertThat(getLoginsInTemplateAndPermission(template.getId(), DEFAULT_PERMISSION)).isEmpty();
95 public void keep_user_permission_not_removed() throws Exception {
96 addUserToTemplate(user, template, ISSUE_ADMIN);
98 loginAsAdminOnDefaultOrganization();
99 newRequest(user.getLogin(), template.getUuid(), DEFAULT_PERMISSION);
101 assertThat(getLoginsInTemplateAndPermission(template.getId(), DEFAULT_PERMISSION)).isEmpty();
102 assertThat(getLoginsInTemplateAndPermission(template.getId(), ISSUE_ADMIN)).containsExactly(user.getLogin());
106 public void keep_other_users_when_one_user_removed() throws Exception {
107 UserDto newUser = db.users().insertUser("new-login");
108 addUserToTemplate(newUser, template, DEFAULT_PERMISSION);
110 loginAsAdminOnDefaultOrganization();
111 newRequest(user.getLogin(), template.getUuid(), DEFAULT_PERMISSION);
113 assertThat(getLoginsInTemplateAndPermission(template.getId(), DEFAULT_PERMISSION)).containsExactly("new-login");
117 public void fail_if_not_a_project_permission() throws Exception {
118 expectedException.expect(IllegalArgumentException.class);
120 loginAsAdminOnDefaultOrganization();
121 newRequest(user.getLogin(), template.getUuid(), GlobalPermissions.PROVISIONING);
125 public void fail_if_insufficient_privileges() throws Exception {
126 expectedException.expect(ForbiddenException.class);
127 userSession.login("john").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
129 newRequest(user.getLogin(), template.getUuid(), DEFAULT_PERMISSION);
133 public void fail_if_not_logged_in() throws Exception {
134 expectedException.expect(UnauthorizedException.class);
135 userSession.anonymous();
137 newRequest(user.getLogin(), template.getUuid(), DEFAULT_PERMISSION);
141 public void fail_if_user_missing() throws Exception {
142 expectedException.expect(IllegalArgumentException.class);
144 loginAsAdminOnDefaultOrganization();
145 newRequest(null, template.getUuid(), DEFAULT_PERMISSION);
149 public void fail_if_permission_missing() throws Exception {
150 expectedException.expect(IllegalArgumentException.class);
152 loginAsAdminOnDefaultOrganization();
153 newRequest(user.getLogin(), template.getUuid(), null);
157 public void fail_if_template_missing() throws Exception {
158 expectedException.expect(BadRequestException.class);
160 loginAsAdminOnDefaultOrganization();
161 newRequest(user.getLogin(), null, DEFAULT_PERMISSION);
165 public void fail_if_user_does_not_exist() throws Exception {
166 expectedException.expect(NotFoundException.class);
167 expectedException.expectMessage("User with login 'unknown-login' is not found");
169 loginAsAdminOnDefaultOrganization();
170 newRequest("unknown-login", template.getUuid(), DEFAULT_PERMISSION);
174 public void fail_if_template_key_does_not_exist() throws Exception {
175 expectedException.expect(NotFoundException.class);
176 expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
178 loginAsAdminOnDefaultOrganization();
179 newRequest(user.getLogin(), "unknown-key", DEFAULT_PERMISSION);
182 private void newRequest(@Nullable String userLogin, @Nullable String templateKey, @Nullable String permission) throws Exception {
183 WsTester.TestRequest request = wsTester.newPostRequest(CONTROLLER, ACTION);
184 if (userLogin != null) {
185 request.setParam(PARAM_USER_LOGIN, userLogin);
187 if (templateKey != null) {
188 request.setParam(org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID, templateKey);
190 if (permission != null) {
191 request.setParam(PARAM_PERMISSION, permission);
197 private List<String> getLoginsInTemplateAndPermission(long templateId, String permission) {
198 PermissionQuery permissionQuery = PermissionQuery.builder().setPermission(permission).build();
199 return db.getDbClient().permissionTemplateDao()
200 .selectUserLoginsByQueryAndTemplate(db.getSession(), permissionQuery, templateId);
203 private void addUserToTemplate(UserDto user, PermissionTemplateDto template, String permission) {
204 db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), template.getId(), user.getId(), permission);