2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
21 package org.sonar.server.permission.ws.template;
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;
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;
77 @Category(DbTests.class)
78 public class ApplyTemplateActionTest {
81 public DbTester db = DbTester.create(System2.INSTANCE);
83 public UserSessionRule userSession = UserSessionRule.standalone();
85 public ExpectedException expectedException = ExpectedException.none();
96 PermissionTemplateDto template1;
97 PermissionTemplateDto template2;
98 IssueAuthorizationIndexer issueAuthorizationIndexer = mock(IssueAuthorizationIndexer.class);
101 public void setUp() {
102 userSession.login("login").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
103 dbClient = db.getDbClient();
104 dbSession = db.getSession();
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);
112 ApplyTemplateAction underTest = new ApplyTemplateAction(dbClient, permissionService, permissionDependenciesFinder);
113 ws = new WsActionTester(underTest);
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"));
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);
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);
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);
143 public void apply_template_with_project_uuid() {
144 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).hasSize(2);
145 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).hasSize(2);
147 newRequest(template1.getUuid(), project.uuid(), null);
149 assertTemplate1AppliedToProject();
150 verify(issueAuthorizationIndexer).index();
154 public void apply_template_with_project_uuid_by_template_name() {
156 .setParam(PARAM_TEMPLATE_NAME, template1.getName().toUpperCase())
157 .setParam(PARAM_PROJECT_ID, project.uuid())
161 assertTemplate1AppliedToProject();
165 public void apply_template_with_project_key() {
166 newRequest(template1.getUuid(), null, project.key());
168 assertTemplate1AppliedToProject();
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");
176 newRequest("unknown-template-uuid", project.uuid(), null);
180 public void fail_when_unknown_project_uuid() {
181 expectedException.expect(NotFoundException.class);
182 expectedException.expectMessage("Project id 'unknown-project-uuid' not found");
184 newRequest(template1.getUuid(), "unknown-project-uuid", null);
188 public void fail_when_unknown_project_key() {
189 expectedException.expect(NotFoundException.class);
190 expectedException.expectMessage("Project key 'unknown-project-key' not found");
192 newRequest(template1.getUuid(), null, "unknown-project-key");
196 public void fail_when_template_is_not_provided() {
197 expectedException.expect(BadRequestException.class);
199 newRequest(null, project.uuid(), null);
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.");
207 newRequest(template1.getUuid(), null, null);
211 public void fail_when_anonymous() {
212 expectedException.expect(UnauthorizedException.class);
213 userSession.anonymous();
215 newRequest(template1.getUuid(), project.uuid(), null);
219 public void fail_when_insufficient_privileges() {
220 expectedException.expect(ForbiddenException.class);
221 userSession.login().setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
223 newRequest(template1.getUuid(), project.uuid(), null);
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());
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);
239 if (projectUuid != null) {
240 request.setParam(PARAM_PROJECT_ID, projectUuid);
242 if (projectKey != null) {
243 request.setParam(PARAM_PROJECT_KEY, projectKey);
246 TestResponse result = request.execute();
252 private ComponentDto insertProject(ComponentDto project) {
253 dbClient.componentDao().insert(dbSession, project);
254 return dbClient.componentDao().selectOrFailByUuid(dbSession, project.uuid());
257 private PermissionTemplateDto insertTemplate(PermissionTemplateDto template) {
258 return dbClient.permissionTemplateDao().insert(dbSession, template);
261 private UserDto insertUser(UserDto userDto) {
262 return dbClient.userDao().insert(dbSession, userDto.setActive(true));
265 private GroupDto insertGroup(GroupDto group) {
266 return dbClient.groupDao().insert(dbSession, group);
269 private void addUserToTemplate(UserDto user, PermissionTemplateDto permissionTemplate, String permission) {
270 dbClient.permissionTemplateDao().insertUserPermission(dbSession, permissionTemplate.getId(), user.getId(), permission);
273 private void addGroupToTemplate(GroupDto group, PermissionTemplateDto permissionTemplate, String permission) {
274 dbClient.permissionTemplateDao().insertGroupPermission(dbSession, permissionTemplate.getId(), group.getId(), permission);
277 private void addUserPermissionToProject(UserDto user, ComponentDto project, String permission) {
278 dbClient.roleDao().insertUserRole(dbSession, new UserRoleDto()
280 .setUserId(user.getId())
281 .setResourceId(project.getId()));
284 private void addGroupPermissionToProject(GroupDto group, ComponentDto project, String permission) {
285 dbClient.roleDao().insertGroupRole(dbSession, new GroupRoleDto()
287 .setResourceId(project.getId())
288 .setGroupId(group.getId()));
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())
297 private List<UserWithPermissionDto> selectProjectPermissionUsers(ComponentDto project, String permission) {
298 return dbClient.permissionDao().selectUsers(dbSession, query(permission), project.getId(), 0, Integer.MAX_VALUE);
301 private void commit() {
305 private static PermissionQuery query(String permission) {
306 return PermissionQuery.builder().membership(IN).permission(permission).build();
309 private static class PermissionNotNull implements Predicate<GroupWithPermissionDto> {
311 public boolean apply(@Nullable GroupWithPermissionDto input) {
312 return input.getPermission() != null;