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 java.util.Collections;
23 import java.util.List;
24 import javax.annotation.Nullable;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.api.config.MapSettings;
29 import org.sonar.api.web.UserRole;
30 import org.sonar.core.permission.GlobalPermissions;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.db.organization.OrganizationDto;
33 import org.sonar.db.permission.PermissionQuery;
34 import org.sonar.db.permission.PermissionRepository;
35 import org.sonar.db.permission.template.PermissionTemplateDto;
36 import org.sonar.db.user.GroupDto;
37 import org.sonar.db.user.UserDto;
38 import org.sonar.server.component.es.ProjectMeasuresIndexDefinition;
39 import org.sonar.server.es.EsTester;
40 import org.sonar.server.exceptions.BadRequestException;
41 import org.sonar.server.exceptions.ForbiddenException;
42 import org.sonar.server.exceptions.NotFoundException;
43 import org.sonar.server.issue.index.IssueIndexDefinition;
44 import org.sonar.server.permission.PermissionTemplateService;
45 import org.sonar.server.permission.index.PermissionIndexer;
46 import org.sonar.server.permission.index.PermissionIndexerTester;
47 import org.sonar.server.permission.ws.BasePermissionWsTest;
48 import org.sonar.server.ws.TestRequest;
49 import org.sonar.server.ws.TestResponse;
51 import static java.util.Collections.singletonList;
52 import static org.assertj.core.api.Assertions.assertThat;
53 import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
54 import static org.sonar.db.component.ComponentTesting.newProjectDto;
55 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID;
56 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY;
57 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
58 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
60 public class ApplyTemplateActionTest extends BasePermissionWsTest<ApplyTemplateAction> {
63 public EsTester esTester = new EsTester(new IssueIndexDefinition(new MapSettings()), new ProjectMeasuresIndexDefinition(new MapSettings()));
65 private UserDto user1;
66 private UserDto user2;
67 private GroupDto group1;
68 private GroupDto group2;
69 private ComponentDto project;
70 private PermissionTemplateDto template1;
71 private PermissionTemplateDto template2;
73 private PermissionIndexerTester authorizationIndexerTester = new PermissionIndexerTester(esTester);
75 private PermissionIndexer permissionIndexer = new PermissionIndexer(db.getDbClient(), esTester.client());
78 protected ApplyTemplateAction buildWsAction() {
79 PermissionRepository repository = new PermissionRepository(db.getDbClient(), new MapSettings());
80 PermissionTemplateService permissionTemplateService = new PermissionTemplateService(db.getDbClient(), repository, permissionIndexer, userSession);
81 return new ApplyTemplateAction(db.getDbClient(), userSession, permissionTemplateService, newPermissionWsSupport());
86 user1 = db.users().insertUser("user-login-1");
87 user2 = db.users().insertUser("user-login-2");
88 OrganizationDto defaultOrg = db.getDefaultOrganization();
89 group1 = db.users().insertGroup(defaultOrg, "group-name-1");
90 group2 = db.users().insertGroup(defaultOrg, "group-name-2");
93 template1 = insertTemplate();
94 addUserToTemplate(user1, template1, UserRole.CODEVIEWER);
95 addUserToTemplate(user2, template1, UserRole.ISSUE_ADMIN);
96 addGroupToTemplate(group1, template1, UserRole.ADMIN);
97 addGroupToTemplate(group2, template1, UserRole.USER);
99 template2 = insertTemplate();
100 addUserToTemplate(user1, template2, UserRole.USER);
101 addUserToTemplate(user2, template2, UserRole.USER);
102 addGroupToTemplate(group1, template2, UserRole.USER);
103 addGroupToTemplate(group2, template2, UserRole.USER);
105 project = db.components().insertComponent(newProjectDto("project-uuid-1"));
106 db.users().insertProjectPermissionOnUser(user1, UserRole.ADMIN, project);
107 db.users().insertProjectPermissionOnUser(user2, UserRole.ADMIN, project);
108 db.users().insertProjectPermissionOnGroup(group1, UserRole.ADMIN, project);
109 db.users().insertProjectPermissionOnGroup(group2, UserRole.ADMIN, project);
113 public void apply_template_with_project_uuid() throws Exception {
114 loginAsAdminOnDefaultOrganization();
116 newRequest(template1.getUuid(), project.uuid(), null);
118 assertTemplate1AppliedToProject();
122 public void apply_template_with_project_uuid_by_template_name() throws Exception {
123 loginAsAdminOnDefaultOrganization();
126 .setParam(PARAM_TEMPLATE_NAME, template1.getName().toUpperCase())
127 .setParam(PARAM_PROJECT_ID, project.uuid())
130 assertTemplate1AppliedToProject();
134 public void apply_template_with_project_key() throws Exception {
135 loginAsAdminOnDefaultOrganization();
137 newRequest(template1.getUuid(), null, project.key());
139 assertTemplate1AppliedToProject();
143 public void fail_when_unknown_template() throws Exception {
144 loginAsAdminOnDefaultOrganization();
146 expectedException.expect(NotFoundException.class);
147 expectedException.expectMessage("Permission template with id 'unknown-template-uuid' is not found");
149 newRequest("unknown-template-uuid", project.uuid(), null);
153 public void fail_when_unknown_project_uuid() throws Exception {
154 loginAsAdminOnDefaultOrganization();
156 expectedException.expect(NotFoundException.class);
157 expectedException.expectMessage("Project id 'unknown-project-uuid' not found");
159 newRequest(template1.getUuid(), "unknown-project-uuid", null);
163 public void fail_when_unknown_project_key() throws Exception {
164 loginAsAdminOnDefaultOrganization();
166 expectedException.expect(NotFoundException.class);
167 expectedException.expectMessage("Project key 'unknown-project-key' not found");
169 newRequest(template1.getUuid(), null, "unknown-project-key");
173 public void fail_when_template_is_not_provided() throws Exception {
174 loginAsAdminOnDefaultOrganization();
176 expectedException.expect(BadRequestException.class);
178 newRequest(null, project.uuid(), null);
182 public void fail_when_project_uuid_and_key_not_provided() throws Exception {
183 loginAsAdminOnDefaultOrganization();
185 expectedException.expect(BadRequestException.class);
186 expectedException.expectMessage("Project id or project key can be provided, not both.");
188 newRequest(template1.getUuid(), null, null);
192 public void fail_when_not_admin_of_organization() throws Exception {
193 userSession.login().addOrganizationPermission("otherOrg", SYSTEM_ADMIN);
195 expectedException.expect(ForbiddenException.class);
196 userSession.login().setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
198 newRequest(template1.getUuid(), project.uuid(), null);
201 private void assertTemplate1AppliedToProject() {
202 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).containsExactly(group1.getName());
203 assertThat(selectProjectPermissionGroups(project, UserRole.USER)).containsExactly(group2.getName());
204 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
205 assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).containsExactly(user1.getId());
206 assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).containsExactly(user2.getId());
208 authorizationIndexerTester.verifyProjectExistsWithPermission(project.uuid(), singletonList(group2.getName()), Collections.emptyList());
211 private TestResponse newRequest(@Nullable String templateUuid, @Nullable String projectUuid, @Nullable String projectKey) throws Exception {
212 TestRequest request = newRequest();
213 if (templateUuid != null) {
214 request.setParam(PARAM_TEMPLATE_ID, templateUuid);
216 if (projectUuid != null) {
217 request.setParam(PARAM_PROJECT_ID, projectUuid);
219 if (projectKey != null) {
220 request.setParam(PARAM_PROJECT_KEY, projectKey);
223 return request.execute();
226 private void addUserToTemplate(UserDto user, PermissionTemplateDto permissionTemplate, String permission) {
227 db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), permissionTemplate.getId(), user.getId(), permission);
231 private void addGroupToTemplate(GroupDto group, PermissionTemplateDto permissionTemplate, String permission) {
232 db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), permissionTemplate.getId(), group.getId(), permission);
236 private List<String> selectProjectPermissionGroups(ComponentDto project, String permission) {
237 PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponentUuid(project.uuid()).build();
238 return db.getDbClient().groupPermissionDao().selectGroupNamesByQuery(db.getSession(), db.getDefaultOrganization().getUuid(), query);
241 private List<Long> selectProjectPermissionUsers(ComponentDto project, String permission) {
242 PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponentUuid(project.uuid()).build();
243 return db.getDbClient().userPermissionDao().selectUserIds(db.getSession(), db.getDefaultOrganization().getUuid(), query);