package it.com.atlassian.stash.admin;

import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.webdriver.bitbucket.element.PermissionKind;
import com.atlassian.webdriver.bitbucket.element.PermissionRow;
import com.atlassian.webdriver.bitbucket.page.admin.AdminHomePage;
import com.atlassian.webdriver.bitbucket.page.admin.GlobalPermissionsPage;
import it.com.atlassian.stash.BaseBrowserFuncTest;
import org.junit.Before;
import org.junit.Test;

import static com.atlassian.webdriver.bitbucket.element.PermissionKind.ADMIN;
import static com.atlassian.webdriver.bitbucket.element.PermissionKind.PROJECT_CREATE;
import static com.atlassian.webdriver.bitbucket.page.admin.GlobalPermissionsPage.PermissionEntityType;
import static org.junit.Assert.assertEquals;

public class GlobalPermissionsTest extends BaseBrowserFuncTest {

    private static final String TEST_USER = "global_permissions_test_user";
    private static final String TEST_GROUP = "global_permissions_test_group";

    // users and groups are created and deleted between tests to ensure one failing test doesn't fail the others.
    @Before
    public void createUserAndGroup() {
        testContext.user(TEST_USER, false)
                   .group(TEST_GROUP);
    }

    @Test
    public void testAddAndRemovePermissions() throws InterruptedException {
        // go to admin > permissions
        GlobalPermissionsPage globalPermissions = loginAsAdmin(AdminHomePage.class).gotoGlobalPermissionsPage();

        // add users & group permissions
        globalPermissions
                .getPermissionsTable(PermissionEntityType.USER)
                .selectItem(TEST_USER)
                .selectPermission(Permission.ADMIN)
                .clickAdd();

        PermissionRow userRow = globalPermissions.getRow(PermissionEntityType.USER, TEST_USER);

        assertEquals(PermissionKind.ADMIN, userRow.getHighestPermission());
        userRow.clickPermission(ADMIN); // uncheck it
        assertEquals(PermissionKind.PROJECT_CREATE, userRow.getHighestPermission());

        globalPermissions
                .getPermissionsTable(PermissionEntityType.GROUP)
                .selectItem(TEST_GROUP)
                .clickAdd();

        PermissionRow groupRow = globalPermissions.getRow(PermissionEntityType.GROUP, TEST_GROUP);
        assertEquals(PermissionKind.LICENSED_USER, groupRow.getHighestPermission());
        groupRow.clickPermission(PROJECT_CREATE);
        assertEquals(PermissionKind.PROJECT_CREATE, groupRow.getHighestPermission());

        userRow.clickPermission(ADMIN);
        groupRow.clickPermission(ADMIN);

        // re-request the page
        globalPermissions = BITBUCKET.visit(AdminHomePage.class).gotoGlobalPermissionsPage();

        //Make visible as they are invisible until hover
        globalPermissions.showAllRemoveButtons();

        // remove the user & group permissions
        globalPermissions.getUserItem(TEST_USER).clickRemove();
        globalPermissions.getGroupItem(TEST_GROUP).clickRemove();
    }
}
