]> source.dussan.org Git - sonarqube.git/blob
89e662229466a752b6ea76a1cd11bed80bd5e17a
[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.Predicate;
23 import com.google.common.collect.FluentIterable;
24 import java.util.List;
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.config.Settings;
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.ComponentDto;
40 import org.sonar.db.component.ResourceTypesRule;
41 import org.sonar.db.permission.GroupWithPermissionDto;
42 import org.sonar.db.permission.PermissionQuery;
43 import org.sonar.db.permission.PermissionRepository;
44 import org.sonar.db.permission.PermissionTemplateDto;
45 import org.sonar.db.permission.UserWithPermissionDto;
46 import org.sonar.db.user.GroupDto;
47 import org.sonar.db.user.GroupRoleDto;
48 import org.sonar.db.user.UserDto;
49 import org.sonar.db.user.UserRoleDto;
50 import org.sonar.server.component.ComponentFinder;
51 import org.sonar.server.exceptions.BadRequestException;
52 import org.sonar.server.exceptions.ForbiddenException;
53 import org.sonar.server.exceptions.NotFoundException;
54 import org.sonar.server.exceptions.UnauthorizedException;
55 import org.sonar.server.issue.index.IssueAuthorizationIndexer;
56 import org.sonar.server.permission.PermissionService;
57 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
58 import org.sonar.server.tester.UserSessionRule;
59 import org.sonar.server.usergroups.ws.UserGroupFinder;
60 import org.sonar.server.ws.TestRequest;
61 import org.sonar.server.ws.TestResponse;
62 import org.sonar.server.ws.WsActionTester;
63 import org.sonar.test.DbTests;
64
65 import static org.assertj.core.api.Assertions.assertThat;
66 import static org.mockito.Mockito.mock;
67 import static org.mockito.Mockito.verify;
68 import static org.sonar.db.component.ComponentTesting.newProjectDto;
69 import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
70 import static org.sonar.db.user.GroupMembershipQuery.IN;
71 import static org.sonar.db.user.GroupTesting.newGroupDto;
72 import static org.sonar.db.user.UserTesting.newUserDto;
73 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID;
74 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY;
75 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
76 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
77
78 @Category(DbTests.class)
79 public class ApplyTemplateActionTest {
80
81   @Rule
82   public DbTester db = DbTester.create(System2.INSTANCE);
83   @Rule
84   public UserSessionRule userSession = UserSessionRule.standalone();
85   @Rule
86   public ExpectedException expectedException = ExpectedException.none();
87   ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
88
89   WsActionTester ws;
90   DbClient dbClient;
91   DbSession dbSession;
92
93   UserDto user1;
94   UserDto user2;
95   GroupDto group1;
96   GroupDto group2;
97   ComponentDto project;
98   PermissionTemplateDto template1;
99   PermissionTemplateDto template2;
100   IssueAuthorizationIndexer issueAuthorizationIndexer = mock(IssueAuthorizationIndexer.class);
101
102   @Before
103   public void setUp() {
104     userSession.login("login").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
105     dbClient = db.getDbClient();
106     dbSession = db.getSession();
107
108     PermissionRepository repository = new PermissionRepository(dbClient, new Settings());
109     ComponentFinder componentFinder = new ComponentFinder(dbClient);
110     PermissionService permissionService = new PermissionService(dbClient, repository, issueAuthorizationIndexer, userSession, componentFinder);
111     PermissionDependenciesFinder permissionDependenciesFinder = new PermissionDependenciesFinder(dbClient, componentFinder, new UserGroupFinder(dbClient), resourceTypes);
112
113     ApplyTemplateAction underTest = new ApplyTemplateAction(dbClient, permissionService, permissionDependenciesFinder);
114     ws = new WsActionTester(underTest);
115
116     user1 = insertUser(newUserDto().setLogin("user-login-1"));
117     user2 = insertUser(newUserDto().setLogin("user-login-2"));
118     group1 = insertGroup(newGroupDto().setName("group-name-1"));
119     group2 = insertGroup(newGroupDto().setName("group-name-2"));
120
121     // template 1
122     template1 = insertTemplate(newPermissionTemplateDto().setUuid("permission-template-uuid-1"));
123     addUserToTemplate(user1, template1, UserRole.CODEVIEWER);
124     addUserToTemplate(user2, template1, UserRole.ISSUE_ADMIN);
125     addGroupToTemplate(group1, template1, UserRole.ADMIN);
126     addGroupToTemplate(group2, template1, UserRole.USER);
127     // template 2
128     template2 = insertTemplate(newPermissionTemplateDto().setUuid("permission-template-uuid-2"));
129     addUserToTemplate(user1, template2, UserRole.USER);
130     addUserToTemplate(user2, template2, UserRole.USER);
131     addGroupToTemplate(group1, template2, UserRole.USER);
132     addGroupToTemplate(group2, template2, UserRole.USER);
133
134     project = insertProject(newProjectDto("project-uuid-1"));
135     addUserPermissionToProject(user1, project, UserRole.ADMIN);
136     addUserPermissionToProject(user2, project, UserRole.ADMIN);
137     addGroupPermissionToProject(group1, project, UserRole.ADMIN);
138     addGroupPermissionToProject(group2, project, UserRole.ADMIN);
139
140     commit();
141   }
142
143   @Test
144   public void apply_template_with_project_uuid() {
145     assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).hasSize(2);
146     assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).hasSize(2);
147
148     newRequest(template1.getUuid(), project.uuid(), null);
149
150     assertTemplate1AppliedToProject();
151     verify(issueAuthorizationIndexer).index();
152   }
153
154   @Test
155   public void apply_template_with_project_uuid_by_template_name() {
156     ws.newRequest()
157       .setParam(PARAM_TEMPLATE_NAME, template1.getName().toUpperCase())
158       .setParam(PARAM_PROJECT_ID, project.uuid())
159       .execute();
160     commit();
161
162     assertTemplate1AppliedToProject();
163   }
164
165   @Test
166   public void apply_template_with_project_key() {
167     newRequest(template1.getUuid(), null, project.key());
168
169     assertTemplate1AppliedToProject();
170   }
171
172   @Test
173   public void fail_when_unknown_template() {
174     expectedException.expect(NotFoundException.class);
175     expectedException.expectMessage("Permission template with id 'unknown-template-uuid' is not found");
176
177     newRequest("unknown-template-uuid", project.uuid(), null);
178   }
179
180   @Test
181   public void fail_when_unknown_project_uuid() {
182     expectedException.expect(NotFoundException.class);
183     expectedException.expectMessage("Project id 'unknown-project-uuid' not found");
184
185     newRequest(template1.getUuid(), "unknown-project-uuid", null);
186   }
187
188   @Test
189   public void fail_when_unknown_project_key() {
190     expectedException.expect(NotFoundException.class);
191     expectedException.expectMessage("Project key 'unknown-project-key' not found");
192
193     newRequest(template1.getUuid(), null, "unknown-project-key");
194   }
195
196   @Test
197   public void fail_when_template_is_not_provided() {
198     expectedException.expect(BadRequestException.class);
199
200     newRequest(null, project.uuid(), null);
201   }
202
203   @Test
204   public void fail_when_project_uuid_and_key_not_provided() {
205     expectedException.expect(BadRequestException.class);
206     expectedException.expectMessage("Project id or project key must be provided, not both.");
207
208     newRequest(template1.getUuid(), null, null);
209   }
210
211   @Test
212   public void fail_when_anonymous() {
213     expectedException.expect(UnauthorizedException.class);
214     userSession.anonymous();
215
216     newRequest(template1.getUuid(), project.uuid(), null);
217   }
218
219   @Test
220   public void fail_when_insufficient_privileges() {
221     expectedException.expect(ForbiddenException.class);
222     userSession.login().setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
223
224     newRequest(template1.getUuid(), project.uuid(), null);
225   }
226
227   private void assertTemplate1AppliedToProject() {
228     assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).extracting("name").containsExactly(group1.getName());
229     assertThat(selectProjectPermissionGroups(project, UserRole.USER)).extracting("name").containsExactly(group2.getName());
230     assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
231     assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).extracting("login").containsExactly(user1.getLogin());
232     assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).extracting("login").containsExactly(user2.getLogin());
233   }
234
235   private TestResponse newRequest(@Nullable String templateUuid, @Nullable String projectUuid, @Nullable String projectKey) {
236     TestRequest request = ws.newRequest();
237     if (templateUuid != null) {
238       request.setParam(PARAM_TEMPLATE_ID, templateUuid);
239     }
240     if (projectUuid != null) {
241       request.setParam(PARAM_PROJECT_ID, projectUuid);
242     }
243     if (projectKey != null) {
244       request.setParam(PARAM_PROJECT_KEY, projectKey);
245     }
246
247     TestResponse result = request.execute();
248     commit();
249
250     return result;
251   }
252
253   private ComponentDto insertProject(ComponentDto project) {
254     dbClient.componentDao().insert(dbSession, project);
255     return dbClient.componentDao().selectOrFailByUuid(dbSession, project.uuid());
256   }
257
258   private PermissionTemplateDto insertTemplate(PermissionTemplateDto template) {
259     return dbClient.permissionTemplateDao().insert(dbSession, template);
260   }
261
262   private UserDto insertUser(UserDto userDto) {
263     return dbClient.userDao().insert(dbSession, userDto.setActive(true));
264   }
265
266   private GroupDto insertGroup(GroupDto group) {
267     return dbClient.groupDao().insert(dbSession, group);
268   }
269
270   private void addUserToTemplate(UserDto user, PermissionTemplateDto permissionTemplate, String permission) {
271     dbClient.permissionTemplateDao().insertUserPermission(dbSession, permissionTemplate.getId(), user.getId(), permission);
272   }
273
274   private void addGroupToTemplate(GroupDto group, PermissionTemplateDto permissionTemplate, String permission) {
275     dbClient.permissionTemplateDao().insertGroupPermission(dbSession, permissionTemplate.getId(), group.getId(), permission);
276   }
277
278   private void addUserPermissionToProject(UserDto user, ComponentDto project, String permission) {
279     dbClient.roleDao().insertUserRole(dbSession, new UserRoleDto()
280       .setRole(permission)
281       .setUserId(user.getId())
282       .setResourceId(project.getId()));
283   }
284
285   private void addGroupPermissionToProject(GroupDto group, ComponentDto project, String permission) {
286     dbClient.roleDao().insertGroupRole(dbSession, new GroupRoleDto()
287       .setRole(permission)
288       .setResourceId(project.getId())
289       .setGroupId(group.getId()));
290   }
291
292   private List<GroupWithPermissionDto> selectProjectPermissionGroups(ComponentDto project, String permission) {
293     return FluentIterable.from(dbClient.permissionDao().selectGroups(dbSession, query(permission), project.getId()))
294       .filter(new PermissionNotNull())
295       .toList();
296   }
297
298   private List<UserWithPermissionDto> selectProjectPermissionUsers(ComponentDto project, String permission) {
299     return dbClient.permissionDao().selectUsers(dbSession, query(permission), project.getId(), 0, Integer.MAX_VALUE);
300   }
301
302   private void commit() {
303     dbSession.commit();
304   }
305
306   private static PermissionQuery query(String permission) {
307     return PermissionQuery.builder().membership(IN).permission(permission).build();
308   }
309
310   private static class PermissionNotNull implements Predicate<GroupWithPermissionDto> {
311     @Override
312     public boolean apply(@Nullable GroupWithPermissionDto input) {
313       return input.getPermission() != null;
314     }
315   }
316 }