2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.
21 package org.sonar.server.permission.ws.template;
23 import com.google.common.base.Function;
24 import java.util.List;
25 import javax.annotation.Nonnull;
26 import javax.annotation.Nullable;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.experimental.categories.Category;
31 import org.junit.rules.ExpectedException;
32 import org.sonar.api.resources.Qualifiers;
33 import org.sonar.api.utils.System2;
34 import org.sonar.core.permission.GlobalPermissions;
35 import org.sonar.db.DbClient;
36 import org.sonar.db.DbSession;
37 import org.sonar.db.DbTester;
38 import org.sonar.db.component.ResourceTypesRule;
39 import org.sonar.db.permission.GroupWithPermissionDto;
40 import org.sonar.db.permission.PermissionQuery;
41 import org.sonar.db.permission.PermissionTemplateDto;
42 import org.sonar.db.user.GroupDto;
43 import org.sonar.server.component.ComponentFinder;
44 import org.sonar.server.exceptions.BadRequestException;
45 import org.sonar.server.exceptions.ForbiddenException;
46 import org.sonar.server.exceptions.NotFoundException;
47 import org.sonar.server.exceptions.UnauthorizedException;
48 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
49 import org.sonar.server.permission.ws.PermissionsWsParameters;
50 import org.sonar.server.tester.UserSessionRule;
51 import org.sonar.server.usergroups.ws.UserGroupFinder;
52 import org.sonar.server.ws.TestRequest;
53 import org.sonar.server.ws.WsActionTester;
54 import org.sonar.test.DbTests;
56 import static com.google.common.collect.FluentIterable.from;
57 import static org.assertj.core.api.Assertions.assertThat;
58 import static org.sonar.api.security.DefaultGroups.ANYONE;
59 import static org.sonar.api.web.UserRole.CODEVIEWER;
60 import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
61 import static org.sonar.db.user.GroupMembershipQuery.IN;
62 import static org.sonar.db.user.GroupTesting.newGroupDto;
63 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_GROUP_ID;
64 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_GROUP_NAME;
65 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_PERMISSION;
66 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
67 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_TEMPLATE_UUID;
69 @Category(DbTests.class)
70 public class RemoveGroupFromTemplateActionTest {
72 private static final String GROUP_NAME = "group-name";
73 private static final String PERMISSION = CODEVIEWER;
75 public DbTester db = DbTester.create(System2.INSTANCE);
77 public ExpectedException expectedException = ExpectedException.none();
79 public UserSessionRule userSession = UserSessionRule.standalone();
80 ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
86 PermissionTemplateDto permissionTemplate;
90 dbClient = db.getDbClient();
91 dbSession = db.getSession();
92 userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
94 PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), new UserGroupFinder(dbClient), resourceTypes);
95 ws = new WsActionTester(new RemoveGroupFromTemplateAction(dbClient, dependenciesFinder, userSession));
97 group = insertGroup(newGroupDto().setName(GROUP_NAME));
98 permissionTemplate = insertPermissionTemplate(newPermissionTemplateDto());
99 addGroupToPermissionTemplate(permissionTemplate.getId(), group.getId(), PERMISSION);
104 public void remove_group_from_template() {
105 assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).containsExactly(GROUP_NAME);
108 newRequest(GROUP_NAME, permissionTemplate.getUuid(), PERMISSION);
110 assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).isEmpty();
114 public void remove_group_from_template_by_name_case_insensitive() {
115 assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).containsExactly(GROUP_NAME);
119 .setParam(PARAM_GROUP_NAME, GROUP_NAME)
120 .setParam(PARAM_PERMISSION, PERMISSION)
121 .setParam(PARAM_TEMPLATE_NAME, permissionTemplate.getName().toUpperCase())
125 assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).isEmpty();
129 public void remove_group_with_group_id() {
130 assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).containsExactly(GROUP_NAME);
134 .setParam(PARAM_TEMPLATE_UUID, permissionTemplate.getUuid())
135 .setParam(PARAM_PERMISSION, PERMISSION)
136 .setParam(PARAM_GROUP_ID, String.valueOf(group.getId()))
139 assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).isEmpty();
143 public void remove_group_twice_without_error() {
144 newRequest(GROUP_NAME, permissionTemplate.getUuid(), PERMISSION);
145 newRequest(GROUP_NAME, permissionTemplate.getUuid(), PERMISSION);
147 assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).isEmpty();
151 public void remove_anyone_group_from_template() {
152 addGroupToPermissionTemplate(permissionTemplate.getId(), null, PERMISSION);
155 newRequest(ANYONE, permissionTemplate.getUuid(), PERMISSION);
157 assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).containsExactly(GROUP_NAME);
161 public void fail_if_not_a_project_permission() {
162 expectedException.expect(BadRequestException.class);
164 newRequest(GROUP_NAME, permissionTemplate.getUuid(), GlobalPermissions.PREVIEW_EXECUTION);
168 public void fail_if_insufficient_privileges() {
169 expectedException.expect(ForbiddenException.class);
170 userSession.setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
172 newRequest(GROUP_NAME, permissionTemplate.getUuid(), PERMISSION);
176 public void fail_if_not_logged_in() {
177 expectedException.expect(UnauthorizedException.class);
178 userSession.anonymous();
180 newRequest(GROUP_NAME, permissionTemplate.getUuid(), PERMISSION);
184 public void fail_if_group_params_missing() {
185 expectedException.expect(BadRequestException.class);
187 newRequest(null, permissionTemplate.getUuid(), PERMISSION);
191 public void fail_if_permission_missing() {
192 expectedException.expect(IllegalArgumentException.class);
194 newRequest(GROUP_NAME, permissionTemplate.getUuid(), null);
198 public void fail_if_template_missing() {
199 expectedException.expect(BadRequestException.class);
201 newRequest(GROUP_NAME, null, PERMISSION);
205 public void fail_if_group_does_not_exist() {
206 expectedException.expect(NotFoundException.class);
207 expectedException.expectMessage("Group with name 'unknown-group-name' is not found");
209 newRequest("unknown-group-name", permissionTemplate.getUuid(), PERMISSION);
213 public void fail_if_template_key_does_not_exist() {
214 expectedException.expect(NotFoundException.class);
215 expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
217 newRequest(GROUP_NAME, "unknown-key", PERMISSION);
220 private void newRequest(@Nullable String groupName, @Nullable String templateKey, @Nullable String permission) {
221 TestRequest request = ws.newRequest();
222 if (groupName != null) {
223 request.setParam(PermissionsWsParameters.PARAM_GROUP_NAME, groupName);
225 if (templateKey != null) {
226 request.setParam(PARAM_TEMPLATE_UUID, templateKey);
228 if (permission != null) {
229 request.setParam(PermissionsWsParameters.PARAM_PERMISSION, permission);
235 private void commit() {
239 private GroupDto insertGroup(GroupDto groupDto) {
240 return dbClient.groupDao().insert(dbSession, groupDto);
243 private PermissionTemplateDto insertPermissionTemplate(PermissionTemplateDto permissionTemplate) {
244 return dbClient.permissionTemplateDao().insert(dbSession, permissionTemplate);
247 private void addGroupToPermissionTemplate(long permissionTemplateId, @Nullable Long groupId, String permission) {
248 dbClient.permissionTemplateDao().insertGroupPermission(dbSession, permissionTemplateId, groupId, permission);
251 private List<String> getGroupNamesInTemplateAndPermission(long templateId, String permission) {
252 PermissionQuery permissionQuery = PermissionQuery.builder().permission(permission).membership(IN).build();
253 return from(dbClient.permissionTemplateDao()
254 .selectGroups(dbSession, permissionQuery, templateId))
255 .transform(GroupWithPermissionToGroupName.INSTANCE)
259 private enum GroupWithPermissionToGroupName implements Function<GroupWithPermissionDto, String> {
263 public String apply(@Nonnull GroupWithPermissionDto groupWithPermission) {
264 return groupWithPermission.getName();