-
Notifications
You must be signed in to change notification settings - Fork 143
/
bounces_test.go
178 lines (150 loc) · 4.56 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package mailgun_test
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"testing"
"time"
"github.com/mailgun/mailgun-go/v4"
"github.com/stretchr/testify/require"
)
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)
}
}
require.NoError(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)
require.NotNil(t, err)
var ure *mailgun.UnexpectedResponseError
require.ErrorAs(t, err, &ure)
require.Equal(t, http.StatusNotFound, ure.Actual)
}
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) {
require.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")
require.NoError(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)
require.NoError(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)
require.NoError(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)
require.NotNil(t, err)
}
func TestAddDelBounceList(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) {
require.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
}
createdAt, err := mailgun.NewRFC2822Time("Thu, 13 Oct 2011 18:02:00 +0000")
if err != nil {
t.Fatalf("invalid time")
}
// Generate a list of bounces
bounces := []mailgun.Bounce{
{
Code: "550",
Address: fmt.Sprintf("%s@%s", strings.ToLower(randomString(8, "bounce")), domain),
Error: "TestAddDelBounces-generated error",
},
{
Code: "550",
Address: fmt.Sprintf("%s@%s", strings.ToLower(randomString(8, "bounce")), domain),
Error: "TestAddDelBounces-generated error",
CreatedAt: createdAt,
},
}
// Add the bounce for our address.
err = mg.AddBounces(ctx, bounces)
require.NoError(t, err)
for _, expect := range bounces {
if !findBounce(expect.Address) {
t.Fatalf("Expected bounce for address %s in list of bounces", expect.Address)
}
bounce, err := mg.GetBounce(ctx, expect.Address)
require.NoError(t, err)
if bounce.Address != expect.Address {
t.Fatalf("Expected at least one bounce for %s", expect.Address)
}
t.Logf("Bounce Created At: %s", bounce.CreatedAt)
if !expect.CreatedAt.IsZero() && !time.Time(bounce.CreatedAt).Equal(time.Time(expect.CreatedAt)) {
t.Fatalf("Expected bounce createdAt to be %s, got %s", expect.CreatedAt, bounce.CreatedAt)
}
}
// Delete the bounce list. This should put us back the way we were.
err = mg.DeleteBounceList(ctx)
require.NoError(t, err)
it := mg.ListBounces(nil)
var page []mailgun.Bounce
if it.Next(ctx, &page) {
t.Fatalf("Expected no item in the bounce list")
}
}