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.permission.ws;
22 import java.io.IOException;
23 import javax.annotation.Nullable;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.experimental.categories.Category;
28 import org.junit.rules.ExpectedException;
29 import org.sonar.api.resources.Qualifiers;
30 import org.sonar.api.utils.System2;
31 import org.sonar.api.web.UserRole;
32 import org.sonar.core.permission.GlobalPermissions;
33 import org.sonar.db.DbClient;
34 import org.sonar.db.DbSession;
35 import org.sonar.db.DbTester;
36 import org.sonar.db.component.ComponentDto;
37 import org.sonar.db.component.ResourceTypesRule;
38 import org.sonar.db.user.GroupDto;
39 import org.sonar.db.user.GroupRoleDto;
40 import org.sonar.db.user.UserDto;
41 import org.sonar.db.user.UserRoleDto;
42 import org.sonar.server.component.ComponentFinder;
43 import org.sonar.server.exceptions.ForbiddenException;
44 import org.sonar.server.exceptions.UnauthorizedException;
45 import org.sonar.server.i18n.I18nRule;
46 import org.sonar.server.tester.UserSessionRule;
47 import org.sonar.server.usergroups.ws.UserGroupFinder;
48 import org.sonar.server.ws.TestResponse;
49 import org.sonar.server.ws.WsActionTester;
50 import org.sonar.test.DbTests;
51 import org.sonarqube.ws.MediaTypes;
52 import org.sonarqube.ws.WsPermissions.SearchProjectPermissionsWsResponse;
54 import static org.assertj.core.api.Assertions.assertThat;
55 import static org.sonar.api.server.ws.WebService.Param.PAGE;
56 import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
57 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
58 import static org.sonar.db.component.ComponentTesting.newDeveloper;
59 import static org.sonar.db.component.ComponentTesting.newProjectCopy;
60 import static org.sonar.db.component.ComponentTesting.newProjectDto;
61 import static org.sonar.db.component.ComponentTesting.newView;
62 import static org.sonar.db.user.GroupTesting.newGroupDto;
63 import static org.sonar.db.user.UserTesting.newUserDto;
64 import static org.sonar.test.JsonAssert.assertJson;
65 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID;
66 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
68 @Category(DbTests.class)
69 public class SearchProjectPermissionsActionTest {
71 public ExpectedException expectedException = ExpectedException.none();
73 public UserSessionRule userSession = UserSessionRule.standalone();
75 public DbTester db = DbTester.create(System2.INSTANCE);
78 I18nRule i18n = new I18nRule();
79 DbClient dbClient = db.getDbClient();
80 DbSession dbSession = db.getSession();
81 ResourceTypesRule resourceTypes = new ResourceTypesRule();
82 SearchProjectPermissionsDataLoader dataLoader;
84 SearchProjectPermissionsAction underTest;
88 resourceTypes.setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
89 ComponentFinder componentFinder = new ComponentFinder(dbClient);
90 PermissionDependenciesFinder finder = new PermissionDependenciesFinder(dbClient, componentFinder, new UserGroupFinder(dbClient), resourceTypes);
91 i18n.setProjectPermissions();
93 dataLoader = new SearchProjectPermissionsDataLoader(dbClient, finder, resourceTypes);
94 underTest = new SearchProjectPermissionsAction(dbClient, userSession, i18n, resourceTypes, dataLoader);
96 ws = new WsActionTester(underTest);
98 userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
102 public void search_project_permissions() {
103 UserDto user1 = insertUser(newUserDto());
104 UserDto user2 = insertUser(newUserDto());
105 UserDto user3 = insertUser(newUserDto());
107 ComponentDto jdk7 = insertJdk7();
108 ComponentDto project2 = insertClang();
109 ComponentDto dev = insertDeveloper();
110 ComponentDto view = insertView();
111 insertProjectInView(jdk7, view);
113 insertUserRole(UserRole.ISSUE_ADMIN, user1.getId(), jdk7.getId());
114 insertUserRole(UserRole.ADMIN, user1.getId(), jdk7.getId());
115 insertUserRole(UserRole.ADMIN, user2.getId(), jdk7.getId());
116 insertUserRole(UserRole.ADMIN, user3.getId(), jdk7.getId());
117 insertUserRole(UserRole.ISSUE_ADMIN, user1.getId(), project2.getId());
118 insertUserRole(UserRole.ISSUE_ADMIN, user1.getId(), dev.getId());
119 insertUserRole(UserRole.ISSUE_ADMIN, user1.getId(), view.getId());
121 insertUserRole(GlobalPermissions.SYSTEM_ADMIN, user1.getId(), null);
123 GroupDto group1 = insertGroup(newGroupDto());
124 GroupDto group2 = insertGroup(newGroupDto());
125 GroupDto group3 = insertGroup(newGroupDto());
127 insertGroupRole(UserRole.ADMIN, jdk7.getId(), null);
128 insertGroupRole(UserRole.ADMIN, jdk7.getId(), group1.getId());
129 insertGroupRole(UserRole.ADMIN, jdk7.getId(), group2.getId());
130 insertGroupRole(UserRole.ADMIN, jdk7.getId(), group3.getId());
131 insertGroupRole(UserRole.ADMIN, dev.getId(), group2.getId());
132 insertGroupRole(UserRole.ADMIN, view.getId(), group2.getId());
136 String result = ws.newRequest().execute().getInput();
138 assertJson(result).isSimilarTo(getClass().getResource("search_project_permissions-example.json"));
142 public void empty_result() {
143 String result = ws.newRequest().execute().getInput();
145 assertJson(result).isSimilarTo(getClass().getResource("SearchProjectPermissionsActionTest/empty.json"));
149 public void search_project_permissions_with_project_permission() {
150 userSession.login().addProjectUuidPermissions(UserRole.ADMIN, "project-uuid");
151 insertComponent(newProjectDto("project-uuid"));
154 String result = ws.newRequest()
155 .setParam(PARAM_PROJECT_ID, "project-uuid")
156 .execute().getInput();
158 assertThat(result).contains("project-uuid");
162 public void has_projects_ordered_by_name() {
163 for (int i = 9; i >= 1; i--) {
164 insertComponent(newProjectDto()
165 .setName("project-name-" + i));
169 String result = ws.newRequest()
171 .setParam(PAGE_SIZE, "3")
172 .execute().getInput();
175 .contains("project-name-1", "project-name-2", "project-name-3")
176 .doesNotContain("project-name-4");
180 public void search_by_query_on_name() {
181 insertComponent(newProjectDto().setName("project-name"));
182 insertComponent(newProjectDto().setName("another-name"));
185 String result = ws.newRequest()
186 .setParam(TEXT_QUERY, "project")
187 .execute().getInput();
189 assertThat(result).contains("project-name")
190 .doesNotContain("another-name");
194 public void search_by_query_on_key() {
195 insertComponent(newProjectDto().setKey("project-key"));
196 insertComponent(newProjectDto().setKey("another-key"));
199 String result = ws.newRequest()
200 .setParam(TEXT_QUERY, "project")
201 .execute().getInput();
203 assertThat(result).contains("project-key")
204 .doesNotContain("another-key");
208 public void handle_more_than_1000_projects() {
209 for (int i = 1; i <= 1001; i++) {
210 insertComponent(newProjectDto("project-uuid-" + i));
214 String result = ws.newRequest()
215 .setParam(TEXT_QUERY, "project")
216 .setParam(PAGE_SIZE, "1001")
217 .execute().getInput();
219 assertThat(result).contains("project-uuid-1", "project-uuid-999", "project-uuid-1001");
223 public void result_depends_of_root_types() {
224 resourceTypes.setRootQualifiers(Qualifiers.PROJECT);
225 insertComponent(newView("view-uuid"));
226 insertComponent(newDeveloper("developer-name"));
227 insertComponent(newProjectDto("project-uuid"));
229 dataLoader = new SearchProjectPermissionsDataLoader(dbClient,
230 new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), new UserGroupFinder(dbClient), resourceTypes),
232 underTest = new SearchProjectPermissionsAction(dbClient, userSession, i18n, resourceTypes, dataLoader);
233 ws = new WsActionTester(underTest);
235 String result = ws.newRequest().execute().getInput();
237 assertThat(result).contains("project-uuid")
238 .doesNotContain("view-uuid")
239 .doesNotContain("developer-name");
243 public void filter_by_qualifier() throws IOException {
244 insertComponent(newView("view-uuid"));
245 insertComponent(newDeveloper("developer-name"));
246 insertComponent(newProjectDto("project-uuid"));
249 TestResponse wsResponse = ws.newRequest()
250 .setMediaType(MediaTypes.PROTOBUF)
251 .setParam(PARAM_QUALIFIER, Qualifiers.PROJECT)
253 SearchProjectPermissionsWsResponse result = SearchProjectPermissionsWsResponse.parseFrom(wsResponse.getInputStream());
255 assertThat(result.getProjectsList())
257 .contains("project-uuid")
258 .doesNotContain("view-uuid")
259 .doesNotContain("developer-name");
263 public void fail_if_not_logged_in() {
264 expectedException.expect(UnauthorizedException.class);
265 userSession.anonymous();
267 ws.newRequest().execute();
271 public void fail_if_not_admin() {
272 expectedException.expect(ForbiddenException.class);
275 ws.newRequest().execute();
278 private ComponentDto insertView() {
279 return insertComponent(newView()
280 .setUuid("752d8bfd-420c-4a83-a4e5-8ab19b13c8fc")
285 private ComponentDto insertProjectInView(ComponentDto project, ComponentDto view) {
286 return insertComponent(newProjectCopy("project-in-view-uuid", project, view));
289 private ComponentDto insertDeveloper() {
290 return insertComponent(newDeveloper("Simon Brandhof")
291 .setUuid("4e607bf9-7ed0-484a-946d-d58ba7dab2fb")
292 .setKey("simon-brandhof"));
295 private ComponentDto insertClang() {
296 return insertComponent(newProjectDto("project-uuid-2")
299 .setUuid("ce4c03d6-430f-40a9-b777-ad877c00aa4d"));
302 private ComponentDto insertJdk7() {
303 return insertComponent(newProjectDto("project-uuid-1")
305 .setKey("net.java.openjdk:jdk7")
306 .setUuid("0bd7b1e7-91d6-439e-a607-4a3a9aad3c6a"));
309 private UserDto insertUser(UserDto user) {
310 return dbClient.userDao().insert(dbSession, user.setActive(true));
313 private void insertUserRole(String permission, long userId, @Nullable Long resourceId) {
314 dbClient.roleDao().insertUserRole(dbSession, new UserRoleDto()
317 .setResourceId(resourceId));
320 private GroupDto insertGroup(GroupDto group) {
321 return dbClient.groupDao().insert(dbSession, group);
324 private void insertGroupRole(String permission, @Nullable Long resourceId, @Nullable Long groupId) {
325 dbClient.roleDao().insertGroupRole(dbSession, new GroupRoleDto().setRole(permission).setResourceId(resourceId).setGroupId(groupId));
328 private ComponentDto insertComponent(ComponentDto component) {
329 dbClient.componentDao().insert(dbSession, component.setEnabled(true));
330 return dbClient.componentDao().selectOrFailByUuid(dbSession, component.uuid());
333 private void commit() {