File size: 1,308 Bytes
83a59ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package emails

import (
	"html/template"
	"testing"

	"github.com/go-hermes/hermes/v2"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestHermesThemeRendersUnsafeDictionaryValueOnlyWhenProvided(t *testing.T) {
	generator := (&HermesGeneratorConfig{
		AppURL:     "https://httpsms.com",
		AppName:    "httpSMS",
		AppLogoURL: "https://httpsms.com/logo.png",
	}).Generator()

	email := hermes.Email{
		Body: hermes.Body{
			Title: "Hello",
			Dictionary: []hermes.Entry{
				{Key: "Normal", Value: "<b>escaped</b>"},
				{
					Key:         "Payload",
					Value:       "plain payload",
					UnsafeValue: template.HTML(`<pre style="white-space:pre-wrap">rich payload</pre>`),
				},
			},
		},
	}

	htmlEmail, err := generator.GenerateHTML(email)
	require.NoError(t, err)

	textEmail, err := generator.GeneratePlainText(email)
	require.NoError(t, err)

	assert.Contains(t, htmlEmail, `&lt;b&gt;escaped&lt;/b&gt;`)
	assert.NotContains(t, htmlEmail, `<b>escaped</b>`)
	assert.Contains(t, htmlEmail, `<pre style="white-space:pre-wrap">rich payload</pre>`)
	assert.NotContains(t, htmlEmail, `<dd>plain payload</dd>`)
	assert.Contains(t, textEmail, "Normal: <b>escaped</b>")
	assert.Contains(t, textEmail, "Payload: plain payload")
	assert.NotContains(t, textEmail, "<pre")
}