package it.com.atlassian.stash;

import com.atlassian.webdriver.bitbucket.page.UserProfilePage;
import it.com.atlassian.stash.rules.LoginRule;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collection;

import static it.com.atlassian.stash.rules.SessionCleanupRule.KeepSession;
import static org.junit.runners.Parameterized.Parameters;

@KeepSession
public class UserProfileSlugTest extends BaseBrowserFuncTest {
    @ClassRule
    public static LoginRule loginOnce = new LoginRule();

    @Parameters(name = "user \"{0}\"")
    public static Collection<String[]> nonAsciiNames() {
        return Arrays.asList(new String[][] {
            // characters in ISO-8859-1 but outside the base US-ASCII ones
            {"\u00b1-userTest", "\u00b5\u00a3\u00a4 fullname", "testuser-nonASCII@example.com"},
            // diacritics
            {"\u016d\u1e61\u00e9\u0213-userTest", "\u00c2 \u016d\u1e61\u00e9\u0213's fullname", "testuser-diacritics@example.com"},
            // CJK
            {"\u6236-userTest", "\u56fd\u969b\u4ea4\u6d41\u57fa fullname", "testuser-cjk@example.com"},
            // XSS
            {"<script>alert('userTest');</script>", "<script>alert('userTest');</script> fullname", "testuser-xss@example.com"},
        });
    }

    private final String username;
    private final String displayName;
    private final String emailAddress;

    public UserProfileSlugTest(String username, String displayName, String emailAddress) {
        this.username = username;
        this.displayName = displayName;
        this.emailAddress = emailAddress;
    }

    @Before
    public void createUser() {
        testContext.user(username, displayName, "password", emailAddress);
    }

    @Test
    public void testCanVisitProfileWithSlug() {
        BITBUCKET.visit(UserProfilePage.class, username);
    }
}
