123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- /*
- * SonarQube
- * Copyright (C) 2009-2017 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-
- package it.organization;
-
- import com.sonar.orchestrator.Orchestrator;
- import it.Category6Suite;
- import org.junit.After;
- import org.junit.BeforeClass;
- import org.junit.ClassRule;
- import org.junit.Rule;
- import org.junit.Test;
- import org.junit.rules.ExpectedException;
- import org.sonarqube.ws.client.HttpException;
- import org.sonarqube.ws.client.WsClient;
- import org.sonarqube.ws.client.organization.CreateWsRequest;
- import org.sonarqube.ws.client.permission.AddUserWsRequest;
- import pageobjects.Navigation;
- import pageobjects.organization.MembersPage;
- import util.user.UserRule;
-
- import static it.Category6Suite.enableOrganizationsSupport;
- import static java.lang.String.format;
- import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
- import static org.assertj.core.api.Assertions.assertThat;
- import static util.ItUtils.deleteOrganizationsIfExists;
- import static util.ItUtils.newAdminWsClient;
- import static util.ItUtils.newOrganizationKey;
- import static util.ItUtils.newUserWsClient;
- import static util.ItUtils.setServerProperty;
-
- public class OrganizationMembershipTest {
-
- private static final String KEY = newOrganizationKey();
-
- @ClassRule
- public static Orchestrator orchestrator = Category6Suite.ORCHESTRATOR;
-
- @ClassRule
- public static UserRule userRule = UserRule.from(orchestrator);
-
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
- @Rule
- public Navigation nav = Navigation.get(orchestrator);
-
- private static WsClient adminClient;
-
- @BeforeClass
- public static void setUp() throws Exception {
- adminClient = newAdminWsClient(orchestrator);
- enableOrganizationsSupport();
- setServerProperty(orchestrator, "sonar.organizations.anyoneCanCreate", "true");
- deleteOrganizationsIfExists(orchestrator, KEY);
- }
-
- @After
- public void tearDown() throws Exception {
- deleteOrganizationsIfExists(orchestrator, KEY);
- }
-
- @Test
- public void add_and_remove_member() throws Exception {
- String organizationKey = createOrganization();
- String login = createUser();
- adminClient.organizations().addMember(organizationKey, login);
- verifyMembership(login, organizationKey, true);
-
- adminClient.organizations().removeMember(organizationKey, login);
- verifyMembership(login, organizationKey, false);
- }
-
- @Test
- public void remove_organization_admin_member() throws Exception {
- String organizationKey = createOrganization();
- String login = createUser();
- adminClient.organizations().addMember(organizationKey, login);
- adminClient.permissions().addUser(new AddUserWsRequest().setLogin(login).setPermission("admin").setOrganization(organizationKey));
- verifyMembership(login, organizationKey, true);
-
- adminClient.organizations().removeMember(organizationKey, login);
- verifyMembership(login, organizationKey, false);
- }
-
- @Test
- public void fail_to_remove_organization_admin_member_when_last_admin() throws Exception {
- String organizationKey = createOrganization();
- String login = createUser();
- adminClient.organizations().addMember(organizationKey, login);
- adminClient.permissions().addUser(new AddUserWsRequest().setLogin(login).setPermission("admin").setOrganization(organizationKey));
- verifyMembership(login, organizationKey, true);
- // Admin is the creator of the organization so he was granted with admin permission
- adminClient.organizations().removeMember(organizationKey, "admin");
-
- expectedException.expect(HttpException.class);
- expectedException.expectMessage("The last administrator member cannot be removed");
-
- adminClient.organizations().removeMember(organizationKey, login);
- }
-
- @Test
- public void remove_user_remove_its_membership() throws Exception {
- String organizationKey = createOrganization();
- String login = createUser();
- adminClient.organizations().addMember(organizationKey, login);
- verifyMembership(login, organizationKey, true);
-
- userRule.deactivateUsers(login);
- verifyMembership(login, organizationKey, false);
- }
-
- @Test
- public void user_creating_an_organization_becomes_member_of_this_organization() throws Exception {
- String keyAndName = newOrganizationKey();
- String login = createUser();
-
- newUserWsClient(orchestrator, login, login).organizations().create(
- new CreateWsRequest.Builder().setKey(keyAndName).setName(keyAndName).build()).getOrganization();
-
- verifyMembership(login, keyAndName, true);
- }
-
- @Test
- public void should_display_members_page() {
- String orgKey = createOrganization();
- userRule.createUser("foo", "pwd");
- userRule.createUser("bar", "pwd");
- createUser();
-
- adminClient.organizations().addMember(orgKey, "foo");
- adminClient.organizations().addMember(orgKey, "bar");
-
- MembersPage page = nav.openOrganizationMembers(orgKey);
- page
- .canNotAddMember()
- .shouldHaveTotal(3);
- page.getMembersByIdx(0).shouldBeNamed("admin", "Administrator");
- page.getMembersByIdx(1).shouldBeNamed("bar", "bar");
- page.getMembersByIdx(2)
- .shouldBeNamed("foo", "foo")
- .shouldNotHaveActions();
- }
-
- @Test
- public void search_for_members() {
- String orgKey = createOrganization();
- String user1 = createUser();
- String user2 = createUser();
- createUser();
-
- adminClient.organizations().addMember(orgKey, user1);
- adminClient.organizations().addMember(orgKey, user2);
-
- MembersPage page = nav.openOrganizationMembers(orgKey);
- page
- .searchForMember("adm")
- .shouldHaveTotal(1);
- page.getMembersByIdx(0).shouldBeNamed("admin", "Administrator");
- page
- .searchForMember(user2)
- .shouldHaveTotal(1);
- page.getMembersByIdx(0).shouldBeNamed(user2, user2);
- }
-
- @Test
- public void admin_can_add_members() {
- String orgKey = createOrganization();
- userRule.createUser("foo", "pwd");
- createUser();
-
- MembersPage page = nav.logIn().asAdmin().openOrganizationMembers(orgKey);
- page
- .shouldHaveTotal(1)
- .addMember("foo")
- .shouldHaveTotal(2);
- page.getMembersByIdx(0).shouldBeNamed("admin", "Administrator").shouldHaveGroups(1);
- page.getMembersByIdx(1).shouldBeNamed("foo", "foo").shouldHaveGroups(0);
- }
-
- @Test
- public void admin_can_remove_members() {
- String orgKey = createOrganization();
- userRule.createUser("foo", "pwd");
- userRule.createUser("bar", "pwd");
-
- adminClient.organizations().addMember(orgKey, "foo");
- adminClient.organizations().addMember(orgKey, "bar");
-
- MembersPage page = nav.logIn().asAdmin().openOrganizationMembers(orgKey);
- page.shouldHaveTotal(3)
- .getMembersByIdx(1).removeMembership();
- page.shouldHaveTotal(2);
- }
-
- @Test
- public void admin_can_manage_groups() {
- String orgKey = createOrganization();
- userRule.createUser("foo", "pwd");
-
- adminClient.organizations().addMember(orgKey, "foo");
-
- MembersPage page = nav.logIn().asAdmin().openOrganizationMembers(orgKey);
- page.getMembersByIdx(1)
- .manageGroupsOpen()
- .manageGroupsSelect("owners")
- .manageGroupsSave()
- .shouldHaveGroups(1);
- page.getMembersByIdx(0)
- .manageGroupsOpen()
- .manageGroupsSelect("owners")
- .manageGroupsSave()
- .shouldHaveGroups(0);
- }
-
- @Test
- public void groups_count_should_be_updated_when_a_member_was_just_added() {
- String orgKey = createOrganization();
- userRule.createUser("foo", "pwd");
-
- MembersPage page = nav.logIn().asAdmin().openOrganizationMembers(orgKey);
- page
- .addMember("foo")
- .getMembersByIdx(1)
- .shouldHaveGroups(0)
- .manageGroupsOpen()
- .manageGroupsSelect("owners")
- .manageGroupsSave()
- .shouldHaveGroups(1);
- }
-
- private void verifyMembership(String login, String organizationKey, boolean isMember) {
- // TODO replace with search member WS
- int count = orchestrator.getDatabase().countSql(format("SELECT COUNT(1) FROM organization_members om " +
- "INNER JOIN users u ON u.id=om.user_id AND u.login='%s' " +
- "INNER JOIN organizations o ON o.uuid=om.organization_uuid AND o.kee='%s'", login, organizationKey));
- assertThat(count).isEqualTo(isMember ? 1 : 0);
- }
-
- private static String createOrganization() {
- adminClient.organizations().create(new CreateWsRequest.Builder().setKey(KEY).setName(KEY).build()).getOrganization();
- return KEY;
- }
-
- private static String createUser() {
- String login = randomAlphabetic(10).toLowerCase();
- userRule.createUser(login, login);
- return login;
- }
-
- }
|