]> source.dussan.org Git - sonarqube.git/blob
2b36c654f1316b027a3da5caa9120d915cf93696
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
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 package org.sonar.server.permission.ws.template;
21
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;
55
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;
64
65 public class DeleteTemplateActionTest {
66
67   static final String TEMPLATE_UUID = "permission-template-uuid";
68
69   @Rule
70   public DbTester db = DbTester.create(System2.INSTANCE);
71   @Rule
72   public UserSessionRule userSession = UserSessionRule.standalone();
73   @Rule
74   public ExpectedException expectedException = ExpectedException.none();
75   ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
76
77   WsActionTester ws;
78   DbClient dbClient;
79   DbSession dbSession;
80   DefaultPermissionTemplateFinder defaultTemplatePermissionFinder;
81
82   PermissionTemplateDto permissionTemplate;
83
84   @Before
85   public void setUp() {
86     userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
87
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));
95   }
96
97   @Test
98   public void delete_template_in_db() {
99     TestResponse result = newRequest(TEMPLATE_UUID);
100
101     assertThat(result.getInput()).isEmpty();
102     assertThat(dbClient.permissionTemplateDao().selectByUuidWithUserAndGroupPermissions(dbSession, TEMPLATE_UUID)).isNull();
103   }
104
105   @Test
106   public void delete_template_by_name_case_insensitive() {
107     ws.newRequest()
108       .setParam(PARAM_TEMPLATE_NAME, permissionTemplate.getName().toUpperCase())
109       .execute();
110     commit();
111
112     assertThat(dbClient.permissionTemplateDao().selectByUuidWithUserAndGroupPermissions(dbSession, TEMPLATE_UUID)).isNull();
113   }
114
115   @Test
116   public void fail_if_uuid_is_not_known() {
117     expectedException.expect(NotFoundException.class);
118
119     newRequest("unknown-template-uuid");
120   }
121
122   @Test
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));
127
128     newRequest(TEMPLATE_UUID);
129   }
130
131   @Test
132   public void fail_if_not_logged_in() {
133     expectedException.expect(UnauthorizedException.class);
134     userSession.anonymous();
135
136     newRequest(TEMPLATE_UUID);
137   }
138
139   @Test
140   public void fail_if_not_admin() {
141     expectedException.expect(ForbiddenException.class);
142     userSession.login().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
143
144     newRequest(TEMPLATE_UUID);
145   }
146
147   @Test
148   public void fail_if_uuid_is_not_provided() {
149     expectedException.expect(BadRequestException.class);
150
151     newRequest(null);
152   }
153
154   @Test
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()));
162     dbSession.commit();
163     assertThat(dbClient.permissionTemplateCharacteristicDao().selectByTemplateIds(dbSession, asList(permissionTemplate.getId()))).hasSize(1);
164
165     newRequest(TEMPLATE_UUID);
166
167     assertThat(dbClient.permissionTemplateCharacteristicDao().selectByTemplateIds(dbSession, asList(permissionTemplate.getId()))).isEmpty();
168   }
169
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);
176     commit();
177
178     return template;
179   }
180
181   private void commit() {
182     dbSession.commit();
183   }
184
185   private TestResponse newRequest(@Nullable String id) {
186     TestRequest request = ws.newRequest();
187     if (id != null) {
188       request.setParam(PARAM_TEMPLATE_ID, id);
189     }
190
191     TestResponse result = executeRequest(request);
192     commit();
193     return result;
194   }
195
196   private static TestResponse executeRequest(TestRequest request) {
197     return request.execute();
198   }
199
200 }