package it.com.atlassian.stash.repository;

import com.atlassian.bitbucket.test.DefaultFuncTestData;
import com.atlassian.webdriver.bitbucket.page.CommitListPage;
import it.com.atlassian.stash.BaseBrowserFuncTest;
import org.apache.commons.lang.SystemUtils;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class TagsInCommitListTest extends BaseBrowserFuncTest {

    // Filenames on Windows can't have / or ;, so the XSS-testing name can't be used there. Since other OSs are not
    // as strict, and since our CI uses Linux agents, non-Windows OSs still use a name to validate XSS protection.
    private static final String TAG_NAME = (SystemUtils.IS_OS_WINDOWS) ? "tag-for-commit-list" : "<script>alert('xss');</script>";

    @Before
    public void setUp() {
        testContext.tag(DefaultFuncTestData.getProject1(), DefaultFuncTestData.getProject1Repository1(),
                TAG_NAME, "0a943a29376f2336b78312d99e65da17048951db",
                "Creating a tag for " + getClass().getSimpleName());
    }

    @Test
    public void testTagsArePresentInCommitList() {
        CommitListPage commitListPage = loginAsAdmin(CommitListPage.class, DefaultFuncTestData.getProject1(),
                DefaultFuncTestData.getProject1Repository1(), "master");

        List<String> tagsFor0a943a = commitListPage.getTagsFor("0a943a29376f2336b78312d99e65da17048951db");
        assertEquals(2, tagsFor0a943a.size());
        assertTrue(tagsFor0a943a.contains(TAG_NAME));
        assertTrue(tagsFor0a943a.contains("signed_tag"));

        List<String> tagsFor57150c = commitListPage.getTagsFor("57150c54c38d6570b2fd5e6d6cfc19476de44e84");
        assertEquals(1, tagsFor57150c.size());
        assertTrue(tagsFor57150c.contains("backdated_annotated_tag"));
    }
}