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.Test;
27 import org.sonar.api.web.UserRole;
28 import org.sonar.core.permission.GlobalPermissions;
29 import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
30 import org.sonar.db.permission.template.PermissionTemplateDto;
31 import org.sonar.db.user.GroupDto;
32 import org.sonar.db.user.GroupTesting;
33 import org.sonar.db.user.UserDto;
34 import org.sonar.db.user.UserTesting;
35 import org.sonar.server.exceptions.BadRequestException;
36 import org.sonar.server.exceptions.ForbiddenException;
37 import org.sonar.server.exceptions.NotFoundException;
38 import org.sonar.server.exceptions.UnauthorizedException;
39 import org.sonar.server.permission.ws.BasePermissionWsTest;
40 import org.sonar.server.ws.WsTester;
42 import static com.google.common.primitives.Longs.asList;
43 import static org.assertj.core.api.Assertions.assertThat;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.when;
46 import static org.mockito.internal.util.collections.Sets.newSet;
47 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.CONTROLLER;
48 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
49 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
51 public class DeleteTemplateActionTest extends BasePermissionWsTest<DeleteTemplateAction> {
53 private static final String ACTION = "delete_template";
55 private DefaultPermissionTemplateFinder defaultTemplatePermissionFinder = mock(DefaultPermissionTemplateFinder.class);
56 private PermissionTemplateDto template;
59 protected DeleteTemplateAction buildWsAction() {
60 return new DeleteTemplateAction(db.getDbClient(), userSession, newPermissionWsSupport(), defaultTemplatePermissionFinder);
65 loginAsAdminOnDefaultOrganization();
66 when(defaultTemplatePermissionFinder.getDefaultTemplateUuids()).thenReturn(Collections.emptySet());
67 template = insertTemplateAndAssociatedPermissions();
71 public void delete_template_in_db() throws Exception {
72 WsTester.Result result = newRequest(template.getUuid());
74 assertThat(result.outputAsString()).isEmpty();
75 assertThat(db.getDbClient().permissionTemplateDao().selectByUuid(db.getSession(), template.getUuid())).isNull();
79 public void delete_template_by_name_case_insensitive() throws Exception {
80 wsTester.newPostRequest(CONTROLLER, ACTION)
81 .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
84 assertThat(db.getDbClient().permissionTemplateDao().selectByUuid(db.getSession(), template.getUuid())).isNull();
88 public void fail_if_uuid_is_not_known() throws Exception {
89 expectedException.expect(NotFoundException.class);
91 newRequest("unknown-template-uuid");
95 public void fail_if_template_is_default() throws Exception {
96 when(defaultTemplatePermissionFinder.getDefaultTemplateUuids()).thenReturn(newSet(template.getUuid()));
98 expectedException.expect(BadRequestException.class);
99 expectedException.expectMessage("It is not possible to delete a default template");
101 newRequest(template.getUuid());
105 public void fail_if_not_logged_in() throws Exception {
106 expectedException.expect(UnauthorizedException.class);
107 userSession.anonymous();
109 newRequest(template.getUuid());
113 public void fail_if_not_admin() throws Exception {
114 expectedException.expect(ForbiddenException.class);
115 userSession.login().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
117 newRequest(template.getUuid());
121 public void fail_if_uuid_is_not_provided() throws Exception {
122 expectedException.expect(BadRequestException.class);
128 public void delete_perm_tpl_characteristic_when_delete_template() throws Exception {
129 db.getDbClient().permissionTemplateCharacteristicDao().insert(db.getSession(), new PermissionTemplateCharacteristicDto()
130 .setPermission(UserRole.USER)
131 .setTemplateId(template.getId())
132 .setWithProjectCreator(true)
133 .setCreatedAt(new Date().getTime())
134 .setUpdatedAt(new Date().getTime()));
137 newRequest(template.getUuid());
139 assertThat(db.getDbClient().permissionTemplateCharacteristicDao().selectByTemplateIds(db.getSession(), asList(template.getId()))).isEmpty();
142 private PermissionTemplateDto insertTemplateAndAssociatedPermissions() {
143 PermissionTemplateDto dto = addTemplateToDefaultOrganization();
144 UserDto user = db.getDbClient().userDao().insert(db.getSession(), UserTesting.newUserDto().setActive(true));
145 GroupDto group = db.getDbClient().groupDao().insert(db.getSession(), GroupTesting.newGroupDto());
146 db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), dto.getId(), user.getId(), UserRole.ADMIN);
147 db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), dto.getId(), group.getId(), UserRole.CODEVIEWER);
152 private WsTester.Result newRequest(@Nullable String id) throws Exception {
153 WsTester.TestRequest request = wsTester.newPostRequest(CONTROLLER, ACTION);
155 request.setParam(PARAM_TEMPLATE_ID, id);
158 return request.execute();