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 java.util.Collections;
24 import javax.annotation.Nullable;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.sonar.api.resources.Qualifiers;
30 import org.sonar.api.utils.System2;
31 import org.sonar.api.web.UserRole;
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.PermissionTemplateDto;
38 import org.sonar.db.user.GroupDto;
39 import org.sonar.db.user.GroupTesting;
40 import org.sonar.db.user.UserDto;
41 import org.sonar.db.user.UserTesting;
42 import org.sonar.server.component.ComponentFinder;
43 import org.sonar.server.exceptions.BadRequestException;
44 import org.sonar.server.exceptions.ForbiddenException;
45 import org.sonar.server.exceptions.NotFoundException;
46 import org.sonar.server.exceptions.UnauthorizedException;
47 import org.sonar.server.permission.ws.DeleteTemplateAction;
48 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
49 import org.sonar.server.tester.UserSessionRule;
50 import org.sonar.server.usergroups.ws.UserGroupFinder;
51 import org.sonar.server.ws.TestRequest;
52 import org.sonar.server.ws.TestResponse;
53 import org.sonar.server.ws.WsActionTester;
55 import static org.assertj.core.api.Assertions.assertThat;
56 import static org.mockito.Mockito.mock;
57 import static org.mockito.Mockito.when;
58 import static org.mockito.internal.util.collections.Sets.newSet;
59 import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
60 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_TEMPLATE_UUID;
61 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
63 public class DeleteTemplateActionTest {
65 static final String TEMPLATE_UUID = "permission-template-uuid";
68 public DbTester db = DbTester.create(System2.INSTANCE);
70 public UserSessionRule userSession = UserSessionRule.standalone();
72 public ExpectedException expectedException = ExpectedException.none();
73 ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
78 DefaultPermissionTemplateFinder defaultTemplatePermissionFinder;
80 PermissionTemplateDto permissionTemplate;
84 userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
86 dbClient = db.getDbClient();
87 dbSession = db.getSession();
88 defaultTemplatePermissionFinder = mock(DefaultPermissionTemplateFinder.class);
89 when(defaultTemplatePermissionFinder.getDefaultTemplateUuids()).thenReturn(Collections.<String>emptySet());
90 PermissionDependenciesFinder finder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), new UserGroupFinder(dbClient), resourceTypes);
91 ws = new WsActionTester(new DeleteTemplateAction(dbClient, userSession, finder, defaultTemplatePermissionFinder));
93 permissionTemplate = insertTemplateAndAssociatedPermissions(newPermissionTemplateDto().setUuid(TEMPLATE_UUID));
94 PermissionTemplateDto permissionTemplateInDatabase = dbClient.permissionTemplateDao().selectByUuidWithUserAndGroupPermissions(dbSession, TEMPLATE_UUID);
95 assertThat(permissionTemplateInDatabase.getUuid()).isEqualTo(TEMPLATE_UUID);
96 assertThat(permissionTemplateInDatabase.getGroupsPermissions()).isNotEmpty();
97 assertThat(permissionTemplateInDatabase.getUsersPermissions()).isNotEmpty();
101 public void delete_template_in_db() {
102 TestResponse result = newRequest(TEMPLATE_UUID);
104 assertThat(result.getInput()).isEmpty();
105 assertThat(dbClient.permissionTemplateDao().selectByUuidWithUserAndGroupPermissions(dbSession, TEMPLATE_UUID)).isNull();
109 public void delete_template_by_name_case_insensitive() {
111 .setParam(PARAM_TEMPLATE_NAME, permissionTemplate.getName().toUpperCase())
115 assertThat(dbClient.permissionTemplateDao().selectByUuidWithUserAndGroupPermissions(dbSession, TEMPLATE_UUID)).isNull();
119 public void fail_if_uuid_is_not_known() {
120 expectedException.expect(NotFoundException.class);
122 newRequest("unknown-template-uuid");
126 public void fail_if_template_is_default() {
127 expectedException.expect(BadRequestException.class);
128 expectedException.expectMessage("It is not possible to delete a default template");
129 when(defaultTemplatePermissionFinder.getDefaultTemplateUuids()).thenReturn(newSet(TEMPLATE_UUID));
131 newRequest(TEMPLATE_UUID);
135 public void fail_if_not_logged_in() {
136 expectedException.expect(UnauthorizedException.class);
137 userSession.anonymous();
139 newRequest(TEMPLATE_UUID);
143 public void fail_if_not_admin() {
144 expectedException.expect(ForbiddenException.class);
145 userSession.login().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
147 newRequest(TEMPLATE_UUID);
151 public void fail_if_uuid_is_not_provided() {
152 expectedException.expect(BadRequestException.class);
157 private PermissionTemplateDto insertTemplateAndAssociatedPermissions(PermissionTemplateDto template) {
158 dbClient.permissionTemplateDao().insert(dbSession, template);
159 UserDto user = dbClient.userDao().insert(dbSession, UserTesting.newUserDto().setActive(true));
160 GroupDto group = dbClient.groupDao().insert(dbSession, GroupTesting.newGroupDto());
161 dbClient.permissionTemplateDao().insertUserPermission(dbSession, template.getId(), user.getId(), UserRole.ADMIN);
162 dbClient.permissionTemplateDao().insertGroupPermission(dbSession, template.getId(), group.getId(), UserRole.CODEVIEWER);
168 private void commit() {
172 private TestResponse newRequest(@Nullable String id) {
173 TestRequest request = ws.newRequest();
175 request.setParam(PARAM_TEMPLATE_UUID, id);
178 TestResponse result = executeRequest(request);
183 private static TestResponse executeRequest(TestRequest request) {
184 return request.execute();