package it.com.atlassian.stash.rest.render;

import com.atlassian.bitbucket.rest.util.RestUtils;
import com.atlassian.bitbucket.test.DefaultFuncTestData;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
import it.com.atlassian.stash.BaseRestTest;
import org.apache.commons.lang3.SystemUtils;
import org.junit.Assume;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class MarkupResourceTest extends BaseRestTest {

    @Test
    public void testPreview() throws Exception {
        Response response = RestAssured.expect().statusCode(200)
                .given()
                .auth().preemptive().basic(DefaultFuncTestData.getAdminUser(), DefaultFuncTestData.getAdminPassword())
                .header(DefaultFuncTestData.HDR_XSRF_NO_CHECK)
                .body("text")
                .expect()
                .statusCode(200)
                .when()
                .post(getPreviewURL());
        assertEquals("<p>text</p>\n", response.jsonPath().get("html"));
    }

    @Test
    public void testPreviewWithCJKCharacters() throws Exception {
        Assume.assumeFalse("Does not work on Windows. The file encoding has been set to UTF-8 for both the server " +
                "and tests in the rest func-test pom.xml, but it _still_ doesn't seem to be respected", SystemUtils.IS_OS_WINDOWS);

        Response response = RestAssured.expect().statusCode(200)
                .given()
                .auth().preemptive().basic(DefaultFuncTestData.getAdminUser(), DefaultFuncTestData.getAdminPassword())
                .body("\u96c6\u5718")
                .contentType(RestUtils.APPLICATION_JSON_UTF8)
                .header(DefaultFuncTestData.HDR_XSRF_NO_CHECK)
                .expect()
                .statusCode(200)
                .when()
                .post(getPreviewURL());
        assertEquals("<p>\u96c6\u5718</p>\n", response.jsonPath().get("html"));
    }

    @Test
    public void testPreviewIsNotAuthorisedForAnonymousUsers() throws Exception {
        RestAssured.expect().statusCode(200)
                .given()
                .body("text")
                .header(DefaultFuncTestData.HDR_XSRF_NO_CHECK)
                .expect()
                .statusCode(401)
                .when()
                .post(getPreviewURL());
    }

    @Test
    public void testEscapedMention() throws Exception {
        // This is the wrong place for this test, but it's better than nothing
        String username = "bad_\"user\" <script>evil</script>";
        testContext.user(username, "Something", "password");
        Response response = RestAssured.expect().statusCode(200)
                .given()
                .auth().preemptive().basic(DefaultFuncTestData.getAdminUser(), DefaultFuncTestData.getAdminPassword())
                .body(String.format("@\"%s\"", username.replace("\"", "\\\"")))
                .header(DefaultFuncTestData.HDR_XSRF_NO_CHECK)
                .expect()
                .when()
                .post(getPreviewURL());
        assertEquals("<p><span class=\"user-mention \">Something</span></p>\n", response.jsonPath().get("html"));
    }

    private String getPreviewURL() {
        return DefaultFuncTestData.getRestURL() + "/markup/preview";
    }
}
