3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info 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.Rule;
26 import org.junit.Test;
27 import org.sonar.api.web.UserRole;
28 import org.sonar.core.util.SequenceUuidFactory;
29 import org.sonar.db.DbTester;
30 import org.sonar.db.component.ComponentDto;
31 import org.sonar.db.component.ResourceTypesRule;
32 import org.sonar.db.permission.PermissionQuery;
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.es.TestProjectIndexers;
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.permission.DefaultTemplatesResolver;
41 import org.sonar.server.permission.DefaultTemplatesResolverImpl;
42 import org.sonar.server.permission.PermissionTemplateService;
43 import org.sonar.server.permission.ws.BasePermissionWsTest;
44 import org.sonar.server.ws.TestRequest;
46 import static org.assertj.core.api.Assertions.assertThat;
47 import static org.assertj.core.api.Assertions.assertThatThrownBy;
48 import static org.sonar.api.resources.Qualifiers.APP;
49 import static org.sonar.api.resources.Qualifiers.PROJECT;
50 import static org.sonar.api.resources.Qualifiers.VIEW;
51 import static org.sonar.api.web.UserRole.CODEVIEWER;
52 import static org.sonar.db.permission.GlobalPermission.SCAN;
53 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID;
54 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY;
55 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
56 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
58 public class ApplyTemplateActionTest extends BasePermissionWsTest<ApplyTemplateAction> {
61 public DbTester dbTester = DbTester.create();
63 private UserDto user1;
64 private UserDto user2;
65 private GroupDto group1;
66 private GroupDto group2;
67 private ComponentDto project;
68 private PermissionTemplateDto template1;
69 private PermissionTemplateDto template2;
71 private ResourceTypesRule resourceTypesRule = new ResourceTypesRule().setRootQualifiers(PROJECT, VIEW, APP);
72 private DefaultTemplatesResolver defaultTemplatesResolver = new DefaultTemplatesResolverImpl(dbTester.getDbClient(), resourceTypesRule);
73 private PermissionTemplateService permissionTemplateService = new PermissionTemplateService(db.getDbClient(),
74 new TestProjectIndexers(), userSession, defaultTemplatesResolver, new SequenceUuidFactory());
77 protected ApplyTemplateAction buildWsAction() {
78 return new ApplyTemplateAction(db.getDbClient(), userSession, permissionTemplateService, newPermissionWsSupport());
83 user1 = db.users().insertUser();
84 user2 = db.users().insertUser();
85 group1 = db.users().insertGroup();
86 group2 = db.users().insertGroup();
89 template1 = db.permissionTemplates().insertTemplate();
90 addUserToTemplate(user1, template1, UserRole.CODEVIEWER);
91 addUserToTemplate(user2, template1, UserRole.ISSUE_ADMIN);
92 addGroupToTemplate(group1, template1, UserRole.ADMIN);
93 addGroupToTemplate(group2, template1, UserRole.USER);
95 template2 = db.permissionTemplates().insertTemplate();
96 addUserToTemplate(user1, template2, UserRole.USER);
97 addUserToTemplate(user2, template2, UserRole.USER);
98 addGroupToTemplate(group1, template2, UserRole.USER);
99 addGroupToTemplate(group2, template2, UserRole.USER);
101 project = db.components().insertPrivateProject();
102 db.users().insertProjectPermissionOnUser(user1, UserRole.ADMIN, project);
103 db.users().insertProjectPermissionOnUser(user2, UserRole.ADMIN, project);
104 db.users().insertProjectPermissionOnGroup(group1, UserRole.ADMIN, project);
105 db.users().insertProjectPermissionOnGroup(group2, UserRole.ADMIN, project);
109 public void apply_template_with_project_uuid() {
112 newRequest(template1.getUuid(), project.uuid(), null);
114 assertTemplate1AppliedToProject();
118 public void apply_template_with_project_uuid_by_template_name() {
122 .setParam(PARAM_TEMPLATE_NAME, template1.getName().toUpperCase())
123 .setParam(PARAM_PROJECT_ID, project.uuid())
126 assertTemplate1AppliedToProject();
130 public void apply_template_with_project_key() {
133 newRequest(template1.getUuid(), null, project.getDbKey());
135 assertTemplate1AppliedToProject();
139 public void fail_when_unknown_template() {
142 assertThatThrownBy(() -> {
143 newRequest("unknown-template-uuid", project.uuid(), null);
145 .isInstanceOf(NotFoundException.class)
146 .hasMessage("Permission template with id 'unknown-template-uuid' is not found");
150 public void fail_when_unknown_project_uuid() {
153 assertThatThrownBy(() -> {
154 newRequest(template1.getUuid(), "unknown-project-uuid", null);
156 .isInstanceOf(NotFoundException.class)
157 .hasMessage("Project id 'unknown-project-uuid' not found");
161 public void fail_when_unknown_project_key() {
164 assertThatThrownBy(() -> {
165 newRequest(template1.getUuid(), null, "unknown-project-key");
167 .isInstanceOf(NotFoundException.class)
168 .hasMessage("Project key 'unknown-project-key' not found");
172 public void fail_when_template_is_not_provided() {
175 assertThatThrownBy(() -> {
176 newRequest(null, project.uuid(), null);
178 .isInstanceOf(BadRequestException.class);
182 public void fail_when_project_uuid_and_key_not_provided() {
185 assertThatThrownBy(() -> {
186 newRequest(template1.getUuid(), null, null);
188 .isInstanceOf(BadRequestException.class)
189 .hasMessage("Project id or project key can be provided, not both.");
193 public void fail_when_not_admin() {
194 userSession.logIn().addPermission(SCAN);
196 assertThatThrownBy(() -> {
197 newRequest(template1.getUuid(), project.uuid(), null);
199 .isInstanceOf(ForbiddenException.class);
202 private void assertTemplate1AppliedToProject() {
203 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).containsExactly(group1.getName());
204 assertThat(selectProjectPermissionGroups(project, UserRole.USER)).containsExactly(group2.getName());
205 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
206 assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).containsExactly(user1.getUuid());
207 assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).containsExactly(user2.getUuid());
210 private void newRequest(@Nullable String templateUuid, @Nullable String projectUuid, @Nullable String projectKey) {
211 TestRequest request = newRequest();
212 if (templateUuid != null) {
213 request.setParam(PARAM_TEMPLATE_ID, templateUuid);
215 if (projectUuid != null) {
216 request.setParam(PARAM_PROJECT_ID, projectUuid);
218 if (projectKey != null) {
219 request.setParam(PARAM_PROJECT_KEY, projectKey);
224 private void addUserToTemplate(UserDto user, PermissionTemplateDto permissionTemplate, String permission) {
225 db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), permissionTemplate.getUuid(), user.getUuid(),
226 permission, permissionTemplate.getName(), user.getLogin());
230 private void addGroupToTemplate(GroupDto group, PermissionTemplateDto permissionTemplate, String permission) {
231 db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), permissionTemplate.getUuid(), group.getUuid(),
232 permission, permissionTemplate.getName(), group.getName());
236 private List<String> selectProjectPermissionGroups(ComponentDto project, String permission) {
237 PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponent(project).build();
238 return db.getDbClient().groupPermissionDao().selectGroupNamesByQuery(db.getSession(), query);
241 private List<String> selectProjectPermissionUsers(ComponentDto project, String permission) {
242 PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponent(project).build();
243 return db.getDbClient().userPermissionDao().selectUserUuidsByQuery(db.getSession(), query);