-
Notifications
You must be signed in to change notification settings - Fork 7
/
email_test.go
135 lines (126 loc) · 2.92 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
// Copyright (c) 2014-2019 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package goemail
import (
"bytes"
"net/mail"
"testing"
)
func TestNewMessageType(t *testing.T) {
type args struct {
from string
subject string
body string
contentType string
}
tests := []struct {
name string
args args
wantName string
wantFrom string
wantNilMsg bool
}{
{
name: "plain",
args: args{
from: "[email protected]",
subject: "boring plain email",
body: "nothing to see here",
contentType: "text/plain",
},
wantName: "",
wantFrom: "[email protected]",
wantNilMsg: false,
},
{
name: "both",
args: args{
from: "Boring Guy <[email protected]>",
subject: "boring plain email",
body: "nothing to see here",
contentType: "text/plain",
},
wantName: "Boring Guy",
wantFrom: "[email protected]",
wantNilMsg: false,
},
{
name: "invalid",
args: args{
from: "boring-email.com",
subject: "boring plain email",
body: "nothing to see here",
contentType: "text/plain",
},
wantName: "",
wantFrom: "",
wantNilMsg: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
msg := NewMessageType(tt.args.from, tt.args.subject, tt.args.body, tt.args.contentType)
if (msg == nil) != tt.wantNilMsg {
t.Errorf("Message was not nil. Should have been invalid.")
}
if msg == nil {
return
}
gotFrom := msg.From()
if gotFrom != tt.wantFrom {
t.Errorf("Incorrect From. Got %s, expected %s",
gotFrom, tt.wantFrom)
}
gotName := msg.Name()
if gotName != tt.wantName {
t.Errorf("Incorrect Name. Got %s, expected %s",
gotName, tt.wantName)
}
t.Logf(`Input: "%s". Name: "%s". From: "%s".`, tt.args.from,
gotName, gotFrom)
})
}
}
func TestIsValidAddress(t *testing.T) {
tests := []struct {
name string
addr string
isOk bool
}{
{"ok plain", "[email protected]", true},
{"ok named", "Boring Guy <[email protected]>", true},
{"bad", "boring-email.com", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsValidAddress(tt.addr); got != tt.isOk {
t.Errorf("IsValidAddress() = %v, want %v", got, tt.isOk)
}
})
}
}
func TestMessage_Body(t *testing.T) {
body := `This is the body of the email.
Yay!`
mailMsg := NewMessage("[email protected]", "boring email", body)
mailMsg.AddTo("[email protected]")
mailMsg.SetName("blah parson")
tests := []struct {
name string
msg *Message
}{
{"good", mailMsg},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
body := tt.msg.Body()
t.Log(string(body))
stdMsg, err := mail.ReadMessage(bytes.NewReader(body))
if err != nil {
t.Fatal(err)
}
t.Log(stdMsg.Header.Get("Content-Type"))
})
}
}