forked from SparkPost/gosparkpost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suppression_list_test.go
605 lines (499 loc) · 19.9 KB
/
suppression_list_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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
package gosparkpost_test
import (
"fmt"
"io/ioutil"
"net/http"
"testing"
"encoding/json"
sp "github.com/SparkPost/gosparkpost"
)
func TestUnmarshal_SupressionEvent(t *testing.T) {
testSetup(t)
defer testTeardown()
var suppressionEventString = loadTestFile(t, "test/json/suppression_entry_simple.json")
suppressionEntry := &sp.SuppressionEntry{}
err := json.Unmarshal([]byte(suppressionEventString), suppressionEntry)
if err != nil {
testFailVerbose(t, nil, "Unmarshal SuppressionEntry returned error: %v", err)
}
verifySuppressionEnty(t, suppressionEntry)
}
// Test parsing of "not found" case
func TestSuppression_Get_notFound(t *testing.T) {
testSetup(t)
defer testTeardown()
// set up the response handler
var mockResponse = loadTestFile(t, "test/json/suppression_not_found_error.json")
mockRestBuilderFormat(t, "GET", sp.SuppressionListsPathFormat, mockResponse)
// hit our local handler
suppressionPage := &sp.SuppressionPage{}
res, err := testClient.SuppressionList(suppressionPage)
if err != nil {
testFailVerbose(t, res, "SuppressionList GET returned error: %v", err)
}
// basic content test
if suppressionPage.Results != nil {
testFailVerbose(t, res, "SuppressionList GET returned non-nil Results (error expected)")
} else if len(suppressionPage.Results) != 0 {
testFailVerbose(t, res, "SuppressionList GET returned %d results, expected %d", len(suppressionPage.Results), 0)
} else if len(suppressionPage.Errors) != 1 {
testFailVerbose(t, res, "SuppressionList GET returned %d errors, expected %d", len(suppressionPage.Errors), 1)
} else if suppressionPage.Errors[0].Message != "Recipient could not be found" {
testFailVerbose(t, res, "SuppressionList GET Unmarshal error; saw [%v] expected [%v]",
res.Errors[0].Message, "Recipient could not be found")
}
}
func TestSuppression_Retrieve(t *testing.T) {
testSetup(t)
defer testTeardown()
// set up the response handler
var mockResponse = loadTestFile(t, "test/json/suppression_retrieve.json")
status := http.StatusOK
email := "[email protected]"
mockRestResponseBuilderFormat(t, "GET", status, sp.SuppressionListsPathFormat+"/"+email, mockResponse)
// hit our local handler
suppressionPage := &sp.SuppressionPage{}
res, err := testClient.SuppressionRetrieve(email, suppressionPage)
if err != nil {
testFailVerbose(t, res, "SuppressionList retrieve returned error: %v", err)
} else if res == nil {
testFailVerbose(t, res, "SuppressionList retrieve expected an HTTP response")
}
if len(suppressionPage.Results) != 1 {
testFailVerbose(t, res, "SuppressionList retrieve expected 1 result: %v", suppressionPage)
} else if suppressionPage.TotalCount != 1 {
testFailVerbose(t, res, "SuppressionList retrieve expected 1 result: %v", suppressionPage)
}
verifySuppressionEnty(t, suppressionPage.Results[0])
}
func TestSuppression_Error_Bad_Path(t *testing.T) {
testSetup(t)
defer testTeardown()
// set up the response handler
var mockResponse = loadTestFile(t, "test/json/suppression_not_found_error.json")
mockRestBuilderFormat(t, "GET", "/bad/path", mockResponse)
// hit our local handler
suppressionPage := &sp.SuppressionPage{}
res, err := testClient.SuppressionList(suppressionPage)
if err.Error() != "Expected json, got [text/plain] with code 404" {
testFailVerbose(t, res, "SuppressionList GET returned error: %v", err)
} else if res.HTTP.StatusCode != 404 {
testFailVerbose(t, res, "Expected a 404 error: %v", res)
}
}
func TestSuppression_Error_Bad_JSON(t *testing.T) {
testSetup(t)
defer testTeardown()
// set up the response handler
mockRestBuilderFormat(t, "GET", sp.SuppressionListsPathFormat, "ThisIsBadJSON")
// hit our local handler
suppressionPage := &sp.SuppressionPage{}
// Bad JSON should generate an Error
res, err := testClient.SuppressionList(suppressionPage)
if err == nil {
testFailVerbose(t, res, "Expected an error due to bad JSON: %v", err)
}
}
func TestSuppression_Error_Wrong_JSON(t *testing.T) {
testSetup(t)
defer testTeardown()
// set up the response handler
mockRestBuilderFormat(t, "GET", sp.SuppressionListsPathFormat, "{\"errors\":\"\"")
// hit our local handler
suppressionPage := &sp.SuppressionPage{}
// Bad JSON should generate an Error
res, err := testClient.SuppressionList(suppressionPage)
if err == nil {
testFailVerbose(t, res, "Expected an error due to bad JSON: %v", err)
}
}
// Test parsing of combined suppression list results
func TestSuppression_Get_combinedList(t *testing.T) {
testSetup(t)
defer testTeardown()
// set up the response handler
var mockResponse = loadTestFile(t, "test/json/suppression_combined.json")
mockRestBuilderFormat(t, "GET", sp.SuppressionListsPathFormat, mockResponse)
// hit our local handler
suppressionPage := &sp.SuppressionPage{}
res, err := testClient.SuppressionList(suppressionPage)
if err != nil {
t.Errorf("SuppressionList GET returned error: %v", err)
for _, e := range res.Verbose {
t.Error(e)
}
return
}
// basic content test
if suppressionPage.Results == nil {
t.Error("SuppressionList GET returned nil Results")
} else if len(suppressionPage.Results) != 1 {
t.Errorf("SuppressionList GET returned %d results, expected %d", len(suppressionPage.Results), 1)
} else if suppressionPage.Results[0].Recipient != "[email protected]" {
t.Errorf("SuppressionList GET Unmarshal error; saw [%v] expected [[email protected]]", suppressionPage.Results[0].Recipient)
}
}
// Test parsing of separate suppression list results
func TestSuppression_Get_separateList(t *testing.T) {
testSetup(t)
defer testTeardown()
// set up the response handler
var mockResponse = loadTestFile(t, "test/json/suppression_seperate_lists.json")
mockRestBuilderFormat(t, "GET", sp.SuppressionListsPathFormat, mockResponse)
// hit our local handler
suppressionPage := &sp.SuppressionPage{}
res, err := testClient.SuppressionList(suppressionPage)
if err != nil {
t.Errorf("SuppressionList GET returned error: %v", err)
for _, e := range res.Verbose {
t.Error(e)
}
return
}
// basic content test
if suppressionPage.Results == nil {
t.Error("SuppressionList GET returned nil Results")
} else if len(suppressionPage.Results) != 2 {
t.Errorf("SuppressionList GET returned %d results, expected %d", len(suppressionPage.Results), 2)
} else if suppressionPage.Results[0].Recipient != "[email protected]" {
t.Errorf("SuppressionList GET Unmarshal error; saw [%v] expected [[email protected]]", suppressionPage.Results[0].Recipient)
}
}
// Tests that links are generally parsed properly
func TestSuppression_links(t *testing.T) {
testSetup(t)
defer testTeardown()
// set up the response handler
var mockResponse = loadTestFile(t, "test/json/suppression_cursor.json")
mockRestBuilderFormat(t, "GET", sp.SuppressionListsPathFormat, mockResponse)
// hit our local handler
suppressionPage := &sp.SuppressionPage{}
res, err := testClient.SuppressionList(suppressionPage)
if err != nil {
t.Errorf("SuppressionList GET returned error: %v", err)
for _, e := range res.Verbose {
t.Error(e)
}
return
}
// basic content test
if suppressionPage.Results == nil {
t.Error("SuppressionList GET returned nil Results")
} else if suppressionPage.TotalCount != 44 {
t.Errorf("SuppressionList GET returned %d results, expected %d", suppressionPage.TotalCount, 44)
} else if len(suppressionPage.Links) != 4 {
t.Errorf("SuppressionList GET returned %d results, expected %d", len(suppressionPage.Links), 2)
} else if suppressionPage.Links[0].Href != "The_HREF_first" {
t.Error("SuppressionList GET returned invalid link[0].Href")
} else if suppressionPage.Links[1].Href != "The_HREF_next" {
t.Error("SuppressionList GET returned invalid link[1].Href")
} else if suppressionPage.Links[0].Rel != "first" {
t.Error("SuppressionList GET returned invalid s.Links[0].Rel")
} else if suppressionPage.Links[1].Rel != "next" {
t.Error("SuppressionList GET returned invalid s.Links[1].Rel")
}
// Check convenience links
if suppressionPage.FirstPage != "The_HREF_first" {
t.Errorf("Unexpected FirstPage value: %s", suppressionPage.FirstPage)
} else if suppressionPage.LastPage != "The_HREF_last" {
t.Errorf("Unexpected LastPage value: %s", suppressionPage.LastPage)
} else if suppressionPage.PrevPage != "The_HREF_previous" {
t.Errorf("Unexpected PrevPage value: %s", suppressionPage.PrevPage)
} else if suppressionPage.NextPage != "The_HREF_next" {
t.Errorf("Unexpected NextPage value: %s", suppressionPage.NextPage)
}
}
func TestSuppression_Empty_NextPage(t *testing.T) {
testSetup(t)
defer testTeardown()
// set up the response handler
var mockResponse = loadTestFile(t, "test/json/suppression_single_page.json")
mockRestBuilderFormat(t, "GET", sp.SuppressionListsPathFormat, mockResponse)
// hit our local handler
suppressionPage := &sp.SuppressionPage{}
res, err := testClient.SuppressionList(suppressionPage)
if err != nil {
t.Errorf("SuppressionList GET returned error: %v", err)
for _, e := range res.Verbose {
t.Error(e)
}
return
}
nextResponse, res, err := suppressionPage.Next()
if nextResponse != nil {
t.Errorf("nextResponse should be nil but was: %v", nextResponse)
} else if res != nil {
t.Errorf("Response should be nil but was: %v", res)
} else if err != nil {
t.Errorf("Error should be nil but was: %v", err)
}
}
//
func TestSuppression_NextPage(t *testing.T) {
testSetup(t)
defer testTeardown()
// set up the response handler
var mockResponse = loadTestFile(t, "test/json/suppression_page1.json")
mockRestBuilderFormat(t, "GET", sp.SuppressionListsPathFormat, mockResponse)
mockResponse = loadTestFile(t, "test/json/suppression_page2.json")
mockRestBuilder(t, "GET", "/test/json/suppression_page2.json", mockResponse)
// hit our local handler
suppressionPage := &sp.SuppressionPage{}
res, err := testClient.SuppressionList(suppressionPage)
if err != nil {
t.Errorf("SuppressionList GET returned error: %v", err)
for _, e := range res.Verbose {
t.Error(e)
}
return
}
if suppressionPage.NextPage != "/test/json/suppression_page2.json" {
t.Errorf("Unexpected NextPage value: %s", suppressionPage.NextPage)
}
nextResponse, res, err := suppressionPage.Next()
if nextResponse.NextPage != "/test/json/suppression_pageLast.json" {
t.Errorf("Unexpected NextPage value: %s", nextResponse.NextPage)
}
}
// Test parsing of combined suppression list results
func TestSuppression_Search_combinedList(t *testing.T) {
testSetup(t)
defer testTeardown()
// set up the response handler
var mockResponse = loadTestFile(t, "test/json/suppression_combined.json")
mockRestBuilderFormat(t, "GET", sp.SuppressionListsPathFormat, mockResponse)
// hit our local handler
suppressionPage := &sp.SuppressionPage{}
res, err := testClient.SuppressionSearch(suppressionPage)
if err != nil {
t.Errorf("SuppressionList GET returned error: %v", err)
for _, e := range res.Verbose {
t.Error(e)
}
return
}
// basic content test
if suppressionPage.Results == nil {
t.Error("SuppressionList GET returned nil Results")
} else if len(suppressionPage.Results) != 1 {
t.Errorf("SuppressionList GET returned %d results, expected %d", len(suppressionPage.Results), 1)
} else if suppressionPage.Results[0].Recipient != "[email protected]" {
t.Errorf("SuppressionList GET Unmarshal error; saw [%v] expected [[email protected]]", suppressionPage.Results[0].Recipient)
}
}
// Test parsing of combined suppression list results
func TestSuppression_Search_params(t *testing.T) {
testSetup(t)
defer testTeardown()
// set up the response handler
var mockResponse = loadTestFile(t, "test/json/suppression_combined.json")
mockRestBuilderFormat(t, "GET", sp.SuppressionListsPathFormat, mockResponse)
// hit our local handler
suppressionPage := &sp.SuppressionPage{}
parameters := map[string]string{
"from": "1970-01-01T00:00",
}
suppressionPage.Params = parameters
res, err := testClient.SuppressionSearch(suppressionPage)
if err != nil {
t.Errorf("SuppressionList GET returned error: %v", err)
for _, e := range res.Verbose {
t.Error(e)
}
return
}
// basic content test
if suppressionPage.Results == nil {
t.Error("SuppressionList GET returned nil Results")
} else if len(suppressionPage.Results) != 1 {
t.Errorf("SuppressionList GET returned %d results, expected %d", len(suppressionPage.Results), 1)
} else if suppressionPage.Results[0].Recipient != "[email protected]" {
t.Errorf("SuppressionList GET Unmarshal error; saw [%v] expected [[email protected]]", suppressionPage.Results[0].Recipient)
}
}
func TestClient_SuppressionUpsert_nil_entry(t *testing.T) {
testSetup(t)
defer testTeardown()
response, err := testClient.SuppressionUpsert(nil)
if response != nil {
t.Errorf("Expected nil response object but got: %v", response)
} else if err == nil {
t.Errorf("Expected an error")
}
}
func TestClient_SuppressionUpsert_bad_json(t *testing.T) {
testSetup(t)
defer testTeardown()
var mockResponse = "{bad json}"
mockRestBuilderFormat(t, "PUT", sp.SuppressionListsPathFormat, mockResponse)
entry := sp.WritableSuppressionEntry{
Recipient: "[email protected]",
Description: "entry description",
Type: "non_transactional",
}
entries := []sp.WritableSuppressionEntry{
entry,
}
response, err := testClient.SuppressionUpsert(entries)
if response == nil {
t.Errorf("Expected a response")
} else if err == nil {
t.Errorf("Expected an error")
}
}
func TestClient_SuppressionUpsert_1_entry(t *testing.T) {
testSetup(t)
defer testTeardown()
var expectedRequest = loadTestFile(t, "test/json/suppression_entry_simple_request.json")
var mockResponse = "{}"
mockRestRequestResponseBuilderFormat(t, "PUT", http.StatusOK, sp.SuppressionListsPathFormat, expectedRequest, mockResponse)
entry := sp.WritableSuppressionEntry{
Recipient: "[email protected]",
Description: "entry description",
Type: "non_transactional",
}
entries := []sp.WritableSuppressionEntry{
entry,
}
response, err := testClient.SuppressionUpsert(entries)
if response == nil {
t.Errorf("Expected a response")
} else if err != nil {
t.Errorf("Did not expect an error: %v", err)
}
}
func TestClient_SuppressionUpsert_error_response(t *testing.T) {
testSetup(t)
defer testTeardown()
var mockResponse = loadTestFile(t, "test/json/suppression_not_found_error.json")
status := http.StatusBadRequest
mockRestResponseBuilderFormat(t, "PUT", status, sp.SuppressionListsPathFormat, mockResponse)
entry := sp.WritableSuppressionEntry{
Recipient: "[email protected]",
Description: "entry description",
Type: "non_transactional",
}
entries := []sp.WritableSuppressionEntry{
entry,
}
response, err := testClient.SuppressionUpsert(entries)
if response == nil {
t.Errorf("Expected a response")
} else if err == nil {
t.Errorf("Expected an error with the HTTP status code")
}
if response.HTTP.StatusCode != status {
testFailVerbose(t, response, "Expected HTTP status code %d but got %d", status, response.HTTP.StatusCode)
} else if len(response.Errors) != 1 {
testFailVerbose(t, response, "SuppressionUpsert PUT returned %d errors, expected %d", len(response.Errors), 1)
} else if response.Errors[0].Message != "Recipient could not be found" {
testFailVerbose(t, response, "SuppressionUpsert PUT Unmarshal error; saw [%v] expected [%v]",
response.Errors[0].Message, "Recipient could not be found")
}
}
func TestClient_Suppression_Delete_nil_email(t *testing.T) {
testSetup(t)
defer testTeardown()
status := http.StatusNotFound
mockRestResponseBuilderFormat(t, "DELETE", status, sp.SuppressionListsPathFormat+"/", "")
_, err := testClient.SuppressionDelete("")
if err == nil {
t.Errorf("Expected an error indicating an email address is required")
}
}
func TestClient_Suppression_Delete(t *testing.T) {
testSetup(t)
defer testTeardown()
email := "[email protected]"
status := http.StatusNoContent
mockRestResponseBuilderFormat(t, "DELETE", status, sp.SuppressionListsPathFormat+"/"+email, "")
response, err := testClient.SuppressionDelete(email)
if err != nil {
t.Errorf("Did not expect an error")
}
if response.HTTP.StatusCode != status {
testFailVerbose(t, response, "Expected HTTP status code %d but got %d", status, response.HTTP.StatusCode)
}
}
func TestClient_Suppression_Delete_Errors(t *testing.T) {
testSetup(t)
defer testTeardown()
email := "[email protected]"
status := http.StatusBadRequest
var mockResponse = loadTestFile(t, "test/json/suppression_not_found_error.json")
mockRestResponseBuilderFormat(t, "DELETE", status, sp.SuppressionListsPathFormat+"/"+email, mockResponse)
response, err := testClient.SuppressionDelete(email)
if err == nil {
t.Errorf("Expected an error")
}
if response.HTTP.StatusCode != status {
testFailVerbose(t, response, "Expected HTTP status code %d but got %d", status, response.HTTP.StatusCode)
} else if len(response.Errors) != 1 {
testFailVerbose(t, response, "SuppressionDelete DELETE returned %d errors, expected %d", len(response.Errors), 1)
} else if response.Errors[0].Message != "Recipient could not be found" {
testFailVerbose(t, response, "SuppressionDelete DELETE Unmarshal error; saw [%v] expected [%v]",
response.Errors[0].Message, "Recipient could not be found")
}
}
/////////////////////
// Internal Helpers
/////////////////////
func verifySuppressionEnty(t *testing.T, suppressionEntry *sp.SuppressionEntry) {
if suppressionEntry.Recipient != "[email protected]" {
testFailVerbose(t, nil, "Unexpected Recipient: %s", suppressionEntry.Recipient)
} else if suppressionEntry.Description != "entry description" {
testFailVerbose(t, nil, "Unexpected Description: %s", suppressionEntry.Description)
} else if suppressionEntry.Source != "manually created" {
testFailVerbose(t, nil, "Unexpected Source: %s", suppressionEntry.Source)
} else if suppressionEntry.Type != "non_transactional" {
testFailVerbose(t, nil, "Unexpected Type: %s", suppressionEntry.Type)
} else if suppressionEntry.Created != "2016-05-02T16:29:56+00:00" {
testFailVerbose(t, nil, "Unexpected Created: %s", suppressionEntry.Created)
} else if suppressionEntry.Updated != "2016-05-02T17:20:50+00:00" {
testFailVerbose(t, nil, "Unexpected Updated: %s", suppressionEntry.Updated)
} else if suppressionEntry.NonTransactional != true {
testFailVerbose(t, nil, "Unexpected NonTransactional value")
}
}
func mockRestBuilderFormat(t *testing.T, method string, pathFormat string, mockResponse string) {
mockRestResponseBuilderFormat(t, method, http.StatusOK, pathFormat, mockResponse)
}
func mockRestBuilder(t *testing.T, method string, path string, mockResponse string) {
mockRestResponseBuilder(t, method, http.StatusOK, path, mockResponse)
}
func mockRestResponseBuilderFormat(t *testing.T, method string, status int, pathFormat string, mockResponse string) {
path := fmt.Sprintf(pathFormat, testClient.Config.ApiVersion)
mockRestResponseBuilder(t, method, status, path, mockResponse)
}
func mockRestResponseBuilder(t *testing.T, method string, status int, path string, mockResponse string) {
mockRestRequestResponseBuilder(t, method, status, path, "", mockResponse)
}
func mockRestRequestResponseBuilderFormat(t *testing.T, method string, status int, pathFormat string, expectedBody string, mockResponse string) {
path := fmt.Sprintf(pathFormat, testClient.Config.ApiVersion)
mockRestRequestResponseBuilder(t, method, status, path, expectedBody, mockResponse)
}
func mockRestRequestResponseBuilder(t *testing.T, method string, status int, path string, expectedBody string, mockResponse string) {
testMux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
if expectedBody != "" {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
testFailVerbose(t, nil, "error: %v", err)
}
ok, err := AreEqualJSON(expectedBody, string(body[:]))
if err != nil {
testFailVerbose(t, nil, "error: %v", err)
}
if !ok {
testFailVerbose(t, nil, "Request did not match expected. \nExpected: \n%s\n\nActual:\n%s\n\n", err)
}
}
testMethod(t, r, method)
if mockResponse != "" {
w.Header().Set("Content-Type", "application/json; charset=utf8")
}
w.WriteHeader(status)
if mockResponse != "" {
w.Write([]byte(mockResponse))
}
})
}