-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
smtp_test.go
200 lines (158 loc) · 4.68 KB
/
smtp_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
package gomail
import (
"bytes"
"fmt"
"io"
"net/smtp"
"os"
"regexp"
"testing"
"github.com/domodwyer/mailyak"
)
// mockSMTPInterface is a mocking interface for SMTP
type mockSMTPInterface struct {
html mailyak.BodyPart
plain mailyak.BodyPart
toAddrs []string
trimRegex *regexp.Regexp
}
// Send will mock sending the email
func (m *mockSMTPInterface) Send() error {
if len(m.toAddrs) > 0 {
// Valid email
if m.toAddrs[0] == "[email protected]" {
return nil
}
// Bad username - Auth
if m.toAddrs[0] == "[email protected]" {
return fmt.Errorf("535 5.7.8")
}
// Bad hostname
if m.toAddrs[0] == "[email protected]" {
return fmt.Errorf("dial tcp: lookup smtp.badhostname.com: no such host")
}
}
// Return success anyway
return nil
}
// MimeBuf will mock the mime type
func (m *mockSMTPInterface) MimeBuf() (*bytes.Buffer, error) {
return nil, nil
}
// String is a mock method
func (m *mockSMTPInterface) String() string {
return ""
}
// HTML is a mock method
func (m *mockSMTPInterface) HTML() *mailyak.BodyPart {
return &m.html
}
// Plain is a mock method
func (m *mockSMTPInterface) Plain() *mailyak.BodyPart {
return &m.plain
}
// To is a mock method
func (m *mockSMTPInterface) To(addrs ...string) {
m.toAddrs = []string{}
for _, addr := range addrs {
trimmed := m.trimRegex.ReplaceAllString(addr, "")
if trimmed == "" {
continue
}
m.toAddrs = append(m.toAddrs, trimmed)
}
}
// Bcc is a mock method
func (m *mockSMTPInterface) Bcc(_ ...string) {}
// WriteBccHeader is a mock method
func (m *mockSMTPInterface) WriteBccHeader(_ bool) {}
// Cc is a mock method
func (m *mockSMTPInterface) Cc(_ ...string) {}
// From is a mock method
func (m *mockSMTPInterface) From(_ string) {}
// FromName is a mock method
func (m *mockSMTPInterface) FromName(_ string) {}
// ReplyTo is a mock method
func (m *mockSMTPInterface) ReplyTo(_ string) {}
// Subject is a mock method
func (m *mockSMTPInterface) Subject(_ string) {}
// AddHeader is a mock method
func (m *mockSMTPInterface) AddHeader(_, _ string) {}
// Attach is a mock method
func (m *mockSMTPInterface) Attach(_ string, _ io.Reader) {}
// AttachWithMimeType is a mock method
func (m *mockSMTPInterface) AttachWithMimeType(_ string, _ io.Reader, _ string) {}
// AttachInline is a mock method
func (m *mockSMTPInterface) AttachInline(_ string, _ io.Reader) {}
// AttachInlineWithMimeType is a mock method
func (m *mockSMTPInterface) AttachInlineWithMimeType(_ string, _ io.Reader, _ string) {}
// ClearAttachments is a mock method
func (m *mockSMTPInterface) ClearAttachments() {}
// TestNewSMTPClient is a basic test for creating a client
func TestNewSMTPClient(t *testing.T) {
auth := smtp.PlainAuth("", "user", "password", "host")
client := newSMTPClient("", auth)
err := client.Send()
if err == nil {
t.Fatalf("error should have occurred, host was empty")
}
client = newSMTPClient("example.com", auth)
err = client.Send()
if err == nil {
t.Fatalf("error should have occurred, host example.com")
}
}
// newMockSMTPClient will create a new mock client for SMTP
func newMockSMTPClient() smtpInterface {
return &mockSMTPInterface{
trimRegex: regexp.MustCompile("\r?\n"),
}
}
// TestSendViaSMTP will test the sendViaSMTP() method
func TestSendViaSMTP(t *testing.T) {
t.Parallel()
// Start the service
mail := new(MailService)
// Set all the defaults, toggle all warnings
mail.AutoText = true
mail.FromDomain = "example.com"
mail.FromName = "No Reply"
mail.FromUsername = "no-reply"
mail.Important = true
mail.TrackClicks = true
mail.TrackOpens = true
// Setup mock client
client := newMockSMTPClient()
// New email
email := mail.NewEmail()
email.HTMLContent = "<html>Test</html>"
email.PlainTextContent = "Test"
// Add an attachment
f, err := os.Open("examples/test-attachment-file.txt")
if err != nil {
t.Fatalf("failed to attach file: %s", err.Error())
} else {
email.AddAttachment("test-attachment-file.txt", "text/plain", f)
}
// Create the list of tests
var tests = []struct {
input string
expectedError bool
}{
{"[email protected]", false},
{"[email protected]", true},
{"[email protected]", true},
}
// Loop tests
for _, test := range tests {
email.Recipients = []string{test.input}
email.RecipientsCc = []string{test.input}
email.RecipientsBcc = []string{test.input}
email.ReplyToAddress = test.input
if err = sendViaSMTP(client, email); err != nil && !test.expectedError {
t.Fatalf("%s Failed: expected to NOT throw an error, inputted and [%s], error [%s]", t.Name(), test.input, err.Error())
} else if err == nil && test.expectedError {
t.Fatalf("%s Failed: expected to throw an error, inputted and [%s]", t.Name(), test.input)
}
}
}