-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathspam_complaints_test.go
105 lines (86 loc) · 2.59 KB
/
spam_complaints_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
package mailgun_test
import (
"context"
"net/http"
"strings"
"testing"
"github.com/mailgun/mailgun-go/v4"
"github.com/stretchr/testify/require"
)
func TestGetComplaints(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
it := mg.ListComplaints(nil)
var page []mailgun.Complaint
for it.Next(ctx, &page) {
}
require.NoError(t, it.Err())
}
func TestGetComplaintFromRandomNoComplaint(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
_, err := mg.GetComplaint(ctx, randomString(64, "")+"@example.com")
require.NotNil(t, err)
var ure *mailgun.UnexpectedResponseError
require.ErrorAs(t, err, &ure)
require.Equal(t, http.StatusNotFound, ure.Actual)
}
func TestCreateDeleteComplaint(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
var hasComplaint = func(email string) bool {
t.Logf("hasComplaint: %s\n", email)
it := mg.ListComplaints(nil)
require.NoError(t, it.Err())
var page []mailgun.Complaint
for it.Next(ctx, &page) {
for _, complaint := range page {
t.Logf("Complaint Address: %s\n", complaint.Address)
if complaint.Address == email {
return true
}
}
}
return false
}
randomMail := strings.ToLower(randomString(64, "")) + "@example.com"
require.False(t, hasComplaint(randomMail))
require.NoError(t, mg.CreateComplaint(ctx, randomMail))
require.True(t, hasComplaint(randomMail))
require.NoError(t, mg.DeleteComplaint(ctx, randomMail))
require.False(t, hasComplaint(randomMail))
}
func TestCreateDeleteComplaintList(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
var hasComplaint = func(email string) bool {
t.Logf("hasComplaint: %s\n", email)
it := mg.ListComplaints(nil)
require.NoError(t, it.Err())
var page []mailgun.Complaint
for it.Next(ctx, &page) {
for _, complaint := range page {
t.Logf("Complaint Address: %s\n", complaint.Address)
if complaint.Address == email {
return true
}
}
}
return false
}
addresses := []string{
strings.ToLower(randomString(64, "")) + "@example1.com",
strings.ToLower(randomString(64, "")) + "@example2.com",
strings.ToLower(randomString(64, "")) + "@example3.com",
}
require.NoError(t, mg.CreateComplaints(ctx, addresses))
for _, address := range addresses {
require.True(t, hasComplaint(address))
require.NoError(t, mg.DeleteComplaint(ctx, address))
require.False(t, hasComplaint(address))
}
}