-
Notifications
You must be signed in to change notification settings - Fork 146
/
domains_test.go
191 lines (154 loc) · 5.11 KB
/
domains_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
package mailgun_test
import (
"context"
"net/http"
"testing"
"github.com/mailgun/mailgun-go/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
testDomain = "mailgun.test"
testKey = "api-fake-key"
testWebhookSigningKey = "webhook-signing-key"
)
func TestListDomains(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
it := mg.ListDomains(nil)
var page []mailgun.Domain
for it.Next(ctx, &page) {
for _, d := range page {
t.Logf("TestListDomains: %#v\n", d)
}
}
t.Logf("TestListDomains: %d domains retrieved\n", it.TotalCount)
require.NoError(t, it.Err())
assert.True(t, it.TotalCount != 0)
}
func TestGetSingleDomain(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
it := mg.ListDomains(nil)
var page []mailgun.Domain
require.True(t, it.Next(ctx, &page))
require.NoError(t, it.Err())
dr, err := mg.GetDomain(ctx, page[0].Name)
require.NoError(t, err)
require.True(t, len(dr.ReceivingDNSRecords) != 0)
require.True(t, len(dr.SendingDNSRecords) != 0)
t.Logf("TestGetSingleDomain: %#v\n", dr)
for _, rxd := range dr.ReceivingDNSRecords {
t.Logf("TestGetSingleDomains: %#v\n", rxd)
}
for _, txd := range dr.SendingDNSRecords {
t.Logf("TestGetSingleDomains: %#v\n", txd)
}
}
func TestGetSingleDomainNotExist(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
_, err := mg.GetDomain(ctx, "unknown.domain")
if err == nil {
t.Fatal("Did not expect a domain to exist")
}
var ure *mailgun.UnexpectedResponseError
require.ErrorAs(t, err, &ure)
require.Equal(t, http.StatusNotFound, ure.Actual)
}
func TestAddUpdateDeleteDomain(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
// First, we need to add the domain.
_, err := mg.CreateDomain(ctx, "mx.mailgun.test",
&mailgun.CreateDomainOptions{SpamAction: mailgun.SpamActionTag, Password: "supersecret", WebScheme: "http"})
require.NoError(t, err)
// Then, we update it.
err = mg.UpdateDomain(ctx, "mx.mailgun.test",
&mailgun.UpdateDomainOptions{WebScheme: "https"})
require.NoError(t, err)
// Next, we delete it.
require.NoError(t, mg.DeleteDomain(ctx, "mx.mailgun.test"))
}
func TestDomainConnection(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
info, err := mg.GetDomainConnection(ctx, testDomain)
require.NoError(t, err)
require.True(t, info.RequireTLS)
require.True(t, info.SkipVerification)
info.RequireTLS = false
err = mg.UpdateDomainConnection(ctx, testDomain, info)
require.NoError(t, err)
info, err = mg.GetDomainConnection(ctx, testDomain)
require.NoError(t, err)
require.False(t, info.RequireTLS)
require.True(t, info.SkipVerification)
}
func TestDomainTracking(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
info, err := mg.GetDomainTracking(ctx, testDomain)
require.NoError(t, err)
require.False(t, info.Unsubscribe.Active)
require.True(t, info.Unsubscribe.HTMLFooter != "")
require.True(t, info.Unsubscribe.TextFooter != "")
require.True(t, info.Click.Active)
require.True(t, info.Open.Active)
// Click Tracking
err = mg.UpdateClickTracking(ctx, testDomain, "no")
require.NoError(t, err)
info, err = mg.GetDomainTracking(ctx, testDomain)
require.NoError(t, err)
require.False(t, info.Click.Active)
// Open Tracking
err = mg.UpdateOpenTracking(ctx, testDomain, "no")
require.NoError(t, err)
info, err = mg.GetDomainTracking(ctx, testDomain)
require.NoError(t, err)
require.False(t, info.Open.Active)
// Unsubscribe
err = mg.UpdateUnsubscribeTracking(ctx, testDomain, "yes", "<h2>Hi</h2>", "Hi")
require.NoError(t, err)
info, err = mg.GetDomainTracking(ctx, testDomain)
require.NoError(t, err)
assert.True(t, info.Unsubscribe.Active)
assert.Equal(t, "<h2>Hi</h2>", info.Unsubscribe.HTMLFooter)
assert.Equal(t, "Hi", info.Unsubscribe.TextFooter)
}
func TestDomainVerify(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
_, err := mg.VerifyDomain(ctx, testDomain)
require.NoError(t, err)
}
func TestDomainVerifyAndReturn(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
_, err := mg.VerifyAndReturnDomain(ctx, testDomain)
require.NoError(t, err)
}
func TestDomainDkimSelector(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
// Update Domain DKIM selector
err := mg.UpdateDomainDkimSelector(ctx, testDomain, "gotest")
require.NoError(t, err)
}
func TestDomainTrackingWebPrefix(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
// Update Domain Tracking Web Prefix
err := mg.UpdateDomainTrackingWebPrefix(ctx, testDomain, "gotest")
require.NoError(t, err)
}