]> source.dussan.org Git - sonarqube.git/blob
1201b6197b017c3ac08bed4a9b6b7cb715fcb108
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 SonarSource SA
4  * mailto:info 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.project.ws;
21
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;
32
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;
40
41 public class SearchMyScannableProjectsActionTest {
42
43   @Rule
44   public UserSessionRule userSession = UserSessionRule.standalone();
45
46   @Rule
47   public DbTester db = DbTester.create();
48
49   private final WsActionTester ws = new WsActionTester(
50     new SearchMyScannableProjectsAction(db.getDbClient(), new ProjectFinder(db.getDbClient(), userSession)));
51
52   @Test
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);
59
60     List<Project> result = ws.newRequest()
61       .setParam(TEXT_QUERY, "project")
62       .executeProtobuf(SearchMyScannableProjectsResponse.class)
63       .getProjectsList();
64
65     assertThat(result)
66       .extracting(Project::getKey, Project::getName)
67       .containsExactlyInAnyOrder(
68         tuple("project:three", "Project Three"),
69         tuple("project:four", "Project Four"));
70   }
71
72   @Test
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);
79
80     List<Project> result = ws.newRequest()
81       .setParam(TEXT_QUERY, "")
82       .executeProtobuf(SearchMyScannableProjectsResponse.class)
83       .getProjectsList();
84
85     assertThat(result)
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"));
92   }
93
94   @Test
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"));
100
101     List<Project> result = ws.newRequest()
102       .executeProtobuf(SearchMyScannableProjectsResponse.class)
103       .getProjectsList();
104
105     assertThat(result).isEmpty();
106   }
107
108   @Test
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);
115
116     WsActionTester ws = new WsActionTester(
117       new SearchMyScannableProjectsAction(db.getDbClient(), new ProjectFinder(db.getDbClient(), userSession.anonymous())));
118
119     List<Project> result = ws.newRequest()
120       .executeProtobuf(SearchMyScannableProjectsResponse.class)
121       .getProjectsList();
122
123     assertThat(result).isEmpty();
124   }
125
126   @Test
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);
133
134     List<Project> result = ws.newRequest()
135       .executeProtobuf(SearchMyScannableProjectsResponse.class)
136       .getProjectsList();
137
138     assertThat(result)
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"));
145   }
146
147   @Test
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);
155
156     String result = ws.newRequest()
157       .execute().getInput();
158
159     assertJson(result).isSimilarTo(ws.getDef().responseExampleAsString());
160     assertJson(ws.getDef().responseExampleAsString()).isSimilarTo(result);
161   }
162
163   @Test
164   public void definition() {
165     WebService.Action definition = ws.getDef();
166
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();
172
173     assertThat(definition.params())
174       .extracting(Param::key, Param::isRequired)
175       .containsExactlyInAnyOrder(
176         tuple("q", false));
177   }
178
179 }