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.core.permission.GlobalPermissions;
27 import org.sonar.db.component.ResourceTypesRule;
28 import org.sonar.db.organization.OrganizationDto;
29 import org.sonar.db.permission.template.PermissionTemplateDto;
30 import org.sonar.db.permission.template.PermissionTemplateGroupDto;
31 import org.sonar.db.user.GroupDto;
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.permission.PermissionService;
37 import org.sonar.server.permission.PermissionServiceImpl;
38 import org.sonar.server.permission.ws.BasePermissionWsTest;
39 import org.sonar.server.permission.ws.RequestValidator;
40 import org.sonar.server.permission.ws.WsParameters;
41 import org.sonarqube.ws.Permissions.WsGroupsResponse;
43 import static org.assertj.core.api.Assertions.assertThat;
44 import static org.sonar.api.server.ws.WebService.Param.PAGE;
45 import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
46 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
47 import static org.sonar.api.web.UserRole.ADMIN;
48 import static org.sonar.api.web.UserRole.CODEVIEWER;
49 import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
50 import static org.sonar.api.web.UserRole.USER;
51 import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateGroupDto;
52 import static org.sonar.db.user.GroupTesting.newGroupDto;
53 import static org.sonar.test.JsonAssert.assertJson;
54 import static org.sonarqube.ws.MediaTypes.PROTOBUF;
55 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
56 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
57 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
59 public class TemplateGroupsActionTest extends BasePermissionWsTest<TemplateGroupsAction> {
61 private ResourceTypes resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT);
62 private PermissionService permissionService = new PermissionServiceImpl(resourceTypes);
63 private WsParameters wsParameters = new WsParameters(permissionService);
64 private RequestValidator requestValidator = new RequestValidator(permissionService);
67 protected TemplateGroupsAction buildWsAction() {
68 return new TemplateGroupsAction(db.getDbClient(), userSession, newPermissionWsSupport(), wsParameters, requestValidator);
72 public void template_groups_of_json_example() {
73 GroupDto adminGroup = insertGroupOnDefaultOrganization("sonar-administrators", "System administrators");
74 GroupDto userGroup = insertGroupOnDefaultOrganization("sonar-users", "Any new users created will automatically join this group");
76 PermissionTemplateDto template = addTemplateToDefaultOrganization();
77 addGroupToTemplate(newPermissionTemplateGroup(ISSUE_ADMIN, template.getId(), adminGroup.getId()));
78 addGroupToTemplate(newPermissionTemplateGroup(ISSUE_ADMIN, template.getId(), userGroup.getId()));
80 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), null));
81 addGroupToTemplate(newPermissionTemplateGroup(ISSUE_ADMIN, template.getId(), null));
83 loginAsAdmin(db.getDefaultOrganization());
85 String response = newRequest()
86 .setParam(PARAM_PERMISSION, ISSUE_ADMIN)
87 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
93 .withStrictArrayOrder()
94 .isSimilarTo(getClass().getResource("template_groups-example.json"));
98 public void do_not_fail_when_group_name_exists_in_multiple_organizations() {
99 PermissionTemplateDto template = addTemplateToDefaultOrganization();
101 String groupName = "group-name";
102 GroupDto group1 = db.users().insertGroup(db.getDefaultOrganization(), groupName);
103 addGroupToTemplate(newPermissionTemplateGroup(CODEVIEWER, template.getId(), group1.getId()));
104 addGroupToTemplate(newPermissionTemplateGroup(ADMIN, template.getId(), group1.getId()));
106 OrganizationDto otherOrganization = db.organizations().insert();
107 db.users().insertGroup(otherOrganization, groupName);
109 loginAsAdmin(db.getDefaultOrganization());
112 .setMediaType(PROTOBUF)
113 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
114 .setParam(TEXT_QUERY, "-nam")
119 public void return_all_permissions_of_matching_groups() {
120 PermissionTemplateDto template = addTemplateToDefaultOrganization();
122 GroupDto group1 = db.users().insertGroup(db.getDefaultOrganization(), "group-1-name");
123 addGroupToTemplate(newPermissionTemplateGroup(CODEVIEWER, template.getId(), group1.getId()));
124 addGroupToTemplate(newPermissionTemplateGroup(ADMIN, template.getId(), group1.getId()));
126 GroupDto group2 = db.users().insertGroup(db.getDefaultOrganization(), "group-2-name");
127 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), group2.getId()));
128 addGroupToTemplate(newPermissionTemplateGroup(ADMIN, template.getId(), group2.getId()));
130 GroupDto group3 = db.users().insertGroup(db.getDefaultOrganization(), "group-3-name");
133 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), null));
134 addGroupToTemplate(newPermissionTemplateGroup(ISSUE_ADMIN, template.getId(), null));
136 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
137 addGroupToTemplate(newPermissionTemplateGroup(ADMIN, anotherTemplate.getId(), group3.getId()));
139 loginAsAdmin(db.getDefaultOrganization());
141 WsGroupsResponse response = newRequest()
142 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
143 .executeProtobuf(WsGroupsResponse.class);
145 assertThat(response.getGroupsList()).extracting("name").containsExactly("Anyone", "group-1-name", "group-2-name", "group-3-name");
146 assertThat(response.getGroups(0).getPermissionsList()).containsOnly("user", "issueadmin");
147 assertThat(response.getGroups(1).getPermissionsList()).containsOnly("codeviewer", "admin");
148 assertThat(response.getGroups(2).getPermissionsList()).containsOnly("user", "admin");
152 public void search_by_permission() {
153 PermissionTemplateDto template = addTemplateToDefaultOrganization();
155 GroupDto group1 = db.users().insertGroup(db.getDefaultOrganization(), "group-1-name");
156 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), group1.getId()));
157 addGroupToTemplate(newPermissionTemplateGroup(CODEVIEWER, template.getId(), group1.getId()));
159 GroupDto group2 = db.users().insertGroup(db.getDefaultOrganization(), "group-2-name");
160 addGroupToTemplate(newPermissionTemplateGroup(ADMIN, template.getId(), group2.getId()));
162 GroupDto group3 = db.users().insertGroup(db.getDefaultOrganization(), "group-3-name");
165 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), null));
167 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
168 addGroupToTemplate(newPermissionTemplateGroup(ADMIN, anotherTemplate.getId(), group3.getId()));
170 loginAsAdmin(db.getDefaultOrganization());
172 WsGroupsResponse response = newRequest()
173 .setParam(PARAM_PERMISSION, USER)
174 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
175 .executeProtobuf(WsGroupsResponse.class);
177 assertThat(response.getGroupsList()).extracting("name").containsExactly("Anyone", "group-1-name");
178 assertThat(response.getGroups(0).getPermissionsList()).containsOnly("user");
179 assertThat(response.getGroups(1).getPermissionsList()).containsOnly("user", "codeviewer");
183 public void search_by_template_name() {
184 OrganizationDto defaultOrg = db.getDefaultOrganization();
185 GroupDto group1 = db.users().insertGroup(defaultOrg, "group-1-name");
186 GroupDto group2 = db.users().insertGroup(defaultOrg, "group-2-name");
187 GroupDto group3 = db.users().insertGroup(defaultOrg, "group-3-name");
189 PermissionTemplateDto template = addTemplateToDefaultOrganization();
190 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), group1.getId()));
191 addGroupToTemplate(newPermissionTemplateGroup(ADMIN, template.getId(), group2.getId()));
192 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), null));
194 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
195 addGroupToTemplate(newPermissionTemplateGroup(USER, anotherTemplate.getId(), group1.getId()));
197 loginAsAdmin(db.getDefaultOrganization());
199 WsGroupsResponse response = newRequest()
200 .setParam(PARAM_TEMPLATE_NAME, template.getName())
201 .executeProtobuf(WsGroupsResponse.class);
203 assertThat(response.getGroupsList()).extracting("name").containsExactly("Anyone", "group-1-name", "group-2-name", "group-3-name");
207 public void search_with_pagination() {
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 loginAsAdmin(db.getDefaultOrganization());
217 WsGroupsResponse response = newRequest()
218 .setParam(PARAM_PERMISSION, USER)
219 .setParam(PARAM_TEMPLATE_NAME, template.getName())
221 .setParam(PAGE_SIZE, "1")
222 .executeProtobuf(WsGroupsResponse.class);
224 assertThat(response.getGroupsList()).extracting("name").containsExactly("group-2-name");
228 public void search_with_text_query() {
229 OrganizationDto defaultOrg = db.getDefaultOrganization();
230 PermissionTemplateDto template = addTemplateToDefaultOrganization();
231 GroupDto group1 = db.users().insertGroup(defaultOrg, "group-1-name");
232 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), group1.getId()));
233 GroupDto group2 = db.users().insertGroup(defaultOrg, "group-2-name");
234 GroupDto group3 = db.users().insertGroup(defaultOrg, "group-3");
236 loginAsAdmin(db.getDefaultOrganization());
238 WsGroupsResponse response = newRequest()
239 .setParam(PARAM_TEMPLATE_NAME, template.getName())
240 .setParam(TEXT_QUERY, "-nam")
241 .executeProtobuf(WsGroupsResponse.class);
243 assertThat(response.getGroupsList()).extracting("name").containsExactly("group-1-name", "group-2-name");
247 public void search_with_text_query_return_all_groups_even_when_no_permission_set() {
248 OrganizationDto defaultOrg = db.getDefaultOrganization();
249 PermissionTemplateDto template = addTemplateToDefaultOrganization();
250 db.users().insertGroup(defaultOrg, "group-1-name");
251 db.users().insertGroup(defaultOrg, "group-2-name");
252 db.users().insertGroup(defaultOrg, "group-3-name");
254 loginAsAdmin(db.getDefaultOrganization());
256 WsGroupsResponse response = newRequest()
257 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
258 .setParam(TEXT_QUERY, "-name")
259 .executeProtobuf(WsGroupsResponse.class);
261 assertThat(response.getGroupsList()).extracting("name").containsExactly("group-1-name", "group-2-name", "group-3-name");
262 assertThat(response.getGroups(0).getPermissionsList()).isEmpty();
263 assertThat(response.getGroups(1).getPermissionsList()).isEmpty();
264 assertThat(response.getGroups(2).getPermissionsList()).isEmpty();
268 public void search_with_text_query_return_anyone_group_even_when_no_permission_set() {
269 PermissionTemplateDto template = addTemplateToDefaultOrganization();
270 GroupDto group = db.users().insertGroup(db.getDefaultOrganization(), "group");
271 addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), group.getId()));
273 loginAsAdmin(db.getDefaultOrganization());
275 WsGroupsResponse response = newRequest()
276 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
277 .setParam(TEXT_QUERY, "nyo")
278 .executeProtobuf(WsGroupsResponse.class);
280 assertThat(response.getGroupsList()).extracting("name").containsExactly("Anyone");
281 assertThat(response.getGroups(0).getPermissionsList()).isEmpty();
285 public void fail_if_not_logged_in() {
286 PermissionTemplateDto template1 = addTemplateToDefaultOrganization();
287 userSession.anonymous();
289 expectedException.expect(UnauthorizedException.class);
292 .setParam(PARAM_PERMISSION, USER)
293 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
298 public void fail_if_insufficient_privileges() {
299 PermissionTemplateDto template1 = addTemplateToDefaultOrganization();
302 expectedException.expect(ForbiddenException.class);
305 .setParam(PARAM_PERMISSION, USER)
306 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
311 public void fail_if_template_uuid_and_name_provided() {
312 PermissionTemplateDto template1 = addTemplateToDefaultOrganization();
313 loginAsAdmin(db.getDefaultOrganization());
315 expectedException.expect(BadRequestException.class);
318 .setParam(PARAM_PERMISSION, USER)
319 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
320 .setParam(PARAM_TEMPLATE_NAME, template1.getName())
325 public void fail_if_template_uuid_nor_name_provided() {
326 loginAsAdmin(db.getDefaultOrganization());
328 expectedException.expect(BadRequestException.class);
331 .setParam(PARAM_PERMISSION, USER)
336 public void fail_if_template_is_not_found() {
337 loginAsAdmin(db.getDefaultOrganization());
339 expectedException.expect(NotFoundException.class);
342 .setParam(PARAM_PERMISSION, USER)
343 .setParam(PARAM_TEMPLATE_ID, "unknown-uuid")
348 public void fail_if_not_a_project_permission() {
349 loginAsAdmin(db.getDefaultOrganization());
350 PermissionTemplateDto template1 = addTemplateToDefaultOrganization();
352 expectedException.expect(IllegalArgumentException.class);
355 .setParam(PARAM_PERMISSION, GlobalPermissions.QUALITY_GATE_ADMIN)
356 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
360 private GroupDto insertGroupOnDefaultOrganization(String name, String description) {
361 return db.users().insertGroup(newGroupDto().setName(name).setDescription(description).setOrganizationUuid(db.getDefaultOrganization().getUuid()));
364 private void addGroupToTemplate(PermissionTemplateGroupDto permissionTemplateGroup) {
365 db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), permissionTemplateGroup);
368 private static PermissionTemplateGroupDto newPermissionTemplateGroup(String permission, long templateId, @Nullable Integer groupId) {
369 return newPermissionTemplateGroupDto()
370 .setPermission(permission)
371 .setTemplateId(templateId)
372 .setGroupId(groupId);
375 private void commit() {