package it.com.atlassian.stash;

import com.atlassian.bitbucket.test.DefaultFuncTestData;
import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.webdriver.bitbucket.element.RepositoryTable;
import com.atlassian.webdriver.bitbucket.page.UserProfilePage;
import com.atlassian.webdriver.bitbucket.page.UserProfileRepositoriesTabPage;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.List;

import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

public class UserProfileRepositoriesTest extends BaseBrowserFuncTest {
    private static final String USER = "userprofilerepostest";
    private static final String USER_PROJ = "~" + USER;

    private static final String OTHER_USER = "anotheruserprofileuser";
    private static final String PASSWORD = USER;

    @BeforeClass
    public static void createSampleUser() {
        getClassTestContext().user(USER, USER, PASSWORD, "test@example.com")
             .user(OTHER_USER, OTHER_USER, PASSWORD, "other@example.com")
             .projectPermission(DefaultFuncTestData.getProject1(), USER, Permission.PROJECT_READ);
    }

    @Before
    public void setUp() {
        testContext.fork(DefaultFuncTestData.getProject1(), DefaultFuncTestData.getProject1Repository1(), USER, PASSWORD);
    }

    @Test
    public void testWhenHasPersonalRepositoriesAsOwner() {
        String personalRepo = "private-personal-repo";
        testContext.repository(USER_PROJ, personalRepo);
        UserProfileRepositoriesTabPage tabPage = loginAs(USER, PASSWORD, UserProfilePage.class, USER).showRepositoriesTab();
        List<RepositoryTable.RepositoryRow> repos = tabPage.getRepositories();

        assertThat(repos, hasSize(2));
        assertEquals(personalRepo, repos.get(0).getRepositoryName());
        assertEquals(DefaultFuncTestData.getProject1Repository1(), repos.get(1).getRepositoryName());
    }

    @Test
    public void testWhenHasPersonalRepositoriesAsOther() {
        testContext.repositoryPermission(USER_PROJ, DefaultFuncTestData.getProject1Repository1(), OTHER_USER, Permission.REPO_READ);

        UserProfileRepositoriesTabPage tabPage = loginAs(OTHER_USER, PASSWORD, UserProfilePage.class, USER).showRepositoriesTab();
        List<RepositoryTable.RepositoryRow> repos = tabPage.getRepositories();

        assertThat(repos, hasSize(1));
        assertEquals(DefaultFuncTestData.getProject1Repository1(), repos.get(0).getRepositoryName());
    }
}
