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.List;
23 import javax.annotation.Nullable;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.sonar.api.config.MapSettings;
27 import org.sonar.api.web.UserRole;
28 import org.sonar.core.permission.GlobalPermissions;
29 import org.sonar.db.component.ComponentDto;
30 import org.sonar.db.organization.OrganizationDto;
31 import org.sonar.db.permission.PermissionQuery;
32 import org.sonar.db.permission.PermissionRepository;
33 import org.sonar.db.permission.template.PermissionTemplateDto;
34 import org.sonar.db.user.GroupDto;
35 import org.sonar.db.user.UserDto;
36 import org.sonar.server.component.ComponentFinder;
37 import org.sonar.server.exceptions.BadRequestException;
38 import org.sonar.server.exceptions.ForbiddenException;
39 import org.sonar.server.exceptions.NotFoundException;
40 import org.sonar.server.issue.index.IssueAuthorizationIndexer;
41 import org.sonar.server.permission.PermissionService;
42 import org.sonar.server.permission.ws.BasePermissionWsTest;
43 import org.sonar.server.ws.WsTester;
45 import static org.assertj.core.api.Assertions.assertThat;
46 import static org.mockito.Mockito.mock;
47 import static org.mockito.Mockito.verify;
48 import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
49 import static org.sonar.db.component.ComponentTesting.newProjectDto;
50 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.CONTROLLER;
51 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID;
52 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY;
53 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
54 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
56 public class ApplyTemplateActionTest extends BasePermissionWsTest<ApplyTemplateAction> {
58 private static final String ACTION = "apply_template";
60 private UserDto user1;
61 private UserDto user2;
62 private GroupDto group1;
63 private GroupDto group2;
64 private ComponentDto project;
65 private PermissionTemplateDto template1;
66 private PermissionTemplateDto template2;
67 private IssueAuthorizationIndexer issueAuthorizationIndexer = mock(IssueAuthorizationIndexer.class);
70 protected ApplyTemplateAction buildWsAction() {
71 PermissionRepository repository = new PermissionRepository(db.getDbClient(), new MapSettings());
72 ComponentFinder componentFinder = new ComponentFinder(db.getDbClient());
73 PermissionService permissionService = new PermissionService(db.getDbClient(), repository, issueAuthorizationIndexer, userSession, componentFinder);
74 return new ApplyTemplateAction(db.getDbClient(), userSession, permissionService, newPermissionWsSupport());
79 user1 = db.users().insertUser("user-login-1");
80 user2 = db.users().insertUser("user-login-2");
81 OrganizationDto defaultOrg = defaultOrganizationProvider.getDto();
82 group1 = db.users().insertGroup(defaultOrg, "group-name-1");
83 group2 = db.users().insertGroup(defaultOrg, "group-name-2");
86 template1 = insertTemplate();
87 addUserToTemplate(user1, template1, UserRole.CODEVIEWER);
88 addUserToTemplate(user2, template1, UserRole.ISSUE_ADMIN);
89 addGroupToTemplate(group1, template1, UserRole.ADMIN);
90 addGroupToTemplate(group2, template1, UserRole.USER);
92 template2 = insertTemplate();
93 addUserToTemplate(user1, template2, UserRole.USER);
94 addUserToTemplate(user2, template2, UserRole.USER);
95 addGroupToTemplate(group1, template2, UserRole.USER);
96 addGroupToTemplate(group2, template2, UserRole.USER);
98 project = db.components().insertComponent(newProjectDto("project-uuid-1"));
99 db.users().insertProjectPermissionOnUser(user1, UserRole.ADMIN, project);
100 db.users().insertProjectPermissionOnUser(user2, UserRole.ADMIN, project);
101 db.users().insertProjectPermissionOnGroup(group1, UserRole.ADMIN, project);
102 db.users().insertProjectPermissionOnGroup(group2, UserRole.ADMIN, project);
106 public void apply_template_with_project_uuid() throws Exception {
107 loginAsAdminOnDefaultOrganization();
109 newRequest(template1.getUuid(), project.uuid(), null);
111 assertTemplate1AppliedToProject();
112 verify(issueAuthorizationIndexer).index();
116 public void apply_template_with_project_uuid_by_template_name() throws Exception {
117 loginAsAdminOnDefaultOrganization();
119 wsTester.newPostRequest(CONTROLLER, ACTION)
120 .setParam(PARAM_TEMPLATE_NAME, template1.getName().toUpperCase())
121 .setParam(PARAM_PROJECT_ID, project.uuid())
124 assertTemplate1AppliedToProject();
128 public void apply_template_with_project_key() throws Exception {
129 loginAsAdminOnDefaultOrganization();
131 newRequest(template1.getUuid(), null, project.key());
133 assertTemplate1AppliedToProject();
137 public void fail_when_unknown_template() throws Exception {
138 loginAsAdminOnDefaultOrganization();
140 expectedException.expect(NotFoundException.class);
141 expectedException.expectMessage("Permission template with id 'unknown-template-uuid' is not found");
143 newRequest("unknown-template-uuid", project.uuid(), null);
147 public void fail_when_unknown_project_uuid() throws Exception {
148 loginAsAdminOnDefaultOrganization();
150 expectedException.expect(NotFoundException.class);
151 expectedException.expectMessage("Project id 'unknown-project-uuid' not found");
153 newRequest(template1.getUuid(), "unknown-project-uuid", null);
157 public void fail_when_unknown_project_key() throws Exception {
158 loginAsAdminOnDefaultOrganization();
160 expectedException.expect(NotFoundException.class);
161 expectedException.expectMessage("Project key 'unknown-project-key' not found");
163 newRequest(template1.getUuid(), null, "unknown-project-key");
167 public void fail_when_template_is_not_provided() throws Exception {
168 loginAsAdminOnDefaultOrganization();
170 expectedException.expect(BadRequestException.class);
172 newRequest(null, project.uuid(), null);
176 public void fail_when_project_uuid_and_key_not_provided() throws Exception {
177 loginAsAdminOnDefaultOrganization();
179 expectedException.expect(BadRequestException.class);
180 expectedException.expectMessage("Project id or project key can be provided, not both.");
182 newRequest(template1.getUuid(), null, null);
186 public void fail_when_not_admin_of_organization() throws Exception {
187 userSession.login().addOrganizationPermission("otherOrg", SYSTEM_ADMIN);
189 expectedException.expect(ForbiddenException.class);
190 userSession.login().setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
192 newRequest(template1.getUuid(), project.uuid(), null);
195 private void assertTemplate1AppliedToProject() {
196 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).containsExactly(group1.getName());
197 assertThat(selectProjectPermissionGroups(project, UserRole.USER)).containsExactly(group2.getName());
198 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
199 assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).containsExactly(user1.getLogin());
200 assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).containsExactly(user2.getLogin());
203 private WsTester.Result newRequest(@Nullable String templateUuid, @Nullable String projectUuid, @Nullable String projectKey) throws Exception {
204 WsTester.TestRequest request = wsTester.newPostRequest(CONTROLLER, ACTION);
205 if (templateUuid != null) {
206 request.setParam(PARAM_TEMPLATE_ID, templateUuid);
208 if (projectUuid != null) {
209 request.setParam(PARAM_PROJECT_ID, projectUuid);
211 if (projectKey != null) {
212 request.setParam(PARAM_PROJECT_KEY, projectKey);
215 return request.execute();
218 private void addUserToTemplate(UserDto user, PermissionTemplateDto permissionTemplate, String permission) {
219 db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), permissionTemplate.getId(), user.getId(), permission);
223 private void addGroupToTemplate(GroupDto group, PermissionTemplateDto permissionTemplate, String permission) {
224 db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), permissionTemplate.getId(), group.getId(), permission);
228 private List<String> selectProjectPermissionGroups(ComponentDto project, String permission) {
229 PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponentUuid(project.uuid()).build();
230 return db.getDbClient().groupPermissionDao().selectGroupNamesByPermissionQuery(db.getSession(), query);
233 private List<String> selectProjectPermissionUsers(ComponentDto project, String permission) {
234 PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponentUuid(project.uuid()).build();
235 return db.getDbClient().userPermissionDao().selectLogins(db.getSession(), query);