forked from keighl/mandrill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandrill_test.go
219 lines (175 loc) · 5.88 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
package mandrill
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
)
func expect(t *testing.T, a interface{}, b interface{}) {
if a != b {
t.Errorf("Expected %v (type %[1]T) - Got %v (type %[2]T)", b, a)
}
}
func refute(t *testing.T, a interface{}, b interface{}) {
if a == b {
t.Errorf("Did not expect %v (type %[1]T) - Got %v (type %[2]T)", b, a)
}
}
func testTools(code int, body string) (*httptest.Server, *Client) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(code)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, body)
}))
tr := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse(server.URL)
},
}
httpClient := &http.Client{Transport: tr}
client := &Client{"APIKEY", server.URL + "/", httpClient}
return server, client
}
// ClientWithKey //////
func Test_ClientWithKey(t *testing.T) {
c := ClientWithKey("CHEEEEEESE")
refute(t, c, nil)
}
// MessagesSendTemplate //////////
func Test_MessagesSendTemplate_Success(t *testing.T) {
server, m := testTools(200, `[{"email":"[email protected]","status":"sent","reject_reason":"hard-bounce","_id":"1"}]`)
defer server.Close()
responses, err := m.MessagesSendTemplate(&Message{}, "cheese", map[string]string{"name": "bob"})
expect(t, len(responses), 1)
expect(t, err, nil)
correctResponse := &Response{
Email: "[email protected]",
Status: "sent",
RejectionReason: "hard-bounce",
Id: "1",
}
expect(t, reflect.DeepEqual(correctResponse, responses[0]), true)
}
func Test_MessagesSendTemplate_Fail(t *testing.T) {
server, m := testTools(400, `{"status":"error","code":12,"name":"Unknown_Subaccount","message":"No subaccount exists with the id 'customer-123'"}`)
defer server.Close()
responses, err := m.MessagesSendTemplate(&Message{}, "cheese", map[string]string{"name": "bob"})
expect(t, len(responses), 0)
correctResponse := &Error{
Status: "error",
Code: 12,
Name: "Unknown_Subaccount",
Message: "No subaccount exists with the id 'customer-123'",
}
expect(t, reflect.DeepEqual(correctResponse, err), true)
}
// MessagesSend //////////
func Test_MessageSend_Success(t *testing.T) {
server, m := testTools(200, `[{"email":"[email protected]","status":"sent","reject_reason":"hard-bounce","_id":"1"}]`)
defer server.Close()
responses, err := m.MessagesSend(&Message{})
expect(t, len(responses), 1)
expect(t, err, nil)
correctResponse := &Response{
Email: "[email protected]",
Status: "sent",
RejectionReason: "hard-bounce",
Id: "1",
}
expect(t, reflect.DeepEqual(correctResponse, responses[0]), true)
}
func Test_MessageSend_Fail(t *testing.T) {
server, m := testTools(400, `{"status":"error","code":12,"name":"Unknown_Subaccount","message":"No subaccount exists with the id 'customer-123'"}`)
defer server.Close()
responses, err := m.MessagesSend(&Message{})
expect(t, len(responses), 0)
correctResponse := &Error{
Status: "error",
Code: 12,
Name: "Unknown_Subaccount",
Message: "No subaccount exists with the id 'customer-123'",
}
expect(t, reflect.DeepEqual(correctResponse, err), true)
}
// Ping //////////
func Test_Ping_Success(t *testing.T) {
server, m := testTools(200, `"PONG!"`)
defer server.Close()
response, err := m.Ping()
expect(t, response, "PONG!")
expect(t, err, nil)
}
func Test_Ping_Fail(t *testing.T) {
server, m := testTools(400, `{"status":"error","code":-1,"name":"Invalid_Key","message":"Invalid API key"}`)
defer server.Close()
response, err := m.Ping()
expect(t, response, "")
correctMessagesResponse := &Error{
Status: "error",
Code: -1,
Name: "Invalid_Key",
Message: "Invalid API key",
}
expect(t, reflect.DeepEqual(correctMessagesResponse, err), true)
}
// TEST Keys //////////
func Test_SANDBOX_SUCCESS(t *testing.T) {
client := ClientWithKey("SANDBOX_SUCCESS")
_, err := client.MessagesSend(&Message{})
expect(t, err, nil)
}
func Test_SANDBOX_ERROR(t *testing.T) {
client := ClientWithKey("SANDBOX_ERROR")
_, err := client.MessagesSend(&Message{})
refute(t, err, nil)
}
// AddRecipient //////////
func Test_AddRecipient(t *testing.T) {
m := &Message{}
m.AddRecipient("[email protected]", "Bob Johnson", "to")
tos := []*To{&To{"[email protected]", "Bob Johnson", "to"}}
expect(t, reflect.DeepEqual(m.To, tos), true)
}
// ConvertMapToVariables /////
func Test_ConvertMapToVariables(t *testing.T) {
m := map[string]interface{}{"name": "bob"}
target := ConvertMapToVariables(m)
hand := []*Variable{&Variable{"name", "bob"}}
expect(t, reflect.DeepEqual(target, hand), true)
}
func Test_ConvertMapToVariables_WithString(t *testing.T) {
m := map[string]string{"name": "bob"}
target := ConvertMapToVariables(m)
hand := []*Variable{&Variable{"name", "bob"}}
expect(t, reflect.DeepEqual(target, hand), true)
}
func Test_ConvertMapToVariables_BadType(t *testing.T) {
target := ConvertMapToVariables("CHEESE")
expect(t, len(target), 0)
}
func Test_MapToVars(t *testing.T) {
m := map[string]interface{}{"name": "bob"}
target := MapToVars(m)
hand := []*Variable{&Variable{"name", "bob"}}
expect(t, reflect.DeepEqual(target, hand), true)
}
// ConvertMapToVariablesForRecipient ////
func Test_ConvertMapToVariablesForRecipient(t *testing.T) {
m := map[string]string{"name": "bob"}
target := ConvertMapToVariablesForRecipient("[email protected]", m)
hand := &RcptMergeVars{"[email protected]", ConvertMapToVariables(m)}
expect(t, reflect.DeepEqual(target, hand), true)
}
func Test_MapToRecipientVars(t *testing.T) {
m := map[string]string{"name": "bob"}
target := MapToRecipientVars("[email protected]", m)
hand := &RcptMergeVars{"[email protected]", ConvertMapToVariables(m)}
expect(t, reflect.DeepEqual(target, hand), true)
}
// Error Interface ////
func Test_ErrorError(t *testing.T) {
e := Error{Message: "CHEEEEEESE"}
expect(t, e.Error(), "CHEEEEEESE")
}