]> source.dussan.org Git - sonarqube.git/blob
1179f9edebe13019cdda80b92b1c8c82a7135866
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.qualityprofile.ws;
21
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.sonar.api.server.ws.WebService;
26 import org.sonar.api.server.ws.WebService.Param;
27 import org.sonar.api.utils.System2;
28 import org.sonar.db.DbTester;
29 import org.sonar.db.component.ComponentDto;
30 import org.sonar.db.organization.OrganizationDto;
31 import org.sonar.db.qualityprofile.QProfileDto;
32 import org.sonar.db.user.UserDto;
33 import org.sonar.server.exceptions.ForbiddenException;
34 import org.sonar.server.exceptions.NotFoundException;
35 import org.sonar.server.organization.TestDefaultOrganizationProvider;
36 import org.sonar.server.tester.UserSessionRule;
37 import org.sonar.server.ws.WsActionTester;
38
39 import static java.lang.String.format;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.sonar.api.server.ws.WebService.Param.PAGE;
42 import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
43 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
44 import static org.sonar.api.web.UserRole.USER;
45 import static org.sonar.db.organization.OrganizationDto.Subscription.PAID;
46 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY;
47
48 public class ProjectsActionTest {
49
50   @Rule
51   public ExpectedException expectedException = ExpectedException.none();
52   @Rule
53   public DbTester db = DbTester.create(System2.INSTANCE);
54   @Rule
55   public UserSessionRule userSession = UserSessionRule.standalone();
56
57   private WsActionTester ws = new WsActionTester(
58     new ProjectsAction(db.getDbClient(), userSession, new QProfileWsSupport(db.getDbClient(), userSession, TestDefaultOrganizationProvider.from(db))));
59
60   @Test
61   public void list_authorized_projects_only() {
62     OrganizationDto organization = db.organizations().insert();
63     ComponentDto project1 = db.components().insertPrivateProject(organization);
64     ComponentDto project2 = db.components().insertPrivateProject(organization);
65     QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
66     associateProjectsWithProfile(qualityProfile, project1, project2);
67     // user only sees project1
68     UserDto user = db.users().insertUser();
69     db.users().insertProjectPermissionOnUser(user, USER, project1);
70     userSession.logIn(user);
71
72     ws.newRequest()
73       .setParam(PARAM_KEY, qualityProfile.getKee())
74       .setParam("selected", "selected")
75       .execute()
76       .assertJson("{\"results\":\n" +
77         "  [\n" +
78         "    {\n" +
79         "      \"key\": \"" + project1.getKey() + "\",\n" +
80         "      \"name\": \"" + project1.name() + "\",\n" +
81         "      \"selected\": true\n" +
82         "    }\n" +
83         "  ]}");
84   }
85
86   @Test
87   public void paginate() {
88     OrganizationDto organization = db.organizations().insert();
89     ComponentDto project1 = db.components().insertPublicProject(organization, p -> p.setName("Project One"));
90     ComponentDto project2 = db.components().insertPublicProject(organization, p -> p.setName("Project Two"));
91     ComponentDto project3 = db.components().insertPublicProject(organization, p -> p.setName("Project Three"));
92     ComponentDto project4 = db.components().insertPublicProject(organization, p -> p.setName("Project Four"));
93     QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
94     associateProjectsWithProfile(qualityProfile, project1, project2, project3, project4);
95
96     ws.newRequest()
97       .setParam(PARAM_KEY, qualityProfile.getKee())
98       .setParam("selected", "selected")
99       .setParam(PAGE_SIZE, "2")
100       .execute()
101       .assertJson("{\n" +
102         "  \"results\":\n" +
103         "  [\n" +
104         "    {\n" +
105         "      \"key\": \"" + project4.getKey() + "\",\n" +
106         "      \"selected\": true\n" +
107         "    },\n" +
108         "    {\n" +
109         "      \"key\": \"" + project1.getKey() + "\",\n" +
110         "      \"selected\": true\n" +
111         "    }\n" +
112         "  ]\n" +
113         "}\n");
114     ws.newRequest()
115       .setParam(PARAM_KEY, qualityProfile.getKee())
116       .setParam("selected", "selected")
117       .setParam(PAGE_SIZE, "2")
118       .setParam(PAGE, "2")
119       .execute()
120       .assertJson("{\n" +
121         "  \"results\":\n" +
122         "  [\n" +
123         "    {\n" +
124         "      \"key\": \"" + project3.getKey() + "\",\n" +
125         "      \"selected\": true\n" +
126         "    },\n" +
127         "    {\n" +
128         "      \"key\": \"" + project2.getKey() + "\",\n" +
129         "      \"selected\": true\n" +
130         "    }\n" +
131         "  ]\n" +
132         "}\n");
133     ws.newRequest()
134       .setParam(PARAM_KEY, qualityProfile.getKee())
135       .setParam("selected", "selected")
136       .setParam(PAGE_SIZE, "2")
137       .setParam(PAGE, "3")
138       .execute()
139       .assertJson("{\"results\":[]}");
140     ws.newRequest()
141       .setParam(PARAM_KEY, qualityProfile.getKee())
142       .setParam("selected", "selected")
143       .setParam(PAGE_SIZE, "2")
144       .setParam(PAGE, "4")
145       .execute()
146       .assertJson("{\"results\":[]}");
147
148     ws.newRequest()
149       .setParam(PARAM_KEY, qualityProfile.getKee())
150       .setParam("selected", "selected")
151       .setParam(PAGE_SIZE, "3")
152       .setParam(PAGE, "1")
153       .execute()
154       .assertJson("{\n" +
155         "  \"results\":\n" +
156         "  [\n" +
157         "    {\n" +
158         "      \"key\": \"" + project4.getKey() + "\",\n" +
159         "      \"selected\": true\n" +
160         "    },\n" +
161         "    {\n" +
162         "      \"key\": \"" + project1.getKey() + "\",\n" +
163         "      \"selected\": true\n" +
164         "    },\n" +
165         "    {\n" +
166         "      \"key\": \"" + project3.getKey() + "\",\n" +
167         "      \"selected\": true\n" +
168         "    }\n" +
169         "  ]\n" +
170         "}\n");
171     ws.newRequest()
172       .setParam(PARAM_KEY, qualityProfile.getKee())
173       .setParam("selected", "selected")
174       .setParam(PAGE_SIZE, "3")
175       .setParam(PAGE, "2")
176       .execute()
177       .assertJson("{\n" +
178         "  \"results\":\n" +
179         "  [\n" +
180         "    {\n" +
181         "      \"key\": \"" + project2.getKey() + "\",\n" +
182         "      \"selected\": true\n" +
183         "    }\n" +
184         "  ]\n" +
185         "}\n");
186     ws.newRequest()
187       .setParam(PARAM_KEY, qualityProfile.getKee())
188       .setParam("selected", "selected")
189       .setParam(PAGE_SIZE, "3")
190       .setParam(PAGE, "3")
191       .execute()
192       .assertJson("{\"results\":[]}");
193   }
194
195   @Test
196   public void show_unselected() {
197     OrganizationDto organization = db.organizations().insert();
198     ComponentDto project1 = db.components().insertPublicProject(organization);
199     ComponentDto project2 = db.components().insertPublicProject(organization);
200     QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
201     associateProjectsWithProfile(qualityProfile, project1);
202
203     ws.newRequest()
204       .setParam(PARAM_KEY, qualityProfile.getKee())
205       .setParam("selected", "deselected")
206       .execute()
207       .assertJson("{ \"results\":\n" +
208         "  [\n" +
209         "    {\n" +
210         "      \"key\": \"" + project2.getKey() + "\",\n" +
211         "      \"selected\": false\n" +
212         "    }\n" +
213         "  ]}");
214   }
215
216   @Test
217   public void show_all() {
218     OrganizationDto organization = db.organizations().insert();
219     ComponentDto project1 = db.components().insertPublicProject(organization, p -> p.setName("Project 1"));
220     ComponentDto project2 = db.components().insertPublicProject(organization, p -> p.setName("Project 2"));
221     ComponentDto project3 = db.components().insertPublicProject(organization, p -> p.setName("Project 3"));
222     ComponentDto project4 = db.components().insertPublicProject(organization, p -> p.setName("Project 4"));
223     QProfileDto qualityProfile1 = db.qualityProfiles().insert(organization);
224     associateProjectsWithProfile(qualityProfile1, project1, project2);
225     QProfileDto qualityProfile2 = db.qualityProfiles().insert(organization);
226     // project3 is associated with P2, must appear as not associated with xooP1
227     associateProjectsWithProfile(qualityProfile2, project3);
228
229     ws.newRequest()
230       .setParam(PARAM_KEY, qualityProfile1.getKee())
231       .setParam("selected", "all")
232       .execute()
233       .assertJson("{\n" +
234         "  \"results\": [\n" +
235         "    {\n" +
236         "      \"key\": \"" + project1.getKey() + "\",\n" +
237         "      \"selected\": true\n" +
238         "    },\n" +
239         "    {\n" +
240         "      \"key\": \"" + project2.getKey() + "\",\n" +
241         "      \"selected\": true\n" +
242         "    },\n" +
243         "    {\n" +
244         "      \"key\": \"" + project3.getKey() + "\",\n" +
245         "      \"selected\": false\n" +
246         "    },\n" +
247         "    {\n" +
248         "      \"key\": \"" + project4.getKey() + "\",\n" +
249         "      \"selected\": false\n" +
250         "    }\n" +
251         "  ]}\n");
252   }
253
254   @Test
255   public void filter_on_name() {
256     OrganizationDto organization = db.organizations().insert();
257     ComponentDto project1 = db.components().insertPublicProject(organization, p -> p.setName("Project One"));
258     ComponentDto project2 = db.components().insertPublicProject(organization, p -> p.setName("Project Two"));
259     ComponentDto project3 = db.components().insertPublicProject(organization, p -> p.setName("Project Three"));
260     ComponentDto project4 = db.components().insertPublicProject(organization, p -> p.setName("Project Four"));
261     QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
262     associateProjectsWithProfile(qualityProfile, project1, project2);
263
264     ws.newRequest()
265       .setParam(PARAM_KEY, qualityProfile.getKee())
266       .setParam("selected", "all")
267       .setParam(TEXT_QUERY, "project t")
268       .execute()
269       .assertJson("{\n" +
270         "  \"results\":\n" +
271         "  [\n" +
272         "    {\n" +
273         "      \"key\": \"" + project3.getKey() + "\",\n" +
274         "      \"name\": \"Project Three\",\n" +
275         "      \"selected\": false\n" +
276         "    },\n" +
277         "    {\n" +
278         "      \"key\": \"" + project2.getKey() + "\",\n" +
279         "      \"name\": \"Project Two\",\n" +
280         "      \"selected\": true\n" +
281         "    }\n" +
282         "  ]}\n");
283   }
284
285   @Test
286   public void return_deprecated_uuid_field() {
287     OrganizationDto organization = db.organizations().insert();
288     ComponentDto project = db.components().insertPublicProject(organization);
289     QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
290     associateProjectsWithProfile(qualityProfile, project);
291
292     ws.newRequest()
293       .setParam(PARAM_KEY, qualityProfile.getKee())
294       .setParam("selected", "all")
295       .execute()
296       .assertJson("{\"results\":\n" +
297         "  [\n" +
298         "    {\n" +
299         "      \"id\": \"" + project.uuid() + "\",\n" +
300         "      \"key\": \"" + project.getKey() + "\",\n" +
301         "    }\n" +
302         "  ]}");
303   }
304
305   @Test
306   public void projects_on_paid_organization() {
307     OrganizationDto organization = db.organizations().insert();
308     ComponentDto project = db.components().insertPublicProject(organization);
309     QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
310     associateProjectsWithProfile(qualityProfile, project);
311     UserDto user = db.users().insertUser();
312     db.organizations().addMember(organization, user);
313     userSession.logIn(user);
314
315     ws.newRequest()
316       .setParam(PARAM_KEY, qualityProfile.getKee())
317       .setParam("selected", "all")
318       .execute()
319       .assertJson("{\"results\":\n" +
320         "  [\n" +
321         "    {\n" +
322         "      \"key\": \"" + project.getKey() + "\",\n" +
323         "    }\n" +
324         "  ]}");
325   }
326
327   @Test
328   public void fail_on_nonexistent_profile() {
329     expectedException.expect(NotFoundException.class);
330
331     ws.newRequest()
332       .setParam(PARAM_KEY, "unknown")
333       .execute();
334   }
335
336   @Test
337   public void fail_on_paid_organization_when_not_member() {
338     OrganizationDto organization = db.organizations().insert(o -> o.setSubscription(PAID));
339     QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
340
341     expectedException.expect(ForbiddenException.class);
342     expectedException.expectMessage(format("You're not member of organization '%s'", organization.getKey()));
343
344     ws.newRequest()
345       .setParam(PARAM_KEY, qualityProfile.getKee())
346       .execute();
347   }
348
349   @Test
350   public void definition() {
351     WebService.Action definition = ws.getDef();
352
353     assertThat(definition.key()).isEqualTo("projects");
354     assertThat(definition.responseExampleAsString()).isNotEmpty();
355     assertThat(definition.params()).extracting(Param::key).containsExactlyInAnyOrder("key", "p", "ps", "q", "selected");
356     Param profile = definition.param("key");
357     assertThat(profile.deprecatedKey()).isNullOrEmpty();
358     assertThat(definition.param("p")).isNotNull();
359     assertThat(definition.param("ps")).isNotNull();
360     Param query = definition.param("q");
361     assertThat(query.deprecatedKey()).isEqualTo("query");
362   }
363
364   private void associateProjectsWithProfile(QProfileDto profile, ComponentDto... projects) {
365     for (ComponentDto project : projects) {
366       db.getDbClient().qualityProfileDao().insertProjectProfileAssociation(db.getSession(), project, profile);
367     }
368     db.commit();
369   }
370 }