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 com.google.common.base.Function;
23 import java.util.List;
24 import javax.annotation.Nonnull;
25 import javax.annotation.Nullable;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.sonar.api.resources.Qualifiers;
31 import org.sonar.api.utils.System2;
32 import org.sonar.core.permission.GlobalPermissions;
33 import org.sonar.db.DbClient;
34 import org.sonar.db.DbSession;
35 import org.sonar.db.DbTester;
36 import org.sonar.db.component.ResourceTypesRule;
37 import org.sonar.db.permission.PermissionQuery;
38 import org.sonar.db.permission.PermissionTemplateDto;
39 import org.sonar.db.permission.UserWithPermissionDto;
40 import org.sonar.db.user.UserDto;
41 import org.sonar.server.component.ComponentFinder;
42 import org.sonar.server.exceptions.BadRequestException;
43 import org.sonar.server.exceptions.ForbiddenException;
44 import org.sonar.server.exceptions.NotFoundException;
45 import org.sonar.server.exceptions.UnauthorizedException;
46 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
47 import org.sonar.server.tester.UserSessionRule;
48 import org.sonar.server.usergroups.ws.UserGroupFinder;
49 import org.sonar.server.ws.TestRequest;
50 import org.sonar.server.ws.WsActionTester;
52 import static com.google.common.collect.FluentIterable.from;
53 import static org.assertj.core.api.Assertions.assertThat;
54 import static org.sonar.api.web.UserRole.CODEVIEWER;
55 import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
56 import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
57 import static org.sonar.db.user.GroupMembershipQuery.IN;
58 import static org.sonar.db.user.UserTesting.newUserDto;
59 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
60 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
61 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN;
64 public class RemoveUserFromTemplateActionTest {
66 private static final String USER_LOGIN = "user-login";
67 private static final String DEFAULT_PERMISSION = CODEVIEWER;
69 public DbTester db = DbTester.create(System2.INSTANCE);
71 public ExpectedException expectedException = ExpectedException.none();
73 public UserSessionRule userSession = UserSessionRule.standalone();
74 ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
80 PermissionTemplateDto permissionTemplate;
84 dbClient = db.getDbClient();
85 dbSession = db.getSession();
86 userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
88 PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), new UserGroupFinder(dbClient), resourceTypes);
89 ws = new WsActionTester(new RemoveUserFromTemplateAction(dbClient, dependenciesFinder, userSession));
91 user = insertUser(newUserDto().setLogin(USER_LOGIN));
92 permissionTemplate = insertPermissionTemplate(newPermissionTemplateDto());
93 addUserToTemplate(user, permissionTemplate, DEFAULT_PERMISSION);
98 public void remove_user_from_template() {
99 newRequest(USER_LOGIN, permissionTemplate.getUuid(), DEFAULT_PERMISSION);
101 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), DEFAULT_PERMISSION)).isEmpty();
105 public void remove_user_from_template_by_name_case_insensitive() {
107 .setParam(PARAM_USER_LOGIN, USER_LOGIN)
108 .setParam(PARAM_PERMISSION, DEFAULT_PERMISSION)
109 .setParam(PARAM_TEMPLATE_NAME, permissionTemplate.getName().toUpperCase())
113 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), DEFAULT_PERMISSION)).isEmpty();
117 public void remove_user_from_template_twice_without_failing() {
118 newRequest(USER_LOGIN, permissionTemplate.getUuid(), DEFAULT_PERMISSION);
119 newRequest(USER_LOGIN, permissionTemplate.getUuid(), DEFAULT_PERMISSION);
121 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), DEFAULT_PERMISSION)).isEmpty();
125 public void keep_user_permission_not_removed() {
126 addUserToTemplate(user, permissionTemplate, ISSUE_ADMIN);
129 newRequest(USER_LOGIN, permissionTemplate.getUuid(), DEFAULT_PERMISSION);
131 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), DEFAULT_PERMISSION)).isEmpty();
132 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), ISSUE_ADMIN)).containsExactly(user.getLogin());
136 public void keep_other_users_when_one_user_removed() {
137 UserDto newUser = insertUser(newUserDto().setLogin("new-login"));
138 addUserToTemplate(newUser, permissionTemplate, DEFAULT_PERMISSION);
141 newRequest(USER_LOGIN, permissionTemplate.getUuid(), DEFAULT_PERMISSION);
143 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), DEFAULT_PERMISSION)).containsExactly("new-login");
147 public void fail_if_not_a_project_permission() {
148 expectedException.expect(BadRequestException.class);
150 newRequest(USER_LOGIN, permissionTemplate.getUuid(), GlobalPermissions.PROVISIONING);
154 public void fail_if_insufficient_privileges() {
155 expectedException.expect(ForbiddenException.class);
156 userSession.setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
158 newRequest(USER_LOGIN, permissionTemplate.getUuid(), DEFAULT_PERMISSION);
162 public void fail_if_not_logged_in() {
163 expectedException.expect(UnauthorizedException.class);
164 userSession.anonymous();
166 newRequest(USER_LOGIN, permissionTemplate.getUuid(), DEFAULT_PERMISSION);
170 public void fail_if_user_missing() {
171 expectedException.expect(IllegalArgumentException.class);
173 newRequest(null, permissionTemplate.getUuid(), DEFAULT_PERMISSION);
177 public void fail_if_permission_missing() {
178 expectedException.expect(IllegalArgumentException.class);
180 newRequest(USER_LOGIN, permissionTemplate.getUuid(), null);
184 public void fail_if_template_missing() {
185 expectedException.expect(BadRequestException.class);
187 newRequest(USER_LOGIN, null, DEFAULT_PERMISSION);
191 public void fail_if_user_does_not_exist() {
192 expectedException.expect(NotFoundException.class);
193 expectedException.expectMessage("User with login 'unknown-login' is not found");
195 newRequest("unknown-login", permissionTemplate.getUuid(), DEFAULT_PERMISSION);
199 public void fail_if_template_key_does_not_exist() {
200 expectedException.expect(NotFoundException.class);
201 expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
203 newRequest(USER_LOGIN, "unknown-key", DEFAULT_PERMISSION);
206 private void newRequest(@Nullable String userLogin, @Nullable String templateKey, @Nullable String permission) {
207 TestRequest request = ws.newRequest();
208 if (userLogin != null) {
209 request.setParam(PARAM_USER_LOGIN, userLogin);
211 if (templateKey != null) {
212 request.setParam(org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID, templateKey);
214 if (permission != null) {
215 request.setParam(PARAM_PERMISSION, permission);
221 private void commit() {
225 private UserDto insertUser(UserDto userDto) {
226 return dbClient.userDao().insert(dbSession, userDto.setActive(true));
229 private PermissionTemplateDto insertPermissionTemplate(PermissionTemplateDto permissionTemplate) {
230 return dbClient.permissionTemplateDao().insert(dbSession, permissionTemplate);
233 private List<String> getLoginsInTemplateAndPermission(long templateId, String permission) {
234 PermissionQuery permissionQuery = PermissionQuery.builder().permission(permission).membership(IN).build();
235 return from(dbClient.permissionTemplateDao()
236 .selectUsers(dbSession, permissionQuery, templateId, 0, Integer.MAX_VALUE))
237 .transform(UserWithPermissionToUserLogin.INSTANCE)
241 private enum UserWithPermissionToUserLogin implements Function<UserWithPermissionDto, String> {
245 public String apply(@Nonnull UserWithPermissionDto userWithPermission) {
246 return userWithPermission.getLogin();
251 private void addUserToTemplate(UserDto user, PermissionTemplateDto permissionTemplate, String permission) {
252 dbClient.permissionTemplateDao().insertUserPermission(dbSession, permissionTemplate.getId(), user.getId(), permission);