]> source.dussan.org Git - sonarqube.git/blob
28015aec9c7b98767fdec1198bdc0cd5e0dc7f88
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.permission.ws.template;
21
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;
46
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;
57
58 public class ApplyTemplateActionIT extends BasePermissionWsIT<ApplyTemplateAction> {
59
60   @Rule
61   public DbTester dbTester = DbTester.create(true);
62
63   private UserDto user1;
64   private UserDto user2;
65   private GroupDto group1;
66   private GroupDto group2;
67   private ProjectDto project;
68   private PermissionTemplateDto template1;
69
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());
74
75   @Override
76   protected ApplyTemplateAction buildWsAction() {
77     return new ApplyTemplateAction(db.getDbClient(), userSession, permissionTemplateService, newPermissionWsSupport());
78   }
79
80   @Before
81   public void setUp() {
82     user1 = db.users().insertUser();
83     user2 = db.users().insertUser();
84     group1 = db.users().insertGroup();
85     group2 = db.users().insertGroup();
86
87     // template 1
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);
93     // template 2
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);
99
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);
105   }
106
107   @Test
108   public void apply_template_with_project_uuid() {
109     loginAsAdmin();
110
111     newRequest(template1.getUuid(), project.getUuid(), null);
112
113     assertTemplate1AppliedToProject();
114   }
115
116   @Test
117   public void apply_template_with_project_uuid_by_template_name() {
118     loginAsAdmin();
119
120     newRequest()
121       .setParam(PARAM_TEMPLATE_NAME, template1.getName().toUpperCase())
122       .setParam(PARAM_PROJECT_ID, project.getUuid())
123       .execute();
124
125     assertTemplate1AppliedToProject();
126   }
127
128   @Test
129   public void apply_template_with_project_key() {
130     loginAsAdmin();
131
132     newRequest(template1.getUuid(), null, project.getKey());
133
134     assertTemplate1AppliedToProject();
135   }
136
137   @Test
138   public void fail_when_unknown_template() {
139     loginAsAdmin();
140
141     assertThatThrownBy(() -> {
142       newRequest("unknown-template-uuid", project.getUuid(), null);
143     })
144       .isInstanceOf(NotFoundException.class)
145       .hasMessage("Permission template with id 'unknown-template-uuid' is not found");
146   }
147
148   @Test
149   public void fail_when_unknown_project_uuid() {
150     loginAsAdmin();
151
152     assertThatThrownBy(() -> {
153       newRequest(template1.getUuid(), "unknown-project-uuid", null);
154     })
155       .isInstanceOf(NotFoundException.class)
156       .hasMessage("Entity not found");
157   }
158
159   @Test
160   public void fail_when_unknown_project_key() {
161     loginAsAdmin();
162
163     assertThatThrownBy(() -> {
164       newRequest(template1.getUuid(), null, "unknown-project-key");
165     })
166       .isInstanceOf(NotFoundException.class)
167       .hasMessage("Entity not found");
168   }
169
170   @Test
171   public void fail_when_template_is_not_provided() {
172     loginAsAdmin();
173
174     assertThatThrownBy(() -> newRequest(null, project.getUuid(), null))
175       .isInstanceOf(BadRequestException.class);
176   }
177
178   @Test
179   public void fail_when_project_uuid_and_key_not_provided() {
180     loginAsAdmin();
181
182     assertThatThrownBy(() -> {
183       newRequest(template1.getUuid(), null, null);
184     })
185       .isInstanceOf(BadRequestException.class)
186       .hasMessage("Project id or project key can be provided, not both.");
187   }
188
189   @Test
190   public void fail_when_not_admin() {
191     userSession.logIn().addPermission(SCAN);
192
193     assertThatThrownBy(() -> newRequest(template1.getUuid(), project.getUuid(), null))
194       .isInstanceOf(ForbiddenException.class);
195   }
196
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());
203   }
204
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);
209     }
210     if (projectUuid != null) {
211       request.setParam(PARAM_PROJECT_ID, projectUuid);
212     }
213     if (projectKey != null) {
214       request.setParam(PARAM_PROJECT_KEY, projectKey);
215     }
216     request.execute();
217   }
218
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());
222     db.commit();
223   }
224
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());
228     db.commit();
229   }
230
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);
234   }
235
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);
239   }
240 }