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.core.permission.GlobalPermissions;
26 import org.sonar.db.organization.OrganizationDto;
27 import org.sonar.db.permission.template.PermissionTemplateDto;
28 import org.sonar.db.permission.template.PermissionTemplateGroupDto;
29 import org.sonar.db.user.GroupDto;
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.sonarqube.ws.WsPermissions.WsGroupsResponse;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.sonar.api.server.ws.WebService.Param.PAGE;
39 import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
40 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
41 import static org.sonar.api.web.UserRole.ADMIN;
42 import static org.sonar.api.web.UserRole.CODEVIEWER;
43 import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
44 import static org.sonar.api.web.UserRole.USER;
45 import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateGroupDto;
46 import static org.sonar.db.user.GroupTesting.newGroupDto;
47 import static org.sonar.test.JsonAssert.assertJson;
48 import static org.sonarqube.ws.MediaTypes.PROTOBUF;
49 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
50 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
51 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
53 public class TemplateGroupsActionTest extends BasePermissionWsTest<TemplateGroupsAction> {
56 protected TemplateGroupsAction buildWsAction() {
57 return new TemplateGroupsAction(db.getDbClient(), userSession, newPermissionWsSupport());
61 public void template_groups_of_json_example() throws Exception {
62 GroupDto adminGroup = insertGroupOnDefaultOrganization("sonar-administrators", "System administrators");
63 GroupDto userGroup = insertGroupOnDefaultOrganization("sonar-users", "Any new users created will automatically join this group");
65 PermissionTemplateDto template = addTemplateToDefaultOrganization();
66 addGroupToTemplate(newPermissionTemplateGroup(ISSUE_ADMIN, template.getId(), adminGroup.getId()));
67 addGroupToTemplate(newPermissionTemplateGroup(ISSUE_ADMIN, template.getId(), userGroup.getId()));
69 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), null));
70 addGroupToTemplate(newPermissionTemplateGroup(ISSUE_ADMIN, template.getId(), null));
72 loginAsAdminOnDefaultOrganization();
74 String response = newRequest()
75 .setParam(PARAM_PERMISSION, ISSUE_ADMIN)
76 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
82 .withStrictArrayOrder()
83 .isSimilarTo(getClass().getResource("template_groups-example.json"));
87 public void do_not_fail_when_group_name_exists_in_multiple_organizations() {
88 PermissionTemplateDto template = addTemplateToDefaultOrganization();
90 String groupName = "group-name";
91 GroupDto group1 = db.users().insertGroup(db.getDefaultOrganization(), groupName);
92 addGroupToTemplate(newPermissionTemplateGroup(CODEVIEWER, template.getId(), group1.getId()));
93 addGroupToTemplate(newPermissionTemplateGroup(ADMIN, template.getId(), group1.getId()));
95 OrganizationDto otherOrganization = db.organizations().insert();
96 db.users().insertGroup(otherOrganization, groupName);
98 loginAsAdminOnDefaultOrganization();
101 .setMediaType(PROTOBUF)
102 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
103 .setParam(TEXT_QUERY, "-nam")
110 public void return_all_permissions_of_matching_groups() throws Exception {
111 PermissionTemplateDto template = addTemplateToDefaultOrganization();
113 GroupDto group1 = db.users().insertGroup(db.getDefaultOrganization(), "group-1-name");
114 addGroupToTemplate(newPermissionTemplateGroup(CODEVIEWER, template.getId(), group1.getId()));
115 addGroupToTemplate(newPermissionTemplateGroup(ADMIN, template.getId(), group1.getId()));
117 GroupDto group2 = db.users().insertGroup(db.getDefaultOrganization(), "group-2-name");
118 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), group2.getId()));
119 addGroupToTemplate(newPermissionTemplateGroup(ADMIN, template.getId(), group2.getId()));
121 GroupDto group3 = db.users().insertGroup(db.getDefaultOrganization(), "group-3-name");
124 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), null));
125 addGroupToTemplate(newPermissionTemplateGroup(ISSUE_ADMIN, template.getId(), null));
127 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
128 addGroupToTemplate(newPermissionTemplateGroup(ADMIN, anotherTemplate.getId(), group3.getId()));
130 loginAsAdminOnDefaultOrganization();
132 InputStream output = newRequest()
133 .setMediaType(PROTOBUF)
134 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
137 WsGroupsResponse response = WsGroupsResponse.parseFrom(output);
139 assertThat(response.getGroupsList()).extracting("name").containsExactly("Anyone", "group-1-name", "group-2-name");
140 assertThat(response.getGroups(0).getPermissionsList()).containsOnly("user", "issueadmin");
141 assertThat(response.getGroups(1).getPermissionsList()).containsOnly("codeviewer", "admin");
142 assertThat(response.getGroups(2).getPermissionsList()).containsOnly("user", "admin");
146 public void search_by_permission() throws Exception {
147 PermissionTemplateDto template = addTemplateToDefaultOrganization();
149 GroupDto group1 = db.users().insertGroup(db.getDefaultOrganization(), "group-1-name");
150 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), group1.getId()));
151 addGroupToTemplate(newPermissionTemplateGroup(CODEVIEWER, template.getId(), group1.getId()));
153 GroupDto group2 = db.users().insertGroup(db.getDefaultOrganization(), "group-2-name");
154 addGroupToTemplate(newPermissionTemplateGroup(ADMIN, template.getId(), group2.getId()));
156 GroupDto group3 = db.users().insertGroup(db.getDefaultOrganization(), "group-3-name");
159 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), null));
161 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
162 addGroupToTemplate(newPermissionTemplateGroup(ADMIN, anotherTemplate.getId(), group3.getId()));
164 loginAsAdminOnDefaultOrganization();
166 InputStream output = newRequest()
167 .setMediaType(PROTOBUF)
168 .setParam(PARAM_PERMISSION, USER)
169 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
172 WsGroupsResponse response = WsGroupsResponse.parseFrom(output);
174 assertThat(response.getGroupsList()).extracting("name").containsExactly("Anyone", "group-1-name");
175 assertThat(response.getGroups(0).getPermissionsList()).containsOnly("user");
176 assertThat(response.getGroups(1).getPermissionsList()).containsOnly("user", "codeviewer");
180 public void search_by_template_name() throws Exception {
181 OrganizationDto defaultOrg = db.getDefaultOrganization();
182 GroupDto group1 = db.users().insertGroup(defaultOrg, "group-1-name");
183 GroupDto group2 = db.users().insertGroup(defaultOrg, "group-2-name");
184 GroupDto group3 = db.users().insertGroup(defaultOrg, "group-3-name");
186 PermissionTemplateDto template = addTemplateToDefaultOrganization();
187 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), group1.getId()));
188 addGroupToTemplate(newPermissionTemplateGroup(ADMIN, template.getId(), group2.getId()));
189 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), null));
191 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
192 addGroupToTemplate(newPermissionTemplateGroup(USER, anotherTemplate.getId(), group1.getId()));
194 loginAsAdminOnDefaultOrganization();
196 InputStream output = newRequest()
197 .setMediaType(PROTOBUF)
198 .setParam(PARAM_TEMPLATE_NAME, template.getName())
201 WsGroupsResponse response = WsGroupsResponse.parseFrom(output);
203 assertThat(response.getGroupsList()).extracting("name").containsExactly("Anyone", "group-1-name", "group-2-name");
207 public void search_with_pagination() throws Exception {
208 OrganizationDto defaultOrg = db.getDefaultOrganization();
209 PermissionTemplateDto template = addTemplateToDefaultOrganization();
210 GroupDto group1 = db.users().insertGroup(defaultOrg, "group-1-name");
211 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), group1.getId()));
212 GroupDto group2 = db.users().insertGroup(defaultOrg, "group-2-name");
213 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), group2.getId()));
215 loginAsAdminOnDefaultOrganization();
217 InputStream output = newRequest()
218 .setMediaType(PROTOBUF)
219 .setParam(PARAM_PERMISSION, USER)
220 .setParam(PARAM_TEMPLATE_NAME, template.getName())
222 .setParam(PAGE_SIZE, "1")
225 WsGroupsResponse response = WsGroupsResponse.parseFrom(output);
227 assertThat(response.getGroupsList()).extracting("name").containsExactly("group-2-name");
231 public void search_with_text_query() throws Exception {
232 OrganizationDto defaultOrg = db.getDefaultOrganization();
233 PermissionTemplateDto template = addTemplateToDefaultOrganization();
234 GroupDto group1 = db.users().insertGroup(defaultOrg, "group-1-name");
235 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), group1.getId()));
236 GroupDto group2 = db.users().insertGroup(defaultOrg, "group-2-name");
237 GroupDto group3 = db.users().insertGroup(defaultOrg, "group-3");
239 loginAsAdminOnDefaultOrganization();
241 InputStream output = newRequest()
242 .setMediaType(PROTOBUF)
243 .setParam(PARAM_TEMPLATE_NAME, template.getName())
244 .setParam(TEXT_QUERY, "-nam")
247 WsGroupsResponse response = WsGroupsResponse.parseFrom(output);
249 assertThat(response.getGroupsList()).extracting("name").containsExactly("group-1-name", "group-2-name");
253 public void search_with_text_query_return_all_groups_even_when_no_permission_set() throws Exception {
254 OrganizationDto defaultOrg = db.getDefaultOrganization();
255 PermissionTemplateDto template = addTemplateToDefaultOrganization();
256 db.users().insertGroup(defaultOrg, "group-1-name");
257 db.users().insertGroup(defaultOrg, "group-2-name");
258 db.users().insertGroup(defaultOrg, "group-3-name");
260 loginAsAdminOnDefaultOrganization();
262 InputStream output = newRequest()
263 .setMediaType(PROTOBUF)
264 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
265 .setParam(TEXT_QUERY, "-name")
268 WsGroupsResponse response = WsGroupsResponse.parseFrom(output);
270 assertThat(response.getGroupsList()).extracting("name").containsExactly("group-1-name", "group-2-name", "group-3-name");
271 assertThat(response.getGroups(0).getPermissionsList()).isEmpty();
272 assertThat(response.getGroups(1).getPermissionsList()).isEmpty();
273 assertThat(response.getGroups(2).getPermissionsList()).isEmpty();
277 public void search_with_text_query_return_anyone_group_even_when_no_permission_set() throws Exception {
278 PermissionTemplateDto template = addTemplateToDefaultOrganization();
279 GroupDto group = db.users().insertGroup(db.getDefaultOrganization(), "group");
280 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), group.getId()));
282 loginAsAdminOnDefaultOrganization();
284 InputStream output = newRequest()
285 .setMediaType(PROTOBUF)
286 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
287 .setParam(TEXT_QUERY, "nyo")
290 WsGroupsResponse response = WsGroupsResponse.parseFrom(output);
292 assertThat(response.getGroupsList()).extracting("name").containsExactly("Anyone");
293 assertThat(response.getGroups(0).getPermissionsList()).isEmpty();
297 public void fail_if_not_logged_in() throws Exception {
298 PermissionTemplateDto template1 = addTemplateToDefaultOrganization();
299 userSession.anonymous();
301 expectedException.expect(UnauthorizedException.class);
304 .setParam(PARAM_PERMISSION, USER)
305 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
310 public void fail_if_insufficient_privileges() throws Exception {
311 PermissionTemplateDto template1 = addTemplateToDefaultOrganization();
314 expectedException.expect(ForbiddenException.class);
317 .setParam(PARAM_PERMISSION, USER)
318 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
323 public void fail_if_template_uuid_and_name_provided() throws Exception {
324 PermissionTemplateDto template1 = addTemplateToDefaultOrganization();
325 loginAsAdminOnDefaultOrganization();
327 expectedException.expect(BadRequestException.class);
330 .setParam(PARAM_PERMISSION, USER)
331 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
332 .setParam(PARAM_TEMPLATE_NAME, template1.getName())
337 public void fail_if_template_uuid_nor_name_provided() throws Exception {
338 loginAsAdminOnDefaultOrganization();
340 expectedException.expect(BadRequestException.class);
343 .setParam(PARAM_PERMISSION, USER)
348 public void fail_if_template_is_not_found() throws Exception {
349 loginAsAdminOnDefaultOrganization();
351 expectedException.expect(NotFoundException.class);
354 .setParam(PARAM_PERMISSION, USER)
355 .setParam(PARAM_TEMPLATE_ID, "unknown-uuid")
360 public void fail_if_not_a_project_permission() throws Exception {
361 loginAsAdminOnDefaultOrganization();
362 PermissionTemplateDto template1 = addTemplateToDefaultOrganization();
364 expectedException.expect(IllegalArgumentException.class);
367 .setParam(PARAM_PERMISSION, GlobalPermissions.QUALITY_GATE_ADMIN)
368 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
372 private GroupDto insertGroupOnDefaultOrganization(String name, String description) {
373 return db.users().insertGroup(newGroupDto().setName(name).setDescription(description).setOrganizationUuid(db.getDefaultOrganization().getUuid()));
376 private void addGroupToTemplate(PermissionTemplateGroupDto permissionTemplateGroup) {
377 db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), permissionTemplateGroup);
380 private static PermissionTemplateGroupDto newPermissionTemplateGroup(String permission, long templateId, @Nullable Long groupId) {
381 return newPermissionTemplateGroupDto()
382 .setPermission(permission)
383 .setTemplateId(templateId)
384 .setGroupId(groupId);
387 private void commit() {