3 * Copyright (C) 2009-2021 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;
22 import java.util.List;
23 import javax.annotation.Nullable;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.sonar.api.web.UserRole;
28 import org.sonar.core.util.SequenceUuidFactory;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.db.component.ResourceTypesRule;
33 import org.sonar.db.permission.template.PermissionTemplateDbTester;
34 import org.sonar.db.permission.template.PermissionTemplateDto;
35 import org.sonar.db.user.GroupDto;
36 import org.sonar.db.user.UserDto;
37 import org.sonar.server.es.ProjectIndexers;
38 import org.sonar.server.es.TestProjectIndexers;
39 import org.sonar.server.tester.UserSessionRule;
41 import static java.util.Collections.singletonList;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.sonar.api.resources.Qualifiers.APP;
44 import static org.sonar.api.resources.Qualifiers.PROJECT;
45 import static org.sonar.api.resources.Qualifiers.VIEW;
46 import static org.sonar.db.permission.GlobalPermission.ADMINISTER;
47 import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
48 import static org.sonar.db.permission.GlobalPermission.SCAN;
50 public class PermissionTemplateServiceTest {
53 public ExpectedException throwable = ExpectedException.none();
55 public DbTester dbTester = DbTester.create();
57 private final ResourceTypesRule resourceTypesRule = new ResourceTypesRule().setRootQualifiers(PROJECT, VIEW, APP);
58 private final DefaultTemplatesResolver defaultTemplatesResolver = new DefaultTemplatesResolverImpl(dbTester.getDbClient(), resourceTypesRule);
59 private final PermissionService permissionService = new PermissionServiceImpl(resourceTypesRule);
60 private final UserSessionRule userSession = UserSessionRule.standalone();
61 private final PermissionTemplateDbTester templateDb = dbTester.permissionTemplates();
62 private final DbSession session = dbTester.getSession();
63 private final ProjectIndexers projectIndexers = new TestProjectIndexers();
64 private final PermissionTemplateService underTest = new PermissionTemplateService(dbTester.getDbClient(), projectIndexers, userSession, defaultTemplatesResolver,
65 new SequenceUuidFactory());
68 public void apply_does_not_insert_permission_to_group_AnyOne_when_applying_template_on_private_project() {
69 ComponentDto privateProject = dbTester.components().insertPrivateProject();
70 PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate();
71 dbTester.permissionTemplates().addAnyoneToTemplate(permissionTemplate, "p1");
73 underTest.applyAndCommit(session, permissionTemplate, singletonList(privateProject));
75 assertThat(selectProjectPermissionsOfGroup(null, privateProject)).isEmpty();
79 public void apply_default_does_not_insert_permission_to_group_AnyOne_when_applying_template_on_private_project() {
80 ComponentDto privateProject = dbTester.components().insertPrivateProject();
81 UserDto creator = dbTester.users().insertUser();
82 PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate();
83 dbTester.permissionTemplates().addAnyoneToTemplate(permissionTemplate, "p1");
84 dbTester.permissionTemplates().setDefaultTemplates(permissionTemplate, null, null);
86 underTest.applyDefaultToNewComponent(session, privateProject, creator.getUuid());
88 assertThat(selectProjectPermissionsOfGroup(null, privateProject)).isEmpty();
92 public void apply_inserts_permissions_to_group_AnyOne_but_USER_and_CODEVIEWER_when_applying_template_on_public_project() {
93 ComponentDto publicProject = dbTester.components().insertPublicProject();
94 PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate();
95 permissionService.getAllProjectPermissions()
96 .forEach(perm -> dbTester.permissionTemplates().addAnyoneToTemplate(permissionTemplate, perm));
97 dbTester.permissionTemplates().addAnyoneToTemplate(permissionTemplate, "p1");
99 underTest.applyAndCommit(session, permissionTemplate, singletonList(publicProject));
101 assertThat(selectProjectPermissionsOfGroup(null, publicProject))
102 .containsOnly("p1", UserRole.ADMIN, UserRole.ISSUE_ADMIN, UserRole.SECURITYHOTSPOT_ADMIN, SCAN.getKey());
106 public void applyDefault_inserts_permissions_to_group_AnyOne_but_USER_and_CODEVIEWER_when_applying_template_on_public_project() {
107 ComponentDto publicProject = dbTester.components().insertPublicProject();
108 PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate();
109 permissionService.getAllProjectPermissions()
110 .forEach(perm -> dbTester.permissionTemplates().addAnyoneToTemplate(permissionTemplate, perm));
111 dbTester.permissionTemplates().addAnyoneToTemplate(permissionTemplate, "p1");
112 dbTester.permissionTemplates().setDefaultTemplates(permissionTemplate, null, null);
114 underTest.applyDefaultToNewComponent(session, publicProject, null);
116 assertThat(selectProjectPermissionsOfGroup(null, publicProject))
117 .containsOnly("p1", UserRole.ADMIN, UserRole.ISSUE_ADMIN, UserRole.SECURITYHOTSPOT_ADMIN, SCAN.getKey());
121 public void apply_inserts_any_permissions_to_group_when_applying_template_on_private_project() {
122 ComponentDto privateProject = dbTester.components().insertPrivateProject();
123 GroupDto group = dbTester.users().insertGroup();
124 PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate();
125 permissionService.getAllProjectPermissions()
126 .forEach(perm -> dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, perm));
127 dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, "p1");
129 underTest.applyAndCommit(session, permissionTemplate, singletonList(privateProject));
131 assertThat(selectProjectPermissionsOfGroup(group, privateProject))
132 .containsOnly("p1", UserRole.CODEVIEWER, UserRole.USER, UserRole.ADMIN, UserRole.ISSUE_ADMIN, UserRole.SECURITYHOTSPOT_ADMIN, SCAN.getKey());
136 public void applyDefault_inserts_any_permissions_to_group_when_applying_template_on_private_project() {
137 GroupDto group = dbTester.users().insertGroup();
138 ComponentDto privateProject = dbTester.components().insertPrivateProject();
139 PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate();
140 permissionService.getAllProjectPermissions()
141 .forEach(perm -> dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, perm));
142 dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, "p1");
143 dbTester.permissionTemplates().setDefaultTemplates(permissionTemplate, null, null);
145 underTest.applyDefaultToNewComponent(session, privateProject, null);
147 assertThat(selectProjectPermissionsOfGroup(group, privateProject))
148 .containsOnly("p1", UserRole.CODEVIEWER, UserRole.USER, UserRole.ADMIN, UserRole.ISSUE_ADMIN, UserRole.SECURITYHOTSPOT_ADMIN, SCAN.getKey());
152 public void apply_inserts_permissions_to_group_but_USER_and_CODEVIEWER_when_applying_template_on_public_project() {
153 PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate();
154 ComponentDto publicProject = dbTester.components().insertPublicProject();
155 GroupDto group = dbTester.users().insertGroup();
156 permissionService.getAllProjectPermissions()
157 .forEach(perm -> dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, perm));
158 dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, "p1");
160 underTest.applyAndCommit(session, permissionTemplate, singletonList(publicProject));
162 assertThat(selectProjectPermissionsOfGroup(group, publicProject))
163 .containsOnly("p1", UserRole.ADMIN, UserRole.ISSUE_ADMIN, UserRole.SECURITYHOTSPOT_ADMIN, SCAN.getKey());
167 public void applyDefault_inserts_permissions_to_group_but_USER_and_CODEVIEWER_when_applying_template_on_public_project() {
168 PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate();
169 ComponentDto publicProject = dbTester.components().insertPublicProject();
170 GroupDto group = dbTester.users().insertGroup();
171 permissionService.getAllProjectPermissions()
172 .forEach(perm -> dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, perm));
173 dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, "p1");
174 dbTester.permissionTemplates().setDefaultTemplates(permissionTemplate, null, null);
176 underTest.applyDefaultToNewComponent(session, publicProject, null);
178 assertThat(selectProjectPermissionsOfGroup(group, publicProject))
179 .containsOnly("p1", UserRole.ADMIN, UserRole.ISSUE_ADMIN, UserRole.SECURITYHOTSPOT_ADMIN, SCAN.getKey());
183 public void apply_inserts_permissions_to_user_but_USER_and_CODEVIEWER_when_applying_template_on_public_project() {
184 PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate();
185 ComponentDto publicProject = dbTester.components().insertPublicProject();
186 UserDto user = dbTester.users().insertUser();
187 permissionService.getAllProjectPermissions()
188 .forEach(perm -> dbTester.permissionTemplates().addUserToTemplate(permissionTemplate, user, perm));
189 dbTester.permissionTemplates().addUserToTemplate(permissionTemplate, user, "p1");
191 underTest.applyAndCommit(session, permissionTemplate, singletonList(publicProject));
193 assertThat(selectProjectPermissionsOfUser(user, publicProject))
194 .containsOnly("p1", UserRole.ADMIN, UserRole.ISSUE_ADMIN, UserRole.SECURITYHOTSPOT_ADMIN, SCAN.getKey());
198 public void applyDefault_inserts_permissions_to_user_but_USER_and_CODEVIEWER_when_applying_template_on_public_project() {
199 PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate();
200 ComponentDto publicProject = dbTester.components().insertPublicProject();
201 UserDto user = dbTester.users().insertUser();
202 permissionService.getAllProjectPermissions()
203 .forEach(perm -> dbTester.permissionTemplates().addUserToTemplate(permissionTemplate, user, perm));
204 dbTester.permissionTemplates().addUserToTemplate(permissionTemplate, user, "p1");
205 dbTester.permissionTemplates().setDefaultTemplates(permissionTemplate, null, null);
207 underTest.applyDefaultToNewComponent(session, publicProject, null);
209 assertThat(selectProjectPermissionsOfUser(user, publicProject))
210 .containsOnly("p1", UserRole.ADMIN, UserRole.ISSUE_ADMIN, UserRole.SECURITYHOTSPOT_ADMIN, SCAN.getKey());
214 public void apply_inserts_any_permissions_to_user_when_applying_template_on_private_project() {
215 PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate();
216 ComponentDto privateProject = dbTester.components().insertPrivateProject();
217 UserDto user = dbTester.users().insertUser();
218 permissionService.getAllProjectPermissions()
219 .forEach(perm -> dbTester.permissionTemplates().addUserToTemplate(permissionTemplate, user, perm));
220 dbTester.permissionTemplates().addUserToTemplate(permissionTemplate, user, "p1");
222 underTest.applyAndCommit(session, permissionTemplate, singletonList(privateProject));
224 assertThat(selectProjectPermissionsOfUser(user, privateProject))
225 .containsOnly("p1", UserRole.CODEVIEWER, UserRole.USER, UserRole.ADMIN, UserRole.ISSUE_ADMIN, UserRole.SECURITYHOTSPOT_ADMIN, SCAN.getKey());
229 public void applyDefault_inserts_any_permissions_to_user_when_applying_template_on_private_project() {
230 PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate();
231 ComponentDto privateProject = dbTester.components().insertPrivateProject();
232 UserDto user = dbTester.users().insertUser();
233 permissionService.getAllProjectPermissions()
234 .forEach(perm -> dbTester.permissionTemplates().addUserToTemplate(permissionTemplate, user, perm));
235 dbTester.permissionTemplates().addUserToTemplate(permissionTemplate, user, "p1");
236 dbTester.permissionTemplates().setDefaultTemplates(permissionTemplate, null, null);
238 underTest.applyDefaultToNewComponent(session, privateProject, null);
240 assertThat(selectProjectPermissionsOfUser(user, privateProject))
241 .containsOnly("p1", UserRole.CODEVIEWER, UserRole.USER, UserRole.ADMIN, UserRole.ISSUE_ADMIN, UserRole.SECURITYHOTSPOT_ADMIN, SCAN.getKey());
245 public void applyDefault_inserts_permissions_to_ProjectCreator_but_USER_and_CODEVIEWER_when_applying_template_on_public_project() {
246 PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate();
247 ComponentDto publicProject = dbTester.components().insertPublicProject();
248 UserDto user = dbTester.users().insertUser();
249 permissionService.getAllProjectPermissions()
250 .forEach(perm -> dbTester.permissionTemplates().addProjectCreatorToTemplate(permissionTemplate, perm));
251 dbTester.permissionTemplates().addProjectCreatorToTemplate(permissionTemplate, "p1");
252 dbTester.permissionTemplates().setDefaultTemplates(permissionTemplate, null, null);
254 underTest.applyDefaultToNewComponent(session, publicProject, user.getUuid());
256 assertThat(selectProjectPermissionsOfUser(user, publicProject))
257 .containsOnly("p1", UserRole.ADMIN, UserRole.ISSUE_ADMIN, UserRole.SECURITYHOTSPOT_ADMIN, SCAN.getKey());
261 public void applyDefault_inserts_any_permissions_to_ProjectCreator_when_applying_template_on_private_project() {
262 PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate();
263 ComponentDto privateProject = dbTester.components().insertPrivateProject();
264 UserDto user = dbTester.users().insertUser();
265 permissionService.getAllProjectPermissions()
266 .forEach(perm -> dbTester.permissionTemplates().addProjectCreatorToTemplate(permissionTemplate, perm));
267 dbTester.permissionTemplates().addProjectCreatorToTemplate(permissionTemplate, "p1");
268 dbTester.permissionTemplates().setDefaultTemplates(permissionTemplate, null, null);
270 underTest.applyDefaultToNewComponent(session, privateProject, user.getUuid());
272 assertThat(selectProjectPermissionsOfUser(user, privateProject))
273 .containsOnly("p1", UserRole.CODEVIEWER, UserRole.USER, UserRole.ADMIN, UserRole.ISSUE_ADMIN, UserRole.SECURITYHOTSPOT_ADMIN, SCAN.getKey());
277 public void apply_template_on_view() {
278 ComponentDto portfolio = dbTester.components().insertPrivatePortfolio();
279 PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate();
280 GroupDto group = dbTester.users().insertGroup();
281 dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, ADMINISTER.getKey());
282 dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, PROVISION_PROJECTS.getKey());
283 dbTester.permissionTemplates().setDefaultTemplates(permissionTemplate, null, null);
285 underTest.applyDefaultToNewComponent(session, portfolio, null);
287 assertThat(selectProjectPermissionsOfGroup(group, portfolio))
288 .containsOnly(ADMINISTER.getKey(), PROVISION_PROJECTS.getKey());
292 public void apply_default_template_on_application() {
293 ComponentDto view = dbTester.components().insertPublicApplication();
294 PermissionTemplateDto projectPermissionTemplate = dbTester.permissionTemplates().insertTemplate();
295 PermissionTemplateDto appPermissionTemplate = dbTester.permissionTemplates().insertTemplate();
296 GroupDto group = dbTester.users().insertGroup();
297 dbTester.permissionTemplates().addGroupToTemplate(appPermissionTemplate, group, ADMINISTER.getKey());
298 dbTester.permissionTemplates().addGroupToTemplate(appPermissionTemplate, group, PROVISION_PROJECTS.getKey());
299 dbTester.permissionTemplates().setDefaultTemplates(projectPermissionTemplate, appPermissionTemplate, null);
301 underTest.applyDefaultToNewComponent(session, view, null);
303 assertThat(selectProjectPermissionsOfGroup(group, view))
304 .containsOnly(ADMINISTER.getKey(), PROVISION_PROJECTS.getKey());
308 public void apply_default_template_on_portfolio() {
309 ComponentDto view = dbTester.components().insertPublicPortfolio();
310 PermissionTemplateDto projectPermissionTemplate = dbTester.permissionTemplates().insertTemplate();
311 PermissionTemplateDto portPermissionTemplate = dbTester.permissionTemplates().insertTemplate();
312 GroupDto group = dbTester.users().insertGroup();
313 dbTester.permissionTemplates().addGroupToTemplate(portPermissionTemplate, group, ADMINISTER.getKey());
314 dbTester.permissionTemplates().addGroupToTemplate(portPermissionTemplate, group, PROVISION_PROJECTS.getKey());
315 dbTester.permissionTemplates().setDefaultTemplates(projectPermissionTemplate, null, portPermissionTemplate);
317 underTest.applyDefaultToNewComponent(session, view, null);
319 assertThat(selectProjectPermissionsOfGroup(group, view))
320 .containsOnly(ADMINISTER.getKey(), PROVISION_PROJECTS.getKey());
324 public void apply_project_default_template_on_view_when_no_view_default_template() {
325 ComponentDto view = dbTester.components().insertPrivatePortfolio();
326 PermissionTemplateDto projectPermissionTemplate = dbTester.permissionTemplates().insertTemplate();
327 GroupDto group = dbTester.users().insertGroup();
328 dbTester.permissionTemplates().addGroupToTemplate(projectPermissionTemplate, group, PROVISION_PROJECTS.getKey());
329 dbTester.permissionTemplates().setDefaultTemplates(projectPermissionTemplate, null, null);
331 underTest.applyDefaultToNewComponent(session, view, null);
333 assertThat(selectProjectPermissionsOfGroup(group, view)).containsOnly(PROVISION_PROJECTS.getKey());
337 public void apply_template_on_applications() {
338 ComponentDto application = dbTester.components().insertPublicApplication();
339 PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate();
340 GroupDto group = dbTester.users().insertGroup();
341 dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, ADMINISTER.getKey());
342 dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, PROVISION_PROJECTS.getKey());
343 dbTester.permissionTemplates().setDefaultTemplates(permissionTemplate, null, null);
345 underTest.applyDefaultToNewComponent(session, application, null);
347 assertThat(selectProjectPermissionsOfGroup(group, application))
348 .containsOnly(ADMINISTER.getKey(), PROVISION_PROJECTS.getKey());
352 public void apply_default_view_template_on_application() {
353 ComponentDto application = dbTester.components().insertPublicApplication();
354 PermissionTemplateDto projectPermissionTemplate = dbTester.permissionTemplates().insertTemplate();
355 PermissionTemplateDto appPermissionTemplate = dbTester.permissionTemplates().insertTemplate();
356 PermissionTemplateDto portPermissionTemplate = dbTester.permissionTemplates().insertTemplate();
357 GroupDto group = dbTester.users().insertGroup();
358 dbTester.permissionTemplates().addGroupToTemplate(appPermissionTemplate, group, ADMINISTER.getKey());
359 dbTester.permissionTemplates().addGroupToTemplate(appPermissionTemplate, group, PROVISION_PROJECTS.getKey());
360 dbTester.permissionTemplates().setDefaultTemplates(projectPermissionTemplate, appPermissionTemplate, portPermissionTemplate);
362 underTest.applyDefaultToNewComponent(session, application, null);
364 assertThat(selectProjectPermissionsOfGroup(group, application))
365 .containsOnly(ADMINISTER.getKey(), PROVISION_PROJECTS.getKey());
369 public void apply_project_default_template_on_application_when_no_application_default_template() {
370 ComponentDto application = dbTester.components().insertPublicApplication();
371 PermissionTemplateDto projectPermissionTemplate = dbTester.permissionTemplates().insertTemplate();
372 GroupDto group = dbTester.users().insertGroup();
373 dbTester.permissionTemplates().addGroupToTemplate(projectPermissionTemplate, group, PROVISION_PROJECTS.getKey());
374 dbTester.permissionTemplates().setDefaultTemplates(projectPermissionTemplate, null, null);
376 underTest.applyDefaultToNewComponent(session, application, null);
378 assertThat(selectProjectPermissionsOfGroup(group, application)).containsOnly(PROVISION_PROJECTS.getKey());
382 public void apply_permission_template() {
383 UserDto user = dbTester.users().insertUser();
384 ComponentDto project = dbTester.components().insertPrivateProject();
385 GroupDto adminGroup = dbTester.users().insertGroup();
386 GroupDto userGroup = dbTester.users().insertGroup();
387 dbTester.users().insertPermissionOnGroup(adminGroup, "admin");
388 dbTester.users().insertPermissionOnGroup(userGroup, "user");
389 dbTester.users().insertPermissionOnUser(user, "admin");
390 PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate();
391 dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, adminGroup, "admin");
392 dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, adminGroup, "issueadmin");
393 dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, userGroup, "user");
394 dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, userGroup, "codeviewer");
395 dbTester.permissionTemplates().addAnyoneToTemplate(permissionTemplate, "user");
396 dbTester.permissionTemplates().addAnyoneToTemplate(permissionTemplate, "codeviewer");
397 dbTester.permissionTemplates().addUserToTemplate(permissionTemplate, user, "admin");
399 assertThat(selectProjectPermissionsOfGroup(adminGroup, project)).isEmpty();
400 assertThat(selectProjectPermissionsOfGroup(userGroup, project)).isEmpty();
401 assertThat(selectProjectPermissionsOfGroup(null, project)).isEmpty();
402 assertThat(selectProjectPermissionsOfUser(user, project)).isEmpty();
404 underTest.applyAndCommit(session, permissionTemplate, singletonList(project));
406 assertThat(selectProjectPermissionsOfGroup(adminGroup, project)).containsOnly("admin", "issueadmin");
407 assertThat(selectProjectPermissionsOfGroup(userGroup, project)).containsOnly("user", "codeviewer");
408 assertThat(selectProjectPermissionsOfGroup(null, project)).isEmpty();
409 assertThat(selectProjectPermissionsOfUser(user, project)).containsOnly("admin");
412 private List<String> selectProjectPermissionsOfGroup(@Nullable GroupDto groupDto, ComponentDto project) {
413 return dbTester.getDbClient().groupPermissionDao().selectProjectPermissionsOfGroup(session, groupDto != null ? groupDto.getUuid() : null, project.uuid());
416 private List<String> selectProjectPermissionsOfUser(UserDto userDto, ComponentDto project) {
417 return dbTester.getDbClient().userPermissionDao().selectProjectPermissionsOfUser(session,
418 userDto.getUuid(), project.uuid());
422 public void would_user_have_scan_permission_with_default_permission_template() {
423 GroupDto group = dbTester.users().insertGroup();
424 UserDto user = dbTester.users().insertUser();
425 dbTester.users().insertMember(group, user);
426 PermissionTemplateDto template = templateDb.insertTemplate();
427 dbTester.permissionTemplates().setDefaultTemplates(template, null, null);
428 templateDb.addProjectCreatorToTemplate(template.getUuid(), SCAN.getKey(), template.getName());
429 templateDb.addUserToTemplate(template.getUuid(), user.getUuid(), UserRole.USER, template.getName(), user.getLogin());
430 templateDb.addGroupToTemplate(template.getUuid(), group.getUuid(), UserRole.CODEVIEWER, template.getName(), group.getName());
431 templateDb.addGroupToTemplate(template.getUuid(), null, UserRole.ISSUE_ADMIN, template.getName(), null);
433 // authenticated user
434 checkWouldUserHaveScanPermission(user.getUuid(), true);
437 checkWouldUserHaveScanPermission(null, false);
441 public void would_user_have_scan_permission_with_unknown_default_permission_template() {
442 dbTester.permissionTemplates().setDefaultTemplates("UNKNOWN_TEMPLATE_UUID", null, null);
444 checkWouldUserHaveScanPermission(null, false);
448 public void would_user_have_scan_permission_with_empty_template() {
449 PermissionTemplateDto template = templateDb.insertTemplate();
450 dbTester.permissionTemplates().setDefaultTemplates(template, null, null);
452 checkWouldUserHaveScanPermission(null, false);
455 private void checkWouldUserHaveScanPermission(@Nullable String userUuid, boolean expectedResult) {
456 assertThat(underTest.wouldUserHaveScanPermissionWithDefaultTemplate(session, userUuid, "PROJECT_KEY"))
457 .isEqualTo(expectedResult);