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.util.List;
23 import javax.annotation.Nullable;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.sonar.core.permission.GlobalPermissions;
27 import org.sonar.db.permission.PermissionQuery;
28 import org.sonar.db.permission.template.PermissionTemplateDto;
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.sonar.server.ws.WsTester;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.sonar.api.security.DefaultGroups.ANYONE;
39 import static org.sonar.api.web.UserRole.CODEVIEWER;
40 import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
41 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.CONTROLLER;
42 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_ID;
43 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_NAME;
44 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
45 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
46 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
48 public class RemoveGroupFromTemplateActionTest extends BasePermissionWsTest<RemoveGroupFromTemplateAction> {
50 private static final String ACTION = "remove_group_from_template";
51 private static final String PERMISSION = CODEVIEWER;
53 private GroupDto group;
54 private PermissionTemplateDto template;
57 protected RemoveGroupFromTemplateAction buildWsAction() {
58 return new RemoveGroupFromTemplateAction(db.getDbClient(), newPermissionWsSupport(), userSession);
63 loginAsAdminOnDefaultOrganization();
65 group = db.users().insertGroup(db.getDefaultOrganization(), "group-name");
66 template = insertTemplate();
67 addGroupToTemplate(template, group.getId(), PERMISSION);
71 public void remove_group_from_template() throws Exception {
72 newRequest(group.getName(), template.getUuid(), PERMISSION);
74 assertThat(getGroupNamesInTemplateAndPermission(template.getId(), PERMISSION)).isEmpty();
78 public void remove_group_from_template_by_name_case_insensitive() throws Exception {
79 wsTester.newPostRequest(CONTROLLER, ACTION)
80 .setParam(PARAM_GROUP_NAME, group.getName())
81 .setParam(PARAM_PERMISSION, PERMISSION)
82 .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
85 assertThat(getGroupNamesInTemplateAndPermission(template.getId(), PERMISSION)).isEmpty();
89 public void remove_group_with_group_id() throws Exception {
90 wsTester.newPostRequest(CONTROLLER, ACTION)
91 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
92 .setParam(PARAM_PERMISSION, PERMISSION)
93 .setParam(PARAM_GROUP_ID, String.valueOf(group.getId()))
96 assertThat(getGroupNamesInTemplateAndPermission(template.getId(), PERMISSION)).isEmpty();
100 public void remove_group_twice_without_error() throws Exception {
101 newRequest(group.getName(), template.getUuid(), PERMISSION);
102 newRequest(group.getName(), template.getUuid(), PERMISSION);
104 assertThat(getGroupNamesInTemplateAndPermission(template.getId(), PERMISSION)).isEmpty();
108 public void remove_anyone_group_from_template() throws Exception {
109 addGroupToTemplate(template, null, PERMISSION);
111 newRequest(ANYONE, template.getUuid(), PERMISSION);
113 assertThat(getGroupNamesInTemplateAndPermission(template.getId(), PERMISSION)).containsExactly(group.getName());
117 public void fail_if_not_a_project_permission() throws Exception {
118 expectedException.expect(IllegalArgumentException.class);
120 newRequest(group.getName(), template.getUuid(), GlobalPermissions.PROVISIONING);
124 public void fail_if_insufficient_privileges() throws Exception {
125 userSession.login().addOrganizationPermission(db.getDefaultOrganization().getUuid(), SCAN_EXECUTION);
127 expectedException.expect(ForbiddenException.class);
129 newRequest(group.getName(), template.getUuid(), PERMISSION);
133 public void fail_if_not_logged_in() throws Exception {
134 expectedException.expect(UnauthorizedException.class);
135 userSession.anonymous();
137 newRequest(group.getName(), template.getUuid(), PERMISSION);
141 public void fail_if_group_params_missing() throws Exception {
142 expectedException.expect(BadRequestException.class);
144 newRequest(null, template.getUuid(), PERMISSION);
148 public void fail_if_permission_missing() throws Exception {
149 expectedException.expect(IllegalArgumentException.class);
151 newRequest(group.getName(), template.getUuid(), null);
155 public void fail_if_template_missing() throws Exception {
156 expectedException.expect(BadRequestException.class);
158 newRequest(group.getName(), null, PERMISSION);
162 public void fail_if_group_does_not_exist() throws Exception {
163 expectedException.expect(NotFoundException.class);
164 expectedException.expectMessage("No group with name 'unknown-group-name'");
166 newRequest("unknown-group-name", template.getUuid(), PERMISSION);
170 public void fail_if_template_key_does_not_exist() throws Exception {
171 expectedException.expect(NotFoundException.class);
172 expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
174 newRequest(group.getName(), "unknown-key", PERMISSION);
177 private void newRequest(@Nullable String groupName, @Nullable String templateKey, @Nullable String permission) throws Exception {
178 WsTester.TestRequest request = wsTester.newPostRequest(CONTROLLER, ACTION);
179 if (groupName != null) {
180 request.setParam(PARAM_GROUP_NAME, groupName);
182 if (templateKey != null) {
183 request.setParam(PARAM_TEMPLATE_ID, templateKey);
185 if (permission != null) {
186 request.setParam(PARAM_PERMISSION, permission);
192 private void addGroupToTemplate(PermissionTemplateDto template, @Nullable Long groupId, String permission) {
193 db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), template.getId(), groupId, permission);
197 private List<String> getGroupNamesInTemplateAndPermission(long templateId, String permission) {
198 PermissionQuery permissionQuery = PermissionQuery.builder().setPermission(permission).build();
199 return db.getDbClient().permissionTemplateDao()
200 .selectGroupNamesByQueryAndTemplate(db.getSession(), permissionQuery, templateId);