-
Notifications
You must be signed in to change notification settings - Fork 40
/
subaccounts_test.go
193 lines (170 loc) · 6.28 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
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
192
193
package gosparkpost_test
import (
"reflect"
"strconv"
"strings"
"testing"
sp "github.com/SparkPost/gosparkpost"
"github.com/pkg/errors"
)
func TestSubaccountCreate(t *testing.T) {
var res200 = loadTestFile(t, "test/json/subaccount_create_200.json")
for idx, test := range []struct {
in *sp.Subaccount
err error
status int
json string
out *sp.Subaccount
}{
{nil, errors.New("Create called with nil Subaccount"), 0, "", nil},
{&sp.Subaccount{Name: "n", KeyLabel: "kl"},
errors.New("Unexpected response to Subaccount creation (results)"), 200,
strings.Replace(res200, `"results"`, `"foo"`, 1), nil},
{&sp.Subaccount{Name: "n", KeyLabel: "kl"},
errors.New("parsing api response: unexpected end of JSON input"), 200,
res200[:len(res200)/2], nil},
{&sp.Subaccount{Name: "n", KeyLabel: "kl"},
errors.New("Unexpected response to Subaccount creation (subaccount_id)"), 200,
strings.Replace(res200, `"subaccount_id"`, `"foo"`, 1), nil},
{&sp.Subaccount{Name: "n", KeyLabel: "kl"},
errors.New("Unexpected response to Subaccount creation (short_key)"), 200,
strings.Replace(res200, `"short_key"`, `"foo"`, 1), nil},
{&sp.Subaccount{Name: "n", KeyLabel: "kl"},
errors.New(`[{"message":"error","code":"","description":""}]`), 400,
`{"errors":[{"message":"error"}]}`, nil},
{&sp.Subaccount{Name: "n", KeyLabel: "kl"}, nil, 200, res200,
&sp.Subaccount{
ID: 888,
Name: "n",
KeyLabel: "kl",
ShortKey: "cf80",
Grants: sp.SubaccountGrants,
}},
} {
testSetup(t)
defer testTeardown()
mockRestResponseBuilderFormat(t, "POST", test.status, sp.SubaccountsPathFormat, test.json)
_, err := testClient.SubaccountCreate(test.in)
if err == nil && test.err != nil || err != nil && test.err == nil {
t.Errorf("SubaccountCreate[%d] => err %q want %q", idx, err, test.err)
} else if err != nil && err.Error() != test.err.Error() {
t.Errorf("SubaccountCreate[%d] => err %q want %q", idx, err, test.err)
} else if test.out != nil && !reflect.DeepEqual(test.in, test.out) {
t.Errorf("SubaccountCreate[%d] => got/want:\n%q\n%q", idx, test.in, test.out)
}
}
}
func TestSubaccountUpdate(t *testing.T) {
for idx, test := range []struct {
in *sp.Subaccount
err error
status int
json string
}{
{nil, errors.New("Subaccount Update called with nil Subaccount"), 0, ""},
{&sp.Subaccount{ID: 42, Name: "n", Status: "super"},
errors.New("Not a valid subaccount status"), 0, ""},
{&sp.Subaccount{ID: 42, Name: "n"},
errors.New("parsing api response: unexpected end of JSON input"), 200,
`{"foo":{"message":"syntax error"}`},
{&sp.Subaccount{ID: 42, Name: "n"},
errors.New(`[{"message":"error","code":"","description":""}]`), 400,
`{"errors":[{"message":"error"}]}`},
{&sp.Subaccount{ID: 42, Name: "n", Status: "active"}, nil, 200,
`{"results":{"message":"Successfully updated subaccount information"}}`},
} {
testSetup(t)
defer testTeardown()
id := "0"
if test.in != nil {
id = strconv.Itoa(test.in.ID)
}
mockRestResponseBuilderFormat(t, "PUT", test.status, sp.SubaccountsPathFormat+"/"+id, test.json)
_, err := testClient.SubaccountUpdate(test.in)
if err == nil && test.err != nil || err != nil && test.err == nil {
t.Errorf("SubaccountUpdate[%d] => err %q want %q", idx, err, test.err)
} else if err != nil && err.Error() != test.err.Error() {
t.Errorf("SubaccountUpdate[%d] => err %q want %q", idx, err, test.err)
}
}
}
func TestSubaccounts(t *testing.T) {
var res200 = loadTestFile(t, "test/json/subaccounts_200.json")
var sa200 = []sp.Subaccount{{
ID: 123,
Name: "Joe's Garage",
Status: "active",
IPPool: "my_ip_pool",
ComplianceStatus: "active",
}, {
ID: 456,
Name: "SharkPost",
Status: "active",
ComplianceStatus: "active",
}, {
ID: 789,
Name: "Dev Avocado",
Status: "suspended",
ComplianceStatus: "active",
}}
for idx, test := range []struct {
err error
status int
json string
out []sp.Subaccount
}{
{errors.New("unexpected end of JSON input"), 200, `{"foo":[]`, nil},
{errors.New("Unexpected response to Subaccount list"), 200, `{"foo":[]}`, nil},
{errors.New(`[{"message":"error","code":"","description":""}]`), 400,
`{"errors":[{"message":"error"}]}`, nil},
{nil, 200, res200, sa200},
} {
testSetup(t)
defer testTeardown()
mockRestResponseBuilderFormat(t, "GET", test.status, sp.SubaccountsPathFormat, test.json)
subs, _, err := testClient.Subaccounts()
if err == nil && test.err != nil || err != nil && test.err == nil {
t.Errorf("Subaccounts[%d] => err %q want %q", idx, err, test.err)
} else if err != nil && err.Error() != test.err.Error() {
t.Errorf("Subaccounts[%d] => err %q want %q", idx, err, test.err)
} else if test.out != nil && !reflect.DeepEqual(subs, test.out) {
t.Errorf("Subaccounts[%d] => got/want:\n%q\n%q", idx, subs, test.out)
}
}
}
func TestSubaccount(t *testing.T) {
var res200 = loadTestFile(t, "test/json/subaccount_200.json")
var sub200 = &sp.Subaccount{
ID: 123,
Name: "Joe's Garage",
Status: "active",
IPPool: "assigned_ip_pool",
ComplianceStatus: "active",
}
for idx, test := range []struct {
in int
err error
status int
json string
out *sp.Subaccount
}{
{42, errors.New("unexpected end of JSON input"), 200, "{", nil},
{42, errors.New("Unexpected response to Subaccount"), 200, `{"foo":{}}`, nil},
{42, errors.New(`[{"message":"error","code":"","description":""}]`), 400,
`{"errors":[{"message":"error"}]}`, nil},
{123, nil, 200, res200, sub200},
} {
testSetup(t)
defer testTeardown()
id := strconv.Itoa(test.in)
mockRestResponseBuilderFormat(t, "GET", test.status, sp.SubaccountsPathFormat+"/"+id, test.json)
sub, _, err := testClient.Subaccount(test.in)
if err == nil && test.err != nil || err != nil && test.err == nil {
t.Errorf("SubaccountCreate[%d] => err %q want %q", idx, err, test.err)
} else if err != nil && err.Error() != test.err.Error() {
t.Errorf("SubaccountCreate[%d] => err %q want %q", idx, err, test.err)
} else if test.out != nil && !reflect.DeepEqual(sub, test.out) {
t.Errorf("SubaccountCreate[%d] => got/want:\n%q\n%q", idx, sub, test.out)
}
}
}