package it.com.atlassian.stash.admin;

import com.atlassian.bitbucket.test.PermissionsTestHelper;
import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.webdriver.bitbucket.page.admin.GlobalPermissionsPage;
import com.atlassian.webdriver.bitbucket.page.admin.GroupListPage;
import com.atlassian.webdriver.bitbucket.page.admin.UserListPage;
import com.google.common.collect.ImmutableList;
import it.com.atlassian.stash.BaseBrowserFuncTest;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;

/**
 * Verifies that permissions are cleared when an user is deleted.
 *
 * @see GlobalPermissionsTest
 */
public class GlobalPermissionsRemovalTest extends BaseBrowserFuncTest {

    private static final List<Permission> PERMISSIONS = ImmutableList.of(Permission.LICENSED_USER, Permission.ADMIN);

    private static final String USERNAME = "userPRFT";
    private static final String PASSWORD = "PASSWORD";
    private static final String EMAIL = "test@example.com";

    private static final String GROUPNAME = "groupPRFT";

    private static final String ADMIN_USER = "userAdminPRFT";
    private static final String ADMIN_GROUP = "groupAdminPRFT";

    @Before
    public void setUp() throws Exception {
        testContext.user(USERNAME, USERNAME, PASSWORD, EMAIL)
                .group(GROUPNAME);
        for (Permission permission : PERMISSIONS) {
            testContext.globalPermission(USERNAME, permission)
                    .globalPermissionForGroup(GROUPNAME, permission);
        }
    }

    @Test
    public void testRemovalExplicitPermission() throws Exception {
        // delete the user through the UI
        loginAsAdmin(UserListPage.class).setFilter(USERNAME)
                .getUser(USERNAME).clickDelete().clickConfirmAndBind();

        // verify the permissions were deleted though REST
        for (Permission permission : PERMISSIONS) {
            assertFalse("user " + USERNAME + " should not have the permission " + permission,
                    PermissionsTestHelper.hasGlobalPermissionForUser(USERNAME, permission.name()));
        }
    }

    @Test
    public void testRemovalPermissionsForGroup() throws Exception {
        // delete the group through the UI
        loginAsAdmin(GroupListPage.class).setFilter(GROUPNAME).getGroup(GROUPNAME).clickDelete().clickConfirm();

        // check the group permissions were deleted
        for (Permission permission : PERMISSIONS) {
            assertFalse("group " + GROUPNAME + " should not have the permission " + permission,
                    PermissionsTestHelper.hasGlobalPermissionForGroup(GROUPNAME, permission.name()));
        }

        // check in the UI
        final GlobalPermissionsPage permissionsPage = BITBUCKET.visit(GlobalPermissionsPage.class);

        assertFalse("Group " + GROUPNAME + "'s permission row should not exist after deletion and recreation.",
                permissionsPage.rowIsVisible(GlobalPermissionsPage.PermissionEntityType.GROUP, GROUPNAME));
    }

    @Test
    public void testRemovalPermissionsForSysAdminGroup() throws Exception {
        // create an user with just admin rights
        testContext.user(ADMIN_USER, ADMIN_USER, PASSWORD, EMAIL)
                   .globalPermission(ADMIN_USER, Permission.ADMIN);

        // create a group with sysadmin rights
        testContext.group(ADMIN_GROUP)
                   .globalPermissionForGroup(ADMIN_GROUP, Permission.SYS_ADMIN);

        // check the user can not delete the group
        final GroupListPage page = loginAs(ADMIN_USER, PASSWORD, GroupListPage.class).setFilter(ADMIN_GROUP)
                .getGroup(ADMIN_GROUP).clickDelete().clickConfirmAndBind();
        assertThat(page.getAuiErrorMessages(), hasItem("You cannot remove the group " + ADMIN_GROUP + " as it has System Administrator privileges, and you are not a System Administrator."));
    }
}
