-
Notifications
You must be signed in to change notification settings - Fork 143
/
subaccounts_test.go
123 lines (93 loc) · 2.98 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
123
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 (
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)
require.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)
require.NoError(t, iterator.Err())
require.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)
require.NotNil(t, iterator)
page := []mailgun.Subaccount{}
require.True(t, iterator.Next(context.Background(), &page))
require.NoError(t, iterator.Err())
resp, err := mg.SubaccountDetails(ctx, page[0].Id)
require.NoError(t, err)
require.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")
}
var ure *mailgun.UnexpectedResponseError
require.ErrorAs(t, err, &ure)
require.Equal(t, http.StatusNotFound, ure.Actual)
}
func TestCreateSubaccount(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
resp, err := mg.CreateSubaccount(ctx, testSubaccountName)
require.NoError(t, err)
require.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)
require.NoError(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)
require.NoError(t, err)
assert.Equal(t, "enabled", resp.Item.Status)
}
func TestDisableSubaccount(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
resp, err := mg.DisableSubaccount(ctx, testEnabledSubaccountId)
require.NoError(t, err)
assert.Equal(t, "disabled", resp.Item.Status)
}
func TestDisableSubaccountAlreadyDisabled(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
_, err := mg.DisableSubaccount(ctx, testDisabledSubaccountId)
require.NoError(t, err)
}