/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */
package org.elasticsearch.watcher.actions.email.service;

import org.elasticsearch.common.collect.ImmutableMap;
import org.elasticsearch.test.ElasticsearchTestCase;
import org.junit.Test;

import static org.hamcrest.Matchers.equalTo;

/**
 */
public class HtmlSanitizeTests extends ElasticsearchTestCase {

    @Test
    public void test_HtmlSanitizer_onclick() {
        String badHtml = "<button type=\"button\"" +
                "onclick=\"document.getElementById('demo').innerHTML = Date()\">" +
                "Click me to display Date and Time.</button>";
        byte[] bytes = new byte[0];
        String sanitizedHtml = Profile.sanitizeHtml(ImmutableMap.of("foo", (Attachment) new Attachment.Bytes("foo", bytes, "")), badHtml);
        assertThat(sanitizedHtml, equalTo("Click me to display Date and Time."));
    }

    @Test
    public void test_HtmlSanitizer_Nonattachment_img() {
        String badHtml = "<img src=\"http://test.com/nastyimage.jpg\"/>This is a bad image";
        byte[] bytes = new byte[0];
        String sanitizedHtml = Profile.sanitizeHtml(ImmutableMap.of("foo", (Attachment) new Attachment.Bytes("foo", bytes, "")), badHtml);
        assertThat(sanitizedHtml, equalTo("This is a bad image"));
    }

    @Test
    public void test_HtmlSanitizer_Goodattachment_img() {
        String goodHtml = "<img src=\"cid:foo\" />This is a good image";
        byte[] bytes = new byte[0];
        String sanitizedHtml = Profile.sanitizeHtml(ImmutableMap.of("foo", (Attachment) new Attachment.Bytes("foo", bytes, "")), goodHtml);
        assertThat(sanitizedHtml, equalTo(goodHtml));
    }

    @Test
    public void test_HtmlSanitizer_table() {
        String goodHtml = "<table><tr><td>cell1</td><td>cell2</td></tr></table>";
        byte[] bytes = new byte[0];
        String sanitizedHtml = Profile.sanitizeHtml(ImmutableMap.of("foo", (Attachment) new Attachment.Bytes("foo", bytes, "")), goodHtml);
        assertThat(sanitizedHtml, equalTo(goodHtml));

    }

    @Test
    public void test_HtmlSanitizer_Badattachment_img() {
        String goodHtml = "<img src=\"cid:bad\" />This is a bad image";
        byte[] bytes = new byte[0];
        String sanitizedHtml = Profile.sanitizeHtml(ImmutableMap.of("foo", (Attachment) new Attachment.Bytes("foo", bytes, "")), goodHtml);
        assertThat(sanitizedHtml, equalTo("This is a bad image"));
    }

    @Test
    public void test_HtmlSanitizer_Script() {
        String badHtml = "<script>doSomethingNefarious()</script>This was a dangerous script";
        byte[] bytes = new byte[0];
        String sanitizedHtml = Profile.sanitizeHtml(ImmutableMap.of("foo", (Attachment) new Attachment.Bytes("foo", bytes, "")), badHtml);
        assertThat(sanitizedHtml, equalTo("This was a dangerous script"));
    }


}

@@ -3022,11 +3022,689 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void test_HtmlSanitizer_FullHtmlWithMetaString() %7B%0A        String needsSanitation = %22%3Chtml%3E%3Chead%3E%3C/head%3E%3Cbody%3E%3Ch1%3EHello %7B%7Bctx.metadata.name%7D%7D%3C/h1%3E meta %3Ca href='https://www.google.com/search?q=%7B%7Bctx.metadata.name%7D%7D'%3ETestlink%3C/a%3Emeta%3C/body%3E%3C/html%3E%22;%0A        byte%5B%5D bytes = new byte%5B0%5D;%0A        String sanitizedHtml = Profile.sanitizeHtml(ImmutableMap.of(%22foo%22, (Attachment) new Attachment.Bytes(%22foo%22, bytes, %22%22)), needsSanitation);%0A        assertThat(sanitizedHtml, equalTo(%22%3Chead%3E%3C/head%3E%3Cbody%3E%3Ch1%3EHello %7B%7Bctx.metadata.name%7D%7D%3C/h1%3E meta %3Ca href=%5C%22https://www.google.com/search?q&#61;%7B%7Bctx.metadata.name%7D%7D%5C%22 rel=%5C%22nofollow%5C%22%3ETestlink%3C/a%3Emeta%3C/body%3E%22));%0A    %7D%0A
 %0A%7D%0A

Add html found during testing to cause problems for the sanitizer.