-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmandrill_test.go
More file actions
120 lines (100 loc) · 2.9 KB
/
mandrill_test.go
File metadata and controls
120 lines (100 loc) · 2.9 KB
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
package gomail
import (
"os"
"testing"
"github.com/mattbaird/gochimp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
testDomain = "example.com"
testFromName = "No Reply"
testUsername = "no-reply"
)
// mockMandrillInterface is a mocking interface for Mandrill
type mockMandrillInterface struct{}
// MessageSend is for mocking
func (m *mockMandrillInterface) MessageSend(message gochimp.Message, _ bool) ([]gochimp.SendResponse, error) {
// todo: is async (bool) needed?
// Success
if message.To[0].Email == "test@domain.com" {
return []gochimp.SendResponse{}, nil
}
// Invalid from domain
if message.To[0].Email == "test@badhostname.com" {
return []gochimp.SendResponse{}, ErrValidationError
}
// Invalid token
if message.To[0].Email == "test@badtoken.com" {
return []gochimp.SendResponse{}, ErrInvalidAPIKey
}
// Invalid status
if message.To[0].Email == "test@badstatus.com" {
return []gochimp.SendResponse{{Status: "unknown"}}, nil
}
// Default is success
return []gochimp.SendResponse{}, nil
}
// newMockMandrillClient will create a new mock client for Mandrill
func newMockMandrillClient() mandrillInterface {
return &mockMandrillInterface{}
}
// TestSendViaMandrill will test the sendViaMandrill() method
func TestSendViaMandrill(t *testing.T) {
t.Parallel()
// Start the service
mail := new(MailService)
// Set all the defaults, toggle all warnings
mail.AutoText = true
mail.FromDomain = testDomain
mail.FromName = testFromName
mail.FromUsername = testUsername
mail.Important = true
mail.TrackClicks = true
mail.TrackOpens = true
// Setup mock client
client := newMockMandrillClient()
// 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 {
require.NoError(t, err, "failed to attach file")
} else {
email.AddAttachment("test-attachment-file.txt", "text/plain", f)
}
// Create the list of tests
tests := []struct {
name string
input string
expectedError bool
}{
{"successful send", "test@domain.com", false},
{"invalid domain error", "test@badhostname.com", true},
{"invalid token error", "test@badtoken.com", true},
{"bad status error", "test@badstatus.com", true},
}
// Loop tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
email.Recipients = []string{test.input}
email.RecipientsCc = []string{test.input}
email.RecipientsBcc = []string{test.input}
email.ReplyToAddress = test.input
err := sendViaMandrill(client, email, false)
if test.expectedError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
// Test bad from address
t.Run("invalid from address error", func(t *testing.T) {
email.FromAddress = "invalid@"
err := sendViaMandrill(client, email, false)
assert.Error(t, err)
})
}