-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
postmark_test.go
106 lines (88 loc) · 2.97 KB
/
postmark_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
package gomail
import (
"context"
"fmt"
"net/http"
"os"
"testing"
"github.com/mrz1836/postmark"
)
// mockPostmarkInterface is a mocking interface for Postmark
type mockPostmarkInterface struct{}
// SendEmail is for mocking
func (m *mockPostmarkInterface) SendEmail(_ context.Context, email postmark.Email) (postmark.EmailResponse, error) {
// Success
if email.To == "[email protected]" {
return *new(postmark.EmailResponse), nil
}
// Invalid domain name
if email.To == "[email protected]" {
return *new(postmark.EmailResponse), fmt.Errorf("400 The 'From' address you supplied (No Reply " + email.To + ") is not a Sender Signature on your account. Please add and confirm this address in order to be able to use it in the 'From' field of your messages")
}
// Invalid token
if email.To == "[email protected]" {
return *new(postmark.EmailResponse), fmt.Errorf("10 The Server Token you provided in the X-Postmark-Server-Token request header was invalid. Please verify that you are using a valid token")
}
// Invalid - bad error code
if email.To == "[email protected]" {
resp := &postmark.EmailResponse{
ErrorCode: http.StatusBadGateway,
}
return *resp, nil
}
// Default is success
return *new(postmark.EmailResponse), nil
}
// newMockPostmarkClient will create a new mock client for Postmark
func newMockPostmarkClient() postmarkInterface {
return &mockPostmarkInterface{}
}
// TestSendViaPostmark will test the sendViaPostmark() method
func TestSendViaPostmark(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 := newMockPostmarkClient()
// 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 = sendViaPostmark(context.Background(), 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)
}
}
}