forked from mpapi/failmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages_test.go
313 lines (271 loc) · 11.3 KB
/
messages_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
package main
import (
"fmt"
"net/mail"
"reflect"
"testing"
"text/template"
"time"
)
type BadReader struct{}
func (r BadReader) Read(p []byte) (int, error) {
return 0, fmt.Errorf("bad reader")
}
func TestReceivedMessageReadBody(t *testing.T) {
msg := makeReceivedMessage(t, "Subject: test\r\n\r\ntest body\r\n")
if body, err := msg.ReadBody(); body != "test body\r\n" || err != nil {
t.Errorf("unexpected message body: %s, %s", body, err)
}
if body, err := msg.ReadBody(); body != "" || err != nil {
t.Errorf("unexpected message body on 2nd call: %s, %s", body, err)
}
}
func TestReceivedMessageReadBodyMissingMessage(t *testing.T) {
msg := &ReceivedMessage{
message: &message{From: "[email protected]", To: []string{"[email protected]"}},
Parsed: &mail.Message{Body: BadReader{}},
}
if body, err := msg.ReadBody(); body != "[unreadable message body]" || err == nil {
t.Errorf("unexpected message body for nil message: %s, %s", body, err)
}
}
func TestReceivedMessageReadBodyUnreadableMessage(t *testing.T) {
msg := &ReceivedMessage{
message: &message{From: "[email protected]", To: []string{"[email protected]"}},
}
if body, err := msg.ReadBody(); body != "[no message body]" || err != nil {
t.Errorf("unexpected message body for nil message: %s, %s", body, err)
}
}
func TestReceivedMessageDisplayDate(t *testing.T) {
msg := makeReceivedMessage(t, "Subject: test\r\n\r\ntest body\r\n")
if date := msg.DisplayDate("default"); date != "default" {
t.Errorf("unexpected display date: %s", date)
}
msg = makeReceivedMessage(t, "Date: Wed, 16 Jul 2014 16:00:00 -0400\r\nSubject: test\r\n\r\ntest body\r\n")
if date := msg.DisplayDate("default"); date != "Wed, 16 Jul 2014 16:00:00 -0400" {
t.Errorf("unexpected display date: %s", date)
}
}
func TestReceivedMessageOutgoing(t *testing.T) {
msg := makeReceivedMessage(t, "From: [email protected]\r\nTo: [email protected]\r\nSubject: test\r\n\r\ntest body\r\n")
if from := msg.Sender(); from != "[email protected]" {
t.Errorf("unexpected from: %s", from)
}
if to := msg.Recipients(); len(to) != 1 || to[0] != "[email protected]" {
t.Errorf("unexpected to: %s", to)
}
if body, err := msg.ReadBody(); body != "test body\r\n" || err != nil {
t.Errorf("unexpected body: %s, %s", body, err)
}
}
func TestCompact(t *testing.T) {
msg1 := makeReceivedMessage(t, "From: [email protected]\r\nTo: [email protected]\r\nDate: Tue, 01 Jul 2014 12:34:56 -0400\r\nSubject: test\r\n\r\ntest body 1\r\n")
msg2 := makeReceivedMessage(t, "From: [email protected]\r\nTo: [email protected]\r\nDate: Wed, 02 Jul 2014 12:34:56 -0400\r\nSubject: test\r\n\r\ntest body 2\r\n")
uniques, err := Compact(GroupByExpr("batch", `{{.Header.Get "Subject"}}`), makeStoredMessages(msg1, msg2))
if err != nil {
t.Errorf("unexpected error in Compact(): %s", err)
} else if count := len(uniques); count != 1 {
t.Errorf("expected one unique message from Compact(), got %d", count)
}
unique := uniques[0]
if start := unique.Start.Format(time.RFC1123Z); start != "Tue, 01 Jul 2014 12:34:56 -0400" {
t.Errorf("unexpected range start from Compact(): %s", start)
}
if end := unique.End.Format(time.RFC1123Z); end != "Wed, 02 Jul 2014 12:34:56 -0400" {
t.Errorf("unexpected range end from Compact(): %s", end)
}
if unique.Subject != "test" {
t.Errorf("unexpected subject from Compact(): %s", unique.Subject)
}
if unique.Body != "test body 2\r\n" {
t.Errorf("unexpected body from Compact(): %s", unique.Body)
}
if unique.Count != 2 {
t.Errorf("unexpected count from Compact(): %d", unique.Count)
}
}
func TestSummarize(t *testing.T) {
defer patchTime(time.Date(2014, time.March, 1, 0, 0, 0, 0, time.UTC))()
msg1 := makeReceivedMessage(t, "From: [email protected]\r\nTo: [email protected]\r\nDate: Tue, 01 Jul 2014 12:34:56 -0400\r\nSubject: test\r\n\r\ntest body 1\r\n")
msg2 := makeReceivedMessage(t, "From: [email protected]\r\nTo: [email protected]\r\nDate: Wed, 02 Jul 2014 12:34:56 -0400\r\nSubject: test 2\r\n\r\ntest body 2\r\n")
summarized, err := Summarize(GroupByExpr("group", `{{.Header.Get "Subject"}}`), "[customPrefix]", "[email protected]", "[email protected]", makeStoredMessages(msg1, msg2))
if err != nil {
t.Errorf("unexpected error in Summarize(): %s", err)
} else if summarized.From != "[email protected]" {
t.Errorf("unexpected from address from Summarize(): %s", summarized.From)
}
if !reflect.DeepEqual(summarized.To, []string{"[email protected]"}) {
t.Errorf("unexpected to address from Summarize(): %#v", summarized.To)
}
if summarized.Subject != "[customPrefix] 2 instances of 2 messages" {
t.Errorf("unexpected subject from Summarize(): %s", summarized.Subject)
}
if headers := summarized.Headers(); headers != "From: [email protected]\r\nTo: [email protected]\r\nSubject: [customPrefix] 2 instances of 2 messages\r\nDate: 01 Mar 14 00:00 UTC\r\n\r\n" {
t.Errorf("unexpected headers from Summarize(): %s", headers)
}
}
func makeMessageBuffer() *MessageBuffer {
return &MessageBuffer{
SoftLimit: 5 * time.Second,
HardLimit: 9 * time.Second,
Batch: GroupByExpr("batch", `{{.Header.Get "Subject"}}`),
Group: GroupByExpr("group", `{{.Header.Get "Subject"}}`),
From: "[email protected]",
SubjectPrefix: "[failmail]",
Store: NewMemoryStore(),
Renderer: &NoRenderer{},
batches: NewBatches(),
}
}
func TestMessageBuffer(t *testing.T) {
buf := makeMessageBuffer()
unpatch := patchTime(time.Unix(1393650000, 0))
defer unpatch()
buf.Store.Add(nowGetter(), makeReceivedMessage(t, "To: [email protected]\r\nSubject: test\r\n\r\ntest 1"))
outgoing := make(chan *SendRequest, 64)
summaries := make([]*SummaryMessage, 0)
go func() {
for req := range outgoing {
summaries = append(summaries, req.Message.(*SummaryMessage))
req.SendErrors <- nil
}
}()
buf.Flush(nowGetter(), outgoing, false)
if count := len(summaries); count != 0 {
t.Errorf("unexpected summaries from flush: %d != 0", count)
} else if count := buf.Stats().ActiveBatches; count != 1 {
t.Errorf("unexpected buffer message count: %d", count)
}
unpatch()
unpatch = patchTime(time.Unix(1393650005, 0))
buf.Store.Add(nowGetter(), makeReceivedMessage(t, "To: [email protected]\r\nSubject: test\r\n\r\ntest 2"))
buf.Flush(nowGetter(), outgoing, false)
if count := len(summaries); count != 0 {
t.Errorf("unexpected summaries from flush: %d != 0", count)
} else if count := buf.Stats().ActiveBatches; count != 1 {
t.Errorf("unexpected buffer message count: %d", count)
} else if count := buf.Stats().ActiveMessages; count != 2 {
t.Errorf("unexpected stats active messages count: %d", count)
}
unpatch()
unpatch = patchTime(time.Unix(1393650008, 0))
buf.Flush(nowGetter(), outgoing, false)
if count := len(summaries); count != 0 {
t.Errorf("unexpected summaries from flush: %d != 0", count)
}
unpatch()
unpatch = patchTime(time.Unix(1393650009, 0))
buf.Flush(nowGetter(), outgoing, false)
if count := len(summaries); count != 1 {
t.Errorf("unexpected summaries from flush: %d != 1", count)
}
if count := buf.Stats().ActiveBatches; count != 0 {
t.Errorf("unexpected buffer message count: %d", count)
}
summary := summaries[0]
if count := len(summary.StoredMessages); count != 2 {
t.Errorf("unexpected summary stored message count: %d", count)
}
if count := len(summary.UniqueMessages); count != 1 {
t.Errorf("unexpected summary received unique message count: %d", count)
}
if subject := summary.Subject; subject != "[failmail] 2 instances: test" {
t.Errorf("unexpected summary subject: %s", subject)
}
stats := buf.Stats()
if stats.ActiveBatches != 0 {
t.Errorf("unexpected stats active batches count: %d", stats.ActiveBatches)
}
if stats.ActiveMessages != 0 {
t.Errorf("unexpected stats active messages count: %d", stats.ActiveMessages)
}
unpatch()
}
func TestFlushForce(t *testing.T) {
buf := makeMessageBuffer()
outgoing := make(chan *SendRequest, 64)
summaries := make([]*SummaryMessage, 0)
go func() {
for req := range outgoing {
summaries = append(summaries, req.Message.(*SummaryMessage))
req.SendErrors <- nil
}
}()
unpatch := patchTime(time.Unix(1393650000, 0))
defer unpatch()
buf.Store.Add(nowGetter(), makeReceivedMessage(t, "To: [email protected]\r\nSubject: test\r\n\r\ntest 1"))
buf.Flush(nowGetter(), outgoing, false)
if count := len(summaries); count != 0 {
t.Errorf("unexpected summaries from flush: %d != 0", count)
} else if count := buf.Stats().ActiveBatches; count != 1 {
t.Errorf("unexpected buffer message count: %d", count)
}
unpatch()
unpatch = patchTime(time.Unix(1393650004, 0))
buf.Flush(nowGetter(), outgoing, false)
if count := len(summaries); count != 0 {
t.Errorf("unexpected summaries from flush: %d != 0", count)
}
buf.Flush(nowGetter(), outgoing, true)
if count := len(summaries); count != 1 {
t.Errorf("unexpected summaries from flush: %d != 1", count)
}
unpatch()
}
func TestDefaultFromAddress(t *testing.T) {
defer patchHost("example.com", nil)()
if from := DefaultFromAddress("test"); from != "[email protected]" {
t.Errorf("unexpected from address: %s", from)
}
}
func TestDefaultFromAddressHostnameError(t *testing.T) {
defer patchHost("", fmt.Errorf("no hostname"))()
if from := DefaultFromAddress("test"); from != "test@localhost" {
t.Errorf("unexpected from address: %s", from)
}
}
func TestPlural(t *testing.T) {
if p := Plural(0, "message", "messages"); p != "0 messages" {
t.Errorf("unexpected plural: %s", p)
}
if p := Plural(1, "message", "messages"); p != "1 message" {
t.Errorf("unexpected plural: %s", p)
}
if p := Plural(11, "message", "messages"); p != "11 messages" {
t.Errorf("unexpected plural: %s", p)
}
}
func TestNormalizeAddress(t *testing.T) {
if norm := NormalizeAddress("bad email address"); norm != "bad email address" {
t.Errorf("unexpected normalization of invalid address: %s", norm)
}
if norm := NormalizeAddress("<[email protected]>"); norm != "[email protected]" {
t.Errorf("unexpected normalization of address: %s", norm)
}
if norm := NormalizeAddress("Test User <[email protected]>"); norm != "[email protected]" {
t.Errorf("unexpected normalization of address: %s", norm)
}
}
func TestTemplateRenderer(t *testing.T) {
defer patchTime(time.Date(2014, time.March, 1, 0, 0, 0, 0, time.UTC))()
msg1 := makeReceivedMessage(t, "From: [email protected]\r\nTo: [email protected]\r\nDate: Tue, 01 Jul 2014 12:34:56 -0400\r\nSubject: test\r\n\r\ntest body 1\r\n")
msg2 := makeReceivedMessage(t, "From: [email protected]\r\nTo: [email protected]\r\nDate: Wed, 02 Jul 2014 12:34:56 -0400\r\nSubject: test\r\n\r\ntest body 2\r\n")
summarized, err := Summarize(GroupByExpr("group", `{{.Header.Get "Subject"}}`), "[failmail]", "[email protected]", "[email protected]", makeStoredMessages(msg1, msg2))
if err != nil {
t.Errorf("unexpected error in Summarize(): %s", err)
}
templ := template.Must(template.New("summary").Parse("{{ range .UniqueMessages }}{{ .Count }} instances of {{ .Subject }}{{ end }}\n"))
renderer := &TemplateRenderer{templ}
rendered := renderer.Render(summarized)
if contents := string(rendered.Contents()); contents != "2 instances of test\r\n" {
t.Errorf("unexpected rendered message: %s", contents)
}
}
func makeStoredMessages(msgs ...*ReceivedMessage) []*StoredMessage {
stored := make([]*StoredMessage, 0, len(msgs))
for i, msg := range msgs {
stored = append(stored, &StoredMessage{MessageId(i), nowGetter(), msg})
}
return stored
}