3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.permission.ws.template;
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;
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;
78 @Category(DbTests.class)
79 public class ApplyTemplateActionTest {
82 public DbTester db = DbTester.create(System2.INSTANCE);
84 public UserSessionRule userSession = UserSessionRule.standalone();
86 public ExpectedException expectedException = ExpectedException.none();
87 ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
98 PermissionTemplateDto template1;
99 PermissionTemplateDto template2;
100 IssueAuthorizationIndexer issueAuthorizationIndexer = mock(IssueAuthorizationIndexer.class);
103 public void setUp() {
104 userSession.login("login").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
105 dbClient = db.getDbClient();
106 dbSession = db.getSession();
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);
113 ApplyTemplateAction underTest = new ApplyTemplateAction(dbClient, permissionService, permissionDependenciesFinder);
114 ws = new WsActionTester(underTest);
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"));
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);
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);
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);
144 public void apply_template_with_project_uuid() {
145 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).hasSize(2);
146 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).hasSize(2);
148 newRequest(template1.getUuid(), project.uuid(), null);
150 assertTemplate1AppliedToProject();
151 verify(issueAuthorizationIndexer).index();
155 public void apply_template_with_project_uuid_by_template_name() {
157 .setParam(PARAM_TEMPLATE_NAME, template1.getName().toUpperCase())
158 .setParam(PARAM_PROJECT_ID, project.uuid())
162 assertTemplate1AppliedToProject();
166 public void apply_template_with_project_key() {
167 newRequest(template1.getUuid(), null, project.key());
169 assertTemplate1AppliedToProject();
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");
177 newRequest("unknown-template-uuid", project.uuid(), null);
181 public void fail_when_unknown_project_uuid() {
182 expectedException.expect(NotFoundException.class);
183 expectedException.expectMessage("Project id 'unknown-project-uuid' not found");
185 newRequest(template1.getUuid(), "unknown-project-uuid", null);
189 public void fail_when_unknown_project_key() {
190 expectedException.expect(NotFoundException.class);
191 expectedException.expectMessage("Project key 'unknown-project-key' not found");
193 newRequest(template1.getUuid(), null, "unknown-project-key");
197 public void fail_when_template_is_not_provided() {
198 expectedException.expect(BadRequestException.class);
200 newRequest(null, project.uuid(), null);
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.");
208 newRequest(template1.getUuid(), null, null);
212 public void fail_when_anonymous() {
213 expectedException.expect(UnauthorizedException.class);
214 userSession.anonymous();
216 newRequest(template1.getUuid(), project.uuid(), null);
220 public void fail_when_insufficient_privileges() {
221 expectedException.expect(ForbiddenException.class);
222 userSession.login().setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
224 newRequest(template1.getUuid(), project.uuid(), null);
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());
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);
240 if (projectUuid != null) {
241 request.setParam(PARAM_PROJECT_ID, projectUuid);
243 if (projectKey != null) {
244 request.setParam(PARAM_PROJECT_KEY, projectKey);
247 TestResponse result = request.execute();
253 private ComponentDto insertProject(ComponentDto project) {
254 dbClient.componentDao().insert(dbSession, project);
255 return dbClient.componentDao().selectOrFailByUuid(dbSession, project.uuid());
258 private PermissionTemplateDto insertTemplate(PermissionTemplateDto template) {
259 return dbClient.permissionTemplateDao().insert(dbSession, template);
262 private UserDto insertUser(UserDto userDto) {
263 return dbClient.userDao().insert(dbSession, userDto.setActive(true));
266 private GroupDto insertGroup(GroupDto group) {
267 return dbClient.groupDao().insert(dbSession, group);
270 private void addUserToTemplate(UserDto user, PermissionTemplateDto permissionTemplate, String permission) {
271 dbClient.permissionTemplateDao().insertUserPermission(dbSession, permissionTemplate.getId(), user.getId(), permission);
274 private void addGroupToTemplate(GroupDto group, PermissionTemplateDto permissionTemplate, String permission) {
275 dbClient.permissionTemplateDao().insertGroupPermission(dbSession, permissionTemplate.getId(), group.getId(), permission);
278 private void addUserPermissionToProject(UserDto user, ComponentDto project, String permission) {
279 dbClient.roleDao().insertUserRole(dbSession, new UserRoleDto()
281 .setUserId(user.getId())
282 .setResourceId(project.getId()));
285 private void addGroupPermissionToProject(GroupDto group, ComponentDto project, String permission) {
286 dbClient.roleDao().insertGroupRole(dbSession, new GroupRoleDto()
288 .setResourceId(project.getId())
289 .setGroupId(group.getId()));
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())
298 private List<UserWithPermissionDto> selectProjectPermissionUsers(ComponentDto project, String permission) {
299 return dbClient.permissionDao().selectUsers(dbSession, query(permission), project.getId(), 0, Integer.MAX_VALUE);
302 private void commit() {
306 private static PermissionQuery query(String permission) {
307 return PermissionQuery.builder().membership(IN).permission(permission).build();
310 private static class PermissionNotNull implements Predicate<GroupWithPermissionDto> {
312 public boolean apply(@Nullable GroupWithPermissionDto input) {
313 return input.getPermission() != null;