]> source.dussan.org Git - sonarqube.git/blob
83f56144c542bf2c4d23528c9edac93ed19ca2a8
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2014 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 package org.sonar.server.permission.ws.template;
22
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;
54
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;
62
63 public class DeleteTemplateActionTest {
64
65   static final String TEMPLATE_UUID = "permission-template-uuid";
66
67   @Rule
68   public DbTester db = DbTester.create(System2.INSTANCE);
69   @Rule
70   public UserSessionRule userSession = UserSessionRule.standalone();
71   @Rule
72   public ExpectedException expectedException = ExpectedException.none();
73   ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
74
75   WsActionTester ws;
76   DbClient dbClient;
77   DbSession dbSession;
78   DefaultPermissionTemplateFinder defaultTemplatePermissionFinder;
79
80   PermissionTemplateDto permissionTemplate;
81
82   @Before
83   public void setUp() {
84     userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
85
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));
92
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();
98   }
99
100   @Test
101   public void delete_template_in_db() {
102     TestResponse result = newRequest(TEMPLATE_UUID);
103
104     assertThat(result.getInput()).isEmpty();
105     assertThat(dbClient.permissionTemplateDao().selectByUuidWithUserAndGroupPermissions(dbSession, TEMPLATE_UUID)).isNull();
106   }
107
108   @Test
109   public void delete_template_by_name_case_insensitive() {
110     ws.newRequest()
111       .setParam(PARAM_TEMPLATE_NAME, permissionTemplate.getName().toUpperCase())
112       .execute();
113     commit();
114
115     assertThat(dbClient.permissionTemplateDao().selectByUuidWithUserAndGroupPermissions(dbSession, TEMPLATE_UUID)).isNull();
116   }
117
118   @Test
119   public void fail_if_uuid_is_not_known() {
120     expectedException.expect(NotFoundException.class);
121
122     newRequest("unknown-template-uuid");
123   }
124
125   @Test
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));
130
131     newRequest(TEMPLATE_UUID);
132   }
133
134   @Test
135   public void fail_if_not_logged_in() {
136     expectedException.expect(UnauthorizedException.class);
137     userSession.anonymous();
138
139     newRequest(TEMPLATE_UUID);
140   }
141
142   @Test
143   public void fail_if_not_admin() {
144     expectedException.expect(ForbiddenException.class);
145     userSession.login().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
146
147     newRequest(TEMPLATE_UUID);
148   }
149
150   @Test
151   public void fail_if_uuid_is_not_provided() {
152     expectedException.expect(BadRequestException.class);
153
154     newRequest(null);
155   }
156
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);
163     commit();
164
165     return template;
166   }
167
168   private void commit() {
169     dbSession.commit();
170   }
171
172   private TestResponse newRequest(@Nullable String id) {
173     TestRequest request = ws.newRequest();
174     if (id != null) {
175       request.setParam(PARAM_TEMPLATE_UUID, id);
176     }
177
178     TestResponse result = executeRequest(request);
179     commit();
180     return result;
181   }
182
183   private static TestResponse executeRequest(TestRequest request) {
184     return request.execute();
185   }
186
187 }