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.TestRequest;
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.PARAM_PERMISSION;
41 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
42 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN;
44 public class RemoveUserFromTemplateActionTest extends BasePermissionWsTest<RemoveUserFromTemplateAction> {
46 private static final String DEFAULT_PERMISSION = CODEVIEWER;
49 private PermissionTemplateDto template;
52 protected RemoveUserFromTemplateAction buildWsAction() {
53 return new RemoveUserFromTemplateAction(db.getDbClient(), newPermissionWsSupport(), userSession);
58 user = db.users().insertUser("user-login");
59 template = insertTemplate();
60 addUserToTemplate(user, template, DEFAULT_PERMISSION);
64 public void remove_user_from_template() throws Exception {
65 loginAsAdminOnDefaultOrganization();
66 newRequest(user.getLogin(), template.getUuid(), DEFAULT_PERMISSION);
68 assertThat(getLoginsInTemplateAndPermission(template.getId(), DEFAULT_PERMISSION)).isEmpty();
72 public void remove_user_from_template_by_name_case_insensitive() throws Exception {
73 loginAsAdminOnDefaultOrganization();
75 .setParam(PARAM_USER_LOGIN, user.getLogin())
76 .setParam(PARAM_PERMISSION, DEFAULT_PERMISSION)
77 .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
80 assertThat(getLoginsInTemplateAndPermission(template.getId(), DEFAULT_PERMISSION)).isEmpty();
84 public void remove_user_from_template_twice_without_failing() throws Exception {
85 loginAsAdminOnDefaultOrganization();
86 newRequest(user.getLogin(), template.getUuid(), DEFAULT_PERMISSION);
87 newRequest(user.getLogin(), template.getUuid(), DEFAULT_PERMISSION);
89 assertThat(getLoginsInTemplateAndPermission(template.getId(), DEFAULT_PERMISSION)).isEmpty();
93 public void keep_user_permission_not_removed() throws Exception {
94 addUserToTemplate(user, template, ISSUE_ADMIN);
96 loginAsAdminOnDefaultOrganization();
97 newRequest(user.getLogin(), template.getUuid(), DEFAULT_PERMISSION);
99 assertThat(getLoginsInTemplateAndPermission(template.getId(), DEFAULT_PERMISSION)).isEmpty();
100 assertThat(getLoginsInTemplateAndPermission(template.getId(), ISSUE_ADMIN)).containsExactly(user.getLogin());
104 public void keep_other_users_when_one_user_removed() throws Exception {
105 UserDto newUser = db.users().insertUser("new-login");
106 addUserToTemplate(newUser, template, DEFAULT_PERMISSION);
108 loginAsAdminOnDefaultOrganization();
109 newRequest(user.getLogin(), template.getUuid(), DEFAULT_PERMISSION);
111 assertThat(getLoginsInTemplateAndPermission(template.getId(), DEFAULT_PERMISSION)).containsExactly("new-login");
115 public void fail_if_not_a_project_permission() throws Exception {
116 expectedException.expect(IllegalArgumentException.class);
118 loginAsAdminOnDefaultOrganization();
119 newRequest(user.getLogin(), template.getUuid(), GlobalPermissions.PROVISIONING);
123 public void fail_if_insufficient_privileges() throws Exception {
124 expectedException.expect(ForbiddenException.class);
125 userSession.logIn("john").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
127 newRequest(user.getLogin(), template.getUuid(), DEFAULT_PERMISSION);
131 public void fail_if_not_logged_in() throws Exception {
132 expectedException.expect(UnauthorizedException.class);
133 userSession.anonymous();
135 newRequest(user.getLogin(), template.getUuid(), DEFAULT_PERMISSION);
139 public void fail_if_user_missing() throws Exception {
140 expectedException.expect(IllegalArgumentException.class);
142 loginAsAdminOnDefaultOrganization();
143 newRequest(null, template.getUuid(), DEFAULT_PERMISSION);
147 public void fail_if_permission_missing() throws Exception {
148 expectedException.expect(IllegalArgumentException.class);
150 loginAsAdminOnDefaultOrganization();
151 newRequest(user.getLogin(), template.getUuid(), null);
155 public void fail_if_template_missing() throws Exception {
156 expectedException.expect(BadRequestException.class);
158 loginAsAdminOnDefaultOrganization();
159 newRequest(user.getLogin(), null, DEFAULT_PERMISSION);
163 public void fail_if_user_does_not_exist() throws Exception {
164 expectedException.expect(NotFoundException.class);
165 expectedException.expectMessage("User with login 'unknown-login' is not found");
167 loginAsAdminOnDefaultOrganization();
168 newRequest("unknown-login", template.getUuid(), DEFAULT_PERMISSION);
172 public void fail_if_template_key_does_not_exist() throws Exception {
173 expectedException.expect(NotFoundException.class);
174 expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
176 loginAsAdminOnDefaultOrganization();
177 newRequest(user.getLogin(), "unknown-key", DEFAULT_PERMISSION);
180 private void newRequest(@Nullable String userLogin, @Nullable String templateKey, @Nullable String permission) throws Exception {
181 TestRequest request = newRequest();
182 if (userLogin != null) {
183 request.setParam(PARAM_USER_LOGIN, userLogin);
185 if (templateKey != null) {
186 request.setParam(org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID, templateKey);
188 if (permission != null) {
189 request.setParam(PARAM_PERMISSION, permission);
195 private List<String> getLoginsInTemplateAndPermission(long templateId, String permission) {
196 PermissionQuery permissionQuery = PermissionQuery.builder().setPermission(permission).build();
197 return db.getDbClient().permissionTemplateDao()
198 .selectUserLoginsByQueryAndTemplate(db.getSession(), permissionQuery, templateId);
201 private void addUserToTemplate(UserDto user, PermissionTemplateDto template, String permission) {
202 db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), template.getId(), user.getId(), permission);