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.computation.task.projectanalysis.step;
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.config.MapSettings;
27 import org.sonar.api.config.Settings;
28 import org.sonar.api.security.DefaultGroups;
29 import org.sonar.api.utils.System2;
30 import org.sonar.api.web.UserRole;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.DbTester;
34 import org.sonar.db.component.ComponentDto;
35 import org.sonar.db.component.ComponentTesting;
36 import org.sonar.db.permission.PermissionRepository;
37 import org.sonar.db.permission.template.PermissionTemplateDto;
38 import org.sonar.server.component.es.ProjectMeasuresIndexDefinition;
39 import org.sonar.server.computation.task.projectanalysis.component.Component;
40 import org.sonar.server.computation.task.projectanalysis.component.MutableDbIdsRepositoryRule;
41 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
42 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
43 import org.sonar.server.computation.task.projectanalysis.component.ViewsComponent;
44 import org.sonar.server.computation.task.step.ComputationStep;
45 import org.sonar.server.es.EsTester;
46 import org.sonar.server.issue.index.IssueIndexDefinition;
47 import org.sonar.server.permission.index.PermissionIndexer;
48 import org.sonar.server.permission.index.PermissionIndexerTester;
50 import static java.util.Collections.emptyList;
51 import static java.util.Collections.singletonList;
52 import static org.assertj.core.api.Assertions.assertThat;
53 import static org.sonar.db.component.ComponentTesting.newView;
54 import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
55 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT;
56 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.VIEW;
58 public class ApplyPermissionsStepTest extends BaseStepTest {
60 private static final String ROOT_KEY = "ROOT_KEY";
61 private static final String ROOT_UUID = "ROOT_UUID";
62 private static final long SOME_DATE = 1000L;
65 public EsTester esTester = new EsTester(new IssueIndexDefinition(new MapSettings()), new ProjectMeasuresIndexDefinition(new MapSettings()));
68 public DbTester dbTester = DbTester.create(System2.INSTANCE);
71 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
74 public MutableDbIdsRepositoryRule dbIdsRepository = MutableDbIdsRepositoryRule.create(treeRootHolder);
76 private PermissionIndexerTester authorizationIndexerTester = new PermissionIndexerTester(esTester);
78 private DbSession dbSession;
79 private DbClient dbClient = dbTester.getDbClient();
80 private Settings settings = new MapSettings();
81 private ApplyPermissionsStep step;
85 dbSession = dbClient.openSession(false);
86 step = new ApplyPermissionsStep(dbClient, dbIdsRepository, new PermissionIndexer(dbClient, esTester.client()), new PermissionRepository(dbClient, settings), treeRootHolder);
90 public void tearDown() {
95 public void grant_permission_on_new_project() {
96 ComponentDto projectDto = ComponentTesting.newProjectDto(ROOT_UUID).setKey(ROOT_KEY);
97 dbClient.componentDao().insert(dbSession, projectDto);
99 // Create a permission template containing browse permission for anonymous group
100 createDefaultPermissionTemplate(UserRole.USER);
102 Component project = ReportComponent.builder(PROJECT, 1).setUuid(ROOT_UUID).setKey(ROOT_KEY).build();
103 dbIdsRepository.setComponentId(project, projectDto.getId());
104 treeRootHolder.setRoot(project);
109 assertThat(dbClient.componentDao().selectOrFailByKey(dbSession, ROOT_KEY).getAuthorizationUpdatedAt()).isNotNull();
110 assertThat(dbClient.roleDao().selectGroupPermissions(dbSession, DefaultGroups.ANYONE, projectDto.getId())).containsOnly(UserRole.USER);
111 authorizationIndexerTester.verifyProjectExistsWithPermission(ROOT_UUID, singletonList(DefaultGroups.ANYONE), emptyList());
115 public void nothing_to_do_on_existing_project() {
116 ComponentDto projectDto = ComponentTesting.newProjectDto(ROOT_UUID).setKey(ROOT_KEY).setAuthorizationUpdatedAt(SOME_DATE);
117 dbTester.components().insertComponent(projectDto);
118 // Permissions are already set on the project
119 dbTester.users().insertProjectPermissionOnAnyone(UserRole.USER, projectDto);
123 Component project = ReportComponent.builder(PROJECT, 1).setUuid(ROOT_UUID).setKey(ROOT_KEY).build();
124 dbIdsRepository.setComponentId(project, projectDto.getId());
125 treeRootHolder.setRoot(project);
130 // Check that authorization updated at has not been changed -> Nothing has been done
131 assertThat(projectDto.getAuthorizationUpdatedAt()).isEqualTo(SOME_DATE);
135 public void grant_permission_on_new_view() {
136 ComponentDto viewDto = newView(ROOT_UUID).setKey(ROOT_KEY);
137 dbClient.componentDao().insert(dbSession, viewDto);
139 String permission = UserRole.USER;
140 // Create a permission template containing browse permission for anonymous group
141 createDefaultPermissionTemplate(permission);
143 Component project = ViewsComponent.builder(VIEW, ROOT_KEY).setUuid(ROOT_UUID).build();
144 dbIdsRepository.setComponentId(project, viewDto.getId());
145 treeRootHolder.setRoot(project);
150 assertThat(dbClient.componentDao().selectOrFailByKey(dbSession, ROOT_KEY).getAuthorizationUpdatedAt()).isNotNull();
151 assertThat(dbClient.roleDao().selectGroupPermissions(dbSession, DefaultGroups.ANYONE, viewDto.getId())).containsOnly(permission);
155 public void nothing_to_do_on_existing_view() {
156 ComponentDto viewDto = newView(ROOT_UUID).setKey(ROOT_KEY).setAuthorizationUpdatedAt(SOME_DATE);
157 dbTester.components().insertComponent(viewDto);
158 // Permissions are already set on the view
159 dbTester.users().insertProjectPermissionOnAnyone(UserRole.USER, viewDto);
161 Component project = ReportComponent.builder(PROJECT, 1).setUuid(ROOT_UUID).setKey(ROOT_KEY).build();
162 dbIdsRepository.setComponentId(project, viewDto.getId());
163 treeRootHolder.setRoot(project);
168 // Check that authorization updated at has not been changed -> Nothing has been done
169 assertThat(viewDto.getAuthorizationUpdatedAt()).isEqualTo(SOME_DATE);
172 private void createDefaultPermissionTemplate(String permission) {
173 PermissionTemplateDto permissionTemplateDto = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setName("Default"));
174 settings.setProperty("sonar.permission.template.default", permissionTemplateDto.getKee());
175 dbClient.permissionTemplateDao().insertGroupPermission(dbSession, permissionTemplateDto.getId(), null, permission);
180 protected ComputationStep step() {