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 AddUserToTemplateActionTest {
70 private static final String USER_LOGIN = "user-login";
72 public DbTester db = DbTester.create(System2.INSTANCE);
74 public ExpectedException expectedException = ExpectedException.none();
76 public UserSessionRule userSession = UserSessionRule.standalone();
77 ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
83 PermissionTemplateDto permissionTemplate;
87 dbClient = db.getDbClient();
88 dbSession = db.getSession();
89 userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
91 PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), new UserGroupFinder(dbClient), resourceTypes);
92 ws = new WsActionTester(new AddUserToTemplateAction(dbClient, dependenciesFinder, userSession));
94 user = insertUser(newUserDto().setLogin(USER_LOGIN));
95 permissionTemplate = insertPermissionTemplate(newPermissionTemplateDto());
100 public void add_user_to_template() {
101 newRequest(USER_LOGIN, permissionTemplate.getUuid(), CODEVIEWER);
103 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), CODEVIEWER)).containsExactly(USER_LOGIN);
107 public void add_user_to_template_by_name() {
109 .setParam(PARAM_USER_LOGIN, USER_LOGIN)
110 .setParam(PARAM_PERMISSION, CODEVIEWER)
111 .setParam(PARAM_TEMPLATE_NAME, permissionTemplate.getName().toUpperCase())
115 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), CODEVIEWER)).containsExactly(USER_LOGIN);
119 public void does_not_add_a_user_twice() {
120 newRequest(USER_LOGIN, permissionTemplate.getUuid(), ISSUE_ADMIN);
121 newRequest(USER_LOGIN, permissionTemplate.getUuid(), ISSUE_ADMIN);
123 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), ISSUE_ADMIN)).containsExactly(USER_LOGIN);
127 public void fail_if_not_a_project_permission() {
128 expectedException.expect(BadRequestException.class);
130 newRequest(USER_LOGIN, permissionTemplate.getUuid(), GlobalPermissions.PREVIEW_EXECUTION);
134 public void fail_if_insufficient_privileges() {
135 expectedException.expect(ForbiddenException.class);
136 userSession.setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
138 newRequest(USER_LOGIN, permissionTemplate.getUuid(), CODEVIEWER);
142 public void fail_if_not_logged_in() {
143 expectedException.expect(UnauthorizedException.class);
144 userSession.anonymous();
146 newRequest(USER_LOGIN, permissionTemplate.getUuid(), CODEVIEWER);
150 public void fail_if_user_missing() {
151 expectedException.expect(IllegalArgumentException.class);
153 newRequest(null, permissionTemplate.getUuid(), CODEVIEWER);
157 public void fail_if_permission_missing() {
158 expectedException.expect(IllegalArgumentException.class);
160 newRequest(USER_LOGIN, permissionTemplate.getUuid(), null);
164 public void fail_if_template_uuid_and_name_are_missing() {
165 expectedException.expect(BadRequestException.class);
167 newRequest(USER_LOGIN, null, CODEVIEWER);
171 public void fail_if_user_does_not_exist() {
172 expectedException.expect(NotFoundException.class);
173 expectedException.expectMessage("User with login 'unknown-login' is not found");
175 newRequest("unknown-login", permissionTemplate.getUuid(), CODEVIEWER);
179 public void fail_if_template_key_does_not_exist() {
180 expectedException.expect(NotFoundException.class);
181 expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
183 newRequest(USER_LOGIN, "unknown-key", CODEVIEWER);
186 private void newRequest(@Nullable String userLogin, @Nullable String templateKey, @Nullable String permission) {
187 TestRequest request = ws.newRequest();
188 if (userLogin != null) {
189 request.setParam(PARAM_USER_LOGIN, userLogin);
191 if (templateKey != null) {
192 request.setParam(PermissionsWsParameters.PARAM_TEMPLATE_UUID, templateKey);
194 if (permission != null) {
195 request.setParam(PermissionsWsParameters.PARAM_PERMISSION, permission);
201 private void commit() {
205 private UserDto insertUser(UserDto userDto) {
206 return dbClient.userDao().insert(dbSession, userDto.setActive(true));
209 private PermissionTemplateDto insertPermissionTemplate(PermissionTemplateDto permissionTemplate) {
210 return dbClient.permissionTemplateDao().insert(dbSession, permissionTemplate);
213 private List<String> getLoginsInTemplateAndPermission(long templateId, String permission) {
214 PermissionQuery permissionQuery = PermissionQuery.builder().permission(permission).membership(IN).build();
215 return from(dbClient.permissionTemplateDao()
216 .selectUsers(dbSession, permissionQuery, templateId, 0, Integer.MAX_VALUE))
217 .transform(UserWithPermissionToUserLogin.INSTANCE)
221 private enum UserWithPermissionToUserLogin implements Function<UserWithPermissionDto, String> {
225 public String apply(@Nonnull UserWithPermissionDto userWithPermission) {
226 return userWithPermission.getLogin();