2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.
21 package org.sonar.server.permission.ws.template;
23 import com.google.common.base.Function;
24 import java.util.List;
25 import javax.annotation.Nonnull;
26 import javax.annotation.Nullable;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.experimental.categories.Category;
31 import org.junit.rules.ExpectedException;
32 import org.sonar.api.resources.Qualifiers;
33 import org.sonar.api.utils.System2;
34 import org.sonar.core.permission.GlobalPermissions;
35 import org.sonar.db.DbClient;
36 import org.sonar.db.DbSession;
37 import org.sonar.db.DbTester;
38 import org.sonar.db.component.ResourceTypesRule;
39 import org.sonar.db.permission.PermissionQuery;
40 import org.sonar.db.permission.PermissionTemplateDto;
41 import org.sonar.db.permission.UserWithPermissionDto;
42 import org.sonar.db.user.UserDto;
43 import org.sonar.server.component.ComponentFinder;
44 import org.sonar.server.exceptions.BadRequestException;
45 import org.sonar.server.exceptions.ForbiddenException;
46 import org.sonar.server.exceptions.NotFoundException;
47 import org.sonar.server.exceptions.UnauthorizedException;
48 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
49 import org.sonar.server.permission.ws.PermissionsWsParameters;
50 import org.sonar.server.tester.UserSessionRule;
51 import org.sonar.server.usergroups.ws.UserGroupFinder;
52 import org.sonar.server.ws.TestRequest;
53 import org.sonar.server.ws.WsActionTester;
54 import org.sonar.test.DbTests;
56 import static com.google.common.collect.FluentIterable.from;
57 import static org.assertj.core.api.Assertions.assertThat;
58 import static org.sonar.api.web.UserRole.CODEVIEWER;
59 import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
60 import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
61 import static org.sonar.db.user.GroupMembershipQuery.IN;
62 import static org.sonar.db.user.UserTesting.newUserDto;
63 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_PERMISSION;
64 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
65 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_USER_LOGIN;
67 @Category(DbTests.class)
68 public class RemoveUserFromTemplateActionTest {
70 private static final String USER_LOGIN = "user-login";
71 private static final String DEFAULT_PERMISSION = CODEVIEWER;
73 public DbTester db = DbTester.create(System2.INSTANCE);
75 public ExpectedException expectedException = ExpectedException.none();
77 public UserSessionRule userSession = UserSessionRule.standalone();
78 ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
84 PermissionTemplateDto permissionTemplate;
88 dbClient = db.getDbClient();
89 dbSession = db.getSession();
90 userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
92 PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), new UserGroupFinder(dbClient), resourceTypes);
93 ws = new WsActionTester(new RemoveUserFromTemplateAction(dbClient, dependenciesFinder, userSession));
95 user = insertUser(newUserDto().setLogin(USER_LOGIN));
96 permissionTemplate = insertPermissionTemplate(newPermissionTemplateDto());
97 addUserToTemplate(user, permissionTemplate, DEFAULT_PERMISSION);
102 public void remove_user_from_template() {
103 newRequest(USER_LOGIN, permissionTemplate.getUuid(), DEFAULT_PERMISSION);
105 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), DEFAULT_PERMISSION)).isEmpty();
109 public void remove_user_from_template_by_name_case_insensitive() {
111 .setParam(PARAM_USER_LOGIN, USER_LOGIN)
112 .setParam(PARAM_PERMISSION, DEFAULT_PERMISSION)
113 .setParam(PARAM_TEMPLATE_NAME, permissionTemplate.getName().toUpperCase())
117 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), DEFAULT_PERMISSION)).isEmpty();
121 public void remove_user_from_template_twice_without_failing() {
122 newRequest(USER_LOGIN, permissionTemplate.getUuid(), DEFAULT_PERMISSION);
123 newRequest(USER_LOGIN, permissionTemplate.getUuid(), DEFAULT_PERMISSION);
125 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), DEFAULT_PERMISSION)).isEmpty();
129 public void keep_user_permission_not_removed() {
130 addUserToTemplate(user, permissionTemplate, ISSUE_ADMIN);
133 newRequest(USER_LOGIN, permissionTemplate.getUuid(), DEFAULT_PERMISSION);
135 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), DEFAULT_PERMISSION)).isEmpty();
136 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), ISSUE_ADMIN)).containsExactly(user.getLogin());
140 public void keep_other_users_when_one_user_removed() {
141 UserDto newUser = insertUser(newUserDto().setLogin("new-login"));
142 addUserToTemplate(newUser, permissionTemplate, DEFAULT_PERMISSION);
145 newRequest(USER_LOGIN, permissionTemplate.getUuid(), DEFAULT_PERMISSION);
147 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), DEFAULT_PERMISSION)).containsExactly("new-login");
151 public void fail_if_not_a_project_permission() {
152 expectedException.expect(BadRequestException.class);
154 newRequest(USER_LOGIN, permissionTemplate.getUuid(), GlobalPermissions.PREVIEW_EXECUTION);
158 public void fail_if_insufficient_privileges() {
159 expectedException.expect(ForbiddenException.class);
160 userSession.setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
162 newRequest(USER_LOGIN, permissionTemplate.getUuid(), DEFAULT_PERMISSION);
166 public void fail_if_not_logged_in() {
167 expectedException.expect(UnauthorizedException.class);
168 userSession.anonymous();
170 newRequest(USER_LOGIN, permissionTemplate.getUuid(), DEFAULT_PERMISSION);
174 public void fail_if_user_missing() {
175 expectedException.expect(IllegalArgumentException.class);
177 newRequest(null, permissionTemplate.getUuid(), DEFAULT_PERMISSION);
181 public void fail_if_permission_missing() {
182 expectedException.expect(IllegalArgumentException.class);
184 newRequest(USER_LOGIN, permissionTemplate.getUuid(), null);
188 public void fail_if_template_missing() {
189 expectedException.expect(BadRequestException.class);
191 newRequest(USER_LOGIN, null, DEFAULT_PERMISSION);
195 public void fail_if_user_does_not_exist() {
196 expectedException.expect(NotFoundException.class);
197 expectedException.expectMessage("User with login 'unknown-login' is not found");
199 newRequest("unknown-login", permissionTemplate.getUuid(), DEFAULT_PERMISSION);
203 public void fail_if_template_key_does_not_exist() {
204 expectedException.expect(NotFoundException.class);
205 expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
207 newRequest(USER_LOGIN, "unknown-key", DEFAULT_PERMISSION);
210 private void newRequest(@Nullable String userLogin, @Nullable String templateKey, @Nullable String permission) {
211 TestRequest request = ws.newRequest();
212 if (userLogin != null) {
213 request.setParam(PARAM_USER_LOGIN, userLogin);
215 if (templateKey != null) {
216 request.setParam(PermissionsWsParameters.PARAM_TEMPLATE_UUID, templateKey);
218 if (permission != null) {
219 request.setParam(PermissionsWsParameters.PARAM_PERMISSION, permission);
225 private void commit() {
229 private UserDto insertUser(UserDto userDto) {
230 return dbClient.userDao().insert(dbSession, userDto.setActive(true));
233 private PermissionTemplateDto insertPermissionTemplate(PermissionTemplateDto permissionTemplate) {
234 return dbClient.permissionTemplateDao().insert(dbSession, permissionTemplate);
237 private List<String> getLoginsInTemplateAndPermission(long templateId, String permission) {
238 PermissionQuery permissionQuery = PermissionQuery.builder().permission(permission).membership(IN).build();
239 return from(dbClient.permissionTemplateDao()
240 .selectUsers(dbSession, permissionQuery, templateId, 0, Integer.MAX_VALUE))
241 .transform(UserWithPermissionToUserLogin.INSTANCE)
245 private enum UserWithPermissionToUserLogin implements Function<UserWithPermissionDto, String> {
249 public String apply(@Nonnull UserWithPermissionDto userWithPermission) {
250 return userWithPermission.getLogin();
255 private void addUserToTemplate(UserDto user, PermissionTemplateDto permissionTemplate, String permission) {
256 dbClient.permissionTemplateDao().insertUserPermission(dbSession, permissionTemplate.getId(), user.getId(), permission);