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;
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.utils.System2;
33 import org.sonar.core.permission.GlobalPermissions;
34 import org.sonar.db.DbClient;
35 import org.sonar.db.DbSession;
36 import org.sonar.db.DbTester;
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.tester.UserSessionRule;
47 import org.sonar.server.ws.TestRequest;
48 import org.sonar.server.ws.WsActionTester;
49 import org.sonar.test.DbTests;
51 import static com.google.common.collect.FluentIterable.from;
52 import static org.assertj.core.api.Assertions.assertThat;
53 import static org.sonar.api.web.UserRole.CODEVIEWER;
54 import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
55 import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
56 import static org.sonar.db.user.GroupMembershipQuery.IN;
57 import static org.sonar.db.user.UserTesting.newUserDto;
59 @Category(DbTests.class)
60 public class RemoveUserFromTemplateActionTest {
62 private static final String USER_LOGIN = "user-login";
63 private static final String DEFAULT_PERMISSION = CODEVIEWER;
65 public DbTester db = DbTester.create(System2.INSTANCE);
67 public ExpectedException expectedException = ExpectedException.none();
69 public UserSessionRule userSession = UserSessionRule.standalone();
75 PermissionTemplateDto permissionTemplate;
79 dbClient = db.getDbClient();
80 dbSession = db.getSession();
81 userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
83 PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient));
84 ws = new WsActionTester(new RemoveUserFromTemplateAction(dbClient, dependenciesFinder, userSession));
86 user = insertUser(newUserDto().setLogin(USER_LOGIN));
87 permissionTemplate = insertPermissionTemplate(newPermissionTemplateDto());
88 addUserToTemplate(user, permissionTemplate, DEFAULT_PERMISSION);
93 public void remove_user_from_template() {
94 newRequest(USER_LOGIN, permissionTemplate.getKee(), DEFAULT_PERMISSION);
96 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), DEFAULT_PERMISSION)).isEmpty();
100 public void remove_user_from_template_twice_without_failing() {
101 newRequest(USER_LOGIN, permissionTemplate.getKee(), DEFAULT_PERMISSION);
102 newRequest(USER_LOGIN, permissionTemplate.getKee(), DEFAULT_PERMISSION);
104 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), DEFAULT_PERMISSION)).isEmpty();
108 public void keep_user_permission_not_removed() {
109 addUserToTemplate(user, permissionTemplate, ISSUE_ADMIN);
112 newRequest(USER_LOGIN, permissionTemplate.getKee(), DEFAULT_PERMISSION);
114 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), DEFAULT_PERMISSION)).isEmpty();
115 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), ISSUE_ADMIN)).containsExactly(user.getLogin());
119 public void keep_other_users_when_one_user_removed() {
120 UserDto newUser = insertUser(newUserDto().setLogin("new-login"));
121 addUserToTemplate(newUser, permissionTemplate, DEFAULT_PERMISSION);
124 newRequest(USER_LOGIN, permissionTemplate.getKee(), DEFAULT_PERMISSION);
126 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), DEFAULT_PERMISSION)).containsExactly("new-login");
130 public void fail_if_not_a_project_permission() {
131 expectedException.expect(BadRequestException.class);
133 newRequest(USER_LOGIN, permissionTemplate.getKee(), GlobalPermissions.PREVIEW_EXECUTION);
137 public void fail_if_insufficient_privileges() {
138 expectedException.expect(ForbiddenException.class);
139 userSession.setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
141 newRequest(USER_LOGIN, permissionTemplate.getKee(), DEFAULT_PERMISSION);
145 public void fail_if_not_logged_in() {
146 expectedException.expect(UnauthorizedException.class);
147 userSession.anonymous();
149 newRequest(USER_LOGIN, permissionTemplate.getKee(), DEFAULT_PERMISSION);
153 public void fail_if_user_missing() {
154 expectedException.expect(IllegalArgumentException.class);
156 newRequest(null, permissionTemplate.getKee(), DEFAULT_PERMISSION);
160 public void fail_if_permission_missing() {
161 expectedException.expect(IllegalArgumentException.class);
163 newRequest(USER_LOGIN, permissionTemplate.getKee(), null);
167 public void fail_if_template_key_missing() {
168 expectedException.expect(IllegalArgumentException.class);
170 newRequest(USER_LOGIN, null, DEFAULT_PERMISSION);
174 public void fail_if_user_does_not_exist() {
175 expectedException.expect(NotFoundException.class);
176 expectedException.expectMessage("User with login 'unknown-login' is not found");
178 newRequest("unknown-login", permissionTemplate.getKee(), DEFAULT_PERMISSION);
182 public void fail_if_template_key_does_not_exist() {
183 expectedException.expect(NotFoundException.class);
184 expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
186 newRequest(USER_LOGIN, "unknown-key", DEFAULT_PERMISSION);
189 private void newRequest(@Nullable String userLogin, @Nullable String templateKey, @Nullable String permission) {
190 TestRequest request = ws.newRequest();
191 if (userLogin != null) {
192 request.setParam(Parameters.PARAM_USER_LOGIN, userLogin);
194 if (templateKey != null) {
195 request.setParam(Parameters.PARAM_TEMPLATE_ID_EXPLICIT, templateKey);
197 if (permission != null) {
198 request.setParam(Parameters.PARAM_PERMISSION, permission);
204 private void commit() {
208 private UserDto insertUser(UserDto userDto) {
209 return dbClient.userDao().insert(dbSession, userDto.setActive(true));
212 private PermissionTemplateDto insertPermissionTemplate(PermissionTemplateDto permissionTemplate) {
213 return dbClient.permissionTemplateDao().insert(dbSession, permissionTemplate);
216 private List<String> getLoginsInTemplateAndPermission(long templateId, String permission) {
217 PermissionQuery permissionQuery = PermissionQuery.builder().permission(permission).membership(IN).build();
218 return from(dbClient.permissionTemplateDao()
219 .selectUsers(dbSession, permissionQuery, templateId, 0, Integer.MAX_VALUE))
220 .transform(UserWithPermissionToUserLogin.INSTANCE)
224 private enum UserWithPermissionToUserLogin implements Function<UserWithPermissionDto, String> {
228 public String apply(@Nonnull UserWithPermissionDto userWithPermission) {
229 return userWithPermission.getLogin();
234 private void addUserToTemplate(UserDto user, PermissionTemplateDto permissionTemplate, String permission) {
235 dbClient.permissionTemplateDao().insertUserPermission(dbSession, permissionTemplate.getId(), user.getId(), permission);