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.

OrganizationMembershipTest.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2017 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 it.organization;
  21. import com.sonar.orchestrator.Orchestrator;
  22. import it.Category6Suite;
  23. import org.junit.After;
  24. import org.junit.BeforeClass;
  25. import org.junit.ClassRule;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.junit.rules.ExpectedException;
  29. import org.sonarqube.ws.client.HttpException;
  30. import org.sonarqube.ws.client.WsClient;
  31. import org.sonarqube.ws.client.organization.CreateWsRequest;
  32. import org.sonarqube.ws.client.permission.AddUserWsRequest;
  33. import pageobjects.Navigation;
  34. import pageobjects.organization.MembersPage;
  35. import util.user.UserRule;
  36. import static it.Category6Suite.enableOrganizationsSupport;
  37. import static java.lang.String.format;
  38. import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
  39. import static org.assertj.core.api.Assertions.assertThat;
  40. import static util.ItUtils.deleteOrganizationsIfExists;
  41. import static util.ItUtils.newAdminWsClient;
  42. import static util.ItUtils.newOrganizationKey;
  43. import static util.ItUtils.newUserWsClient;
  44. import static util.ItUtils.setServerProperty;
  45. public class OrganizationMembershipTest {
  46. private static final String KEY = newOrganizationKey();
  47. @ClassRule
  48. public static Orchestrator orchestrator = Category6Suite.ORCHESTRATOR;
  49. @ClassRule
  50. public static UserRule userRule = UserRule.from(orchestrator);
  51. @Rule
  52. public ExpectedException expectedException = ExpectedException.none();
  53. @Rule
  54. public Navigation nav = Navigation.get(orchestrator);
  55. private static WsClient adminClient;
  56. @BeforeClass
  57. public static void setUp() throws Exception {
  58. adminClient = newAdminWsClient(orchestrator);
  59. enableOrganizationsSupport();
  60. setServerProperty(orchestrator, "sonar.organizations.anyoneCanCreate", "true");
  61. deleteOrganizationsIfExists(orchestrator, KEY);
  62. }
  63. @After
  64. public void tearDown() throws Exception {
  65. deleteOrganizationsIfExists(orchestrator, KEY);
  66. }
  67. @Test
  68. public void add_and_remove_member() throws Exception {
  69. String organizationKey = createOrganization();
  70. String login = createUser();
  71. adminClient.organizations().addMember(organizationKey, login);
  72. verifyMembership(login, organizationKey, true);
  73. adminClient.organizations().removeMember(organizationKey, login);
  74. verifyMembership(login, organizationKey, false);
  75. }
  76. @Test
  77. public void remove_organization_admin_member() throws Exception {
  78. String organizationKey = createOrganization();
  79. String login = createUser();
  80. adminClient.organizations().addMember(organizationKey, login);
  81. adminClient.permissions().addUser(new AddUserWsRequest().setLogin(login).setPermission("admin").setOrganization(organizationKey));
  82. verifyMembership(login, organizationKey, true);
  83. adminClient.organizations().removeMember(organizationKey, login);
  84. verifyMembership(login, organizationKey, false);
  85. }
  86. @Test
  87. public void fail_to_remove_organization_admin_member_when_last_admin() throws Exception {
  88. String organizationKey = createOrganization();
  89. String login = createUser();
  90. adminClient.organizations().addMember(organizationKey, login);
  91. adminClient.permissions().addUser(new AddUserWsRequest().setLogin(login).setPermission("admin").setOrganization(organizationKey));
  92. verifyMembership(login, organizationKey, true);
  93. // Admin is the creator of the organization so he was granted with admin permission
  94. adminClient.organizations().removeMember(organizationKey, "admin");
  95. expectedException.expect(HttpException.class);
  96. expectedException.expectMessage("The last administrator member cannot be removed");
  97. adminClient.organizations().removeMember(organizationKey, login);
  98. }
  99. @Test
  100. public void remove_user_remove_its_membership() throws Exception {
  101. String organizationKey = createOrganization();
  102. String login = createUser();
  103. adminClient.organizations().addMember(organizationKey, login);
  104. verifyMembership(login, organizationKey, true);
  105. userRule.deactivateUsers(login);
  106. verifyMembership(login, organizationKey, false);
  107. }
  108. @Test
  109. public void user_creating_an_organization_becomes_member_of_this_organization() throws Exception {
  110. String keyAndName = newOrganizationKey();
  111. String login = createUser();
  112. newUserWsClient(orchestrator, login, login).organizations().create(
  113. new CreateWsRequest.Builder().setKey(keyAndName).setName(keyAndName).build()).getOrganization();
  114. verifyMembership(login, keyAndName, true);
  115. }
  116. @Test
  117. public void should_display_members_page() {
  118. String orgKey = createOrganization();
  119. userRule.createUser("foo", "pwd");
  120. userRule.createUser("bar", "pwd");
  121. createUser();
  122. adminClient.organizations().addMember(orgKey, "foo");
  123. adminClient.organizations().addMember(orgKey, "bar");
  124. MembersPage page = nav.openOrganizationMembers(orgKey);
  125. page
  126. .canNotAddMember()
  127. .shouldHaveTotal(3);
  128. page.getMembersByIdx(0).shouldBeNamed("admin", "Administrator");
  129. page.getMembersByIdx(1).shouldBeNamed("bar", "bar");
  130. page.getMembersByIdx(2)
  131. .shouldBeNamed("foo", "foo")
  132. .shouldNotHaveActions();
  133. }
  134. @Test
  135. public void search_for_members() {
  136. String orgKey = createOrganization();
  137. String user1 = createUser();
  138. String user2 = createUser();
  139. createUser();
  140. adminClient.organizations().addMember(orgKey, user1);
  141. adminClient.organizations().addMember(orgKey, user2);
  142. MembersPage page = nav.openOrganizationMembers(orgKey);
  143. page
  144. .searchForMember("adm")
  145. .shouldHaveTotal(1);
  146. page.getMembersByIdx(0).shouldBeNamed("admin", "Administrator");
  147. page
  148. .searchForMember(user2)
  149. .shouldHaveTotal(1);
  150. page.getMembersByIdx(0).shouldBeNamed(user2, user2);
  151. }
  152. @Test
  153. public void admin_can_add_members() {
  154. String orgKey = createOrganization();
  155. userRule.createUser("foo", "pwd");
  156. createUser();
  157. MembersPage page = nav.logIn().asAdmin().openOrganizationMembers(orgKey);
  158. page
  159. .shouldHaveTotal(1)
  160. .addMember("foo")
  161. .shouldHaveTotal(2);
  162. page.getMembersByIdx(0).shouldBeNamed("admin", "Administrator").shouldHaveGroups(1);
  163. page.getMembersByIdx(1).shouldBeNamed("foo", "foo").shouldHaveGroups(0);
  164. }
  165. @Test
  166. public void admin_can_remove_members() {
  167. String orgKey = createOrganization();
  168. userRule.createUser("foo", "pwd");
  169. userRule.createUser("bar", "pwd");
  170. adminClient.organizations().addMember(orgKey, "foo");
  171. adminClient.organizations().addMember(orgKey, "bar");
  172. MembersPage page = nav.logIn().asAdmin().openOrganizationMembers(orgKey);
  173. page.shouldHaveTotal(3)
  174. .getMembersByIdx(1).removeMembership();
  175. page.shouldHaveTotal(2);
  176. }
  177. @Test
  178. public void admin_can_manage_groups() {
  179. String orgKey = createOrganization();
  180. userRule.createUser("foo", "pwd");
  181. adminClient.organizations().addMember(orgKey, "foo");
  182. MembersPage page = nav.logIn().asAdmin().openOrganizationMembers(orgKey);
  183. page.getMembersByIdx(1)
  184. .manageGroupsOpen()
  185. .manageGroupsSelect("owners")
  186. .manageGroupsSave()
  187. .shouldHaveGroups(1);
  188. page.getMembersByIdx(0)
  189. .manageGroupsOpen()
  190. .manageGroupsSelect("owners")
  191. .manageGroupsSave()
  192. .shouldHaveGroups(0);
  193. }
  194. @Test
  195. public void groups_count_should_be_updated_when_a_member_was_just_added() {
  196. String orgKey = createOrganization();
  197. userRule.createUser("foo", "pwd");
  198. MembersPage page = nav.logIn().asAdmin().openOrganizationMembers(orgKey);
  199. page
  200. .addMember("foo")
  201. .getMembersByIdx(1)
  202. .shouldHaveGroups(0)
  203. .manageGroupsOpen()
  204. .manageGroupsSelect("owners")
  205. .manageGroupsSave()
  206. .shouldHaveGroups(1);
  207. }
  208. private void verifyMembership(String login, String organizationKey, boolean isMember) {
  209. // TODO replace with search member WS
  210. int count = orchestrator.getDatabase().countSql(format("SELECT COUNT(1) FROM organization_members om " +
  211. "INNER JOIN users u ON u.id=om.user_id AND u.login='%s' " +
  212. "INNER JOIN organizations o ON o.uuid=om.organization_uuid AND o.kee='%s'", login, organizationKey));
  213. assertThat(count).isEqualTo(isMember ? 1 : 0);
  214. }
  215. private static String createOrganization() {
  216. adminClient.organizations().create(new CreateWsRequest.Builder().setKey(KEY).setName(KEY).build()).getOrganization();
  217. return KEY;
  218. }
  219. private static String createUser() {
  220. String login = randomAlphabetic(10).toLowerCase();
  221. userRule.createUser(login, login);
  222. return login;
  223. }
  224. }