3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info 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 javax.annotation.Nullable;
23 import org.junit.Test;
24 import org.sonar.api.resources.Qualifiers;
25 import org.sonar.api.resources.ResourceTypes;
26 import org.sonar.api.server.ws.WebService;
27 import org.sonar.core.permission.GlobalPermissions;
28 import org.sonar.db.component.ResourceTypesRule;
29 import org.sonar.db.permission.template.PermissionTemplateDto;
30 import org.sonar.db.permission.template.PermissionTemplateUserDto;
31 import org.sonar.db.user.UserDto;
32 import org.sonar.server.exceptions.BadRequestException;
33 import org.sonar.server.exceptions.ForbiddenException;
34 import org.sonar.server.exceptions.NotFoundException;
35 import org.sonar.server.exceptions.UnauthorizedException;
36 import org.sonar.server.issue.ws.AvatarResolverImpl;
37 import org.sonar.server.permission.PermissionService;
38 import org.sonar.server.permission.PermissionServiceImpl;
39 import org.sonar.server.permission.ws.BasePermissionWsTest;
40 import org.sonar.server.permission.ws.RequestValidator;
41 import org.sonar.server.permission.ws.WsParameters;
42 import org.sonar.server.ws.TestRequest;
43 import org.sonarqube.ws.Permissions;
45 import static org.assertj.core.api.Assertions.assertThat;
46 import static org.sonar.api.web.UserRole.ADMIN;
47 import static org.sonar.api.web.UserRole.CODEVIEWER;
48 import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
49 import static org.sonar.api.web.UserRole.USER;
50 import static org.sonar.db.permission.OrganizationPermission.SCAN;
51 import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateUserDto;
52 import static org.sonar.db.user.UserTesting.newUserDto;
53 import static org.sonar.test.JsonAssert.assertJson;
54 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
55 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
56 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
58 public class TemplateUsersActionTest extends BasePermissionWsTest<TemplateUsersAction> {
60 private ResourceTypes resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT);
61 private PermissionService permissionService = new PermissionServiceImpl(resourceTypes);
62 private WsParameters wsParameters = new WsParameters(permissionService);
63 private RequestValidator requestValidator = new RequestValidator(permissionService);
66 protected TemplateUsersAction buildWsAction() {
67 return new TemplateUsersAction(db.getDbClient(), userSession, newPermissionWsSupport(), new AvatarResolverImpl(), wsParameters, requestValidator);
71 public void search_for_users_with_response_example() {
72 UserDto user1 = insertUser(newUserDto().setLogin("admin").setName("Administrator").setEmail("admin@admin.com"));
73 UserDto user2 = insertUser(newUserDto().setLogin("george.orwell").setName("George Orwell").setEmail("george.orwell@1984.net"));
75 PermissionTemplateDto template1 = addTemplateToDefaultOrganization();
76 addUserToTemplate(newPermissionTemplateUser(CODEVIEWER, template1, user1));
77 addUserToTemplate(newPermissionTemplateUser(CODEVIEWER, template1, user2));
78 addUserToTemplate(newPermissionTemplateUser(ADMIN, template1, user2));
79 loginAsAdmin(db.getDefaultOrganization());
81 String result = newRequest(null, template1.getUuid()).execute().getInput();
82 assertJson(result).isSimilarTo(getClass().getResource("template_users-example.json"));
86 public void search_for_users_by_template_name() {
87 loginAsAdmin(db.getDefaultOrganization());
89 UserDto user1 = insertUser(newUserDto().setLogin("login-1").setName("name-1").setEmail("email-1"));
90 UserDto user2 = insertUser(newUserDto().setLogin("login-2").setName("name-2").setEmail("email-2"));
91 UserDto user3 = insertUser(newUserDto().setLogin("login-3").setName("name-3").setEmail("email-3"));
93 PermissionTemplateDto template = addTemplateToDefaultOrganization();
94 addUserToTemplate(newPermissionTemplateUser(USER, template, user1));
95 addUserToTemplate(newPermissionTemplateUser(USER, template, user2));
96 addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template, user1));
97 addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template, user3));
99 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
100 addUserToTemplate(newPermissionTemplateUser(USER, anotherTemplate, user1));
102 Permissions.UsersWsResponse response = newRequest(null, null)
103 .setParam(PARAM_TEMPLATE_NAME, template.getName())
104 .executeProtobuf(Permissions.UsersWsResponse.class);
106 assertThat(response.getUsersList()).extracting("login").containsExactly("login-1", "login-2", "login-3");
107 assertThat(response.getUsers(0).getPermissionsList()).containsOnly("issueadmin", "user");
108 assertThat(response.getUsers(1).getPermissionsList()).containsOnly("user");
109 assertThat(response.getUsers(2).getPermissionsList()).containsOnly("issueadmin");
113 public void search_using_text_query() {
114 loginAsAdmin(db.getDefaultOrganization());
116 UserDto user1 = insertUser(newUserDto().setLogin("login-1").setName("name-1").setEmail("email-1"));
117 UserDto user2 = insertUser(newUserDto().setLogin("login-2").setName("name-2").setEmail("email-2"));
118 UserDto user3 = insertUser(newUserDto().setLogin("login-3").setName("name-3").setEmail("email-3"));
120 PermissionTemplateDto template = addTemplateToDefaultOrganization();
121 addUserToTemplate(newPermissionTemplateUser(USER, template, user1));
122 addUserToTemplate(newPermissionTemplateUser(USER, template, user2));
123 addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template, user1));
124 addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template, user3));
126 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
127 addUserToTemplate(newPermissionTemplateUser(USER, anotherTemplate, user1));
129 Permissions.UsersWsResponse response = newRequest(null, null)
130 .setParam(PARAM_TEMPLATE_NAME, template.getName())
131 .setParam(WebService.Param.TEXT_QUERY, "ame-1")
132 .executeProtobuf(Permissions.UsersWsResponse.class);
134 assertThat(response.getUsersList()).extracting("login").containsOnly("login-1");
138 public void search_using_permission() {
139 UserDto user1 = insertUser(newUserDto().setLogin("login-1").setName("name-1").setEmail("email-1"));
140 UserDto user2 = insertUser(newUserDto().setLogin("login-2").setName("name-2").setEmail("email-2"));
141 UserDto user3 = insertUser(newUserDto().setLogin("login-3").setName("name-3").setEmail("email-3"));
143 PermissionTemplateDto template = addTemplateToDefaultOrganization();
144 addUserToTemplate(newPermissionTemplateUser(USER, template, user1));
145 addUserToTemplate(newPermissionTemplateUser(USER, template, user2));
146 addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template, user1));
147 addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template, user3));
149 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
150 addUserToTemplate(newPermissionTemplateUser(USER, anotherTemplate, user1));
152 loginAsAdmin(db.getDefaultOrganization());
153 Permissions.UsersWsResponse response = newRequest(USER, template.getUuid())
154 .executeProtobuf(Permissions.UsersWsResponse.class);
155 assertThat(response.getUsersList()).extracting("login").containsExactly("login-1", "login-2");
156 assertThat(response.getUsers(0).getPermissionsList()).containsOnly("issueadmin", "user");
157 assertThat(response.getUsers(1).getPermissionsList()).containsOnly("user");
161 public void search_with_pagination() {
162 UserDto user1 = insertUser(newUserDto().setLogin("login-1").setName("name-1").setEmail("email-1"));
163 UserDto user2 = insertUser(newUserDto().setLogin("login-2").setName("name-2").setEmail("email-2"));
164 UserDto user3 = insertUser(newUserDto().setLogin("login-3").setName("name-3").setEmail("email-3"));
166 PermissionTemplateDto template = addTemplateToDefaultOrganization();
167 addUserToTemplate(newPermissionTemplateUser(USER, template, user1));
168 addUserToTemplate(newPermissionTemplateUser(USER, template, user2));
169 addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template, user1));
170 addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template, user3));
172 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
173 addUserToTemplate(newPermissionTemplateUser(USER, anotherTemplate, user1));
175 loginAsAdmin(db.getDefaultOrganization());
176 Permissions.UsersWsResponse response = newRequest(USER, null)
177 .setParam(PARAM_TEMPLATE_NAME, template.getName())
178 .setParam(WebService.Param.SELECTED, "all")
179 .setParam(WebService.Param.PAGE, "2")
180 .setParam(WebService.Param.PAGE_SIZE, "1")
181 .executeProtobuf(Permissions.UsersWsResponse.class);
183 assertThat(response.getUsersList()).extracting("login").containsOnly("login-2");
187 public void users_are_sorted_by_name() {
188 UserDto user1 = insertUser(newUserDto().setLogin("login-2").setName("name-2"));
189 UserDto user2 = insertUser(newUserDto().setLogin("login-3").setName("name-3"));
190 UserDto user3 = insertUser(newUserDto().setLogin("login-1").setName("name-1"));
192 PermissionTemplateDto template = addTemplateToDefaultOrganization();
193 addUserToTemplate(newPermissionTemplateUser(USER, template, user1));
194 addUserToTemplate(newPermissionTemplateUser(USER, template, user2));
195 addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template, user3));
197 loginAsAdmin(db.getDefaultOrganization());
198 Permissions.UsersWsResponse response = newRequest(null, null)
199 .setParam(PARAM_TEMPLATE_NAME, template.getName())
200 .executeProtobuf(Permissions.UsersWsResponse.class);
202 assertThat(response.getUsersList()).extracting("login").containsExactly("login-1", "login-2", "login-3");
206 public void fail_if_not_a_project_permission() {
207 PermissionTemplateDto template = addTemplateToDefaultOrganization();
208 loginAsAdmin(db.getDefaultOrganization());
210 expectedException.expect(IllegalArgumentException.class);
212 newRequest(GlobalPermissions.PROVISIONING, template.getUuid())
217 public void fail_if_no_template_param() {
218 loginAsAdmin(db.getDefaultOrganization());
220 expectedException.expect(BadRequestException.class);
222 newRequest(null, null)
227 public void fail_if_template_does_not_exist() {
228 loginAsAdmin(db.getDefaultOrganization());
230 expectedException.expect(NotFoundException.class);
232 newRequest(null, "unknown-template-uuid")
237 public void fail_if_template_uuid_and_name_provided() {
238 PermissionTemplateDto template = addTemplateToDefaultOrganization();
239 loginAsAdmin(db.getDefaultOrganization());
241 expectedException.expect(BadRequestException.class);
243 newRequest(null, template.getUuid())
244 .setParam(PARAM_TEMPLATE_NAME, template.getName())
249 public void fail_if_not_logged_in() {
250 PermissionTemplateDto template = addTemplateToDefaultOrganization();
251 userSession.anonymous();
253 expectedException.expect(UnauthorizedException.class);
255 newRequest(null, template.getUuid()).execute();
259 public void fail_if_insufficient_privileges() {
260 PermissionTemplateDto template = addTemplateToDefaultOrganization();
261 userSession.logIn().addPermission(SCAN, db.getDefaultOrganization());
263 expectedException.expect(ForbiddenException.class);
265 newRequest(null, template.getUuid()).execute();
268 private UserDto insertUser(UserDto userDto) {
269 db.users().insertUser(userDto);
270 db.organizations().addMember(db.getDefaultOrganization(), userDto);
274 private void addUserToTemplate(PermissionTemplateUserDto dto) {
275 db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), dto.getTemplateId(), dto.getUserId(), dto.getPermission());
279 private static PermissionTemplateUserDto newPermissionTemplateUser(String permission, PermissionTemplateDto template, UserDto user) {
280 return newPermissionTemplateUserDto()
281 .setPermission(permission)
282 .setTemplateId(template.getId())
283 .setUserId(user.getId());
286 private TestRequest newRequest(@Nullable String permission, @Nullable String templateUuid) {
287 TestRequest request = newRequest();
288 if (permission != null) {
289 request.setParam(PARAM_PERMISSION, permission);
291 if (templateUuid != null) {
292 request.setParam(PARAM_TEMPLATE_ID, templateUuid);