-
Notifications
You must be signed in to change notification settings - Fork 146
/
mailing_lists_test.go
192 lines (163 loc) · 4.86 KB
/
mailing_lists_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
package mailgun_test
import (
"context"
"fmt"
"testing"
"github.com/mailgun/mailgun-go/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMailingListMembers(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
address := randomEmail("list", testDomain)
_, err := mg.CreateMailingList(ctx, mailgun.MailingList{
Address: address,
Name: address,
Description: "TestMailingListMembers-related mailing list",
AccessLevel: mailgun.AccessLevelMembers,
})
require.NoError(t, err)
defer func() {
require.NoError(t, mg.DeleteMailingList(ctx, address))
}()
var countMembers = func() int {
var page []mailgun.Member
var count int
it := mg.ListMembers(address, nil)
for it.Next(ctx, &page) {
count += len(page)
}
require.NoError(t, it.Err())
return count
}
startCount := countMembers()
protoJoe := mailgun.Member{
Address: "[email protected]",
Name: "Joe Example",
Subscribed: mailgun.Subscribed,
}
require.NoError(t, mg.CreateMember(ctx, true, address, protoJoe))
newCount := countMembers()
require.False(t, newCount <= startCount)
theMember, err := mg.GetMember(ctx, "[email protected]", address)
require.NoError(t, err)
assert.Equal(t, protoJoe.Address, theMember.Address)
assert.Equal(t, protoJoe.Name, theMember.Name)
assert.Equal(t, protoJoe.Subscribed, theMember.Subscribed)
assert.Len(t, theMember.Vars, 0)
_, err = mg.UpdateMember(ctx, "[email protected]", address, mailgun.Member{
Name: "Joe Cool",
})
require.NoError(t, err)
theMember, err = mg.GetMember(ctx, "[email protected]", address)
require.NoError(t, err)
assert.Equal(t, "Joe Cool", theMember.Name)
require.NoError(t, mg.DeleteMember(ctx, "[email protected]", address))
assert.Equal(t, startCount, countMembers())
err = mg.CreateMemberList(ctx, nil, address, []any{
mailgun.Member{
Address: "[email protected]",
Name: "Joe's debugging account",
Subscribed: mailgun.Unsubscribed,
},
mailgun.Member{
Address: "Joe Cool <[email protected]>",
Name: "Joe's Cool Account",
Subscribed: mailgun.Subscribed,
},
mailgun.Member{
Address: "[email protected]",
Vars: map[string]any{
"packet-email": "KW9ABC @ BOGBBS-4.#NCA.CA.USA.NOAM",
},
},
})
require.NoError(t, err)
theMember, err = mg.GetMember(ctx, "[email protected]", address)
require.NoError(t, err)
assert.Equal(t, "Joe's Cool Account", theMember.Name)
require.NotNil(t, theMember.Subscribed)
assert.True(t, *theMember.Subscribed)
}
func TestMailingLists(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
address := randomEmail("list", testDomain)
protoList := mailgun.MailingList{
Address: address,
Name: "List1",
Description: "A list created by an acceptance test.",
AccessLevel: mailgun.AccessLevelMembers,
ReplyPreference: mailgun.ReplyPreferenceSender,
}
var countLists = func() int {
var count int
it := mg.ListMailingLists(nil)
var page []mailgun.MailingList
for it.Next(ctx, &page) {
count += len(page)
}
require.NoError(t, it.Err())
return count
}
_, err := mg.CreateMailingList(ctx, protoList)
require.NoError(t, err)
defer func() {
require.NoError(t, mg.DeleteMailingList(ctx, address))
_, err := mg.GetMailingList(ctx, address)
require.NotNil(t, err)
}()
actualCount := countLists()
require.False(t, actualCount < 1)
theList, err := mg.GetMailingList(ctx, address)
require.NoError(t, err)
protoList.CreatedAt = theList.CreatedAt // ignore this field when comparing.
assert.Equal(t, theList, protoList)
_, err = mg.UpdateMailingList(ctx, address, mailgun.MailingList{
Description: "A list whose description changed",
})
require.NoError(t, err)
theList, err = mg.GetMailingList(ctx, address)
require.NoError(t, err)
newList := protoList
newList.Description = "A list whose description changed"
assert.Equal(t, theList, newList)
}
func TestListMailingListRegression(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
address := "[email protected]"
_, err := mg.CreateMailingList(ctx, mailgun.MailingList{
Address: address,
Name: "paging",
Description: "Test paging",
})
require.NoError(t, err)
for i := 0; i < 200; i++ {
var vars map[string]any
if i == 5 {
vars = map[string]any{"has": "vars"}
}
err := mg.CreateMember(ctx, false, address, mailgun.Member{
Address: fmt.Sprintf("%[email protected]", i),
Vars: vars,
})
require.NoError(t, err)
}
it := mg.ListMembers(address, nil)
var members []mailgun.Member
var found int
for it.Next(ctx, &members) {
for _, m := range members {
if m.Vars != nil {
found++
}
}
}
require.NoError(t, it.Err())
assert.Equal(t, 1, found)
}