-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_test.go
367 lines (328 loc) · 10.1 KB
/
email_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package main
import (
"os"
"testing"
)
var FromEmail string
var ToEmail string
func TestInit(t *testing.T) {
FromEmail = os.Getenv("FROM_EMAIL")
ToEmail = os.Getenv("TO_EMAIL")
if ToEmail == "" {
t.Fail()
}
if FromEmail == "" {
t.Fail()
}
}
func TestMessageDefaults(t *testing.T) {
msg := &Message{}
if msg.Html != "" {
t.Errorf("Html was not false: %s", msg.Html)
}
if msg.Track != false {
t.Errorf("Track was not false: %t", msg.Track)
}
}
func TestRequiresTo(t *testing.T) {
service := NewMailService()
message := Message{From: FromEmail, Subject: "Queued Message", Text: "This tests that messages can be queued"}
err := service.SendEmail(&message)
if err == nil {
t.FailNow()
}
if err.Error() != "missing param: to address not set" {
t.FailNow()
}
}
func TestSendsEmail(t *testing.T) {
service := NewMailService()
message := Message{From: FromEmail, To: ToEmail, Subject: "Queued Message", Text: "This tests that messages can be queued"}
err := service.SendEmail(&message)
if err != nil {
t.Errorf("Error: %s", err.Error())
t.FailNow()
}
}
func TestSendsHtmlEmail(t *testing.T) {
service := NewMailService()
message := Message{From: FromEmail, To: ToEmail, Subject: "Queued Message", Text: "This tests that messages can be queued", Html: "This tests that emails can contain html"}
err := service.SendEmail(&message)
if err != nil {
t.Errorf("Error: %s", err.Error())
t.FailNow()
}
}
func TestSendsEmailWithReplyTo(t *testing.T) {
service := NewMailService()
message := Message{From: FromEmail, ReplyTo: FromEmail, To: ToEmail, Subject: "Queued Message", Text: "This tests that messages can be queued"}
err := service.SendEmail(&message)
if err != nil {
t.Errorf("Error: %s", err.Error())
t.FailNow()
}
}
func TestFromEmailBlocked(t *testing.T) {
os.Setenv("BLOCK_LIST_ADDRESSES", `\.co\.uk$`)
service := NewMailService()
message := Message{From: FromEmail, To: ToEmail, Subject: "Blocked Message", Text: "This tests that messages can be blocked"}
err := service.SendEmail(&message)
os.Unsetenv("BLOCK_LIST_ADDRESSES")
if err == nil {
t.FailNow()
}
if "blocked email" != err.Error() {
t.Errorf("Error: %s", err.Error())
t.FailNow()
}
}
func TestReplyToEmailBlocked(t *testing.T) {
os.Setenv("BLOCK_LIST_ADDRESSES", `mailinator\.com$`)
service := NewMailService()
message := Message{From: FromEmail, To: ToEmail, ReplyTo: "test@mailinator.com", Subject: "Blocked Message", Text: "This tests that messages can be blocked"}
err := service.SendEmail(&message)
os.Unsetenv("BLOCK_LIST_ADDRESSES")
if err == nil {
t.FailNow()
}
if "blocked email" != err.Error() {
t.Errorf("Error: %s", err.Error())
t.FailNow()
}
}
func TestSenderEmailIsBlocked(t *testing.T) {
os.Setenv("BLOCK_LIST_ADDRESSES", `tester@mailinator\.com$`)
service := NewMailService()
message := Message{From: FromEmail, To: "tester@mailinator.com", ReplyTo: "test@mailinator.com", Subject: "Blocked Message", Text: "This tests that messages can be blocked"}
err := service.SendEmail(&message)
os.Unsetenv("BLOCK_LIST_ADDRESSES")
if err == nil {
t.FailNow()
}
if "blocked sender email" != err.Error() {
t.Errorf("Error: %s", err.Error())
t.FailNow()
}
}
func TestContentEmailBlocked(t *testing.T) {
os.Setenv("BLOCK_LIST_CONTENT", `blocked|test`)
service := NewMailService()
message := Message{From: FromEmail, To: ToEmail, ReplyTo: "test@mailinator.com", Subject: "This message will be blocked", Text: "This tests that messages can be blocked when keywords are being filtered"}
err := service.SendEmail(&message)
os.Unsetenv("BLOCK_LIST_CONTENT")
if err == nil {
t.FailNow()
}
if "blocked phrase" != err.Error() {
t.Errorf("Error: %s", err.Error())
t.FailNow()
}
}
func TestContentEmailBlockedIgnoreTrailingPipe(t *testing.T) {
blockList := []string{
"this string will not get blocked",
}
os.Setenv("BLOCK_LIST_CONTENT", `|a|`)
service := NewMailService()
for _, element := range blockList {
message := Message{From: FromEmail, To: ToEmail, ReplyTo: "test@mailinator.com", Subject: "This is a subject", Text: element}
err := service.SendEmail(&message)
os.Unsetenv("BLOCK_LIST_CONTENT")
if err != nil {
t.FailNow()
}
}
}
func TestContentEmailBlockedIgnoresCase(t *testing.T) {
blockList := []string{
"lowercase blocked string",
"Title Case Blocked String",
"UPPERCASE BLOCKED STRING",
"RaNdOm CaSe BlOcKeD sTrInG",
"THIS IS ANOTHER STRING",
}
os.Setenv("BLOCK_LIST_CONTENT", `blocked|other`)
service := NewMailService()
for _, element := range blockList {
message := Message{From: FromEmail, To: ToEmail, ReplyTo: "test@mailinator.com", Subject: "This is a subject", Text: element}
err := service.SendEmail(&message)
os.Unsetenv("BLOCK_LIST_CONTENT")
if err == nil {
t.FailNow()
}
if "blocked phrase" != err.Error() {
t.Errorf("Error: %s", err.Error())
t.FailNow()
}
}
}
func TestContentEmailBlockedRegex(t *testing.T) {
blockList := []string{
"Two Words",
"TwoTogether",
"<a href=https",
}
os.Setenv("BLOCK_LIST_CONTENT", `a href=|two ?(words|together)|single`)
service := NewMailService()
for _, element := range blockList {
message := Message{From: FromEmail, To: ToEmail, ReplyTo: "test@mailinator.com", Subject: "This is a subject", Text: element}
err := service.SendEmail(&message)
os.Unsetenv("BLOCK_LIST_CONTENT")
if err == nil {
t.FailNow()
}
if "blocked phrase" != err.Error() {
t.Errorf("Error: %s", err.Error())
t.FailNow()
}
}
}
func TestContentEmailEmptyBlockList(t *testing.T) {
os.Setenv("BLOCK_LIST_CONTENT", ``)
service := NewMailService()
message := Message{From: FromEmail, ReplyTo: FromEmail, To: ToEmail, Subject: "Queued Message", Text: "This tests that messages can be queued"}
err := service.SendEmail(&message)
os.Unsetenv("BLOCK_LIST_CONTENT")
if err != nil {
t.Errorf("Error: %s", err.Error())
t.FailNow()
}
}
func TestContentEmailBlockedUrl(t *testing.T) {
os.Setenv("BLOCK_LIST_CONTENT", `http[^ ]+http`)
service := NewMailService()
message := Message{From: FromEmail, To: ToEmail, ReplyTo: "test@mailinator.com", Subject: "Blocked Message", Text: "This tests that messages can be http://google.com/somewhere?http://blocked.com/address when keywords are being filtered"}
err := service.SendEmail(&message)
os.Unsetenv("BLOCK_LIST_CONTENT")
if err == nil {
t.FailNow()
}
if "blocked phrase" != err.Error() {
t.Errorf("Error: %s", err.Error())
t.FailNow()
}
}
func TestSendsEmailIpBlocked(t *testing.T) {
service := NewMailService()
message := Message{From: FromEmail, To: ToEmail, Subject: "Queued Message", Text: "This tests that messages can be queued", SenderIp: "185.104.184.126"}
err := service.SendEmail(&message)
if err == nil {
t.FailNow()
}
if "blocked ip" != err.Error() {
t.Errorf("Error: %s", err.Error())
t.FailNow()
}
}
func TestQueuesEmail(t *testing.T) {
service := NewMailService()
if nil == service.Messages {
t.Error("Message queue has not been set")
t.FailNow()
}
message := Message{From: FromEmail, To: ToEmail, Subject: "Queued Message", Text: "This tests that messages can be queued"}
service.QueueEmail(&message)
response := <-service.Messages
if response.Text != message.Text {
t.FailNow()
}
}
func TestSendTemplatedEmail(t *testing.T) {
service := NewMailService()
message := Message{To: ToEmail, Subject: "Queued Message", Template: "test"}
err := service.SendTemplatedEmail(&message)
if err != nil {
t.Errorf("Error: %s\n", err.Error())
t.FailNow()
}
}
func TestSendTemplatedEmailRequiresTo(t *testing.T) {
service := NewMailService()
message := Message{Subject: "Queued Message", Template: "test"}
err := service.SendTemplatedEmail(&message)
if err == nil {
t.FailNow()
}
if err.Error() != "missing param: to address not set" {
t.FailNow()
}
}
func TestSendTemplatedEmailWithBlockedContent(t *testing.T) {
blockList := []string{
"Two Words",
"TwoTogether",
"<a href=https",
}
os.Setenv("BLOCK_LIST_CONTENT", `a href=|two ?(words|together)|single`)
service := NewMailService()
for _, element := range blockList {
params := map[string]interface{}{"content": element}
message := Message{To: ToEmail, Subject: "Queued Message", Template: "test", Params: params}
err := service.SendTemplatedEmail(&message)
os.Unsetenv("BLOCK_LIST_CONTENT")
if err == nil {
t.Errorf("Expecting a blocked phrase error")
t.FailNow()
}
if "blocked phrase" != err.Error() {
t.Errorf("Error: %s", err.Error())
t.FailNow()
}
}
}
func TestQueueTemplatedEmail(t *testing.T) {
service := NewMailService()
message := Message{From: FromEmail, To: ToEmail, Subject: "Queued Message", Template: "test"}
service.QueueEmail(&message)
response := <-service.Messages
if response.Template != message.Template {
t.FailNow()
}
}
func TestAssembleTemplateWithInvalidTemplate(t *testing.T) {
service := NewMailService()
message := Message{To: ToEmail, Template: "nonexistant"}
err := service.AssembleTemplate(&message)
if err == nil {
t.FailNow()
}
}
func TestAssembleTemplate(t *testing.T) {
service := NewMailService()
message := Message{To: ToEmail, Template: "test"}
service.AssembleTemplate(&message)
if message.From == "" {
t.Errorf("Error: from for email was not assembled\n")
t.FailNow()
}
if message.Subject == "" {
t.Errorf("Error: subject for email was not assembled\n")
t.FailNow()
}
if message.Text == "" {
t.Errorf("Error: text for email was not assembled\n")
t.FailNow()
}
if message.Html == "" {
t.Errorf("Error: html for email was not assembled\n")
t.FailNow()
}
}
func TestAssembleTemplateWithReplyTo(t *testing.T) {
service := NewMailService()
message := Message{To: ToEmail, ReplyTo: FromEmail, Template: "test"}
service.AssembleTemplate(&message)
if message.ReplyTo == "" {
t.Errorf("Error: reply to for email was not assembled\n")
t.FailNow()
}
}
func TestAssembleTemplateDoesNotChangeSubject(t *testing.T) {
service := NewMailService()
message := Message{To: ToEmail, Subject: "Queued Message", Template: "test"}
service.AssembleTemplate(&message)
if message.Subject != "Queued Message" {
t.Errorf("Error: subject for email was not assembled\n")
t.FailNow()
}
}