]> source.dussan.org Git - sonarqube.git/blob
fd83a6f5682f3a54ac7aead0d622163efea77ade
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.computation.task.projectanalysis.step;
21
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;
49
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;
57
58 public class ApplyPermissionsStepTest extends BaseStepTest {
59
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;
63
64   @Rule
65   public EsTester esTester = new EsTester(new IssueIndexDefinition(new MapSettings()), new ProjectMeasuresIndexDefinition(new MapSettings()));
66
67   @Rule
68   public DbTester dbTester = DbTester.create(System2.INSTANCE);
69
70   @Rule
71   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
72
73   @Rule
74   public MutableDbIdsRepositoryRule dbIdsRepository = MutableDbIdsRepositoryRule.create(treeRootHolder);
75
76   private PermissionIndexerTester authorizationIndexerTester = new PermissionIndexerTester(esTester);
77
78   private DbSession dbSession;
79   private DbClient dbClient = dbTester.getDbClient();
80   private Settings settings = new MapSettings();
81   private ApplyPermissionsStep step;
82
83   @Before
84   public void setUp() {
85     dbSession = dbClient.openSession(false);
86     step = new ApplyPermissionsStep(dbClient, dbIdsRepository, new PermissionIndexer(dbClient, esTester.client()), new PermissionRepository(dbClient, settings), treeRootHolder);
87   }
88
89   @After
90   public void tearDown() {
91     dbSession.close();
92   }
93
94   @Test
95   public void grant_permission_on_new_project() {
96     ComponentDto projectDto = ComponentTesting.newProjectDto(ROOT_UUID).setKey(ROOT_KEY);
97     dbClient.componentDao().insert(dbSession, projectDto);
98
99     // Create a permission template containing browse permission for anonymous group
100     createDefaultPermissionTemplate(UserRole.USER);
101
102     Component project = ReportComponent.builder(PROJECT, 1).setUuid(ROOT_UUID).setKey(ROOT_KEY).build();
103     dbIdsRepository.setComponentId(project, projectDto.getId());
104     treeRootHolder.setRoot(project);
105
106     step.execute();
107     dbSession.commit();
108
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());
112   }
113
114   @Test
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);
120
121     dbSession.commit();
122
123     Component project = ReportComponent.builder(PROJECT, 1).setUuid(ROOT_UUID).setKey(ROOT_KEY).build();
124     dbIdsRepository.setComponentId(project, projectDto.getId());
125     treeRootHolder.setRoot(project);
126
127     step.execute();
128     dbSession.commit();
129
130     // Check that authorization updated at has not been changed -> Nothing has been done
131     assertThat(projectDto.getAuthorizationUpdatedAt()).isEqualTo(SOME_DATE);
132   }
133
134   @Test
135   public void grant_permission_on_new_view() {
136     ComponentDto viewDto = newView(ROOT_UUID).setKey(ROOT_KEY);
137     dbClient.componentDao().insert(dbSession, viewDto);
138
139     String permission = UserRole.USER;
140     // Create a permission template containing browse permission for anonymous group
141     createDefaultPermissionTemplate(permission);
142
143     Component project = ViewsComponent.builder(VIEW, ROOT_KEY).setUuid(ROOT_UUID).build();
144     dbIdsRepository.setComponentId(project, viewDto.getId());
145     treeRootHolder.setRoot(project);
146
147     step.execute();
148     dbSession.commit();
149
150     assertThat(dbClient.componentDao().selectOrFailByKey(dbSession, ROOT_KEY).getAuthorizationUpdatedAt()).isNotNull();
151     assertThat(dbClient.roleDao().selectGroupPermissions(dbSession, DefaultGroups.ANYONE, viewDto.getId())).containsOnly(permission);
152   }
153
154   @Test
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);
160
161     Component project = ReportComponent.builder(PROJECT, 1).setUuid(ROOT_UUID).setKey(ROOT_KEY).build();
162     dbIdsRepository.setComponentId(project, viewDto.getId());
163     treeRootHolder.setRoot(project);
164
165     step.execute();
166     dbSession.commit();
167
168     // Check that authorization updated at has not been changed -> Nothing has been done
169     assertThat(viewDto.getAuthorizationUpdatedAt()).isEqualTo(SOME_DATE);
170   }
171
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);
176     dbSession.commit();
177   }
178
179   @Override
180   protected ComputationStep step() {
181     return step;
182   }
183 }