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.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.permission.ws.PermissionDependenciesFinder;
47 import org.sonar.server.permission.ws.WsPermissionParameters;
48 import org.sonar.server.tester.UserSessionRule;
49 import org.sonar.server.ws.TestRequest;
50 import org.sonar.server.ws.WsActionTester;
51 import org.sonar.test.DbTests;
53 import static com.google.common.collect.FluentIterable.from;
54 import static org.assertj.core.api.Assertions.assertThat;
55 import static org.sonar.api.web.UserRole.CODEVIEWER;
56 import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
57 import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
58 import static org.sonar.db.user.GroupMembershipQuery.IN;
59 import static org.sonar.db.user.UserTesting.newUserDto;
60 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PERMISSION;
61 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_TEMPLATE_NAME;
62 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_USER_LOGIN;
64 @Category(DbTests.class)
65 public class AddUserToTemplateActionTest {
67 private static final String USER_LOGIN = "user-login";
69 public DbTester db = DbTester.create(System2.INSTANCE);
71 public ExpectedException expectedException = ExpectedException.none();
73 public UserSessionRule userSession = UserSessionRule.standalone();
79 PermissionTemplateDto permissionTemplate;
83 dbClient = db.getDbClient();
84 dbSession = db.getSession();
85 userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
87 PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient));
88 ws = new WsActionTester(new AddUserToTemplateAction(dbClient, dependenciesFinder, userSession));
90 user = insertUser(newUserDto().setLogin(USER_LOGIN));
91 permissionTemplate = insertPermissionTemplate(newPermissionTemplateDto());
96 public void add_user_to_template() {
97 newRequest(USER_LOGIN, permissionTemplate.getUuid(), CODEVIEWER);
99 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), CODEVIEWER)).containsExactly(USER_LOGIN);
103 public void add_user_to_template_by_name() {
105 .setParam(PARAM_USER_LOGIN, USER_LOGIN)
106 .setParam(PARAM_PERMISSION, CODEVIEWER)
107 .setParam(PARAM_TEMPLATE_NAME, permissionTemplate.getName().toUpperCase())
111 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), CODEVIEWER)).containsExactly(USER_LOGIN);
115 public void does_not_add_a_user_twice() {
116 newRequest(USER_LOGIN, permissionTemplate.getUuid(), ISSUE_ADMIN);
117 newRequest(USER_LOGIN, permissionTemplate.getUuid(), ISSUE_ADMIN);
119 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), ISSUE_ADMIN)).containsExactly(USER_LOGIN);
123 public void fail_if_not_a_project_permission() {
124 expectedException.expect(BadRequestException.class);
126 newRequest(USER_LOGIN, permissionTemplate.getUuid(), GlobalPermissions.PREVIEW_EXECUTION);
130 public void fail_if_insufficient_privileges() {
131 expectedException.expect(ForbiddenException.class);
132 userSession.setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
134 newRequest(USER_LOGIN, permissionTemplate.getUuid(), CODEVIEWER);
138 public void fail_if_not_logged_in() {
139 expectedException.expect(UnauthorizedException.class);
140 userSession.anonymous();
142 newRequest(USER_LOGIN, permissionTemplate.getUuid(), CODEVIEWER);
146 public void fail_if_user_missing() {
147 expectedException.expect(IllegalArgumentException.class);
149 newRequest(null, permissionTemplate.getUuid(), CODEVIEWER);
153 public void fail_if_permission_missing() {
154 expectedException.expect(IllegalArgumentException.class);
156 newRequest(USER_LOGIN, permissionTemplate.getUuid(), null);
160 public void fail_if_template_uuid_and_name_are_missing() {
161 expectedException.expect(BadRequestException.class);
163 newRequest(USER_LOGIN, null, CODEVIEWER);
167 public void fail_if_user_does_not_exist() {
168 expectedException.expect(NotFoundException.class);
169 expectedException.expectMessage("User with login 'unknown-login' is not found");
171 newRequest("unknown-login", permissionTemplate.getUuid(), CODEVIEWER);
175 public void fail_if_template_key_does_not_exist() {
176 expectedException.expect(NotFoundException.class);
177 expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
179 newRequest(USER_LOGIN, "unknown-key", CODEVIEWER);
182 private void newRequest(@Nullable String userLogin, @Nullable String templateKey, @Nullable String permission) {
183 TestRequest request = ws.newRequest();
184 if (userLogin != null) {
185 request.setParam(PARAM_USER_LOGIN, userLogin);
187 if (templateKey != null) {
188 request.setParam(WsPermissionParameters.PARAM_TEMPLATE_UUID, templateKey);
190 if (permission != null) {
191 request.setParam(WsPermissionParameters.PARAM_PERMISSION, permission);
197 private void commit() {
201 private UserDto insertUser(UserDto userDto) {
202 return dbClient.userDao().insert(dbSession, userDto.setActive(true));
205 private PermissionTemplateDto insertPermissionTemplate(PermissionTemplateDto permissionTemplate) {
206 return dbClient.permissionTemplateDao().insert(dbSession, permissionTemplate);
209 private List<String> getLoginsInTemplateAndPermission(long templateId, String permission) {
210 PermissionQuery permissionQuery = PermissionQuery.builder().permission(permission).membership(IN).build();
211 return from(dbClient.permissionTemplateDao()
212 .selectUsers(dbSession, permissionQuery, templateId, 0, Integer.MAX_VALUE))
213 .transform(UserWithPermissionToUserLogin.INSTANCE)
217 private enum UserWithPermissionToUserLogin implements Function<UserWithPermissionDto, String> {
221 public String apply(@Nonnull UserWithPermissionDto userWithPermission) {
222 return userWithPermission.getLogin();