forked from SparkPost/gosparkpost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates_test.go
388 lines (349 loc) · 15.4 KB
/
templates_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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package gosparkpost_test
import (
"bytes"
"encoding/json"
"reflect"
"strings"
"testing"
sp "github.com/SparkPost/gosparkpost"
"github.com/pkg/errors"
)
// ExampleTemplate builds a native Go Template structure from a JSON string
func ExampleTemplate() {
template := &sp.Template{}
jsonStr := `{
"name": "testy template",
"content": {
"html": "this is a <b>test</b> email!",
"subject": "test email",
"from": {
"name": "tester",
"email": "tester@example.com"
},
"reply_to": "tester@example.com"
}
}`
err := json.Unmarshal([]byte(jsonStr), template)
if err != nil {
panic(err)
}
}
func TestTemplateFromValidation(t *testing.T) {
for idx, test := range []struct {
in interface{}
err error
out sp.From
}{
{sp.From{"a@b.com", "A B"}, nil, sp.From{"a@b.com", "A B"}},
{sp.Address{"a@b.com", "A B", "c@d.com"}, nil, sp.From{"a@b.com", "A B"}},
{"a@b.com", nil, sp.From{"a@b.com", ""}},
{nil, errors.New("unsupported Content.From value type [<nil>]"), sp.From{"", ""}},
{[]byte("a@b.com"), errors.New("unsupported Content.From value type [[]uint8]"), sp.From{"", ""}},
{"", errors.New("Content.From may not be empty"), sp.From{"", ""}},
{map[string]interface{}{"name": "A B", "email": "a@b.com"}, nil, sp.From{"a@b.com", "A B"}},
{map[string]interface{}{"name": 1, "email": "a@b.com"}, errors.New("strings are required for all Content.From values"),
sp.From{"a@b.com", ""}},
{map[string]string{"name": "A B", "email": "a@b.com"}, nil, sp.From{"a@b.com", "A B"}},
} {
f, err := sp.ParseFrom(test.in)
if err == nil && test.err != nil || err != nil && test.err == nil {
t.Errorf("ParseFrom[%d] => err %q, want %q", idx, err, test.err)
} else if err != nil && err.Error() != test.err.Error() {
t.Errorf("ParseFrom[%d] => err %q, want %q", idx, err, test.err)
} else if f.Email != test.out.Email {
t.Errorf("ParseFrom[%d] => Email %q, want %q", idx, f.Email, test.out.Email)
} else if f.Name != test.out.Name {
t.Errorf("ParseFrom[%d] => Name %q, want %q", idx, f.Name, test.out.Name)
}
}
}
// Assert that options are actually ... optional,
// and that unspecified options don't default to their zero values.
func TestTemplateOptions(t *testing.T) {
var jsonb []byte
var err error
var opt bool
te := &sp.Template{}
to := &sp.TmplOptions{Transactional: &opt}
te.Options = to
jsonb, err = json.Marshal(te)
if err != nil {
t.Fatal(err)
}
if !bytes.Contains(jsonb, []byte(`"options":{"transactional":false}`)) {
t.Fatal("expected transactional option to be false")
}
opt = true
jsonb, err = json.Marshal(te)
if err != nil {
t.Fatal(err)
}
if !bytes.Contains(jsonb, []byte(`"options":{"transactional":true}`)) {
t.Fatalf("expected transactional option to be true:\n%s", string(jsonb))
}
}
func TestTemplateValidation(t *testing.T) {
for idx, test := range []struct {
in *sp.Template
err error
out *sp.Template
}{
{nil, errors.New("Can't Validate a nil Template"), nil},
{&sp.Template{}, errors.New("Template requires a non-empty Content.Subject"), nil},
{&sp.Template{Content: sp.Content{Subject: "s"}}, errors.New("Template requires either Content.HTML or Content.Text"), nil},
{&sp.Template{Content: sp.Content{Subject: "s", HTML: "h", From: ""}},
errors.New("Content.From may not be empty"), nil},
{&sp.Template{ID: strings.Repeat("id", 33), Content: sp.Content{Subject: "s", HTML: "h", From: "f"}},
errors.New("Template id may not be longer than 64 bytes"), nil},
{&sp.Template{Name: strings.Repeat("name", 257), Content: sp.Content{Subject: "s", HTML: "h", From: "f"}},
errors.New("Template name may not be longer than 1024 bytes"), nil},
{&sp.Template{Description: strings.Repeat("desc", 257), Content: sp.Content{Subject: "s", HTML: "h", From: "f"}},
errors.New("Template description may not be longer than 1024 bytes"), nil},
{&sp.Template{
Content: sp.Content{
Subject: "s", HTML: "h", From: "f",
Attachments: []sp.Attachment{{Filename: strings.Repeat("f", 256)}},
}},
errors.Errorf("Attachment name length must be <= 255: [%s]", strings.Repeat("f", 256)), nil},
{&sp.Template{
Content: sp.Content{
Subject: "s", HTML: "h", From: "f",
Attachments: []sp.Attachment{{B64Data: "\r\n"}},
}},
errors.New("Attachment data may not contain line breaks [\\r\\n]"), nil},
{&sp.Template{
Content: sp.Content{
Subject: "s", HTML: "h", From: "f",
InlineImages: []sp.InlineImage{{Filename: strings.Repeat("f", 256)}},
}},
errors.Errorf("InlineImage name length must be <= 255: [%s]", strings.Repeat("f", 256)), nil},
{&sp.Template{
Content: sp.Content{
Subject: "s", HTML: "h", From: "f",
InlineImages: []sp.InlineImage{{B64Data: "\r\n"}},
}},
errors.New("InlineImage data may not contain line breaks [\\r\\n]"), nil},
{&sp.Template{Content: sp.Content{EmailRFC822: "From:foo@example.com\r\n", Subject: "removeme"}},
nil, &sp.Template{Content: sp.Content{EmailRFC822: "From:foo@example.com\r\n"}}},
} {
err := test.in.Validate()
if err == nil && test.err != nil || err != nil && test.err == nil {
t.Errorf("Template.Validate[%d] => err %q, want %q", idx, err, test.err)
} else if err != nil && err.Error() != test.err.Error() {
t.Errorf("Template.Validate[%d] => err %q, want %q", idx, err, test.err)
} else if test.out != nil && !reflect.DeepEqual(test.in, test.out) {
t.Errorf("Template.Validate[%d] => failed post-condition check for %v", idx, test.in)
}
}
}
func TestTemplateCreate(t *testing.T) {
for idx, test := range []struct {
in *sp.Template
err error
status int
json string
id string
}{
{nil, errors.New("Create called with nil Template"), 0, "", ""},
{&sp.Template{}, errors.New("Template requires a non-empty Content.Subject"), 0, "", ""},
{&sp.Template{Content: sp.Content{Subject: "s", HTML: "h", From: "f"}},
errors.New("Unexpected response to Template creation (results)"),
200, `{"foo":{"id":"new-template"}}`, ""},
{&sp.Template{Content: sp.Content{Subject: "s", HTML: "h", From: "f"}},
errors.New("Unexpected response to Template creation (id)"),
200, `{"results":{"ID":"new-template"}}`, ""},
{&sp.Template{Content: sp.Content{Subject: "s", HTML: "h", From: "f"}},
errors.New("parsing api response: unexpected end of JSON input"),
200, `{"truncated":{}`, ""},
{&sp.Template{Content: sp.Content{Subject: "s{{", HTML: "h", From: "f"}},
sp.SPErrors([]sp.SPError{{
Message: "substitution language syntax error in template content",
Description: "Error while compiling header Subject: substitution statement missing ending '}}'",
Code: "3000",
Part: "Header:Subject",
}}),
422, `{ "errors": [ { "message": "substitution language syntax error in template content", "description": "Error while compiling header Subject: substitution statement missing ending '}}'", "code": "3000", "part": "Header:Subject" } ] }`, ""},
{&sp.Template{Content: sp.Content{Subject: "s", HTML: "h", From: "f"}},
errors.New(`parsing api response: invalid character 'B' looking for beginning of value`),
503, `Bad Gateway`, ""},
{&sp.Template{Content: sp.Content{Subject: "s", HTML: "h", From: "f"}}, nil,
200, `{"results":{"id":"new-template"}}`, "new-template"},
} {
testSetup(t)
defer testTeardown()
mockRestResponseBuilderFormat(t, "POST", test.status, sp.TemplatesPathFormat, test.json)
id, _, err := testClient.TemplateCreate(test.in)
if err == nil && test.err != nil || err != nil && test.err == nil {
t.Errorf("TemplateCreate[%d] => err %q want %q", idx, err, test.err)
} else if err != nil && err.Error() != test.err.Error() {
t.Errorf("TemplateCreate[%d] => err %q want %q", idx, err, test.err)
} else if id != test.id {
t.Errorf("TemplateCreate[%d] => id %q want %q", idx, id, test.id)
}
}
}
func TestTemplateGet(t *testing.T) {
for idx, test := range []struct {
in *sp.Template
draft bool
err error
status int
json string
out *sp.Template
}{
{nil, false, errors.New("TemplateGet called with nil Template"), 200, "", nil},
{&sp.Template{ID: ""}, false, errors.New("TemplateGet called with blank id"), 200, "", nil},
{&sp.Template{ID: "nope"}, false, errors.New(`[{"message":"Resource could not be found","code":"","description":""}]`), 404, `{ "errors": [ { "message": "Resource could not be found" } ] }`, nil},
{&sp.Template{ID: "nope"}, false, errors.New(`parsing api response: unexpected end of JSON input`), 400, `{`, nil},
{&sp.Template{ID: "id"}, false, errors.New("Unexpected response to TemplateGet"), 200, `{"foo":{}}`, nil},
{&sp.Template{ID: "id"}, false, errors.New("parsing api response: unexpected end of JSON input"), 200, `{"foo":{}`, nil},
{&sp.Template{ID: "id"}, false, errors.New(`parsing api response: invalid character 'B' looking for beginning of value`), 503, `Bad Gateway`, nil},
{&sp.Template{ID: "id"}, false, nil, 200, `{"results":{"content":{"from":{"email":"a@b.com","name": "a b"},"html":"<blink>hi!</blink>","subject":"blink","text":"no blink ;_;"},"id":"id"}}`, &sp.Template{ID: "id", Content: sp.Content{From: map[string]interface{}{"email": "a@b.com", "name": "a b"}, HTML: "<blink>hi!</blink>", Text: "no blink ;_;", Subject: "blink"}}},
} {
testSetup(t)
defer testTeardown()
id := ""
if test.in != nil {
id = test.in.ID
}
mockRestResponseBuilderFormat(t, "GET", test.status, sp.TemplatesPathFormat+"/"+id, test.json)
_, err := testClient.TemplateGet(test.in, test.draft)
if err == nil && test.err != nil || err != nil && test.err == nil {
t.Errorf("TemplateGet[%d] => err %v want %v", idx, err, test.err)
} else if err != nil && err.Error() != test.err.Error() {
t.Errorf("TemplateGet[%d] => err %v want %v", idx, err, test.err)
} else if test.out != nil {
if !reflect.DeepEqual(test.out, test.in) {
t.Errorf("TemplateGet[%d] => template got/want:\n%v\n%v", idx, test.in, test.out)
}
}
}
}
func TestTemplateUpdate(t *testing.T) {
for idx, test := range []struct {
in *sp.Template
pub bool
err error
status int
json string
}{
{nil, false, errors.New("Update called with nil Template"), 500, ""},
{&sp.Template{ID: ""}, false, errors.New("Update called with blank id"), 500, ""},
{&sp.Template{ID: "id", Content: sp.Content{}}, false, errors.New("Template requires a non-empty Content.Subject"), 500, ""},
{&sp.Template{ID: "id", Content: sp.Content{Subject: "s", HTML: "h", From: "f"}}, false, errors.New("parsing api response: unexpected end of JSON input"), 500, `{ "errors": [ { "message": "truncated json" }`},
{&sp.Template{ID: "id", Content: sp.Content{Subject: "s{{", HTML: "h", From: "f"}}, false,
sp.SPErrors([]sp.SPError{{
Message: "substitution language syntax error in template content",
Description: "Error while compiling header Subject: substitution statement missing ending '}}'",
Code: "3000",
Part: "Header:Subject",
}}),
422, `{ "errors": [ { "message": "substitution language syntax error in template content", "description": "Error while compiling header Subject: substitution statement missing ending '}}'", "code": "3000", "part": "Header:Subject" } ] }`},
{&sp.Template{ID: "id", Content: sp.Content{Subject: "s", HTML: "h", From: "f"}}, false, nil, 200, ""},
} {
testSetup(t)
defer testTeardown()
id := ""
if test.in != nil {
id = test.in.ID
}
mockRestResponseBuilderFormat(t, "PUT", test.status, sp.TemplatesPathFormat+"/"+id, test.json)
_, err := testClient.TemplateUpdate(test.in, test.pub)
if err == nil && test.err != nil || err != nil && test.err == nil {
t.Errorf("TemplateUpdate[%d] => err %q want %q", idx, err, test.err)
} else if err != nil && err.Error() != test.err.Error() {
t.Errorf("TemplateUpdate[%d] => err %q want %q", idx, err, test.err)
}
}
}
func TestTemplates(t *testing.T) {
for idx, test := range []struct {
err error
status int
json string
}{
{errors.New("parsing api response: unexpected end of JSON input"), 500, `{ "errors": [ { "message": "truncated json" }`},
{errors.New("[{\"message\":\"truncated json\",\"code\":\"\",\"description\":\"\"}]"), 500, `{ "errors": [ { "message": "truncated json" } ] }`},
{nil, 200, `{ "results": [ { "description": "A test message from SparkPost.com", "id": "my-first-email", "last_update_time": "2006-01-02T15:04:05+00:00", "name": "My First Email", "published": false } ] }`},
} {
testSetup(t)
defer testTeardown()
mockRestResponseBuilderFormat(t, "GET", test.status, sp.TemplatesPathFormat, test.json)
_, _, err := testClient.Templates()
if err == nil && test.err != nil || err != nil && test.err == nil {
t.Errorf("Templates[%d] => err %q want %q", idx, err, test.err)
} else if err != nil && err.Error() != test.err.Error() {
t.Errorf("Templates[%d] => err %q want %q", idx, err, test.err)
}
}
}
func TestTemplateDelete(t *testing.T) {
for idx, test := range []struct {
id string
err error
status int
json string
}{
{"", errors.New("Delete called with blank id"), 0, ""},
{"nope", errors.New(`[{"message":"Resource could not be found","code":"","description":""}]`), 404, `{ "errors": [ { "message": "Resource could not be found" } ] }`},
{"id", nil, 200, "{}"},
} {
testSetup(t)
defer testTeardown()
mockRestResponseBuilderFormat(t, "DELETE", test.status, sp.TemplatesPathFormat+"/"+test.id, test.json)
_, err := testClient.TemplateDelete(test.id)
if err == nil && test.err != nil || err != nil && test.err == nil {
t.Errorf("TemplateDelete[%d] => err %q want %q", idx, err, test.err)
} else if err != nil && err.Error() != test.err.Error() {
t.Errorf("TemplateDelete[%d] => err %q want %q", idx, err, test.err)
}
}
}
func TestTemplatePreview(t *testing.T) {
for idx, test := range []struct {
id string
opts *sp.PreviewOptions
err error
status int
json string
}{
{"", nil, errors.New("Preview called with blank id"), 200, ""},
{"id", &sp.PreviewOptions{map[string]interface{}{
"func": func() { return }},
}, errors.New("json: unsupported type: func()"), 200, ""},
{"id", nil, errors.New("parsing api response: unexpected end of JSON input"), 200, "{"},
{"nope", nil, errors.New(`[{"message":"Resource could not be found","code":"","description":""}]`), 404, `{ "errors": [ { "message": "Resource could not be found" } ] }`},
{"id", nil, nil, 200, ""},
} {
testSetup(t)
defer testTeardown()
mockRestResponseBuilderFormat(t, "POST", test.status, sp.TemplatesPathFormat+"/"+test.id+"/preview", test.json)
_, err := testClient.TemplatePreview(test.id, test.opts)
if err == nil && test.err != nil || err != nil && test.err == nil {
t.Errorf("TemplatePreview[%d] => err %q want %q", idx, err, test.err)
} else if err != nil && err.Error() != test.err.Error() {
t.Errorf("TemplatePreview[%d] => err %q want %q", idx, err, test.err)
}
}
}
func TestTemplatePublish(t *testing.T) {
for idx, test := range []struct {
id string
err error
status int
json string
}{
{"", errors.New("Publish called with blank id"), 200, ""},
{"template", nil, 200, ""},
} {
testSetup(t)
defer testTeardown()
mockRestResponseBuilderFormat(t, "PUT", test.status, sp.TemplatesPathFormat+"/"+test.id, test.json)
_, err := testClient.TemplatePublish(test.id)
if err == nil && test.err != nil || err != nil && test.err == nil {
t.Errorf("TemplatePublish[%d] => err %q want %q", idx, err, test.err)
} else if err != nil && err.Error() != test.err.Error() {
t.Errorf("TemplatePublish[%d] => err %q want %q", idx, err, test.err)
}
}
}