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