You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SearchActionTest.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.usergroups.ws;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.junit.rules.ExpectedException;
  24. import org.sonar.api.server.ws.Change;
  25. import org.sonar.api.server.ws.WebService;
  26. import org.sonar.api.utils.System2;
  27. import org.sonar.db.DbTester;
  28. import org.sonar.db.organization.OrganizationDto;
  29. import org.sonar.db.user.GroupDto;
  30. import org.sonar.db.user.UserDto;
  31. import org.sonar.server.exceptions.UnauthorizedException;
  32. import org.sonar.server.organization.TestDefaultOrganizationProvider;
  33. import org.sonar.server.tester.UserSessionRule;
  34. import org.sonar.server.usergroups.DefaultGroupFinder;
  35. import org.sonar.server.ws.TestRequest;
  36. import org.sonar.server.ws.WsActionTester;
  37. import org.sonarqube.ws.Common.Paging;
  38. import org.sonarqube.ws.MediaTypes;
  39. import static org.apache.commons.lang.StringUtils.capitalize;
  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.FIELDS;
  43. import static org.sonar.api.server.ws.WebService.Param.PAGE;
  44. import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
  45. import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
  46. import static org.sonar.db.permission.OrganizationPermission.ADMINISTER;
  47. import static org.sonar.db.user.GroupTesting.newGroupDto;
  48. import static org.sonar.test.JsonAssert.assertJson;
  49. import static org.sonarqube.ws.UserGroups.Group;
  50. import static org.sonarqube.ws.UserGroups.SearchWsResponse;
  51. public class SearchActionTest {
  52. @Rule
  53. public DbTester db = DbTester.create(System2.INSTANCE);
  54. @Rule
  55. public UserSessionRule userSession = UserSessionRule.standalone();
  56. @Rule
  57. public ExpectedException expectedException = ExpectedException.none();
  58. private final WsActionTester ws = new WsActionTester(new SearchAction(db.getDbClient(), userSession,
  59. new DefaultGroupFinder(db.getDbClient())));
  60. @Test
  61. public void define_search_action() {
  62. WebService.Action action = ws.getDef();
  63. assertThat(action).isNotNull();
  64. assertThat(action.key()).isEqualTo("search");
  65. assertThat(action.responseExampleAsString()).isNotEmpty();
  66. assertThat(action.params()).hasSize(4);
  67. assertThat(action.changelog()).extracting(Change::getVersion, Change::getDescription).containsOnly(
  68. tuple("8.4", "Field 'id' in the response is deprecated. Format changes from integer to string."),
  69. tuple("6.4", "Paging response fields moved to a Paging object"),
  70. tuple("6.4", "'default' response field has been added"));
  71. }
  72. @Test
  73. public void search_without_parameters() {
  74. insertDefaultGroup(0);
  75. insertGroup("admins", 0);
  76. insertGroup("customer1", 0);
  77. insertGroup("customer2", 0);
  78. insertGroup("customer3", 0);
  79. loginAsAdmin();
  80. SearchWsResponse response = call(ws.newRequest());
  81. assertThat(response.getGroupsList()).extracting(Group::getName, Group::getDescription, Group::getMembersCount).containsOnly(
  82. tuple("admins", "Admins", 0),
  83. tuple("customer1", "Customer1", 0),
  84. tuple("customer2", "Customer2", 0),
  85. tuple("customer3", "Customer3", 0),
  86. tuple("sonar-users", "Users", 0));
  87. }
  88. @Test
  89. public void search_with_members() {
  90. insertDefaultGroup(5);
  91. insertGroup("admins", 1);
  92. insertGroup("customer1", 0);
  93. insertGroup("customer2", 4);
  94. insertGroup("customer3", 0);
  95. loginAsAdmin();
  96. SearchWsResponse response = call(ws.newRequest());
  97. assertThat(response.getGroupsList()).extracting(Group::getName, Group::getDescription, Group::getMembersCount).containsOnly(
  98. tuple("admins", "Admins", 1),
  99. tuple("customer1", "Customer1", 0),
  100. tuple("customer2", "Customer2", 4),
  101. tuple("customer3", "Customer3", 0),
  102. tuple("sonar-users", "Users", 5));
  103. }
  104. @Test
  105. public void search_with_query() {
  106. insertDefaultGroup(0);
  107. insertGroup("admins", 0);
  108. insertGroup("customer%_%/1", 0);
  109. insertGroup("customer%_%/2", 0);
  110. insertGroup("customer%_%/3", 0);
  111. loginAsAdmin();
  112. SearchWsResponse response = call(ws.newRequest().setParam(TEXT_QUERY, "tomer%_%/"));
  113. assertThat(response.getGroupsList()).extracting(Group::getName, Group::getDescription, Group::getMembersCount).containsOnly(
  114. tuple("customer%_%/1", "Customer%_%/1", 0),
  115. tuple("customer%_%/2", "Customer%_%/2", 0),
  116. tuple("customer%_%/3", "Customer%_%/3", 0));
  117. }
  118. @Test
  119. public void search_with_paging() {
  120. insertDefaultGroup(0);
  121. insertGroup("admins", 0);
  122. insertGroup("customer1", 0);
  123. insertGroup("customer2", 0);
  124. insertGroup("customer3", 0);
  125. loginAsAdmin();
  126. SearchWsResponse response = call(ws.newRequest().setParam(PAGE_SIZE, "3"));
  127. assertThat(response.getPaging()).extracting(Paging::getPageIndex, Paging::getPageSize, Paging::getTotal).containsOnly(1, 3, 5);
  128. assertThat(response.getGroupsList()).extracting(Group::getName, Group::getDescription, Group::getMembersCount).containsOnly(
  129. tuple("admins", "Admins", 0),
  130. tuple("customer1", "Customer1", 0),
  131. tuple("customer2", "Customer2", 0));
  132. response = call(ws.newRequest().setParam(PAGE_SIZE, "3").setParam(PAGE, "2"));
  133. assertThat(response.getPaging()).extracting(Paging::getPageIndex, Paging::getPageSize, Paging::getTotal).containsOnly(2, 3, 5);
  134. assertThat(response.getGroupsList()).extracting(Group::getName, Group::getDescription, Group::getMembersCount).containsOnly(
  135. tuple("customer3", "Customer3", 0),
  136. tuple("sonar-users", "Users", 0));
  137. response = call(ws.newRequest().setParam(PAGE_SIZE, "3").setParam(PAGE, "3"));
  138. assertThat(response.getPaging()).extracting(Paging::getPageIndex, Paging::getPageSize, Paging::getTotal).containsOnly(3, 3, 5);
  139. assertThat(response.getGroupsList()).isEmpty();
  140. }
  141. @Test
  142. public void search_with_fields() {
  143. insertDefaultGroup(0);
  144. loginAsAdmin();
  145. assertThat(call(ws.newRequest()).getGroupsList()).extracting(Group::hasId, Group::hasName, Group::hasDescription, Group::hasMembersCount)
  146. .containsOnly(tuple(true, true, true, true));
  147. assertThat(call(ws.newRequest().setParam(FIELDS, "")).getGroupsList()).extracting(Group::hasId, Group::hasName, Group::hasDescription, Group::hasMembersCount)
  148. .containsOnly(tuple(true, true, true, true));
  149. assertThat(call(ws.newRequest().setParam(FIELDS, "name")).getGroupsList()).extracting(Group::hasId, Group::hasName, Group::hasDescription, Group::hasMembersCount)
  150. .containsOnly(tuple(true, true, false, false));
  151. assertThat(call(ws.newRequest().setParam(FIELDS, "description")).getGroupsList()).extracting(Group::hasId, Group::hasName, Group::hasDescription, Group::hasMembersCount)
  152. .containsOnly(tuple(true, false, true, false));
  153. assertThat(call(ws.newRequest().setParam(FIELDS, "membersCount")).getGroupsList()).extracting(Group::hasId, Group::hasName, Group::hasDescription, Group::hasMembersCount)
  154. .containsOnly(tuple(true, false, false, true));
  155. }
  156. @Test
  157. public void return_default_group() {
  158. db.users().insertDefaultGroup();
  159. loginAsAdmin();
  160. SearchWsResponse response = call(ws.newRequest());
  161. assertThat(response.getGroupsList()).extracting(Group::getName, Group::getDefault).containsOnly(tuple("sonar-users", true));
  162. }
  163. @Test
  164. public void fail_when_not_logged_in() {
  165. userSession.anonymous();
  166. expectedException.expect(UnauthorizedException.class);
  167. call(ws.newRequest());
  168. }
  169. @Test
  170. public void test_json_example() {
  171. insertDefaultGroup(17);
  172. insertGroup("administrators", 2);
  173. loginAsAdmin();
  174. String response = ws.newRequest().setMediaType(MediaTypes.JSON).execute().getInput();
  175. assertJson(response).ignoreFields("id").isSimilarTo(ws.getDef().responseExampleAsString());
  176. }
  177. @Test
  178. public void verify_definition() {
  179. WebService.Action action = ws.getDef();
  180. assertThat(action.since()).isEqualTo("5.2");
  181. assertThat(action.isPost()).isFalse();
  182. assertThat(action.isInternal()).isFalse();
  183. assertThat(action.responseExampleAsString()).isNotEmpty();
  184. assertThat(action.params()).extracting(WebService.Param::key).containsOnly("p", "q", "ps", "f");
  185. assertThat(action.param("f").possibleValues()).containsOnly("name", "description", "membersCount");
  186. }
  187. private SearchWsResponse call(TestRequest request) {
  188. return request.executeProtobuf(SearchWsResponse.class);
  189. }
  190. private void insertDefaultGroup(int numberOfMembers) {
  191. GroupDto group = db.users().insertDefaultGroup();
  192. addMembers(group, numberOfMembers);
  193. }
  194. private void insertGroup(String name, int numberOfMembers) {
  195. GroupDto group = newGroupDto().setName(name).setDescription(capitalize(name));
  196. db.users().insertGroup(group);
  197. addMembers(group, numberOfMembers);
  198. }
  199. private void addMembers(GroupDto group, int numberOfMembers) {
  200. for (int i = 0; i < numberOfMembers; i++) {
  201. UserDto user = db.users().insertUser();
  202. db.users().insertMember(group, user);
  203. }
  204. }
  205. private void loginAsAdmin() {
  206. userSession.logIn("user").addPermission(ADMINISTER);
  207. }
  208. }