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 javax.annotation.Nullable;
23 import org.junit.Test;
24 import org.sonar.core.permission.GlobalPermissions;
25 import org.sonar.db.organization.OrganizationDto;
26 import org.sonar.db.permission.template.PermissionTemplateDto;
27 import org.sonar.db.permission.template.PermissionTemplateGroupDto;
28 import org.sonar.db.user.GroupDto;
29 import org.sonar.server.exceptions.BadRequestException;
30 import org.sonar.server.exceptions.ForbiddenException;
31 import org.sonar.server.exceptions.NotFoundException;
32 import org.sonar.server.exceptions.UnauthorizedException;
33 import org.sonar.server.permission.ws.BasePermissionWsTest;
34 import org.sonar.server.ws.WsTester;
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.CONTROLLER;
50 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
51 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
52 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
54 public class TemplateGroupsActionTest extends BasePermissionWsTest<TemplateGroupsAction> {
57 protected TemplateGroupsAction buildWsAction() {
58 return new TemplateGroupsAction(db.getDbClient(), userSession, newPermissionWsSupport());
62 public void template_groups_of_json_example() throws Exception {
63 GroupDto adminGroup = insertGroupOnDefaultOrganization("sonar-administrators", "System administrators");
64 GroupDto userGroup = insertGroupOnDefaultOrganization("sonar-users", "Any new users created will automatically join this group");
66 PermissionTemplateDto template = addTemplateToDefaultOrganization();
67 addGroupToTemplate(newPermissionTemplateGroup(ISSUE_ADMIN, template.getId(), adminGroup.getId()));
68 addGroupToTemplate(newPermissionTemplateGroup(ISSUE_ADMIN, template.getId(), userGroup.getId()));
70 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), null));
71 addGroupToTemplate(newPermissionTemplateGroup(ISSUE_ADMIN, template.getId(), null));
73 loginAsAdminOnDefaultOrganization();
75 String response = newRequest()
76 .setParam(PARAM_PERMISSION, ISSUE_ADMIN)
77 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
78 .execute().outputAsString();
82 .withStrictArrayOrder()
83 .isSimilarTo(getClass().getResource("template_groups-example.json"));
87 public void return_all_permissions_of_matching_groups() throws Exception {
88 PermissionTemplateDto template = addTemplateToDefaultOrganization();
90 GroupDto group1 = db.users().insertGroup(db.getDefaultOrganization(), "group-1-name");
91 addGroupToTemplate(newPermissionTemplateGroup(CODEVIEWER, template.getId(), group1.getId()));
92 addGroupToTemplate(newPermissionTemplateGroup(ADMIN, template.getId(), group1.getId()));
94 GroupDto group2 = db.users().insertGroup(db.getDefaultOrganization(), "group-2-name");
95 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), group2.getId()));
96 addGroupToTemplate(newPermissionTemplateGroup(ADMIN, template.getId(), group2.getId()));
98 GroupDto group3 = db.users().insertGroup(db.getDefaultOrganization(), "group-3-name");
101 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), null));
102 addGroupToTemplate(newPermissionTemplateGroup(ISSUE_ADMIN, template.getId(), null));
104 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
105 addGroupToTemplate(newPermissionTemplateGroup(ADMIN, anotherTemplate.getId(), group3.getId()));
107 loginAsAdminOnDefaultOrganization();
109 byte[] output = newRequest()
110 .setMediaType(PROTOBUF)
111 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
114 WsGroupsResponse response = WsGroupsResponse.parseFrom(output);
116 assertThat(response.getGroupsList()).extracting("name").containsExactly("Anyone", "group-1-name", "group-2-name");
117 assertThat(response.getGroups(0).getPermissionsList()).containsOnly("user", "issueadmin");
118 assertThat(response.getGroups(1).getPermissionsList()).containsOnly("codeviewer", "admin");
119 assertThat(response.getGroups(2).getPermissionsList()).containsOnly("user", "admin");
123 public void search_by_permission() throws Exception {
124 PermissionTemplateDto template = addTemplateToDefaultOrganization();
126 GroupDto group1 = db.users().insertGroup(db.getDefaultOrganization(), "group-1-name");
127 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), group1.getId()));
128 addGroupToTemplate(newPermissionTemplateGroup(CODEVIEWER, template.getId(), group1.getId()));
130 GroupDto group2 = db.users().insertGroup(db.getDefaultOrganization(), "group-2-name");
131 addGroupToTemplate(newPermissionTemplateGroup(ADMIN, template.getId(), group2.getId()));
133 GroupDto group3 = db.users().insertGroup(db.getDefaultOrganization(), "group-3-name");
136 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), null));
138 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
139 addGroupToTemplate(newPermissionTemplateGroup(ADMIN, anotherTemplate.getId(), group3.getId()));
141 loginAsAdminOnDefaultOrganization();
143 byte[] output = newRequest()
144 .setMediaType(PROTOBUF)
145 .setParam(PARAM_PERMISSION, USER)
146 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
149 WsGroupsResponse response = WsGroupsResponse.parseFrom(output);
151 assertThat(response.getGroupsList()).extracting("name").containsExactly("Anyone", "group-1-name");
152 assertThat(response.getGroups(0).getPermissionsList()).containsOnly("user");
153 assertThat(response.getGroups(1).getPermissionsList()).containsOnly("user", "codeviewer");
157 public void search_by_template_name() throws Exception {
158 OrganizationDto defaultOrg = db.getDefaultOrganization();
159 GroupDto group1 = db.users().insertGroup(defaultOrg, "group-1-name");
160 GroupDto group2 = db.users().insertGroup(defaultOrg, "group-2-name");
161 GroupDto group3 = db.users().insertGroup(defaultOrg, "group-3-name");
163 PermissionTemplateDto template = addTemplateToDefaultOrganization();
164 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), group1.getId()));
165 addGroupToTemplate(newPermissionTemplateGroup(ADMIN, template.getId(), group2.getId()));
166 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), null));
168 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
169 addGroupToTemplate(newPermissionTemplateGroup(USER, anotherTemplate.getId(), group1.getId()));
171 loginAsAdminOnDefaultOrganization();
173 byte[] output = newRequest()
174 .setMediaType(PROTOBUF)
175 .setParam(PARAM_TEMPLATE_NAME, template.getName())
178 WsGroupsResponse response = WsGroupsResponse.parseFrom(output);
180 assertThat(response.getGroupsList()).extracting("name").containsExactly("Anyone", "group-1-name", "group-2-name");
184 public void search_with_pagination() throws Exception {
185 OrganizationDto defaultOrg = db.getDefaultOrganization();
186 PermissionTemplateDto template = addTemplateToDefaultOrganization();
187 GroupDto group1 = db.users().insertGroup(defaultOrg, "group-1-name");
188 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), group1.getId()));
189 GroupDto group2 = db.users().insertGroup(defaultOrg, "group-2-name");
190 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), group2.getId()));
192 loginAsAdminOnDefaultOrganization();
194 byte[] output = newRequest()
195 .setMediaType(PROTOBUF)
196 .setParam(PARAM_PERMISSION, USER)
197 .setParam(PARAM_TEMPLATE_NAME, template.getName())
199 .setParam(PAGE_SIZE, "1")
202 WsGroupsResponse response = WsGroupsResponse.parseFrom(output);
204 assertThat(response.getGroupsList()).extracting("name").containsExactly("group-2-name");
208 public void search_with_text_query() throws Exception {
209 OrganizationDto defaultOrg = db.getDefaultOrganization();
210 PermissionTemplateDto template = addTemplateToDefaultOrganization();
211 GroupDto group1 = db.users().insertGroup(defaultOrg, "group-1-name");
212 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), group1.getId()));
213 GroupDto group2 = db.users().insertGroup(defaultOrg, "group-2-name");
214 GroupDto group3 = db.users().insertGroup(defaultOrg, "group-3");
216 loginAsAdminOnDefaultOrganization();
218 byte[] output = newRequest()
219 .setMediaType(PROTOBUF)
220 .setParam(PARAM_TEMPLATE_NAME, template.getName())
221 .setParam(TEXT_QUERY, "-nam")
224 WsGroupsResponse response = WsGroupsResponse.parseFrom(output);
226 assertThat(response.getGroupsList()).extracting("name").containsExactly("group-1-name", "group-2-name");
230 public void search_with_text_query_return_all_groups_even_when_no_permission_set() throws Exception {
231 OrganizationDto defaultOrg = db.getDefaultOrganization();
232 PermissionTemplateDto template = addTemplateToDefaultOrganization();
233 db.users().insertGroup(defaultOrg, "group-1-name");
234 db.users().insertGroup(defaultOrg, "group-2-name");
235 db.users().insertGroup(defaultOrg, "group-3-name");
237 loginAsAdminOnDefaultOrganization();
239 byte[] output = newRequest()
240 .setMediaType(PROTOBUF)
241 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
242 .setParam(TEXT_QUERY, "-name")
245 WsGroupsResponse response = WsGroupsResponse.parseFrom(output);
247 assertThat(response.getGroupsList()).extracting("name").containsExactly("group-1-name", "group-2-name", "group-3-name");
248 assertThat(response.getGroups(0).getPermissionsList()).isEmpty();
249 assertThat(response.getGroups(1).getPermissionsList()).isEmpty();
250 assertThat(response.getGroups(2).getPermissionsList()).isEmpty();
254 public void search_with_text_query_return_anyone_group_even_when_no_permission_set() throws Exception {
255 PermissionTemplateDto template = addTemplateToDefaultOrganization();
256 GroupDto group = db.users().insertGroup(db.getDefaultOrganization(), "group");
257 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), group.getId()));
259 loginAsAdminOnDefaultOrganization();
261 byte[] output = newRequest()
262 .setMediaType(PROTOBUF)
263 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
264 .setParam(TEXT_QUERY, "nyo")
267 WsGroupsResponse response = WsGroupsResponse.parseFrom(output);
269 assertThat(response.getGroupsList()).extracting("name").containsExactly("Anyone");
270 assertThat(response.getGroups(0).getPermissionsList()).isEmpty();
274 public void fail_if_not_logged_in() throws Exception {
275 PermissionTemplateDto template1 = addTemplateToDefaultOrganization();
276 userSession.anonymous();
278 expectedException.expect(UnauthorizedException.class);
281 .setParam(PARAM_PERMISSION, USER)
282 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
287 public void fail_if_insufficient_privileges() throws Exception {
288 PermissionTemplateDto template1 = addTemplateToDefaultOrganization();
291 expectedException.expect(ForbiddenException.class);
294 .setParam(PARAM_PERMISSION, USER)
295 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
300 public void fail_if_template_uuid_and_name_provided() throws Exception {
301 PermissionTemplateDto template1 = addTemplateToDefaultOrganization();
302 loginAsAdminOnDefaultOrganization();
304 expectedException.expect(BadRequestException.class);
307 .setParam(PARAM_PERMISSION, USER)
308 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
309 .setParam(PARAM_TEMPLATE_NAME, template1.getName())
314 public void fail_if_template_uuid_nor_name_provided() throws Exception {
315 loginAsAdminOnDefaultOrganization();
317 expectedException.expect(BadRequestException.class);
320 .setParam(PARAM_PERMISSION, USER)
325 public void fail_if_template_is_not_found() throws Exception {
326 loginAsAdminOnDefaultOrganization();
328 expectedException.expect(NotFoundException.class);
331 .setParam(PARAM_PERMISSION, USER)
332 .setParam(PARAM_TEMPLATE_ID, "unknown-uuid")
337 public void fail_if_not_a_project_permission() throws Exception {
338 loginAsAdminOnDefaultOrganization();
339 PermissionTemplateDto template1 = addTemplateToDefaultOrganization();
341 expectedException.expect(IllegalArgumentException.class);
344 .setParam(PARAM_PERMISSION, GlobalPermissions.QUALITY_GATE_ADMIN)
345 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
349 private GroupDto insertGroupOnDefaultOrganization(String name, String description) {
350 return db.users().insertGroup(newGroupDto().setName(name).setDescription(description).setOrganizationUuid(db.getDefaultOrganization().getUuid()));
353 private void addGroupToTemplate(PermissionTemplateGroupDto permissionTemplateGroup) {
354 db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), permissionTemplateGroup);
357 private static PermissionTemplateGroupDto newPermissionTemplateGroup(String permission, long templateId, @Nullable Long groupId) {
358 return newPermissionTemplateGroupDto()
359 .setPermission(permission)
360 .setTemplateId(templateId)
361 .setGroupId(groupId);
364 private void commit() {
368 private WsTester.TestRequest newRequest() {
369 return wsTester.newPostRequest(CONTROLLER, "template_groups");