forked from mailgun/mailgun-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bounces_test.go
156 lines (128 loc) · 3.86 KB
/
bounces_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
package mailgun_test
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"testing"
"time"
"github.com/mailgun/mailgun-go/v4"
"github.com/facebookgo/ensure"
)
func TestGetBounces(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
it := mg.ListBounces(nil)
var page []mailgun.Bounce
for it.Next(ctx, &page) {
for _, bounce := range page {
t.Logf("Bounce: %+v\n", bounce)
}
}
ensure.Nil(t, it.Err())
}
func TestGetSingleBounce(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
exampleEmail := fmt.Sprintf("%s@%s", strings.ToLower(randomString(64, "")),
os.Getenv("MG_DOMAIN"))
_, err := mg.GetBounce(ctx, exampleEmail)
ensure.NotNil(t, err)
ure, ok := err.(*mailgun.UnexpectedResponseError)
ensure.True(t, ok)
ensure.DeepEqual(t, ure.Actual, http.StatusNotFound)
}
func TestAddDelBounces(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
findBounce := func(address string) bool {
it := mg.ListBounces(nil)
var page []mailgun.Bounce
for it.Next(ctx, &page) {
ensure.True(t, len(page) != 0)
for _, bounce := range page {
t.Logf("Bounce Address: %s\n", bounce.Address)
if bounce.Address == address {
return true
}
}
}
if it.Err() != nil {
t.Logf("BounceIterator err: %s", it.Err())
}
return false
}
// Compute an e-mail address for our Bounce.
exampleEmail := fmt.Sprintf("%s@%s", strings.ToLower(randomString(8, "bounce")), domain)
// Add the bounce for our address.
err := mg.AddBounce(ctx, exampleEmail, "550", "TestAddDelBounces-generated error")
ensure.Nil(t, err)
// Give API some time to refresh cache
time.Sleep(time.Second)
// We should now have one bounce listed when we query the API.
if !findBounce(exampleEmail) {
t.Fatalf("Expected bounce for address %s in list of bounces", exampleEmail)
}
bounce, err := mg.GetBounce(ctx, exampleEmail)
ensure.Nil(t, err)
if bounce.Address != exampleEmail {
t.Fatalf("Expected at least one bounce for %s", exampleEmail)
}
t.Logf("Bounce Created At: %s", bounce.CreatedAt)
// Delete it. This should put us back the way we were.
err = mg.DeleteBounce(ctx, exampleEmail)
ensure.Nil(t, err)
// Make sure we're back to the way we were.
if findBounce(exampleEmail) {
t.Fatalf("Un-expected bounce for address %s in list of bounces", exampleEmail)
}
_, err = mg.GetBounce(ctx, exampleEmail)
ensure.NotNil(t, err)
}
func TestDelBounceList(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
findBounce := func(address string) bool {
it := mg.ListBounces(nil)
var page []mailgun.Bounce
for it.Next(ctx, &page) {
ensure.True(t, len(page) != 0)
for _, bounce := range page {
t.Logf("Bounce Address: %s\n", bounce.Address)
if bounce.Address == address {
return true
}
}
}
if it.Err() != nil {
t.Logf("BounceIterator err: %s", it.Err())
}
return false
}
for i := 0; i < 3; i++ {
// Compute an e-mail address for our Bounce.
exampleEmail := fmt.Sprintf("%s@%s", strings.ToLower(randomString(8, "bounce")), domain)
// Add the bounce for our address.
err := mg.AddBounce(ctx, exampleEmail, "550", "TestAddDelBounces-generated error")
ensure.Nil(t, err)
// Give API some time to refresh cache
time.Sleep(time.Second)
// We should now have one bounce listed when we query the API.
if !findBounce(exampleEmail) {
t.Fatalf("Expected bounce for address %s in list of bounces", exampleEmail)
}
}
// Delete the bounce list. This should put us back the way we were.
err := mg.DeleteBounceList(ctx)
ensure.Nil(t, err)
it := mg.ListBounces(nil)
var page []mailgun.Bounce
if it.Next(ctx, &page) {
t.Fatalf("Expected no item in the bounce list")
}
}