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.experimental.categories.Category;
30 import org.junit.rules.ExpectedException;
31 import org.sonar.api.resources.Qualifiers;
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.component.ResourceTypesRule;
38 import org.sonar.db.permission.PermissionQuery;
39 import org.sonar.db.permission.PermissionTemplateDto;
40 import org.sonar.db.permission.UserWithPermissionDto;
41 import org.sonar.db.user.UserDto;
42 import org.sonar.server.component.ComponentFinder;
43 import org.sonar.server.exceptions.BadRequestException;
44 import org.sonar.server.exceptions.ForbiddenException;
45 import org.sonar.server.exceptions.NotFoundException;
46 import org.sonar.server.exceptions.UnauthorizedException;
47 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
48 import org.sonar.server.tester.UserSessionRule;
49 import org.sonar.server.usergroups.ws.UserGroupFinder;
50 import org.sonar.server.ws.TestRequest;
51 import org.sonar.server.ws.WsActionTester;
52 import org.sonar.test.DbTests;
54 import static com.google.common.collect.FluentIterable.from;
55 import static org.assertj.core.api.Assertions.assertThat;
56 import static org.sonar.api.web.UserRole.CODEVIEWER;
57 import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
58 import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
59 import static org.sonar.db.user.GroupMembershipQuery.IN;
60 import static org.sonar.db.user.UserTesting.newUserDto;
61 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
62 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
63 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN;
65 @Category(DbTests.class)
66 public class AddUserToTemplateActionTest {
68 private static final String USER_LOGIN = "user-login";
70 public DbTester db = DbTester.create(System2.INSTANCE);
72 public ExpectedException expectedException = ExpectedException.none();
74 public UserSessionRule userSession = UserSessionRule.standalone();
75 ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
81 PermissionTemplateDto permissionTemplate;
85 dbClient = db.getDbClient();
86 dbSession = db.getSession();
87 userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
89 PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), new UserGroupFinder(dbClient), resourceTypes);
90 ws = new WsActionTester(new AddUserToTemplateAction(dbClient, dependenciesFinder, userSession));
92 user = insertUser(newUserDto().setLogin(USER_LOGIN));
93 permissionTemplate = insertPermissionTemplate(newPermissionTemplateDto());
98 public void add_user_to_template() {
99 newRequest(USER_LOGIN, permissionTemplate.getUuid(), CODEVIEWER);
101 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), CODEVIEWER)).containsExactly(USER_LOGIN);
105 public void add_user_to_template_by_name() {
107 .setParam(PARAM_USER_LOGIN, USER_LOGIN)
108 .setParam(PARAM_PERMISSION, CODEVIEWER)
109 .setParam(PARAM_TEMPLATE_NAME, permissionTemplate.getName().toUpperCase())
113 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), CODEVIEWER)).containsExactly(USER_LOGIN);
117 public void does_not_add_a_user_twice() {
118 newRequest(USER_LOGIN, permissionTemplate.getUuid(), ISSUE_ADMIN);
119 newRequest(USER_LOGIN, permissionTemplate.getUuid(), ISSUE_ADMIN);
121 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), ISSUE_ADMIN)).containsExactly(USER_LOGIN);
125 public void fail_if_not_a_project_permission() {
126 expectedException.expect(BadRequestException.class);
128 newRequest(USER_LOGIN, permissionTemplate.getUuid(), GlobalPermissions.PREVIEW_EXECUTION);
132 public void fail_if_insufficient_privileges() {
133 expectedException.expect(ForbiddenException.class);
134 userSession.setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
136 newRequest(USER_LOGIN, permissionTemplate.getUuid(), CODEVIEWER);
140 public void fail_if_not_logged_in() {
141 expectedException.expect(UnauthorizedException.class);
142 userSession.anonymous();
144 newRequest(USER_LOGIN, permissionTemplate.getUuid(), CODEVIEWER);
148 public void fail_if_user_missing() {
149 expectedException.expect(IllegalArgumentException.class);
151 newRequest(null, permissionTemplate.getUuid(), CODEVIEWER);
155 public void fail_if_permission_missing() {
156 expectedException.expect(IllegalArgumentException.class);
158 newRequest(USER_LOGIN, permissionTemplate.getUuid(), null);
162 public void fail_if_template_uuid_and_name_are_missing() {
163 expectedException.expect(BadRequestException.class);
165 newRequest(USER_LOGIN, null, CODEVIEWER);
169 public void fail_if_user_does_not_exist() {
170 expectedException.expect(NotFoundException.class);
171 expectedException.expectMessage("User with login 'unknown-login' is not found");
173 newRequest("unknown-login", permissionTemplate.getUuid(), CODEVIEWER);
177 public void fail_if_template_key_does_not_exist() {
178 expectedException.expect(NotFoundException.class);
179 expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
181 newRequest(USER_LOGIN, "unknown-key", CODEVIEWER);
184 private void newRequest(@Nullable String userLogin, @Nullable String templateKey, @Nullable String permission) {
185 TestRequest request = ws.newRequest();
186 if (userLogin != null) {
187 request.setParam(PARAM_USER_LOGIN, userLogin);
189 if (templateKey != null) {
190 request.setParam(org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID, templateKey);
192 if (permission != null) {
193 request.setParam(PARAM_PERMISSION, permission);
199 private void commit() {
203 private UserDto insertUser(UserDto userDto) {
204 return dbClient.userDao().insert(dbSession, userDto.setActive(true));
207 private PermissionTemplateDto insertPermissionTemplate(PermissionTemplateDto permissionTemplate) {
208 return dbClient.permissionTemplateDao().insert(dbSession, permissionTemplate);
211 private List<String> getLoginsInTemplateAndPermission(long templateId, String permission) {
212 PermissionQuery permissionQuery = PermissionQuery.builder().permission(permission).membership(IN).build();
213 return from(dbClient.permissionTemplateDao()
214 .selectUsers(dbSession, permissionQuery, templateId, 0, Integer.MAX_VALUE))
215 .transform(UserWithPermissionToUserLogin.INSTANCE)
219 private enum UserWithPermissionToUserLogin implements Function<UserWithPermissionDto, String> {
223 public String apply(@Nonnull UserWithPermissionDto userWithPermission) {
224 return userWithPermission.getLogin();