]> source.dussan.org Git - sonarqube.git/blob
319cc795a37cdb0bff1d281aa0efebd685bcb6a4
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 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.resources.Languages;
26 import org.sonar.api.server.ws.WebService;
27 import org.sonar.db.DbTester;
28 import org.sonar.db.permission.GlobalPermission;
29 import org.sonar.db.qualityprofile.QProfileDto;
30 import org.sonar.db.user.GroupDto;
31 import org.sonar.db.user.UserDto;
32 import org.sonar.server.exceptions.ForbiddenException;
33 import org.sonar.server.exceptions.NotFoundException;
34 import org.sonar.server.language.LanguageTesting;
35 import org.sonar.server.tester.UserSessionRule;
36 import org.sonar.server.ws.WsActionTester;
37 import org.sonarqube.ws.Qualityprofiles.SearchGroupsResponse;
38
39 import static java.lang.String.format;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.assertj.core.api.Assertions.tuple;
42 import static org.sonar.api.server.ws.WebService.Param.PAGE;
43 import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
44 import static org.sonar.api.server.ws.WebService.Param.SELECTED;
45 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
46 import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES;
47 import static org.sonar.db.user.GroupTesting.newGroupDto;
48 import static org.sonar.test.JsonAssert.assertJson;
49 import static org.sonarqube.ws.MediaTypes.JSON;
50 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
51 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_QUALITY_PROFILE;
52
53 public class SearchGroupsActionTest {
54   private static final String XOO = "xoo";
55   private static final String FOO = "foo";
56   private static final Languages LANGUAGES = LanguageTesting.newLanguages(XOO, FOO);
57
58   @Rule
59   public ExpectedException expectedException = ExpectedException.none();
60   @Rule
61   public UserSessionRule userSession = UserSessionRule.standalone();
62   @Rule
63   public DbTester db = DbTester.create();
64
65   private final QProfileWsSupport wsSupport = new QProfileWsSupport(db.getDbClient(), userSession);
66
67   private WsActionTester ws = new WsActionTester(new SearchGroupsAction(db.getDbClient(), wsSupport, LANGUAGES));
68
69   @Test
70   public void test_definition() {
71     WebService.Action def = ws.getDef();
72     assertThat(def.key()).isEqualTo("search_groups");
73     assertThat(def.isPost()).isFalse();
74     assertThat(def.isInternal()).isTrue();
75     assertThat(def.params()).extracting(WebService.Param::key)
76       .containsExactlyInAnyOrder("qualityProfile", "language", "selected", "q", "p", "ps");
77   }
78
79   @Test
80   public void test_example() {
81     QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
82     GroupDto group1 = db.users().insertGroup(newGroupDto().setName("users").setDescription("Users"));
83     GroupDto group2 = db.users().insertGroup(newGroupDto().setName("administrators").setDescription("Administrators"));
84     db.qualityProfiles().addGroupPermission(profile, group1);
85     userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
86
87     String result = ws.newRequest()
88       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
89       .setParam(PARAM_LANGUAGE, XOO)
90       .setParam(SELECTED, "all")
91       .setMediaType(JSON)
92       .execute()
93       .getInput();
94
95     assertJson(ws.getDef().responseExampleAsString()).isSimilarTo(result);
96   }
97
98   @Test
99   public void search_all_groups() {
100     QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
101     GroupDto group1 = db.users().insertGroup();
102     GroupDto group2 = db.users().insertGroup();
103     db.qualityProfiles().addGroupPermission(profile, group1);
104     userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
105
106     SearchGroupsResponse response = ws.newRequest()
107       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
108       .setParam(PARAM_LANGUAGE, XOO)
109       .setParam(SELECTED, "all")
110       .executeProtobuf(SearchGroupsResponse.class);
111
112     assertThat(response.getGroupsList()).extracting(SearchGroupsResponse.Group::getName, SearchGroupsResponse.Group::getDescription, SearchGroupsResponse.Group::getSelected)
113       .containsExactlyInAnyOrder(
114         tuple(group1.getName(), group1.getDescription(), true),
115         tuple(group2.getName(), group2.getDescription(), false));
116   }
117
118   @Test
119   public void search_selected_groups() {
120     QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
121     GroupDto group1 = db.users().insertGroup();
122     GroupDto group2 = db.users().insertGroup();
123     db.qualityProfiles().addGroupPermission(profile, group1);
124     userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
125
126     SearchGroupsResponse response = ws.newRequest()
127       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
128       .setParam(PARAM_LANGUAGE, XOO)
129       .setParam(SELECTED, "selected")
130       .executeProtobuf(SearchGroupsResponse.class);
131
132     assertThat(response.getGroupsList()).extracting(SearchGroupsResponse.Group::getName, SearchGroupsResponse.Group::getDescription, SearchGroupsResponse.Group::getSelected)
133       .containsExactlyInAnyOrder(
134         tuple(group1.getName(), group1.getDescription(), true));
135   }
136
137   @Test
138   public void search_deselected_groups() {
139     QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
140     GroupDto group1 = db.users().insertGroup();
141     GroupDto group2 = db.users().insertGroup();
142     db.qualityProfiles().addGroupPermission(profile, group1);
143     userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
144
145     SearchGroupsResponse response = ws.newRequest()
146       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
147       .setParam(PARAM_LANGUAGE, XOO)
148       .setParam(SELECTED, "deselected")
149       .executeProtobuf(SearchGroupsResponse.class);
150
151     assertThat(response.getGroupsList()).extracting(SearchGroupsResponse.Group::getName, SearchGroupsResponse.Group::getDescription, SearchGroupsResponse.Group::getSelected)
152       .containsExactlyInAnyOrder(
153         tuple(group2.getName(), group2.getDescription(), false));
154   }
155
156   @Test
157   public void search_by_name() {
158     QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
159     GroupDto group1 = db.users().insertGroup("sonar-users-project");
160     GroupDto group2 = db.users().insertGroup("sonar-users-qprofile");
161     GroupDto group3 = db.users().insertGroup("sonar-admin");
162     db.qualityProfiles().addGroupPermission(profile, group1);
163     db.qualityProfiles().addGroupPermission(profile, group2);
164     db.qualityProfiles().addGroupPermission(profile, group3);
165     userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
166
167     SearchGroupsResponse response = ws.newRequest()
168       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
169       .setParam(PARAM_LANGUAGE, XOO)
170       .setParam(TEXT_QUERY, "UsErS")
171       .setParam(SELECTED, "all")
172       .executeProtobuf(SearchGroupsResponse.class);
173
174     assertThat(response.getGroupsList()).extracting(SearchGroupsResponse.Group::getName)
175       .containsExactlyInAnyOrder(group1.getName(), group2.getName());
176   }
177
178   @Test
179   public void group_without_description() {
180     QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
181     GroupDto group = db.users().insertGroup(newGroupDto().setDescription(null));
182     db.qualityProfiles().addGroupPermission(profile, group);
183     userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
184
185     SearchGroupsResponse response = ws.newRequest()
186       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
187       .setParam(PARAM_LANGUAGE, XOO)
188       .setParam(SELECTED, "all")
189       .executeProtobuf(SearchGroupsResponse.class);
190
191     assertThat(response.getGroupsList()).extracting(SearchGroupsResponse.Group::getName, SearchGroupsResponse.Group::hasDescription)
192       .containsExactlyInAnyOrder(tuple(group.getName(), false));
193   }
194
195   @Test
196   public void paging_search() {
197     QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
198     GroupDto group3 = db.users().insertGroup("group3");
199     GroupDto group1 = db.users().insertGroup("group1");
200     GroupDto group2 = db.users().insertGroup("group2");
201     db.qualityProfiles().addGroupPermission(profile, group1);
202     db.qualityProfiles().addGroupPermission(profile, group2);
203     userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
204
205     assertThat(ws.newRequest()
206       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
207       .setParam(PARAM_LANGUAGE, XOO)
208       .setParam(SELECTED, "all")
209       .setParam(PAGE, "1")
210       .setParam(PAGE_SIZE, "1")
211       .executeProtobuf(SearchGroupsResponse.class).getGroupsList())
212         .extracting(SearchGroupsResponse.Group::getName)
213         .containsExactly(group1.getName());
214
215     assertThat(ws.newRequest()
216       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
217       .setParam(PARAM_LANGUAGE, XOO)
218       .setParam(SELECTED, "all")
219       .setParam(PAGE, "3")
220       .setParam(PAGE_SIZE, "1")
221       .executeProtobuf(SearchGroupsResponse.class).getGroupsList())
222         .extracting(SearchGroupsResponse.Group::getName)
223         .containsExactly(group3.getName());
224
225     assertThat(ws.newRequest()
226       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
227       .setParam(PARAM_LANGUAGE, XOO)
228       .setParam(SELECTED, "all")
229       .setParam(PAGE, "1")
230       .setParam(PAGE_SIZE, "10")
231       .executeProtobuf(SearchGroupsResponse.class).getGroupsList())
232         .extracting(SearchGroupsResponse.Group::getName)
233         .containsExactly(group1.getName(), group2.getName(), group3.getName());
234   }
235
236   @Test
237   public void uses_default_organization_when_no_organization() {
238     QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
239     GroupDto group = db.users().insertGroup();
240     db.qualityProfiles().addGroupPermission(profile, group);
241     userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
242
243     SearchGroupsResponse response = ws.newRequest()
244       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
245       .setParam(PARAM_LANGUAGE, XOO)
246       .setParam(SELECTED, "all")
247       .executeProtobuf(SearchGroupsResponse.class);
248
249     assertThat(response.getGroupsList()).extracting(SearchGroupsResponse.Group::getName).containsExactlyInAnyOrder(group.getName());
250   }
251
252   @Test
253   public void qp_administers_can_search_groups() {
254     QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
255     GroupDto group = db.users().insertGroup();
256     db.qualityProfiles().addGroupPermission(profile, group);
257     userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
258
259     SearchGroupsResponse response = ws.newRequest()
260       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
261       .setParam(PARAM_LANGUAGE, XOO)
262       .setParam(SELECTED, "all")
263       .executeProtobuf(SearchGroupsResponse.class);
264
265     assertThat(response.getGroupsList()).extracting(SearchGroupsResponse.Group::getName).containsExactlyInAnyOrder(group.getName());
266   }
267
268   @Test
269   public void qp_editors_can_search_groups() {
270     QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
271     GroupDto group = db.users().insertGroup();
272     db.qualityProfiles().addGroupPermission(profile, group);
273     UserDto userAllowedToEditProfile = db.users().insertUser();
274     db.qualityProfiles().addUserPermission(profile, userAllowedToEditProfile);
275     userSession.logIn(userAllowedToEditProfile);
276
277     SearchGroupsResponse response = ws.newRequest()
278       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
279       .setParam(PARAM_LANGUAGE, XOO)
280       .setParam(SELECTED, "all")
281       .executeProtobuf(SearchGroupsResponse.class);
282
283     assertThat(response.getGroupsList()).extracting(SearchGroupsResponse.Group::getName).containsExactlyInAnyOrder(group.getName());
284   }
285
286   @Test
287   public void fail_when_qprofile_does_not_exist() {
288     userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
289
290     expectedException.expect(NotFoundException.class);
291     expectedException.expectMessage("Quality Profile for language 'xoo' and name 'unknown' does not exist");
292
293     ws.newRequest()
294       .setParam(PARAM_QUALITY_PROFILE, "unknown")
295       .setParam(PARAM_LANGUAGE, XOO)
296       .execute();
297   }
298
299   @Test
300   public void fail_when_wrong_language() {
301     QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
302     userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
303
304     expectedException.expect(NotFoundException.class);
305     expectedException.expectMessage(format("Quality Profile for language 'foo' and name '%s' does not exist", profile.getName()));
306
307     ws.newRequest()
308       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
309       .setParam(PARAM_LANGUAGE, FOO)
310       .executeProtobuf(SearchGroupsResponse.class);
311   }
312
313   @Test
314   public void fail_when_not_enough_permission() {
315     QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
316     userSession.logIn(db.users().insertUser()).addPermission(GlobalPermission.ADMINISTER_QUALITY_GATES);
317
318     expectedException.expect(ForbiddenException.class);
319
320     ws.newRequest()
321       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
322       .setParam(PARAM_LANGUAGE, XOO)
323       .execute();
324   }
325 }