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 com.google.common.base.Function;
23 import java.util.List;
24 import javax.annotation.Nonnull;
25 import javax.annotation.Nullable;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.sonar.api.resources.Qualifiers;
31 import org.sonar.api.utils.System2;
32 import org.sonar.core.permission.GlobalPermissions;
33 import org.sonar.db.DbClient;
34 import org.sonar.db.DbSession;
35 import org.sonar.db.DbTester;
36 import org.sonar.db.component.ResourceTypesRule;
37 import org.sonar.db.permission.GroupWithPermissionDto;
38 import org.sonar.db.permission.PermissionQuery;
39 import org.sonar.db.permission.PermissionTemplateDto;
40 import org.sonar.db.user.GroupDto;
41 import org.sonar.server.component.ComponentFinder;
42 import org.sonar.server.exceptions.BadRequestException;
43 import org.sonar.server.exceptions.ForbiddenException;
44 import org.sonar.server.exceptions.NotFoundException;
45 import org.sonar.server.exceptions.UnauthorizedException;
46 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
47 import org.sonar.server.tester.UserSessionRule;
48 import org.sonar.server.usergroups.ws.UserGroupFinder;
49 import org.sonar.server.ws.TestRequest;
50 import org.sonar.server.ws.WsActionTester;
52 import static com.google.common.collect.FluentIterable.from;
53 import static org.assertj.core.api.Assertions.assertThat;
54 import static org.sonar.api.security.DefaultGroups.ANYONE;
55 import static org.sonar.api.web.UserRole.CODEVIEWER;
56 import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
57 import static org.sonar.db.user.GroupMembershipQuery.IN;
58 import static org.sonar.db.user.GroupTesting.newGroupDto;
59 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_ID;
60 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_NAME;
61 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
62 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
63 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
66 public class RemoveGroupFromTemplateActionTest {
68 private static final String GROUP_NAME = "group-name";
69 private static final String PERMISSION = CODEVIEWER;
71 public DbTester db = DbTester.create(System2.INSTANCE);
73 public ExpectedException expectedException = ExpectedException.none();
75 public UserSessionRule userSession = UserSessionRule.standalone();
76 ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
82 PermissionTemplateDto permissionTemplate;
86 dbClient = db.getDbClient();
87 dbSession = db.getSession();
88 userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
90 PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), new UserGroupFinder(dbClient), resourceTypes);
91 ws = new WsActionTester(new RemoveGroupFromTemplateAction(dbClient, dependenciesFinder, userSession));
93 group = insertGroup(newGroupDto().setName(GROUP_NAME));
94 permissionTemplate = insertPermissionTemplate(newPermissionTemplateDto());
95 addGroupToPermissionTemplate(permissionTemplate.getId(), group.getId(), PERMISSION);
100 public void remove_group_from_template() {
101 assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).containsExactly(GROUP_NAME);
104 newRequest(GROUP_NAME, permissionTemplate.getUuid(), PERMISSION);
106 assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).isEmpty();
110 public void remove_group_from_template_by_name_case_insensitive() {
111 assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).containsExactly(GROUP_NAME);
115 .setParam(PARAM_GROUP_NAME, GROUP_NAME)
116 .setParam(PARAM_PERMISSION, PERMISSION)
117 .setParam(PARAM_TEMPLATE_NAME, permissionTemplate.getName().toUpperCase())
121 assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).isEmpty();
125 public void remove_group_with_group_id() {
126 assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).containsExactly(GROUP_NAME);
130 .setParam(PARAM_TEMPLATE_ID, permissionTemplate.getUuid())
131 .setParam(PARAM_PERMISSION, PERMISSION)
132 .setParam(PARAM_GROUP_ID, String.valueOf(group.getId()))
135 assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).isEmpty();
139 public void remove_group_twice_without_error() {
140 newRequest(GROUP_NAME, permissionTemplate.getUuid(), PERMISSION);
141 newRequest(GROUP_NAME, permissionTemplate.getUuid(), PERMISSION);
143 assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).isEmpty();
147 public void remove_anyone_group_from_template() {
148 addGroupToPermissionTemplate(permissionTemplate.getId(), null, PERMISSION);
151 newRequest(ANYONE, permissionTemplate.getUuid(), PERMISSION);
153 assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).containsExactly(GROUP_NAME);
157 public void fail_if_not_a_project_permission() {
158 expectedException.expect(BadRequestException.class);
160 newRequest(GROUP_NAME, permissionTemplate.getUuid(), GlobalPermissions.PROVISIONING);
164 public void fail_if_insufficient_privileges() {
165 expectedException.expect(ForbiddenException.class);
166 userSession.setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
168 newRequest(GROUP_NAME, permissionTemplate.getUuid(), PERMISSION);
172 public void fail_if_not_logged_in() {
173 expectedException.expect(UnauthorizedException.class);
174 userSession.anonymous();
176 newRequest(GROUP_NAME, permissionTemplate.getUuid(), PERMISSION);
180 public void fail_if_group_params_missing() {
181 expectedException.expect(BadRequestException.class);
183 newRequest(null, permissionTemplate.getUuid(), PERMISSION);
187 public void fail_if_permission_missing() {
188 expectedException.expect(IllegalArgumentException.class);
190 newRequest(GROUP_NAME, permissionTemplate.getUuid(), null);
194 public void fail_if_template_missing() {
195 expectedException.expect(BadRequestException.class);
197 newRequest(GROUP_NAME, null, PERMISSION);
201 public void fail_if_group_does_not_exist() {
202 expectedException.expect(NotFoundException.class);
203 expectedException.expectMessage("Group with name 'unknown-group-name' is not found");
205 newRequest("unknown-group-name", permissionTemplate.getUuid(), PERMISSION);
209 public void fail_if_template_key_does_not_exist() {
210 expectedException.expect(NotFoundException.class);
211 expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
213 newRequest(GROUP_NAME, "unknown-key", PERMISSION);
216 private void newRequest(@Nullable String groupName, @Nullable String templateKey, @Nullable String permission) {
217 TestRequest request = ws.newRequest();
218 if (groupName != null) {
219 request.setParam(PARAM_GROUP_NAME, groupName);
221 if (templateKey != null) {
222 request.setParam(PARAM_TEMPLATE_ID, templateKey);
224 if (permission != null) {
225 request.setParam(PARAM_PERMISSION, permission);
231 private void commit() {
235 private GroupDto insertGroup(GroupDto groupDto) {
236 return dbClient.groupDao().insert(dbSession, groupDto);
239 private PermissionTemplateDto insertPermissionTemplate(PermissionTemplateDto permissionTemplate) {
240 return dbClient.permissionTemplateDao().insert(dbSession, permissionTemplate);
243 private void addGroupToPermissionTemplate(long permissionTemplateId, @Nullable Long groupId, String permission) {
244 dbClient.permissionTemplateDao().insertGroupPermission(dbSession, permissionTemplateId, groupId, permission);
247 private List<String> getGroupNamesInTemplateAndPermission(long templateId, String permission) {
248 PermissionQuery permissionQuery = PermissionQuery.builder().permission(permission).membership(IN).build();
249 return from(dbClient.permissionTemplateDao()
250 .selectGroups(dbSession, permissionQuery, templateId))
251 .transform(GroupWithPermissionToGroupName.INSTANCE)
255 private enum GroupWithPermissionToGroupName implements Function<GroupWithPermissionDto, String> {
259 public String apply(@Nonnull GroupWithPermissionDto groupWithPermission) {
260 return groupWithPermission.getName();