-
Notifications
You must be signed in to change notification settings - Fork 397
/
account_test.go
256 lines (213 loc) · 6.26 KB
/
account_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package integration
import (
"fmt"
"net/http"
"net/http/httptest"
"net/http/httputil"
"os/exec"
"strings"
"testing"
"time"
"github.com/sclevine/spec"
"github.com/stretchr/testify/require"
)
var _ = suite("account/get", func(t *testing.T, when spec.G, it spec.S) {
var (
expect *require.Assertions
server *httptest.Server
)
it.Before(func() {
expect = require.New(t)
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("content-type", "application/json")
switch req.URL.Path {
case "/v2/account":
auth := req.Header.Get("Authorization")
if auth == "Bearer user-context-token" {
w.Write([]byte(userContextGetResponse))
return
}
if auth != "Bearer some-magic-token" {
w.WriteHeader(http.StatusUnauthorized)
return
}
if req.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
w.Write([]byte(accountGetResponse))
default:
dump, err := httputil.DumpRequest(req, true)
if err != nil {
t.Fatal("failed to dump request")
}
t.Fatalf("received unknown request: %s", dump)
}
}))
})
it("returns the details of my account", func() {
cmd := exec.Command(builtBinaryPath,
"-t", "some-magic-token",
"-u", server.URL,
"account",
"get",
)
output, err := cmd.CombinedOutput()
expect.NoError(err)
expect.Equal(strings.TrimSpace(accountOutput), strings.TrimSpace(string(output)))
})
when("format flags are passed", func() {
it("only displays the correct fields", func() {
cmd := exec.Command(builtBinaryPath,
"-t", "some-magic-token",
"-u", server.URL,
"account",
"get",
"--format", "Email,UUID,TeamUUID",
)
output, err := cmd.CombinedOutput()
expect.NoError(err, string(output))
expect.Equal(strings.TrimSpace(formattedAccountOutput), strings.TrimSpace(string(output)))
})
})
it("doesn't panic in a user context", func() {
cmd := exec.Command(builtBinaryPath,
"-t", "user-context-token",
"-u", server.URL,
"account",
"get",
)
output, err := cmd.CombinedOutput()
expect.NoError(err)
expect.Equal(strings.TrimSpace(userContextOutput), strings.TrimSpace(string(output)))
})
})
var _ = suite("account/ratelimit", func(t *testing.T, when spec.G, it spec.S) {
var (
expect *require.Assertions
server *httptest.Server
)
it.Before(func() {
expect = require.New(t)
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
switch req.URL.Path {
case "/v2/account":
if req.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
auth := req.Header.Get("Authorization")
if auth == "Bearer some-magic-token" {
w.Header().Add("RateLimit-Limit", "200")
w.Header().Add("RateLimit-Remaining", "199")
w.Header().Add("RateLimit-Reset", "1565385881")
w.Write([]byte(`{ "account":{}}`))
return
}
if auth == "Bearer token-with-ratelimit-exhausted" {
w.Header().Add("RateLimit-Limit", "200")
w.Header().Add("RateLimit-Remaining", "0")
w.Header().Add("RateLimit-Reset", "1565385881")
w.WriteHeader(http.StatusTooManyRequests)
w.Write([]byte(`{ "id":"too_many_requests", "message":"Too many requests"}`))
return
}
w.WriteHeader(http.StatusUnauthorized)
return
default:
dump, err := httputil.DumpRequest(req, true)
if err != nil {
t.Fatal("failed to dump request")
}
t.Fatalf("received unknown request: %s", dump)
}
}))
})
it("returns the current rate limits for my account", func() {
cmd := exec.Command(builtBinaryPath,
"-t", "some-magic-token",
"-u", server.URL,
"account",
"ratelimit",
)
output, err := cmd.CombinedOutput()
expect.NoError(err)
t := time.Unix(1565385881, 0)
expectedOutput := strings.TrimSpace(fmt.Sprintf(ratelimitOutput, t))
expect.Equal(expectedOutput, strings.TrimSpace(string(output)))
})
it("doesn't return an error when rate-limited", func() {
cmd := exec.Command(builtBinaryPath,
"-t", "token-with-ratelimit-exhausted",
"-u", server.URL,
"account",
"ratelimit",
)
output, err := cmd.CombinedOutput()
expect.NoError(err, string(output))
t := time.Unix(1565385881, 0)
expectedOutput := strings.TrimSpace(fmt.Sprintf(ratelimitExhaustedOutput, t))
expect.Equal(expectedOutput, strings.TrimSpace(string(output)))
})
it("doesn't retry when rate-limited", func() {
cmd := exec.Command(builtBinaryPath,
"-t", "token-with-ratelimit-exhausted",
"-u", server.URL,
"account",
"ratelimit", "--trace",
)
output, err := cmd.CombinedOutput()
expect.NoError(err, string(output))
expect.NotContains(strings.TrimSpace(string(output)), "retrying in")
})
})
const (
accountGetResponse = `
{
"account": {
"droplet_limit": 25,
"floating_ip_limit": 5,
"email": "[email protected]",
"uuid": "b6fr89dbf6d9156cace5f3c78dc9851d957381ef",
"email_verified": true,
"status": "active",
"status_message": "",
"team": {
"uuid": "e8566708-f6fd-11ec-aac1-7f9bcd99de41",
"name": "My Team"
}
}
}`
userContextGetResponse = `
{
"account": {
"droplet_limit": 25,
"floating_ip_limit": 5,
"email": "[email protected]",
"uuid": "b6fr89dbf6d9156cace5f3c78dc9851d957381ef",
"email_verified": true,
"status": "active",
"status_message": ""
}
}`
accountOutput = `
User Email Team Droplet Limit Email Verified User UUID Status
[email protected] My Team 25 true b6fr89dbf6d9156cace5f3c78dc9851d957381ef active
`
userContextOutput = `
User Email Team Droplet Limit Email Verified User UUID Status
[email protected] <nil> 25 true b6fr89dbf6d9156cace5f3c78dc9851d957381ef active
`
formattedAccountOutput = `
User Email User UUID Team UUID
[email protected] b6fr89dbf6d9156cace5f3c78dc9851d957381ef e8566708-f6fd-11ec-aac1-7f9bcd99de41
`
ratelimitOutput = `
Limit Remaining Reset
200 199 %s
`
ratelimitExhaustedOutput = `
Limit Remaining Reset
200 0 %s
`
)