forked from dexidp/dex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_test.go
155 lines (143 loc) · 4.06 KB
/
template_test.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package email
import (
htmltemplate "html/template"
"testing"
"text/template"
)
const (
textTemplateString = `{{define "T1.txt"}}{{.gift}} from {{.from}} to {{.to}}.{{end}}
{{define "T3.txt"}}Hello there, {{.name}}!{{end}}
{{define "T4.txt"}}Hello there, {{.name}}! Welcome to {{.planet}}!{{end}}
`
htmlTemplateString = `{{define "T1.html"}}<html><body>{{.gift}} from {{.from}} to {{.to}}.</body></html>{{end}}
{{define "T2.html"}}<html><body>Hello, {{.name}}!</body></html>{{end}}
{{define "T4.html"}}<html><body>Hello there, {{.name}}! Welcome to {{.planet}}!</body></html>{{end}}
`
)
type testEmailer struct {
from, subject, text, html string
to []string
}
func (t *testEmailer) SendMail(from, subject, text, html string, to ...string) error {
t.from = from
t.subject = subject
t.text = text
t.html = html
t.to = to
return nil
}
func TestTemplatizedEmailSendMail(t *testing.T) {
textTemplates := template.New("text")
_, err := textTemplates.Parse(textTemplateString)
if err != nil {
t.Fatalf("error parsing text templates: %v", err)
}
htmlTemplates := htmltemplate.New("html")
_, err = htmlTemplates.Parse(htmlTemplateString)
if err != nil {
t.Fatalf("error parsing html templates: %v", err)
}
htmlStart := "<html><body>"
htmlEnd := "</body></html>"
tests := []struct {
tplName string
from, to, subject string
data map[string]interface{}
wantText string
wantHtml string
wantErr bool
ctx map[string]interface{}
}{
{
tplName: "T1",
from: "[email protected]",
to: "[email protected]",
subject: "hello there",
data: map[string]interface{}{
"gift": "Greetings",
},
wantText: "Greetings from [email protected] to [email protected].",
wantHtml: htmlStart + "Greetings from [email protected] to [email protected]." + htmlEnd,
},
{
tplName: "T2",
from: "[email protected]",
to: "[email protected]",
subject: "hello there",
data: map[string]interface{}{
"name": "Alice",
},
wantText: "",
wantHtml: htmlStart + "Hello, Alice!" + htmlEnd,
},
{
tplName: "T3",
from: "[email protected]",
to: "[email protected]",
subject: "hello there",
data: map[string]interface{}{
"name": "Alice",
},
wantText: "Hello there, Alice!",
wantHtml: "",
},
{
// make sure HTML stuff gets escaped.
tplName: "T2",
from: "[email protected]",
to: "[email protected]",
subject: "hello there",
data: map[string]interface{}{
"name": "Alice<script>alert('hacked!')</script>",
},
wantText: "",
wantHtml: htmlStart + "Hello, Alice<script>alert('hacked!')</script>!" + htmlEnd,
},
{
tplName: "T4",
from: "[email protected]",
to: "[email protected]",
subject: "hello there",
data: map[string]interface{}{
"name": "Alice",
},
wantText: "Hello there, Alice! Welcome to Mars!",
ctx: map[string]interface{}{
"planet": "Mars",
},
wantHtml: "<html><body>Hello there, Alice! Welcome to Mars!</body></html>",
},
}
for i, tt := range tests {
emailer := &testEmailer{}
templatizer := NewTemplatizedEmailerFromTemplates(textTemplates, htmlTemplates, emailer)
if tt.ctx != nil {
templatizer.SetGlobalContext(tt.ctx)
}
err := templatizer.SendMail(tt.from, tt.subject, tt.tplName, tt.data, tt.to)
if tt.wantErr {
if err == nil {
t.Errorf("case %d: err == nil, want non-nil err", i)
}
continue
}
if emailer.from != tt.from {
t.Errorf("case %d: want=%q, got=%q", i, tt.from, emailer.from)
}
if emailer.subject != tt.subject {
t.Errorf("case %d: want=%q, got=%q", i, tt.subject, emailer.subject)
}
if emailer.text != tt.wantText {
t.Errorf("case %d: want=%q, got=%q", i, tt.wantText, emailer.text)
}
if emailer.html != tt.wantHtml {
t.Errorf("case %d: want=%q, got=%q", i, tt.wantHtml, emailer.html)
}
if len(emailer.to) != 1 {
t.Errorf("case %d: want=1, got=%d", i, len(emailer.to))
}
if emailer.to[0] != tt.to {
t.Errorf("case %d: want=%q, got=%q", i, tt.to, emailer.to[0])
}
}
}