/
subaccounts_test.go
122 lines (92 loc) · 2.9 KB
/
subaccounts_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
package mailgun_test
import (
"context"
"net/http"
"testing"
"github.com/facebookgo/ensure"
"github.com/mailgun/mailgun-go/v4"
)
const (
testSubaccountName = "mailgun.test"
testEnabledSubaccountId = "enabled.subaccount"
testDisabledSubaccountId = "disabled.subaccount"
)
func TestListSubaccounts(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
iterator := mg.ListSubaccounts(nil)
ensure.NotNil(t, iterator)
ctx := context.Background()
var page []mailgun.Subaccount
for iterator.Next(ctx, &page) {
for _, d := range page {
t.Logf("TestListSubaccounts: %#v\n", d)
}
}
t.Logf("TestListSubaccounts: %d subaccounts retrieved\n", iterator.Total)
ensure.Nil(t, iterator.Err())
ensure.True(t, iterator.Total != 0)
}
func TestSubaccountDetails(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
iterator := mg.ListSubaccounts(nil)
ensure.NotNil(t, iterator)
page := []mailgun.Subaccount{}
ensure.True(t, iterator.Next(context.Background(), &page))
ensure.Nil(t, iterator.Err())
resp, err := mg.SubaccountDetails(ctx, page[0].Id)
ensure.Nil(t, err)
ensure.NotNil(t, resp)
}
func TestSubaccountDetailsStatusNotFound(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
_, err := mg.SubaccountDetails(ctx, "unexisting.id")
if err == nil {
t.Fatal("Did not expect a subaccount to exist")
}
ure, ok := err.(*mailgun.UnexpectedResponseError)
ensure.True(t, ok)
ensure.DeepEqual(t, ure.Actual, http.StatusNotFound)
}
func TestCreateSubaccount(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
resp, err := mg.CreateSubaccount(ctx, testSubaccountName)
ensure.Nil(t, err)
ensure.NotNil(t, resp)
}
func TestEnableSubaccountAlreadyEnabled(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
_, err := mg.EnableSubaccount(ctx, testEnabledSubaccountId)
ensure.Nil(t, err)
}
func TestEnableSubaccount(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
resp, err := mg.EnableSubaccount(ctx, testDisabledSubaccountId)
ensure.Nil(t, err)
ensure.DeepEqual(t, resp.Item.Status, "enabled")
}
func TestDisableSubaccount(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
resp, err := mg.DisableSubaccount(ctx, testEnabledSubaccountId)
ensure.Nil(t, err)
ensure.DeepEqual(t, resp.Item.Status, "disabled")
}
func TestDisableSubaccountAlreadyDisabled(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
_, err := mg.DisableSubaccount(ctx, testDisabledSubaccountId)
ensure.Nil(t, err)
}