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.utils.System2;
30 import org.sonar.api.web.UserRole;
31 import org.sonar.core.permission.GlobalPermissions;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.DbTester;
35 import org.sonar.db.permission.PermissionTemplateDto;
36 import org.sonar.db.user.GroupDto;
37 import org.sonar.db.user.GroupTesting;
38 import org.sonar.db.user.UserDto;
39 import org.sonar.db.user.UserTesting;
40 import org.sonar.server.component.ComponentFinder;
41 import org.sonar.server.exceptions.BadRequestException;
42 import org.sonar.server.exceptions.ForbiddenException;
43 import org.sonar.server.exceptions.NotFoundException;
44 import org.sonar.server.exceptions.UnauthorizedException;
45 import org.sonar.server.permission.ws.DeleteTemplateAction;
46 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
47 import org.sonar.server.tester.UserSessionRule;
48 import org.sonar.server.ws.TestRequest;
49 import org.sonar.server.ws.TestResponse;
50 import org.sonar.server.ws.WsActionTester;
52 import static org.assertj.core.api.Assertions.assertThat;
53 import static org.mockito.Mockito.mock;
54 import static org.mockito.Mockito.when;
55 import static org.mockito.internal.util.collections.Sets.newSet;
56 import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
57 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_TEMPLATE_UUID;
58 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_TEMPLATE_NAME;
60 public class DeleteTemplateActionTest {
62 static final String TEMPLATE_UUID = "permission-template-uuid";
65 public DbTester db = DbTester.create(System2.INSTANCE);
67 public UserSessionRule userSession = UserSessionRule.standalone();
69 public ExpectedException expectedException = ExpectedException.none();
74 DefaultPermissionTemplateFinder defaultTemplatePermissionFinder;
76 PermissionTemplateDto permissionTemplate;
80 userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
82 dbClient = db.getDbClient();
83 dbSession = db.getSession();
84 defaultTemplatePermissionFinder = mock(DefaultPermissionTemplateFinder.class);
85 when(defaultTemplatePermissionFinder.getDefaultTemplateUuids()).thenReturn(Collections.<String>emptySet());
86 PermissionDependenciesFinder finder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient));
87 ws = new WsActionTester(new DeleteTemplateAction(dbClient, userSession, finder, defaultTemplatePermissionFinder));
89 permissionTemplate = insertTemplateAndAssociatedPermissions(newPermissionTemplateDto().setUuid(TEMPLATE_UUID));
90 PermissionTemplateDto permissionTemplateInDatabase = dbClient.permissionTemplateDao().selectByUuidWithUserAndGroupPermissions(dbSession, TEMPLATE_UUID);
91 assertThat(permissionTemplateInDatabase.getUuid()).isEqualTo(TEMPLATE_UUID);
92 assertThat(permissionTemplateInDatabase.getGroupsPermissions()).isNotEmpty();
93 assertThat(permissionTemplateInDatabase.getUsersPermissions()).isNotEmpty();
97 public void delete_template_in_db() {
98 TestResponse result = newRequest(TEMPLATE_UUID);
100 assertThat(result.getInput()).isEmpty();
101 assertThat(dbClient.permissionTemplateDao().selectByUuidWithUserAndGroupPermissions(dbSession, TEMPLATE_UUID)).isNull();
105 public void delete_template_by_name_case_insensitive() {
107 .setParam(PARAM_TEMPLATE_NAME, permissionTemplate.getName().toUpperCase())
111 assertThat(dbClient.permissionTemplateDao().selectByUuidWithUserAndGroupPermissions(dbSession, TEMPLATE_UUID)).isNull();
115 public void fail_if_uuid_is_not_known() {
116 expectedException.expect(NotFoundException.class);
118 newRequest("unknown-template-uuid");
122 public void fail_if_template_is_default() {
123 expectedException.expect(BadRequestException.class);
124 expectedException.expectMessage("It is not possible to delete a default template");
125 when(defaultTemplatePermissionFinder.getDefaultTemplateUuids()).thenReturn(newSet(TEMPLATE_UUID));
127 newRequest(TEMPLATE_UUID);
131 public void fail_if_not_logged_in() {
132 expectedException.expect(UnauthorizedException.class);
133 userSession.anonymous();
135 newRequest(TEMPLATE_UUID);
139 public void fail_if_not_admin() {
140 expectedException.expect(ForbiddenException.class);
141 userSession.login().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
143 newRequest(TEMPLATE_UUID);
147 public void fail_if_uuid_is_not_provided() {
148 expectedException.expect(BadRequestException.class);
153 private PermissionTemplateDto insertTemplateAndAssociatedPermissions(PermissionTemplateDto template) {
154 dbClient.permissionTemplateDao().insert(dbSession, template);
155 UserDto user = dbClient.userDao().insert(dbSession, UserTesting.newUserDto().setActive(true));
156 GroupDto group = dbClient.groupDao().insert(dbSession, GroupTesting.newGroupDto());
157 dbClient.permissionTemplateDao().insertUserPermission(dbSession, template.getId(), user.getId(), UserRole.ADMIN);
158 dbClient.permissionTemplateDao().insertGroupPermission(dbSession, template.getId(), group.getId(), UserRole.CODEVIEWER);
164 private void commit() {
168 private TestResponse newRequest(@Nullable String id) {
169 TestRequest request = ws.newRequest();
171 request.setParam(PARAM_TEMPLATE_UUID, id);
174 TestResponse result = executeRequest(request);
179 private static TestResponse executeRequest(TestRequest request) {
180 return request.execute();