3 * Copyright (C) 2009-2017 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.ws;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.sonar.api.resources.Qualifiers;
25 import org.sonar.api.web.UserRole;
26 import org.sonar.db.component.ComponentDbTester;
27 import org.sonar.db.component.ComponentDto;
28 import org.sonar.db.component.ComponentTesting;
29 import org.sonar.db.component.ResourceTypesRule;
30 import org.sonar.db.organization.OrganizationDto;
31 import org.sonar.db.user.GroupDto;
32 import org.sonar.db.user.UserDto;
33 import org.sonar.server.exceptions.ForbiddenException;
34 import org.sonar.server.exceptions.UnauthorizedException;
35 import org.sonar.server.i18n.I18nRule;
36 import org.sonarqube.ws.WsPermissions;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.sonar.api.server.ws.WebService.Param.PAGE;
40 import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
41 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
42 import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
43 import static org.sonar.db.component.ComponentTesting.newProjectCopy;
44 import static org.sonar.db.component.ComponentTesting.newView;
45 import static org.sonar.db.permission.OrganizationPermission.ADMINISTER;
46 import static org.sonar.test.JsonAssert.assertJson;
47 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID;
48 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
50 public class SearchProjectPermissionsActionTest extends BasePermissionWsTest<SearchProjectPermissionsAction> {
52 private ComponentDbTester componentDb = new ComponentDbTester(db);
53 private I18nRule i18n = new I18nRule();
57 i18n.setProjectPermissions();
58 userSession.logIn().setSystemAdministrator();
62 protected SearchProjectPermissionsAction buildWsAction() {
63 i18n.setProjectPermissions();
64 ResourceTypesRule rootResourceTypes = newRootResourceTypes();
65 PermissionWsSupport wsSupport = newPermissionWsSupport();
66 SearchProjectPermissionsDataLoader dataLoader = new SearchProjectPermissionsDataLoader(db.getDbClient(), wsSupport, rootResourceTypes);
67 return new SearchProjectPermissionsAction(db.getDbClient(), userSession, i18n, rootResourceTypes, dataLoader, wsSupport);
71 public void search_project_permissions_counts_0_users_and_0_groups_on_public_project_without_any_specified_permission_in_DB() {
72 ComponentDto project = db.components().insertPublicProject();
74 String result = newRequest().execute().getInput();
77 .ignoreFields("permissions")
80 " \"pageIndex\": 1," +
81 " \"pageSize\": 25," +
86 " \"id\": \"" + project.uuid() + "\"," +
87 " \"key\": \"" + project.getDbKey() + "\"," +
88 " \"name\": \"" + project.name() + "\"," +
89 " \"qualifier\": \"TRK\"," +
90 " \"permissions\": []" +
97 public void search_project_permissions_counts_0_users_and_0_groups_on_private_project_without_any_specified_permission_in_DB() {
98 ComponentDto project = db.components().insertPrivateProject();
100 String result = newRequest().execute().getInput();
103 .ignoreFields("permissions")
106 " \"pageIndex\": 1," +
107 " \"pageSize\": 25," +
112 " \"id\": \"" + project.uuid() + "\"," +
113 " \"key\": \"" + project.getDbKey() + "\"," +
114 " \"name\": \"" + project.name() + "\"," +
115 " \"qualifier\": \"TRK\"," +
116 " \"permissions\": []" +
123 public void search_project_permissions() throws Exception {
124 UserDto user1 = db.users().insertUser();
125 UserDto user2 = db.users().insertUser();
126 UserDto user3 = db.users().insertUser();
128 ComponentDto jdk7 = insertJdk7();
129 ComponentDto project2 = insertClang();
130 ComponentDto view = insertView();
131 insertProjectInView(jdk7, view);
133 db.users().insertProjectPermissionOnUser(user1, UserRole.ISSUE_ADMIN, jdk7);
134 db.users().insertProjectPermissionOnUser(user1, UserRole.ADMIN, jdk7);
135 db.users().insertProjectPermissionOnUser(user2, UserRole.ADMIN, jdk7);
136 db.users().insertProjectPermissionOnUser(user3, UserRole.ADMIN, jdk7);
137 db.users().insertProjectPermissionOnUser(user1, UserRole.ISSUE_ADMIN, project2);
138 db.users().insertProjectPermissionOnUser(user1, UserRole.ISSUE_ADMIN, view);
140 db.users().insertPermissionOnUser(user1, ADMINISTER);
142 GroupDto group1 = db.users().insertGroup();
143 GroupDto group2 = db.users().insertGroup();
144 GroupDto group3 = db.users().insertGroup();
146 db.users().insertProjectPermissionOnAnyone(UserRole.ADMIN, jdk7);
147 db.users().insertProjectPermissionOnGroup(group1, UserRole.ADMIN, jdk7);
148 db.users().insertProjectPermissionOnGroup(group2, UserRole.ADMIN, jdk7);
149 db.users().insertProjectPermissionOnGroup(group3, UserRole.ADMIN, jdk7);
150 db.users().insertProjectPermissionOnGroup(group2, UserRole.ADMIN, view);
154 String result = newRequest().execute().getInput();
157 .ignoreFields("permissions")
158 .isSimilarTo(getClass().getResource("search_project_permissions-example.json"));
162 public void empty_result() throws Exception {
163 String result = newRequest().execute().getInput();
166 .ignoreFields("permissions")
167 .isSimilarTo(getClass().getResource("SearchProjectPermissionsActionTest/empty.json"));
171 public void search_project_permissions_with_project_permission() throws Exception {
172 ComponentDto project = db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid"));
173 userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
175 String result = newRequest()
176 .setParam(PARAM_PROJECT_ID, "project-uuid")
177 .execute().getInput();
179 assertThat(result).contains("project-uuid");
183 public void has_projects_ordered_by_name() throws Exception {
184 OrganizationDto organizationDto = db.organizations().insert();
185 for (int i = 9; i >= 1; i--) {
186 db.components().insertComponent(ComponentTesting.newPrivateProjectDto(organizationDto)
187 .setName("project-name-" + i));
190 String result = newRequest()
192 .setParam(PAGE_SIZE, "3")
193 .execute().getInput();
196 .contains("project-name-1", "project-name-2", "project-name-3")
197 .doesNotContain("project-name-4");
201 public void search_by_query_on_name() throws Exception {
202 componentDb.insertProjectAndSnapshot(ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()).setName("project-name"));
203 componentDb.insertProjectAndSnapshot(ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()).setName("another-name"));
205 String result = newRequest()
206 .setParam(TEXT_QUERY, "project")
207 .execute().getInput();
209 assertThat(result).contains("project-name")
210 .doesNotContain("another-name");
214 public void search_by_query_on_key_must_match_exactly() throws Exception {
215 OrganizationDto organizationDto = db.organizations().insert();
216 componentDb.insertProjectAndSnapshot(ComponentTesting.newPrivateProjectDto(organizationDto).setDbKey("project-key"));
217 componentDb.insertProjectAndSnapshot(ComponentTesting.newPrivateProjectDto(organizationDto).setDbKey("another-key"));
219 String result = newRequest()
220 .setParam(TEXT_QUERY, "project-key")
224 assertThat(result).contains("project-key")
225 .doesNotContain("another-key");
229 public void handle_more_than_1000_projects() throws Exception {
230 for (int i = 1; i <= 1001; i++) {
231 componentDb.insertProjectAndSnapshot(newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid-" + i));
234 String result = newRequest()
235 .setParam(TEXT_QUERY, "project")
236 .setParam(PAGE_SIZE, "1001")
240 assertThat(result).contains("project-uuid-1", "project-uuid-999", "project-uuid-1001");
244 public void filter_by_qualifier() throws Exception {
245 OrganizationDto organizationDto = db.organizations().insert();
246 db.components().insertComponent(newView(organizationDto, "view-uuid"));
247 db.components().insertComponent(newPrivateProjectDto(organizationDto, "project-uuid"));
249 WsPermissions.SearchProjectPermissionsWsResponse result = newRequest()
250 .setParam(PARAM_QUALIFIER, Qualifiers.PROJECT)
251 .executeProtobuf(WsPermissions.SearchProjectPermissionsWsResponse.class);
253 assertThat(result.getProjectsList())
255 .contains("project-uuid")
256 .doesNotContain("view-uuid");
260 public void fail_if_not_logged_in() throws Exception {
261 userSession.anonymous();
263 expectedException.expect(UnauthorizedException.class);
265 newRequest().execute();
269 public void fail_if_not_admin() throws Exception {
272 expectedException.expect(ForbiddenException.class);
274 newRequest().execute();
278 public void display_all_project_permissions() throws Exception {
279 String result = newRequest().execute().getInput();
282 .ignoreFields("permissions")
283 .isSimilarTo(getClass().getResource("SearchProjectPermissionsActionTest/display_all_project_permissions.json"));
286 private ComponentDto insertView() {
287 return db.components().insertComponent(newView(db.getDefaultOrganization())
288 .setUuid("752d8bfd-420c-4a83-a4e5-8ab19b13c8fc")
293 private ComponentDto insertProjectInView(ComponentDto project, ComponentDto view) {
294 return db.components().insertComponent(newProjectCopy("project-in-view-uuid", project, view));
297 private ComponentDto insertClang() {
298 return db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid-2")
301 .setUuid("ce4c03d6-430f-40a9-b777-ad877c00aa4d"));
304 private ComponentDto insertJdk7() {
305 return db.components().insertComponent(ComponentTesting.newPublicProjectDto(db.getDefaultOrganization())
307 .setDbKey("net.java.openjdk:jdk7")
308 .setUuid("0bd7b1e7-91d6-439e-a607-4a3a9aad3c6a"));