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.Collections;
23 import java.util.Date;
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.permission.template.PermissionTemplateCharacteristicDto;
39 import org.sonar.db.user.GroupDto;
40 import org.sonar.db.user.GroupTesting;
41 import org.sonar.db.user.UserDto;
42 import org.sonar.db.user.UserTesting;
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.DeleteTemplateAction;
49 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
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.TestResponse;
54 import org.sonar.server.ws.WsActionTester;
56 import static com.google.common.primitives.Longs.asList;
57 import static org.assertj.core.api.Assertions.assertThat;
58 import static org.mockito.Mockito.mock;
59 import static org.mockito.Mockito.when;
60 import static org.mockito.internal.util.collections.Sets.newSet;
61 import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
62 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
63 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
65 public class DeleteTemplateActionTest {
67 static final String TEMPLATE_UUID = "permission-template-uuid";
70 public DbTester db = DbTester.create(System2.INSTANCE);
72 public UserSessionRule userSession = UserSessionRule.standalone();
74 public ExpectedException expectedException = ExpectedException.none();
75 ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
80 DefaultPermissionTemplateFinder defaultTemplatePermissionFinder;
82 PermissionTemplateDto permissionTemplate;
86 userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
88 dbClient = db.getDbClient();
89 dbSession = db.getSession();
90 defaultTemplatePermissionFinder = mock(DefaultPermissionTemplateFinder.class);
91 when(defaultTemplatePermissionFinder.getDefaultTemplateUuids()).thenReturn(Collections.<String>emptySet());
92 PermissionDependenciesFinder finder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), new UserGroupFinder(dbClient), resourceTypes);
93 ws = new WsActionTester(new DeleteTemplateAction(dbClient, userSession, finder, defaultTemplatePermissionFinder));
94 permissionTemplate = insertTemplateAndAssociatedPermissions(newPermissionTemplateDto().setUuid(TEMPLATE_UUID));
98 public void delete_template_in_db() {
99 TestResponse result = newRequest(TEMPLATE_UUID);
101 assertThat(result.getInput()).isEmpty();
102 assertThat(dbClient.permissionTemplateDao().selectByUuidWithUserAndGroupPermissions(dbSession, TEMPLATE_UUID)).isNull();
106 public void delete_template_by_name_case_insensitive() {
108 .setParam(PARAM_TEMPLATE_NAME, permissionTemplate.getName().toUpperCase())
112 assertThat(dbClient.permissionTemplateDao().selectByUuidWithUserAndGroupPermissions(dbSession, TEMPLATE_UUID)).isNull();
116 public void fail_if_uuid_is_not_known() {
117 expectedException.expect(NotFoundException.class);
119 newRequest("unknown-template-uuid");
123 public void fail_if_template_is_default() {
124 expectedException.expect(BadRequestException.class);
125 expectedException.expectMessage("It is not possible to delete a default template");
126 when(defaultTemplatePermissionFinder.getDefaultTemplateUuids()).thenReturn(newSet(TEMPLATE_UUID));
128 newRequest(TEMPLATE_UUID);
132 public void fail_if_not_logged_in() {
133 expectedException.expect(UnauthorizedException.class);
134 userSession.anonymous();
136 newRequest(TEMPLATE_UUID);
140 public void fail_if_not_admin() {
141 expectedException.expect(ForbiddenException.class);
142 userSession.login().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
144 newRequest(TEMPLATE_UUID);
148 public void fail_if_uuid_is_not_provided() {
149 expectedException.expect(BadRequestException.class);
155 public void delete_perm_tpl_characteristic_when_delete_template() throws Exception {
156 dbClient.permissionTemplateCharacteristicDao().insert(dbSession, new PermissionTemplateCharacteristicDto()
157 .setPermission(UserRole.USER)
158 .setTemplateId(permissionTemplate.getId())
159 .setWithProjectCreator(true)
160 .setCreatedAt(new Date().getTime())
161 .setUpdatedAt(new Date().getTime()));
163 assertThat(dbClient.permissionTemplateCharacteristicDao().selectByTemplateIds(dbSession, asList(permissionTemplate.getId()))).hasSize(1);
165 newRequest(TEMPLATE_UUID);
167 assertThat(dbClient.permissionTemplateCharacteristicDao().selectByTemplateIds(dbSession, asList(permissionTemplate.getId()))).isEmpty();
170 private PermissionTemplateDto insertTemplateAndAssociatedPermissions(PermissionTemplateDto template) {
171 dbClient.permissionTemplateDao().insert(dbSession, template);
172 UserDto user = dbClient.userDao().insert(dbSession, UserTesting.newUserDto().setActive(true));
173 GroupDto group = dbClient.groupDao().insert(dbSession, GroupTesting.newGroupDto());
174 dbClient.permissionTemplateDao().insertUserPermission(dbSession, template.getId(), user.getId(), UserRole.ADMIN);
175 dbClient.permissionTemplateDao().insertGroupPermission(dbSession, template.getId(), group.getId(), UserRole.CODEVIEWER);
181 private void commit() {
185 private TestResponse newRequest(@Nullable String id) {
186 TestRequest request = ws.newRequest();
188 request.setParam(PARAM_TEMPLATE_ID, id);
191 TestResponse result = executeRequest(request);
196 private static TestResponse executeRequest(TestRequest request) {
197 return request.execute();