-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathdomains_test.go
155 lines (125 loc) · 4.13 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
package mailgun_test
import (
"context"
"net/http"
"testing"
"github.com/facebookgo/ensure"
"github.com/mailgun/mailgun-go/v4"
)
const (
testDomain = "mailgun.test"
testKey = "api-fake-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)
ensure.Nil(t, it.Err())
ensure.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
ensure.True(t, it.Next(ctx, &page))
ensure.Nil(t, it.Err())
dr, err := mg.GetDomain(ctx, page[0].Name)
ensure.Nil(t, err)
ensure.DeepEqual(t, len(dr.ReceivingDNSRecords) != 0, true)
ensure.DeepEqual(t, len(dr.SendingDNSRecords) != 0, true)
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")
}
ure, ok := err.(*mailgun.UnexpectedResponseError)
ensure.True(t, ok)
ensure.DeepEqual(t, ure.Actual, http.StatusNotFound)
}
func TestAddDeleteDomain(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"})
ensure.Nil(t, err)
// Next, we delete it.
ensure.Nil(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)
ensure.Nil(t, err)
ensure.DeepEqual(t, info.RequireTLS, true)
ensure.DeepEqual(t, info.SkipVerification, true)
info.RequireTLS = false
err = mg.UpdateDomainConnection(ctx, testDomain, info)
ensure.Nil(t, err)
info, err = mg.GetDomainConnection(ctx, testDomain)
ensure.Nil(t, err)
ensure.DeepEqual(t, info.RequireTLS, false)
ensure.DeepEqual(t, info.SkipVerification, true)
}
func TestDomainTracking(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
info, err := mg.GetDomainTracking(ctx, testDomain)
ensure.Nil(t, err)
ensure.DeepEqual(t, info.Unsubscribe.Active, false)
ensure.DeepEqual(t, len(info.Unsubscribe.HTMLFooter) != 0, true)
ensure.DeepEqual(t, len(info.Unsubscribe.TextFooter) != 0, true)
ensure.DeepEqual(t, info.Click.Active, true)
ensure.DeepEqual(t, info.Open.Active, true)
// Click Tracking
err = mg.UpdateClickTracking(ctx, testDomain, "no")
ensure.Nil(t, err)
info, err = mg.GetDomainTracking(ctx, testDomain)
ensure.Nil(t, err)
ensure.DeepEqual(t, info.Click.Active, false)
// Open Tracking
err = mg.UpdateOpenTracking(ctx, testDomain, "no")
ensure.Nil(t, err)
info, err = mg.GetDomainTracking(ctx, testDomain)
ensure.Nil(t, err)
ensure.DeepEqual(t, info.Open.Active, false)
// Unsubscribe
err = mg.UpdateUnsubscribeTracking(ctx, testDomain, "yes", "<h2>Hi</h2>", "Hi")
ensure.Nil(t, err)
info, err = mg.GetDomainTracking(ctx, testDomain)
ensure.Nil(t, err)
ensure.DeepEqual(t, info.Unsubscribe.Active, true)
ensure.DeepEqual(t, info.Unsubscribe.HTMLFooter, "<h2>Hi</h2>")
ensure.DeepEqual(t, info.Unsubscribe.TextFooter, "Hi")
}
func TestDomainVerify(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
_, err := mg.VerifyDomain(ctx, testDomain)
ensure.Nil(t, err)
}