-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
mandrill_test.go
115 lines (94 loc) · 3.08 KB
/
mandrill_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
package gomail
import (
"fmt"
"os"
"testing"
"github.com/mattbaird/gochimp"
)
// 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 == "[email protected]" {
return []gochimp.SendResponse{}, nil
}
// Invalid from domain
if message.To[0].Email == "[email protected]" {
return []gochimp.SendResponse{}, fmt.Errorf(`-2: Validation error: {"message":{"from_email":"The domain portion of the email address is invalid (the portion after the @: badhostname.com)"}}`)
}
// Invalid token
if message.To[0].Email == "[email protected]" {
return []gochimp.SendResponse{}, fmt.Errorf(`-1: Invalid API key`)
}
// Invalid status
if message.To[0].Email == "[email protected]" {
var responses []gochimp.SendResponse
resp := gochimp.SendResponse{
Status: "unknown",
}
responses = append(responses, resp)
return responses, 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 = "example.com"
mail.FromName = "No Reply"
mail.FromUsername = "no-reply"
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 {
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},
{"[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 = sendViaMandrill(client, email, false); 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)
}
}
// Test bad from address
email.FromAddress = "invalid@"
if err = sendViaMandrill(client, email, false); err == nil {
t.Fatalf("%s Failed: expected to throw an error, inputted and [%s]", t.Name(), email.FromAddress)
}
}