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 java.io.InputStream;
23 import javax.annotation.Nullable;
24 import org.junit.Test;
25 import org.sonar.api.server.ws.WebService;
26 import org.sonar.core.permission.GlobalPermissions;
27 import org.sonar.db.permission.template.PermissionTemplateDto;
28 import org.sonar.db.permission.template.PermissionTemplateUserDto;
29 import org.sonar.db.user.UserDto;
30 import org.sonar.server.exceptions.BadRequestException;
31 import org.sonar.server.exceptions.ForbiddenException;
32 import org.sonar.server.exceptions.NotFoundException;
33 import org.sonar.server.exceptions.UnauthorizedException;
34 import org.sonar.server.permission.ws.BasePermissionWsTest;
35 import org.sonar.server.ws.TestRequest;
36 import org.sonarqube.ws.WsPermissions;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.sonar.api.web.UserRole.ADMIN;
40 import static org.sonar.api.web.UserRole.CODEVIEWER;
41 import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
42 import static org.sonar.api.web.UserRole.USER;
43 import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
44 import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateUserDto;
45 import static org.sonar.db.user.UserTesting.newUserDto;
46 import static org.sonar.test.JsonAssert.assertJson;
47 import static org.sonarqube.ws.MediaTypes.PROTOBUF;
48 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
49 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
50 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
52 public class TemplateUsersActionTest extends BasePermissionWsTest<TemplateUsersAction> {
55 protected TemplateUsersAction buildWsAction() {
56 return new TemplateUsersAction(db.getDbClient(), userSession, newPermissionWsSupport());
60 public void search_for_users_with_response_example() throws Exception {
61 UserDto user1 = insertUser(newUserDto().setLogin("admin").setName("Administrator").setEmail("admin@admin.com"));
62 UserDto user2 = insertUser(newUserDto().setLogin("george.orwell").setName("George Orwell").setEmail("george.orwell@1984.net"));
64 PermissionTemplateDto template1 = addTemplateToDefaultOrganization();
65 addUserToTemplate(newPermissionTemplateUser(CODEVIEWER, template1, user1));
66 addUserToTemplate(newPermissionTemplateUser(CODEVIEWER, template1, user2));
67 addUserToTemplate(newPermissionTemplateUser(ADMIN, template1, user2));
68 loginAsAdminOnDefaultOrganization();
70 String result = newRequest(null, template1.getUuid()).execute().getInput();
71 assertJson(result).isSimilarTo(getClass().getResource("template_users-example.json"));
75 public void search_for_users_by_template_name() throws Exception {
76 loginAsAdminOnDefaultOrganization();
78 UserDto user1 = insertUser(newUserDto().setLogin("login-1").setName("name-1").setEmail("email-1"));
79 UserDto user2 = insertUser(newUserDto().setLogin("login-2").setName("name-2").setEmail("email-2"));
80 UserDto user3 = insertUser(newUserDto().setLogin("login-3").setName("name-3").setEmail("email-3"));
82 PermissionTemplateDto template = addTemplateToDefaultOrganization();
83 addUserToTemplate(newPermissionTemplateUser(USER, template, user1));
84 addUserToTemplate(newPermissionTemplateUser(USER, template, user2));
85 addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template, user1));
86 addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template, user3));
88 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
89 addUserToTemplate(newPermissionTemplateUser(USER, anotherTemplate, user1));
91 InputStream bytes = newRequest(null, null)
92 .setParam(PARAM_TEMPLATE_NAME, template.getName())
93 .setMediaType(PROTOBUF)
94 .execute().getInputStream();
96 WsPermissions.UsersWsResponse response = WsPermissions.UsersWsResponse.parseFrom(bytes);
97 assertThat(response.getUsersList()).extracting("login").containsExactly("login-1", "login-2", "login-3");
98 assertThat(response.getUsers(0).getPermissionsList()).containsOnly("issueadmin", "user");
99 assertThat(response.getUsers(1).getPermissionsList()).containsOnly("user");
100 assertThat(response.getUsers(2).getPermissionsList()).containsOnly("issueadmin");
104 public void search_using_text_query() throws Exception {
105 loginAsAdminOnDefaultOrganization();
107 UserDto user1 = insertUser(newUserDto().setLogin("login-1").setName("name-1").setEmail("email-1"));
108 UserDto user2 = insertUser(newUserDto().setLogin("login-2").setName("name-2").setEmail("email-2"));
109 UserDto user3 = insertUser(newUserDto().setLogin("login-3").setName("name-3").setEmail("email-3"));
111 PermissionTemplateDto template = addTemplateToDefaultOrganization();
112 addUserToTemplate(newPermissionTemplateUser(USER, template, user1));
113 addUserToTemplate(newPermissionTemplateUser(USER, template, user2));
114 addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template, user1));
115 addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template, user3));
117 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
118 addUserToTemplate(newPermissionTemplateUser(USER, anotherTemplate, user1));
120 InputStream bytes = newRequest(null, null)
121 .setParam(PARAM_TEMPLATE_NAME, template.getName())
122 .setParam(WebService.Param.TEXT_QUERY, "ame-1")
123 .setMediaType(PROTOBUF)
124 .execute().getInputStream();
126 WsPermissions.UsersWsResponse response = WsPermissions.UsersWsResponse.parseFrom(bytes);
127 assertThat(response.getUsersList()).extracting("login").containsOnly("login-1");
131 public void search_using_permission() throws Exception {
132 UserDto user1 = insertUser(newUserDto().setLogin("login-1").setName("name-1").setEmail("email-1"));
133 UserDto user2 = insertUser(newUserDto().setLogin("login-2").setName("name-2").setEmail("email-2"));
134 UserDto user3 = insertUser(newUserDto().setLogin("login-3").setName("name-3").setEmail("email-3"));
136 PermissionTemplateDto template = addTemplateToDefaultOrganization();
137 addUserToTemplate(newPermissionTemplateUser(USER, template, user1));
138 addUserToTemplate(newPermissionTemplateUser(USER, template, user2));
139 addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template, user1));
140 addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template, user3));
142 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
143 addUserToTemplate(newPermissionTemplateUser(USER, anotherTemplate, user1));
145 loginAsAdminOnDefaultOrganization();
146 InputStream bytes = newRequest(USER, template.getUuid())
147 .setMediaType(PROTOBUF)
148 .execute().getInputStream();
149 WsPermissions.UsersWsResponse response = WsPermissions.UsersWsResponse.parseFrom(bytes);
150 assertThat(response.getUsersList()).extracting("login").containsExactly("login-1", "login-2");
151 assertThat(response.getUsers(0).getPermissionsList()).containsOnly("issueadmin", "user");
152 assertThat(response.getUsers(1).getPermissionsList()).containsOnly("user");
156 public void search_with_pagination() throws Exception {
157 UserDto user1 = insertUser(newUserDto().setLogin("login-1").setName("name-1").setEmail("email-1"));
158 UserDto user2 = insertUser(newUserDto().setLogin("login-2").setName("name-2").setEmail("email-2"));
159 UserDto user3 = insertUser(newUserDto().setLogin("login-3").setName("name-3").setEmail("email-3"));
161 PermissionTemplateDto template = addTemplateToDefaultOrganization();
162 addUserToTemplate(newPermissionTemplateUser(USER, template, user1));
163 addUserToTemplate(newPermissionTemplateUser(USER, template, user2));
164 addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template, user1));
165 addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template, user3));
167 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
168 addUserToTemplate(newPermissionTemplateUser(USER, anotherTemplate, user1));
170 loginAsAdminOnDefaultOrganization();
171 InputStream bytes = newRequest(USER, null)
172 .setParam(PARAM_TEMPLATE_NAME, template.getName())
173 .setParam(WebService.Param.SELECTED, "all")
174 .setParam(WebService.Param.PAGE, "2")
175 .setParam(WebService.Param.PAGE_SIZE, "1")
176 .setMediaType(PROTOBUF)
177 .execute().getInputStream();
179 WsPermissions.UsersWsResponse response = WsPermissions.UsersWsResponse.parseFrom(bytes);
180 assertThat(response.getUsersList()).extracting("login").containsOnly("login-2");
184 public void users_are_sorted_by_name() throws Exception {
185 UserDto user1 = insertUser(newUserDto().setLogin("login-2").setName("name-2"));
186 UserDto user2 = insertUser(newUserDto().setLogin("login-3").setName("name-3"));
187 UserDto user3 = insertUser(newUserDto().setLogin("login-1").setName("name-1"));
189 PermissionTemplateDto template = addTemplateToDefaultOrganization();
190 addUserToTemplate(newPermissionTemplateUser(USER, template, user1));
191 addUserToTemplate(newPermissionTemplateUser(USER, template, user2));
192 addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template, user3));
194 loginAsAdminOnDefaultOrganization();
195 InputStream bytes = newRequest(null, null)
196 .setParam(PARAM_TEMPLATE_NAME, template.getName())
197 .setMediaType(PROTOBUF)
198 .execute().getInputStream();
200 WsPermissions.UsersWsResponse response = WsPermissions.UsersWsResponse.parseFrom(bytes);
201 assertThat(response.getUsersList()).extracting("login").containsExactly("login-1", "login-2", "login-3");
205 public void empty_result_when_no_user_on_template() throws Exception {
206 UserDto user = insertUser(newUserDto().setLogin("login-1").setName("name-1").setEmail("email-1"));
207 PermissionTemplateDto template = addTemplateToDefaultOrganization();
208 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
209 addUserToTemplate(newPermissionTemplateUser(USER, anotherTemplate, user));
211 loginAsAdminOnDefaultOrganization();
212 InputStream bytes = newRequest(null, null)
213 .setParam(PARAM_TEMPLATE_NAME, template.getName())
214 .setMediaType(PROTOBUF)
218 WsPermissions.UsersWsResponse response = WsPermissions.UsersWsResponse.parseFrom(bytes);
219 assertThat(response.getUsersList()).isEmpty();
223 public void fail_if_not_a_project_permission() throws Exception {
224 PermissionTemplateDto template = addTemplateToDefaultOrganization();
225 loginAsAdminOnDefaultOrganization();
227 expectedException.expect(IllegalArgumentException.class);
229 newRequest(GlobalPermissions.PROVISIONING, template.getUuid())
234 public void fail_if_no_template_param() throws Exception {
235 loginAsAdminOnDefaultOrganization();
237 expectedException.expect(BadRequestException.class);
239 newRequest(null, null)
244 public void fail_if_template_does_not_exist() throws Exception {
245 loginAsAdminOnDefaultOrganization();
247 expectedException.expect(NotFoundException.class);
249 newRequest(null, "unknown-template-uuid")
254 public void fail_if_template_uuid_and_name_provided() throws Exception {
255 PermissionTemplateDto template = addTemplateToDefaultOrganization();
256 loginAsAdminOnDefaultOrganization();
258 expectedException.expect(BadRequestException.class);
260 newRequest(null, template.getUuid())
261 .setParam(PARAM_TEMPLATE_NAME, template.getName())
266 public void fail_if_not_logged_in() throws Exception {
267 PermissionTemplateDto template = addTemplateToDefaultOrganization();
268 userSession.anonymous();
270 expectedException.expect(UnauthorizedException.class);
272 newRequest(null, template.getUuid()).execute();
276 public void fail_if_insufficient_privileges() throws Exception {
277 PermissionTemplateDto template = addTemplateToDefaultOrganization();
278 userSession.logIn().addOrganizationPermission(db.getDefaultOrganization().getUuid(), SCAN_EXECUTION);
280 expectedException.expect(ForbiddenException.class);
282 newRequest(null, template.getUuid()).execute();
285 private UserDto insertUser(UserDto userDto) {
286 return db.users().insertUser(userDto);
289 private void addUserToTemplate(PermissionTemplateUserDto dto) {
290 db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), dto.getTemplateId(), dto.getUserId(), dto.getPermission());
294 private static PermissionTemplateUserDto newPermissionTemplateUser(String permission, PermissionTemplateDto template, UserDto user) {
295 return newPermissionTemplateUserDto()
296 .setPermission(permission)
297 .setTemplateId(template.getId())
298 .setUserId(user.getId());
301 private TestRequest newRequest(@Nullable String permission, @Nullable String templateUuid) {
302 TestRequest request = newRequest();
303 if (permission != null) {
304 request.setParam(PARAM_PERMISSION, permission);
306 if (templateUuid != null) {
307 request.setParam(PARAM_TEMPLATE_ID, templateUuid);