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