forked from keighl/postmark
-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathemail_test.go
More file actions
133 lines (124 loc) · 3.18 KB
/
email_test.go
File metadata and controls
133 lines (124 loc) · 3.18 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
121
122
123
124
125
126
127
128
129
130
131
132
133
package postmark
import (
"context"
"net/http"
)
func getTestEmail() Email {
return Email{
From: "sender@example.com",
To: "receiver@example.com",
Cc: "copied@example.com",
Bcc: "blank-copied@example.com",
Subject: "Test",
Tag: "Invitation",
HTMLBody: "<b>Hello</b>",
TextBody: "Hello",
ReplyTo: "reply@example.com",
Headers: []Header{
{
Name: "CUSTOM-HEADER",
Value: "value",
},
},
TrackOpens: true,
InlineCSS: true,
Attachments: []Attachment{
{
Name: "readme.txt",
Content: "dGVzdCBjb250ZW50",
ContentType: "text/plain",
},
{
Name: "report.pdf",
Content: "dGVzdCBjb250ZW50",
ContentType: "application/octet-stream",
},
},
}
}
func (s *PostmarkTestSuite) TestSendEmail() {
tests := []struct {
name string
responseJSON string
wantErr bool
expectedID string
statusCode int
}{
{
name: "successful email send",
responseJSON: `{
"To": "receiver@example.com",
"SubmittedAt": "2014-02-17T07:25:01.4178645-05:00",
"MessageID": "0a129aee-e1cd-480d-b08d-4f48548ff48d",
"ErrorCode": 0,
"Message": "OK"
}`,
wantErr: false,
expectedID: "0a129aee-e1cd-480d-b08d-4f48548ff48d",
statusCode: http.StatusOK,
},
{
name: "email send failure with error code",
responseJSON: `{
"To": "receiver@example.com",
"SubmittedAt": "2014-02-17T07:25:01.4178645-05:00",
"MessageID": "0a129aee-e1cd-480d-b08d-4f48548ff48d",
"ErrorCode": 401,
"Message": "Sender signature not confirmed"
}`,
wantErr: true,
statusCode: http.StatusOK,
},
{
name: "email send HTTP error",
responseJSON: `{
"ErrorCode": 500,
"Message": "Internal Server Error"
}`,
wantErr: true,
statusCode: http.StatusInternalServerError,
},
}
for _, tt := range tests {
s.Run(tt.name, func() {
s.mux.Post("/email", func(w http.ResponseWriter, _ *http.Request) {
if tt.statusCode != http.StatusOK {
w.WriteHeader(tt.statusCode)
}
_, _ = w.Write([]byte(tt.responseJSON))
})
res, err := s.client.SendEmail(context.Background(), getTestEmail())
if tt.wantErr {
s.Require().Error(err, "SendEmail should have failed")
} else {
s.Require().NoError(err, "SendEmail should not have failed")
s.Equal(tt.expectedID, res.MessageID, "SendEmail returned wrong message ID")
}
})
}
}
func (s *PostmarkTestSuite) TestSendEmailBatch() {
responseJSON := `[
{
"ErrorCode": 0,
"Message": "OK",
"MessageID": "b7bc2f4a-e38e-4336-af7d-e6c392c2f817",
"SubmittedAt": "2010-11-26T12:01:05.1794748-05:00",
"To": "receiver1@example.com"
},
{
"ErrorCode": 0,
"Message": "OK",
"MessageID": "e2ecbbfc-fe12-463d-b933-9fe22915106d",
"SubmittedAt": "2010-11-26T12:01:05.1794748-05:00",
"To": "receiver2@example.com"
}
]`
s.mux.Post("/email/batch", func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(responseJSON))
})
testEmail := getTestEmail()
res, err := s.client.SendEmailBatch(context.Background(), []Email{testEmail, testEmail})
s.Require().NoError(err, "SendEmailBatch should not have failed")
s.Len(res, 2, "SendEmailBatch should return 2 results")
}