]> source.dussan.org Git - sonarqube.git/blob
37a89b0457e725eacac11783bb50e1b0c38acc5a
[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 com.google.common.base.Function;
24 import java.util.List;
25 import javax.annotation.Nonnull;
26 import javax.annotation.Nullable;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.experimental.categories.Category;
31 import org.junit.rules.ExpectedException;
32 import org.sonar.api.resources.Qualifiers;
33 import org.sonar.api.utils.System2;
34 import org.sonar.core.permission.GlobalPermissions;
35 import org.sonar.db.DbClient;
36 import org.sonar.db.DbSession;
37 import org.sonar.db.DbTester;
38 import org.sonar.db.component.ResourceTypesRule;
39 import org.sonar.db.permission.GroupWithPermissionDto;
40 import org.sonar.db.permission.PermissionQuery;
41 import org.sonar.db.permission.PermissionTemplateDto;
42 import org.sonar.db.user.GroupDto;
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.PermissionDependenciesFinder;
49 import org.sonar.server.permission.ws.PermissionsWsParameters;
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.WsActionTester;
54 import org.sonar.test.DbTests;
55
56 import static com.google.common.collect.FluentIterable.from;
57 import static org.assertj.core.api.Assertions.assertThat;
58 import static org.sonar.api.security.DefaultGroups.ANYONE;
59 import static org.sonar.api.web.UserRole.CODEVIEWER;
60 import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
61 import static org.sonar.db.user.GroupMembershipQuery.IN;
62 import static org.sonar.db.user.GroupTesting.newGroupDto;
63 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_GROUP_ID;
64 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_GROUP_NAME;
65 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_PERMISSION;
66 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
67 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_TEMPLATE_UUID;
68
69 @Category(DbTests.class)
70 public class RemoveGroupFromTemplateActionTest {
71
72   private static final String GROUP_NAME = "group-name";
73   private static final String PERMISSION = CODEVIEWER;
74   @Rule
75   public DbTester db = DbTester.create(System2.INSTANCE);
76   @Rule
77   public ExpectedException expectedException = ExpectedException.none();
78   @Rule
79   public UserSessionRule userSession = UserSessionRule.standalone();
80   ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
81
82   WsActionTester ws;
83   DbClient dbClient;
84   DbSession dbSession;
85   GroupDto group;
86   PermissionTemplateDto permissionTemplate;
87
88   @Before
89   public void setUp() {
90     dbClient = db.getDbClient();
91     dbSession = db.getSession();
92     userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
93
94     PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), new UserGroupFinder(dbClient), resourceTypes);
95     ws = new WsActionTester(new RemoveGroupFromTemplateAction(dbClient, dependenciesFinder, userSession));
96
97     group = insertGroup(newGroupDto().setName(GROUP_NAME));
98     permissionTemplate = insertPermissionTemplate(newPermissionTemplateDto());
99     addGroupToPermissionTemplate(permissionTemplate.getId(), group.getId(), PERMISSION);
100     commit();
101   }
102
103   @Test
104   public void remove_group_from_template() {
105     assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).containsExactly(GROUP_NAME);
106     commit();
107
108     newRequest(GROUP_NAME, permissionTemplate.getUuid(), PERMISSION);
109
110     assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).isEmpty();
111   }
112
113   @Test
114   public void remove_group_from_template_by_name_case_insensitive() {
115     assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).containsExactly(GROUP_NAME);
116     commit();
117
118     ws.newRequest()
119       .setParam(PARAM_GROUP_NAME, GROUP_NAME)
120       .setParam(PARAM_PERMISSION, PERMISSION)
121       .setParam(PARAM_TEMPLATE_NAME, permissionTemplate.getName().toUpperCase())
122       .execute();
123     commit();
124
125     assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).isEmpty();
126   }
127
128   @Test
129   public void remove_group_with_group_id() {
130     assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).containsExactly(GROUP_NAME);
131     commit();
132
133     ws.newRequest()
134       .setParam(PARAM_TEMPLATE_UUID, permissionTemplate.getUuid())
135       .setParam(PARAM_PERMISSION, PERMISSION)
136       .setParam(PARAM_GROUP_ID, String.valueOf(group.getId()))
137       .execute();
138
139     assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).isEmpty();
140   }
141
142   @Test
143   public void remove_group_twice_without_error() {
144     newRequest(GROUP_NAME, permissionTemplate.getUuid(), PERMISSION);
145     newRequest(GROUP_NAME, permissionTemplate.getUuid(), PERMISSION);
146
147     assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).isEmpty();
148   }
149
150   @Test
151   public void remove_anyone_group_from_template() {
152     addGroupToPermissionTemplate(permissionTemplate.getId(), null, PERMISSION);
153     commit();
154
155     newRequest(ANYONE, permissionTemplate.getUuid(), PERMISSION);
156
157     assertThat(getGroupNamesInTemplateAndPermission(permissionTemplate.getId(), PERMISSION)).containsExactly(GROUP_NAME);
158   }
159
160   @Test
161   public void fail_if_not_a_project_permission() {
162     expectedException.expect(BadRequestException.class);
163
164     newRequest(GROUP_NAME, permissionTemplate.getUuid(), GlobalPermissions.PREVIEW_EXECUTION);
165   }
166
167   @Test
168   public void fail_if_insufficient_privileges() {
169     expectedException.expect(ForbiddenException.class);
170     userSession.setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
171
172     newRequest(GROUP_NAME, permissionTemplate.getUuid(), PERMISSION);
173   }
174
175   @Test
176   public void fail_if_not_logged_in() {
177     expectedException.expect(UnauthorizedException.class);
178     userSession.anonymous();
179
180     newRequest(GROUP_NAME, permissionTemplate.getUuid(), PERMISSION);
181   }
182
183   @Test
184   public void fail_if_group_params_missing() {
185     expectedException.expect(BadRequestException.class);
186
187     newRequest(null, permissionTemplate.getUuid(), PERMISSION);
188   }
189
190   @Test
191   public void fail_if_permission_missing() {
192     expectedException.expect(IllegalArgumentException.class);
193
194     newRequest(GROUP_NAME, permissionTemplate.getUuid(), null);
195   }
196
197   @Test
198   public void fail_if_template_missing() {
199     expectedException.expect(BadRequestException.class);
200
201     newRequest(GROUP_NAME, null, PERMISSION);
202   }
203
204   @Test
205   public void fail_if_group_does_not_exist() {
206     expectedException.expect(NotFoundException.class);
207     expectedException.expectMessage("Group with name 'unknown-group-name' is not found");
208
209     newRequest("unknown-group-name", permissionTemplate.getUuid(), PERMISSION);
210   }
211
212   @Test
213   public void fail_if_template_key_does_not_exist() {
214     expectedException.expect(NotFoundException.class);
215     expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
216
217     newRequest(GROUP_NAME, "unknown-key", PERMISSION);
218   }
219
220   private void newRequest(@Nullable String groupName, @Nullable String templateKey, @Nullable String permission) {
221     TestRequest request = ws.newRequest();
222     if (groupName != null) {
223       request.setParam(PermissionsWsParameters.PARAM_GROUP_NAME, groupName);
224     }
225     if (templateKey != null) {
226       request.setParam(PARAM_TEMPLATE_UUID, templateKey);
227     }
228     if (permission != null) {
229       request.setParam(PermissionsWsParameters.PARAM_PERMISSION, permission);
230     }
231
232     request.execute();
233   }
234
235   private void commit() {
236     dbSession.commit();
237   }
238
239   private GroupDto insertGroup(GroupDto groupDto) {
240     return dbClient.groupDao().insert(dbSession, groupDto);
241   }
242
243   private PermissionTemplateDto insertPermissionTemplate(PermissionTemplateDto permissionTemplate) {
244     return dbClient.permissionTemplateDao().insert(dbSession, permissionTemplate);
245   }
246
247   private void addGroupToPermissionTemplate(long permissionTemplateId, @Nullable Long groupId, String permission) {
248     dbClient.permissionTemplateDao().insertGroupPermission(dbSession, permissionTemplateId, groupId, permission);
249   }
250
251   private List<String> getGroupNamesInTemplateAndPermission(long templateId, String permission) {
252     PermissionQuery permissionQuery = PermissionQuery.builder().permission(permission).membership(IN).build();
253     return from(dbClient.permissionTemplateDao()
254       .selectGroups(dbSession, permissionQuery, templateId))
255       .transform(GroupWithPermissionToGroupName.INSTANCE)
256       .toList();
257   }
258
259   private enum GroupWithPermissionToGroupName implements Function<GroupWithPermissionDto, String> {
260     INSTANCE;
261
262     @Override
263     public String apply(@Nonnull GroupWithPermissionDto groupWithPermission) {
264       return groupWithPermission.getName();
265     }
266
267   }
268 }