summaryrefslogtreecommitdiffstats
path: root/utils/html_test.go
blob: 8dc70242a889f6bd4736a982ab77b0bb8006fd2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package utils

import (
	"html/template"
	"testing"
)

func TestTranslateAsHtml(t *testing.T) {
	TranslationsPreInit()

	translateFunc := TfuncWithFallback("en")

	expected := "To finish updating your email address for YOUR TEAM HERE, please click the link below to confirm this is the right address."
	if actual := TranslateAsHtml(translateFunc, "api.templates.email_change_verify_body.info",
		map[string]interface{}{"TeamDisplayName": "YOUR TEAM HERE"}); actual != template.HTML(expected) {
		t.Fatalf("Incorrectly translated template, got %v, expected %v", actual, expected)
	}

	expected = "To finish updating your email address for <b>YOUR TEAM HERE</b>, please click the link below to confirm this is the right address."
	if actual := TranslateAsHtml(translateFunc, "api.templates.email_change_verify_body.info",
		map[string]interface{}{"TeamDisplayName": "<b>YOUR TEAM HERE</b>"}); actual != template.HTML(expected) {
		t.Fatalf("Incorrectly translated template, got %v, expected %v", actual, expected)
	}
}

func TestEscapeForHtml(t *testing.T) {
	input := "abc"
	expected := "abc"
	if actual := escapeForHtml(input).(string); actual != expected {
		t.Fatalf("incorrectly escaped %v, got %v expected %v", input, actual, expected)
	}

	input = "<b>abc</b>"
	expected = "&lt;b&gt;abc&lt;/b&gt;"
	if actual := escapeForHtml(input).(string); actual != expected {
		t.Fatalf("incorrectly escaped %v, got %v expected %v", input, actual, expected)
	}

	inputMap := map[string]interface{}{
		"abc": "abc",
		"123": "<b>123</b>",
	}
	expectedMap := map[string]interface{}{
		"abc": "abc",
		"123": "&lt;b&gt;123&lt;/b&gt;",
	}
	if actualMap := escapeForHtml(inputMap).(map[string]interface{}); actualMap["abc"] != expectedMap["abc"] || actualMap["123"] != expectedMap["123"] {
		t.Fatalf("incorrectly escaped %v, got %v expected %v", inputMap, actualMap, expectedMap)
	}
}