3 * Copyright (C) 2009-2022 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.project.ws;
22 import java.util.List;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.server.ws.WebService;
26 import org.sonar.api.server.ws.WebService.Param;
27 import org.sonar.db.DbTester;
28 import org.sonar.db.permission.GlobalPermission;
29 import org.sonar.db.project.ProjectDto;
30 import org.sonar.server.tester.UserSessionRule;
31 import org.sonar.server.ws.WsActionTester;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.assertj.core.api.Assertions.tuple;
35 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
36 import static org.sonar.api.web.UserRole.SCAN;
37 import static org.sonar.test.JsonAssert.assertJson;
38 import static org.sonarqube.ws.Projects.SearchMyScannableProjectsResponse;
39 import static org.sonarqube.ws.Projects.SearchMyScannableProjectsResponse.Project;
41 public class SearchMyScannableProjectsActionTest {
44 public UserSessionRule userSession = UserSessionRule.standalone();
47 public DbTester db = DbTester.create();
49 private final WsActionTester ws = new WsActionTester(
50 new SearchMyScannableProjectsAction(db.getDbClient(), new ProjectFinder(db.getDbClient(), userSession)));
53 public void projects_filtered_by_query() {
54 ProjectDto project1 = db.components().insertPrivateProjectDto(p -> p.setDbKey("project:one").setName("Projet Un"));
55 ProjectDto project2 = db.components().insertPrivateProjectDto(p -> p.setDbKey("project:two").setName("Projet Deux"));
56 ProjectDto project3 = db.components().insertPrivateProjectDto(p -> p.setDbKey("project:three").setName("Project Three"));
57 ProjectDto project4 = db.components().insertPublicProjectDto(p -> p.setDbKey("project:four").setName("Project Four"));
58 userSession.addProjectPermission(SCAN, project1, project2, project3, project4);
60 List<Project> result = ws.newRequest()
61 .setParam(TEXT_QUERY, "project")
62 .executeProtobuf(SearchMyScannableProjectsResponse.class)
66 .extracting(Project::getKey, Project::getName)
67 .containsExactlyInAnyOrder(
68 tuple("project:three", "Project Three"),
69 tuple("project:four", "Project Four"));
73 public void projects_not_filtered_by_empty_query() {
74 ProjectDto project1 = db.components().insertPrivateProjectDto(p -> p.setDbKey("project:one").setName("Projet Un"));
75 ProjectDto project2 = db.components().insertPrivateProjectDto(p -> p.setDbKey("project:two").setName("Projet Deux"));
76 ProjectDto project3 = db.components().insertPrivateProjectDto(p -> p.setDbKey("project:three").setName("Project Three"));
77 ProjectDto project4 = db.components().insertPublicProjectDto(p -> p.setDbKey("project:four").setName("Project Four"));
78 userSession.addProjectPermission(SCAN, project1, project2, project3, project4);
80 List<Project> result = ws.newRequest()
81 .setParam(TEXT_QUERY, "")
82 .executeProtobuf(SearchMyScannableProjectsResponse.class)
86 .extracting(Project::getKey, Project::getName)
87 .containsExactlyInAnyOrder(
88 tuple("project:one", "Projet Un"),
89 tuple("project:two", "Projet Deux"),
90 tuple("project:three", "Project Three"),
91 tuple("project:four", "Project Four"));
95 public void projects_filtered_by_scan_permission() {
96 db.components().insertPrivateProjectDto(p -> p.setDbKey("project:one").setName("Projet Un"));
97 db.components().insertPrivateProjectDto(p -> p.setDbKey("project:two").setName("Projet Deux"));
98 db.components().insertPrivateProjectDto(p -> p.setDbKey("project:three").setName("Project Three"));
99 db.components().insertPublicProjectDto(p -> p.setDbKey("project:four").setName("Project Four"));
101 List<Project> result = ws.newRequest()
102 .executeProtobuf(SearchMyScannableProjectsResponse.class)
105 assertThat(result).isEmpty();
109 public void projects_filtered_for_anonymous_user() {
110 ProjectDto project1 = db.components().insertPrivateProjectDto(p -> p.setDbKey("project:one").setName("Projet Un"));
111 ProjectDto project2 = db.components().insertPrivateProjectDto(p -> p.setDbKey("project:two").setName("Projet Deux"));
112 ProjectDto project3 = db.components().insertPrivateProjectDto(p -> p.setDbKey("project:three").setName("Project Three"));
113 ProjectDto project4 = db.components().insertPublicProjectDto(p -> p.setDbKey("project:four").setName("Project Four"));
114 userSession.addProjectPermission(SCAN, project1, project2, project3, project4);
116 WsActionTester ws = new WsActionTester(
117 new SearchMyScannableProjectsAction(db.getDbClient(), new ProjectFinder(db.getDbClient(), userSession.anonymous())));
119 List<Project> result = ws.newRequest()
120 .executeProtobuf(SearchMyScannableProjectsResponse.class)
123 assertThat(result).isEmpty();
127 public void projects_not_filtered_due_to_global_scan_permission() {
128 db.components().insertPrivateProjectDto(p -> p.setDbKey("project:one").setName("Projet Un"));
129 db.components().insertPrivateProjectDto(p -> p.setDbKey("project:two").setName("Projet Deux"));
130 db.components().insertPrivateProjectDto(p -> p.setDbKey("project:three").setName("Project Three"));
131 db.components().insertPublicProjectDto(p -> p.setDbKey("project:four").setName("Project Four"));
132 userSession.addPermission(GlobalPermission.SCAN);
134 List<Project> result = ws.newRequest()
135 .executeProtobuf(SearchMyScannableProjectsResponse.class)
139 .extracting(Project::getKey, Project::getName)
140 .containsExactlyInAnyOrder(
141 tuple("project:one", "Projet Un"),
142 tuple("project:two", "Projet Deux"),
143 tuple("project:three", "Project Three"),
144 tuple("project:four", "Project Four"));
148 public void json_example() {
149 ProjectDto project1 = db.components().insertPrivateProjectDto(p -> p.setDbKey("project-key-1").setName("Project 1"));
150 ProjectDto project2 = db.components().insertPrivateProjectDto(p -> p.setDbKey("project-key-2").setName("Project 2"));
151 ProjectDto project3 = db.components().insertPublicProjectDto(p -> p.setDbKey("public-project-without-scan-permissions")
152 .setName("Public Project with Scan Permissions"));
153 userSession.addProjectPermission(SCAN, project1, project2);
154 userSession.registerProjects(project3);
156 String result = ws.newRequest()
157 .execute().getInput();
159 assertJson(result).isSimilarTo(ws.getDef().responseExampleAsString());
160 assertJson(ws.getDef().responseExampleAsString()).isSimilarTo(result);
164 public void definition() {
165 WebService.Action definition = ws.getDef();
167 assertThat(definition.key()).isEqualTo("search_my_scannable_projects");
168 assertThat(definition.since()).isEqualTo("9.5");
169 assertThat(definition.isInternal()).isTrue();
170 assertThat(definition.isPost()).isFalse();
171 assertThat(definition.responseExampleAsString()).isNotEmpty();
173 assertThat(definition.params())
174 .extracting(Param::key, Param::isRequired)
175 .containsExactlyInAnyOrder(