-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
aws_ses_test.go
110 lines (91 loc) · 2.81 KB
/
aws_ses_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
package gomail
import (
"fmt"
"os"
"strings"
"testing"
)
// Normal result from a successful email
var successResult = `<SendRawEmailResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
<SendRawEmailResult>
<MessageId>01000172d9097ae4-d7e95511-f9d4-434d-9d2f-a0d860c18ee8-000000</MessageId>
</SendRawEmailResult>
<ResponseMetadata>
<RequestId>8a9c266b-7b2d-4a93-89f5-9ca0031fezas</RequestId>
</ResponseMetadata>
</SendRawEmailResponse>`
// mockAwsSesInterface is a mocking interface for AWS SES
type mockAwsSesInterface struct{}
// SendRawEmail is for mocking
func (m *mockAwsSesInterface) SendRawEmail(raw []byte) (string, error) {
if len(raw) == 0 {
return "", fmt.Errorf("missing email contents")
}
rawString := string(raw)
// Success
if strings.Contains(rawString, "To: [email protected]") {
return successResult, nil
}
// Bad hostname
if strings.Contains(rawString, "To: [email protected]") {
return "", fmt.Errorf("bad hostname error")
}
// Bad result
if strings.Contains(rawString, "To: [email protected]") {
return "<ErrorMessage>Failed!</ErrorMessage>", nil
}
// Default is success
return successResult, nil
}
// newMockAwsSesClient will create a new mock client for AWS SES
func newMockAwsSesClient() awsSesInterface {
return &mockAwsSesInterface{}
}
// TestSendViaAwsSes will test the sendViaAwsSes() method
func TestSendViaAwsSes(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 := newMockAwsSesClient()
// 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 = sendViaAwsSes(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)
}
}
}