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.

GroupsActionTest.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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. * SonarQube 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.permission.ws;
  21. import com.google.common.io.Resources;
  22. import org.junit.Before;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.junit.experimental.categories.Category;
  26. import org.junit.rules.ExpectedException;
  27. import org.sonar.api.security.DefaultGroups;
  28. import org.sonar.api.server.ws.WebService.Param;
  29. import org.sonar.api.server.ws.WebService.SelectionMode;
  30. import org.sonar.api.utils.System2;
  31. import org.sonar.core.permission.GlobalPermissions;
  32. import org.sonar.db.DbClient;
  33. import org.sonar.db.DbSession;
  34. import org.sonar.db.DbTester;
  35. import org.sonar.db.user.GroupDto;
  36. import org.sonar.db.user.GroupRoleDto;
  37. import org.sonar.server.exceptions.ForbiddenException;
  38. import org.sonar.server.exceptions.UnauthorizedException;
  39. import org.sonar.server.permission.PermissionFinder;
  40. import org.sonar.server.tester.UserSessionRule;
  41. import org.sonar.server.ws.WsActionTester;
  42. import org.sonar.test.DbTests;
  43. import static org.assertj.core.api.Assertions.assertThat;
  44. import static org.sonar.test.JsonAssert.assertJson;
  45. @Category(DbTests.class)
  46. public class GroupsActionTest {
  47. @Rule
  48. public ExpectedException expectedException = ExpectedException.none();
  49. @Rule
  50. public UserSessionRule userSession = UserSessionRule.standalone();
  51. @Rule
  52. public DbTester db = DbTester.create(System2.INSTANCE);
  53. DbClient dbClient;
  54. DbSession dbSession;
  55. WsActionTester ws;
  56. PermissionFinder permissionFinder;
  57. GroupsAction underTest;
  58. @Before
  59. public void setUp() {
  60. dbClient = db.getDbClient();
  61. dbSession = db.getSession();
  62. permissionFinder = new PermissionFinder(dbClient);
  63. userSession.login("login").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
  64. underTest = new GroupsAction(userSession, permissionFinder);
  65. ws = new WsActionTester(underTest);
  66. GroupDto group1 = dbClient.groupDao().insert(dbSession, new GroupDto()
  67. .setName("group-1-name")
  68. .setDescription("group-1-description"));
  69. GroupDto group2 = dbClient.groupDao().insert(dbSession, new GroupDto()
  70. .setName("group-2-name")
  71. .setDescription("group-2-description"));
  72. GroupDto group3 = dbClient.groupDao().insert(dbSession, new GroupDto()
  73. .setName("group-3-name")
  74. .setDescription("group-3-description"));
  75. dbClient.roleDao().insertGroupRole(dbSession, new GroupRoleDto()
  76. .setGroupId(group1.getId())
  77. .setRole(GlobalPermissions.SCAN_EXECUTION));
  78. dbClient.roleDao().insertGroupRole(dbSession, new GroupRoleDto()
  79. .setGroupId(group2.getId())
  80. .setRole(GlobalPermissions.SCAN_EXECUTION));
  81. dbClient.roleDao().insertGroupRole(dbSession, new GroupRoleDto()
  82. .setGroupId(group3.getId())
  83. .setRole(GlobalPermissions.SYSTEM_ADMIN));
  84. dbSession.commit();
  85. }
  86. @Test
  87. public void search_for_groups_with_one_permission() {
  88. String result = ws.newRequest()
  89. .setParam("permission", "scan")
  90. .execute().getInput();
  91. assertJson(result).isSimilarTo(Resources.getResource(getClass(), "GroupsActionTest/groups.json"));
  92. }
  93. @Test
  94. public void search_with_selection() {
  95. String result = ws.newRequest()
  96. .setParam("permission", "scan")
  97. .setParam("selected", SelectionMode.ALL.value())
  98. .execute().getInput();
  99. assertThat(result).containsSequence(DefaultGroups.ANYONE, "group-1", "group-2", "group-3");
  100. }
  101. @Test
  102. public void search_groups_with_pagination() {
  103. String result = ws.newRequest()
  104. .setParam("permission", "scan")
  105. .setParam(Param.PAGE_SIZE, "1")
  106. .setParam(Param.PAGE, "2")
  107. .execute().getInput();
  108. assertThat(result).contains("group-2")
  109. .doesNotContain("group-1")
  110. .doesNotContain("group-3");
  111. }
  112. @Test
  113. public void search_groups_with_query() {
  114. String result = ws.newRequest()
  115. .setParam("permission", "scan")
  116. .setParam(Param.TEXT_QUERY, "group-")
  117. .execute().getInput();
  118. assertThat(result)
  119. .contains("group-1", "group-2", "group-3")
  120. .doesNotContain(DefaultGroups.ANYONE);
  121. }
  122. @Test
  123. public void fail_if_not_logged_in() {
  124. expectedException.expect(UnauthorizedException.class);
  125. userSession.anonymous();
  126. ws.newRequest()
  127. .setParam("permission", "scan")
  128. .execute();
  129. }
  130. @Test
  131. public void fail_if_insufficient_privileges() {
  132. expectedException.expect(ForbiddenException.class);
  133. userSession.login("login");
  134. ws.newRequest()
  135. .setParam("permission", "scan")
  136. .execute();
  137. }
  138. @Test
  139. public void fail_if_permission_is_not_specified() {
  140. expectedException.expect(IllegalArgumentException.class);
  141. ws.newRequest()
  142. .execute();
  143. }
  144. }