-
Notifications
You must be signed in to change notification settings - Fork 64
/
mandrill_test.go
355 lines (333 loc) · 10.9 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright 2013 Matthew Baird
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gochimp
import (
"fmt"
"io/ioutil"
"os"
"testing"
)
var mandrill, err = NewMandrill(os.Getenv("MANDRILL_KEY"))
var user string = os.Getenv("MANDRILL_USER")
func TestPing(t *testing.T) {
response, err := mandrill.Ping()
if response != "PONG!" {
t.Error(fmt.Sprintf("failed to return PONG!, returned [%s]", response), err)
}
}
func TestUserInfo(t *testing.T) {
response, err := mandrill.UserInfo()
if err != nil {
t.Error("Error:", err)
}
if response.Username != user {
t.Errorf("wrong user, was expecting %s", response.Username)
}
}
func TestUserSenders(t *testing.T) {
response, err := mandrill.UserSenders()
if response == nil {
t.Error("response was nil", err)
}
if err != nil {
t.Error("Error:", err)
}
}
func TestMessageSending(t *testing.T) {
var message Message = Message{Html: "<b>hi there</b>", Text: "hello text", Subject: "Test Mail", FromEmail: user,
FromName: user}
message.AddRecipients(Recipient{Email: user, Name: user, Type: "to"})
message.AddRecipients(Recipient{Email: user, Name: user, Type: "cc"})
message.AddRecipients(Recipient{Email: user, Name: user, Type: "bcc"})
response, err := mandrill.MessageSend(message, false)
if err != nil {
t.Errorf("Error:%v", err)
}
if response != nil && len(response) > 0 {
if len(response) != 3 {
t.Errorf("Did not send to all users. Expected 3, got %d", len(response))
} else {
if response[0].Email != user || response[1].Email != user || response[2].Email != user {
t.Errorf(
"Wrong email recipient, expecting %s,, got (%s, %s, %s)",
user,
response[0].Email,
response[1].Email,
response[2].Email,
)
}
}
} else {
t.Errorf("No response, probably due to API KEY issues")
}
}
const testTemplateName string = "test_transactional_template"
func TestTemplateAdd(t *testing.T) {
// delete the test template if it exists already
mandrill.TemplateDelete(testTemplateName)
template, err := mandrill.TemplateAdd(testTemplateName, readTemplate("templates/transactional_basic.html"), true)
if err != nil {
t.Errorf("Error: %v", err)
}
if template.Name != "test_transactional_template" {
t.Errorf("Wrong template name, expecting %s, got %s", testTemplateName, template.Name)
}
// try recreating, should error out
template, err = mandrill.TemplateAdd(testTemplateName, readTemplate("templates/transactional_basic.html"), true)
if err == nil {
t.Error("Should have error'd on duplicate template")
}
}
func TestTemplateList(t *testing.T) {
_, err := mandrill.TemplateAdd("listTest", "testing 123", true)
if err != nil {
t.Errorf("Error: %v", err)
}
templates, err := mandrill.TemplateList()
if err != nil {
t.Errorf("Error:%v", err)
}
if len(templates) <= 0 {
t.Error("Should have retrieved templates")
}
mandrill.TemplateDelete("listTest")
}
func TestTemplateInfo(t *testing.T) {
template, err := mandrill.TemplateInfo(testTemplateName)
if err != nil {
t.Errorf("Error:%v", err)
}
if template.Name != "test_transactional_template" {
t.Errorf("Wrong template name, expecting %s, got %s", testTemplateName, template.Name)
}
}
func TestTemplateUpdate(t *testing.T) {
// add a simple template
template, err := mandrill.TemplateAdd("updateTest", "testing 123", true)
template, err = mandrill.TemplateUpdate("updateTest", "testing 321", true)
if err != nil {
t.Errorf("Error:%v", err)
}
if template.Name != "updateTest" {
t.Errorf("Wrong template name, expecting %s, got %s", "updateTest", template.Name)
}
if template.Code != "testing 321" {
t.Errorf("Wrong template code, expecting %s, got %s", "testing 321", template.Code)
}
// be nice and tear down after test
mandrill.TemplateDelete("updateTest")
}
func TestTemplatePublish(t *testing.T) {
mandrill.TemplateDelete("publishTest")
// add a simple template
template, err := mandrill.TemplateAdd("publishTest", "testing 123", false)
if err != nil {
t.Errorf("Error:%v", err)
}
if template.Name != "publishTest" {
t.Errorf("Wrong template name, expecting %s, got %v", testTemplateName, template.Name)
}
if template.PublishCode != "" {
t.Errorf("Template should not have a publish code, got %v", template.PublishCode)
}
template, err = mandrill.TemplatePublish("publishTest")
if err != nil {
t.Errorf("Error:%v", err)
}
if template.Name != "publishTest" {
t.Errorf("Wrong template name, expecting %s, got %v", testTemplateName, template.Name)
}
if template.PublishCode == "" {
t.Errorf("Template should have a publish code, got %v", template.PublishCode)
}
mandrill.TemplateDelete("publishTest")
}
func TestTemplateRender(t *testing.T) {
//make sure it's freshly added
mandrill.TemplateDelete("renderTest")
mandrill.TemplateAdd("renderTest", "*|MC:SUBJECT|*", true)
//weak - should check results
mergeVars := []Var{*NewVar("SUBJECT", "Hello, welcome")}
result, err := mandrill.TemplateRender("renderTest", nil, mergeVars)
if err != nil {
t.Errorf("Error:%v", err)
}
if result != "Hello, welcome" {
t.Errorf("Rendered Result incorrect, expecting %s, got %v", "Hello, welcome", result)
}
}
func TestTemplateRender2(t *testing.T) {
//make sure it's freshly added
mandrill.TemplateDelete("renderTest")
mandrill.TemplateAdd("renderTest", "<div mc:edit=\"std_content00\"></div>", true)
//weak - should check results
templateContent := []Var{*NewVar("std_content00", "Hello, welcome")}
result, err := mandrill.TemplateRender("renderTest", templateContent, nil)
if err != nil {
t.Errorf("Error:%v", err)
}
if result != "<div>Hello, welcome</div>" {
t.Errorf("Rendered Result incorrect, expecting %s, got %s", "<div>Hello, welcome</div>", result)
}
}
func TestMessageTemplateSend(t *testing.T) {
//make sure it's freshly added
mandrill.TemplateDelete(testTemplateName)
mandrill.TemplateAdd(testTemplateName, readTemplate("templates/transactional_basic.html"), true)
//weak - should check results
templateContent := []Var{*NewVar("std_content00", "Hello, welcome")}
mergeVars := []Var{*NewVar("SUBJECT", "Hello, welcome")}
var message Message = Message{Subject: "Test Template Mail", FromEmail: user,
FromName: user, GlobalMergeVars: mergeVars}
message.AddRecipients(Recipient{Email: user, Name: user})
_, err := mandrill.MessageSendTemplate(testTemplateName, templateContent, message, true)
if err != nil {
t.Errorf("Error:%v", err)
}
//todo - how do we test this better?
}
func readTemplate(path string) string {
b, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
return string(b)
}
// senders tests
func TestSendersList(t *testing.T) {
//make sure it's freshly added
results, err := mandrill.SenderList()
if err != nil {
t.Errorf("Error:%v", err)
}
var foundUser = false
for i := range results {
var info Sender = results[i]
fmt.Printf("sender:%v %s", info, info.Address)
if info.Address == user {
foundUser = true
}
}
if !foundUser {
t.Errorf("should have found User %s in [%v] length array", user, len(results))
}
}
// incoming tests
func TestInboundDomainListAddCheckDelete(t *testing.T) {
domainName := "improbable.example.com"
domains, err := mandrill.InboundDomainList()
if err != nil {
t.Errorf("Error:%v", err)
}
originalCount := len(domains)
domain, err := mandrill.InboundDomainAdd(domainName)
if err != nil {
t.Errorf("Error:%v", err)
}
domains, err = mandrill.InboundDomainList()
if err != nil {
t.Errorf("Error:%v", err)
}
newCount := len(domains)
if newCount != originalCount+1 {
t.Errorf("Expected %v domains, found %v after adding %v.", originalCount+1, newCount, domainName)
}
newDomain, err := mandrill.InboundDomainCheck(domainName)
if err != nil {
t.Errorf("Error:%v", err)
}
if domain.CreatedAt != newDomain.CreatedAt {
t.Errorf("Domain check of %v and %v do not match.", domain.CreatedAt, newDomain.CreatedAt)
}
if domain.Domain != newDomain.Domain {
t.Errorf("Domain check of %v and %v do not match.", domain.Domain, newDomain.Domain)
}
if domain.ValidMx != newDomain.ValidMx {
t.Errorf("Domain check of %v and %v do not match.", domain.ValidMx, newDomain.ValidMx)
}
_, err = mandrill.InboundDomainDelete(domainName)
if err != nil {
t.Errorf("Error:%v", err)
}
domains, err = mandrill.InboundDomainList()
if err != nil {
t.Errorf("Error:%v", err)
}
deletedCount := len(domains)
if deletedCount != originalCount {
t.Errorf("Expected %v domains, found %v after deleting %v.", originalCount, deletedCount, domainName)
}
}
func TestInboundDomainRoutesAndRaw(t *testing.T) {
domainName := "www.google.com"
emailAddress := "test"
webhookUrl := fmt.Sprintf("http://%v/", domainName)
_, err := mandrill.InboundDomainAdd(domainName)
if err != nil {
t.Errorf("Error:%v", err)
}
routeList, err := mandrill.RouteList(domainName)
if err != nil {
t.Errorf("Error:%v", err)
}
count := len(routeList)
if count != 0 {
t.Errorf("Expected no routes at %v, found %v.", domainName, count)
}
route, err := mandrill.RouteAdd(domainName, emailAddress, webhookUrl)
if err != nil {
t.Errorf("Error:%v", err)
}
if route.Pattern != emailAddress {
t.Errorf("Expected pattern %v, found %v.", emailAddress, route.Pattern)
}
if route.Url != webhookUrl {
t.Errorf("Expected URL %v, found %v.", webhookUrl, route.Url)
}
newDomainName := "www.google.com"
newEmailAddress := "test2"
newWebhookUrl := fmt.Sprintf("http://%v/", newDomainName)
_, err = mandrill.InboundDomainCheck(newDomainName)
if err != nil {
t.Errorf("Error:%v", err)
}
route, err = mandrill.RouteUpdate(route.Id, newDomainName, newEmailAddress, newWebhookUrl)
if err != nil {
t.Errorf("Error:%v", err)
}
if route.Pattern != newEmailAddress {
t.Errorf("Expected pattern %v, found %v.", newEmailAddress, route.Pattern)
}
if route.Url != newWebhookUrl {
t.Errorf("Expected URL %v, found %v.", newWebhookUrl, route.Pattern)
}
route, err = mandrill.RouteDelete(route.Id)
if err != nil {
t.Errorf("Error:%v", err)
}
routeList, err = mandrill.RouteList(domainName)
if err != nil {
t.Errorf("Error:%v", err)
}
newCount := len(routeList)
if newCount != count {
t.Errorf("Expected %v routes at %v, found %v.", count, domainName, newCount)
}
rawMessage := "From: sender@example.com\nTo: test2@www.google.com\nSubject: Some Subject\n\nSome content."
_, err = mandrill.SendRawMIME(rawMessage, []string{"test2@www.google.com"}, "test@www.google.com", "", "127.0.0.1")
if err != nil {
t.Errorf("Error:%v", err)
}
_, err = mandrill.InboundDomainDelete(domainName)
if err != nil {
t.Errorf("Error:%v", err)
}
}