package it.com.atlassian.stash.admin;

import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.webdriver.bitbucket.element.GroupUsersTable;
import com.atlassian.webdriver.bitbucket.page.admin.GroupEditPage;
import com.atlassian.webdriver.bitbucket.page.admin.GroupListPage;
import it.com.atlassian.stash.BaseBrowserFuncTest;
import it.com.atlassian.stash.rules.LoginRule;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;

import static it.com.atlassian.stash.rules.SessionCleanupRule.KeepSession;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.*;

@KeepSession
public class GroupAddAndRemoveUsersTest extends BaseBrowserFuncTest {

    private static final String ADMIN_GROUP = "sysadmin-group";
    private static final String TEST_USER = "test-sysadmin";

    @ClassRule
    public static LoginRule loginOnce = new LoginRule();

    @BeforeClass
    public static void createSysadminGroup() {
        getClassTestContext().user(TEST_USER, TEST_USER, TEST_USER, "test@test.com")
                             .group(ADMIN_GROUP, TEST_USER)
                             .globalPermissionForGroup(ADMIN_GROUP, Permission.SYS_ADMIN);
    }

    @Test
    public void testAddAndRemoveUsersFromGroup() {
        String groupName = "addRemoveUser";
        String user1 = "user1";
        String user2 = "user2";

        testContext.group(groupName)
                   .user(user1, false)
                   .user(user2, false);

        final GroupEditPage viewPage = goToGroupView(groupName, BITBUCKET.visit(GroupListPage.class));
        GroupUsersTable users = viewPage.getUsers().add(user1);

        // verify the user is now part of the group
        assertNotNull("the user wasn't added", users.find(user1));
        assertFalse("the user is already in the group, thus it should not possible to re-add it", users.canAdd(user1));

        users.add(user2);

        // verify the second user was added to the group
        assertNotNull(users.find(user1));
        assertNotNull(users.find(user2));
        assertFalse(users.canAdd(user1));
        assertFalse(users.canAdd(user2));

        // now remove the users
        users.remove(user1).remove(user2);

        assertNull(users.find(user1));
        assertNull(users.find(user2));
        assertTrue(users.canAdd(user1));
        assertTrue(users.canAdd(user2));
    }

    @Test
    public void testCanNotRemoveOwnPrivilegesByRemovingYourselfFromGroup() {
        GroupListPage listPage = loginAs(TEST_USER, TEST_USER, GroupListPage.class);
        GroupEditPage editPage = goToGroupView(ADMIN_GROUP, listPage);
        editPage.getUsers().remove(TEST_USER, true);
        assertThat(editPage.getAuiErrorMessages(), hasItem("You cannot remove yourself from this group as it would remove your own privilege."));
    }

    @Test
    public void testCanNotRemoveOwnPrivilegesByDeletingGroup() {
        final GroupListPage listPage = loginAs(TEST_USER, TEST_USER, GroupListPage.class)
                .setFilter(ADMIN_GROUP)
                .getGroup(ADMIN_GROUP)
                .clickDelete()
                .clickConfirmAndBind();
        assertThat(listPage.getAuiErrorMessages(), hasItem("You cannot remove this group as it would remove your own privileges."));
        assertNotNull(listPage.getGroup(ADMIN_GROUP));
    }

    private GroupEditPage goToGroupView(String group, GroupListPage page) {
        return page.setFilter(group).getGroup(group).clickView();
    }

}
