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