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