3 * Copyright (C) 2009-2023 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.ResourceTypesRule;
31 import org.sonar.db.entity.EntityDto;
32 import org.sonar.db.permission.PermissionQuery;
33 import org.sonar.db.permission.template.PermissionTemplateDto;
34 import org.sonar.db.project.ProjectDto;
35 import org.sonar.db.user.GroupDto;
36 import org.sonar.db.user.UserDto;
37 import org.sonar.server.es.TestProjectIndexers;
38 import org.sonar.server.exceptions.BadRequestException;
39 import org.sonar.server.exceptions.ForbiddenException;
40 import org.sonar.server.exceptions.NotFoundException;
41 import org.sonar.server.permission.DefaultTemplatesResolver;
42 import org.sonar.server.permission.DefaultTemplatesResolverImpl;
43 import org.sonar.server.permission.PermissionTemplateService;
44 import org.sonar.server.permission.ws.BasePermissionWsIT;
45 import org.sonar.server.ws.TestRequest;
47 import static org.assertj.core.api.Assertions.assertThat;
48 import static org.assertj.core.api.Assertions.assertThatThrownBy;
49 import static org.sonar.api.resources.Qualifiers.APP;
50 import static org.sonar.api.resources.Qualifiers.PROJECT;
51 import static org.sonar.api.resources.Qualifiers.VIEW;
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 ApplyTemplateActionIT extends BasePermissionWsIT<ApplyTemplateAction> {
61 public DbTester dbTester = DbTester.create(true);
63 private UserDto user1;
64 private UserDto user2;
65 private GroupDto group1;
66 private GroupDto group2;
67 private ProjectDto project;
68 private PermissionTemplateDto template1;
70 private final ResourceTypesRule resourceTypesRule = new ResourceTypesRule().setRootQualifiers(PROJECT, VIEW, APP);
71 private final DefaultTemplatesResolver defaultTemplatesResolver = new DefaultTemplatesResolverImpl(dbTester.getDbClient(), resourceTypesRule);
72 private final PermissionTemplateService permissionTemplateService = new PermissionTemplateService(db.getDbClient(),
73 new TestProjectIndexers(), userSession, defaultTemplatesResolver, new SequenceUuidFactory());
76 protected ApplyTemplateAction buildWsAction() {
77 return new ApplyTemplateAction(db.getDbClient(), userSession, permissionTemplateService, newPermissionWsSupport());
82 user1 = db.users().insertUser();
83 user2 = db.users().insertUser();
84 group1 = db.users().insertGroup();
85 group2 = db.users().insertGroup();
88 template1 = db.permissionTemplates().insertTemplate();
89 addUserToTemplate(user1, template1, UserRole.CODEVIEWER);
90 addUserToTemplate(user2, template1, UserRole.ISSUE_ADMIN);
91 addGroupToTemplate(group1, template1, UserRole.ADMIN);
92 addGroupToTemplate(group2, template1, UserRole.USER);
94 PermissionTemplateDto template2 = db.permissionTemplates().insertTemplate();
95 addUserToTemplate(user1, template2, UserRole.USER);
96 addUserToTemplate(user2, template2, UserRole.USER);
97 addGroupToTemplate(group1, template2, UserRole.USER);
98 addGroupToTemplate(group2, template2, UserRole.USER);
100 project = db.components().insertPrivateProject().getProjectDto();
101 db.users().insertProjectPermissionOnUser(user1, UserRole.ADMIN, project);
102 db.users().insertProjectPermissionOnUser(user2, UserRole.ADMIN, project);
103 db.users().insertEntityPermissionOnGroup(group1, UserRole.ADMIN, project);
104 db.users().insertEntityPermissionOnGroup(group2, UserRole.ADMIN, project);
108 public void apply_template_with_project_uuid() {
111 newRequest(template1.getUuid(), project.getUuid(), null);
113 assertTemplate1AppliedToProject();
117 public void apply_template_with_project_uuid_by_template_name() {
121 .setParam(PARAM_TEMPLATE_NAME, template1.getName().toUpperCase())
122 .setParam(PARAM_PROJECT_ID, project.getUuid())
125 assertTemplate1AppliedToProject();
129 public void apply_template_with_project_key() {
132 newRequest(template1.getUuid(), null, project.getKey());
134 assertTemplate1AppliedToProject();
138 public void fail_when_unknown_template() {
141 assertThatThrownBy(() -> {
142 newRequest("unknown-template-uuid", project.getUuid(), null);
144 .isInstanceOf(NotFoundException.class)
145 .hasMessage("Permission template with id 'unknown-template-uuid' is not found");
149 public void fail_when_unknown_project_uuid() {
152 assertThatThrownBy(() -> {
153 newRequest(template1.getUuid(), "unknown-project-uuid", null);
155 .isInstanceOf(NotFoundException.class)
156 .hasMessage("Entity not found");
160 public void fail_when_unknown_project_key() {
163 assertThatThrownBy(() -> {
164 newRequest(template1.getUuid(), null, "unknown-project-key");
166 .isInstanceOf(NotFoundException.class)
167 .hasMessage("Entity not found");
171 public void fail_when_template_is_not_provided() {
174 assertThatThrownBy(() -> newRequest(null, project.getUuid(), null))
175 .isInstanceOf(BadRequestException.class);
179 public void fail_when_project_uuid_and_key_not_provided() {
182 assertThatThrownBy(() -> {
183 newRequest(template1.getUuid(), null, null);
185 .isInstanceOf(BadRequestException.class)
186 .hasMessage("Project id or project key can be provided, not both.");
190 public void fail_when_not_admin() {
191 userSession.logIn().addPermission(SCAN);
193 assertThatThrownBy(() -> newRequest(template1.getUuid(), project.getUuid(), null))
194 .isInstanceOf(ForbiddenException.class);
197 private void assertTemplate1AppliedToProject() {
198 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).containsExactly(group1.getName());
199 assertThat(selectProjectPermissionGroups(project, UserRole.USER)).containsExactly(group2.getName());
200 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
201 assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).containsExactly(user1.getUuid());
202 assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).containsExactly(user2.getUuid());
205 private void newRequest(@Nullable String templateUuid, @Nullable String projectUuid, @Nullable String projectKey) {
206 TestRequest request = newRequest();
207 if (templateUuid != null) {
208 request.setParam(PARAM_TEMPLATE_ID, templateUuid);
210 if (projectUuid != null) {
211 request.setParam(PARAM_PROJECT_ID, projectUuid);
213 if (projectKey != null) {
214 request.setParam(PARAM_PROJECT_KEY, projectKey);
219 private void addUserToTemplate(UserDto user, PermissionTemplateDto permissionTemplate, String permission) {
220 db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), permissionTemplate.getUuid(), user.getUuid(),
221 permission, permissionTemplate.getName(), user.getLogin());
225 private void addGroupToTemplate(GroupDto group, PermissionTemplateDto permissionTemplate, String permission) {
226 db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), permissionTemplate.getUuid(), group.getUuid(),
227 permission, permissionTemplate.getName(), group.getName());
231 private List<String> selectProjectPermissionGroups(EntityDto entity, String permission) {
232 PermissionQuery query = PermissionQuery.builder().setPermission(permission).setEntity(entity).build();
233 return db.getDbClient().groupPermissionDao().selectGroupNamesByQuery(db.getSession(), query);
236 private List<String> selectProjectPermissionUsers(EntityDto entity, String permission) {
237 PermissionQuery query = PermissionQuery.builder().setPermission(permission).setEntity(entity).build();
238 return db.getDbClient().userPermissionDao().selectUserUuidsByQuery(db.getSession(), query);