forked from digitalocean/doctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_test.go
147 lines (123 loc) · 3.31 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
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 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)))
})
})
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":
auth := req.Header.Get("Authorization")
if auth != "Bearer some-magic-token" {
w.WriteHeader(http.StatusUnauthorized)
return
}
if req.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
w.Header().Add("RateLimit-Limit", "200")
w.Header().Add("RateLimit-Remaining", "199")
w.Header().Add("RateLimit-Reset", "1565385881")
w.Write([]byte(`{ "account":{}}`))
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)))
})
})
const (
accountGetResponse = `
{
"account": {
"droplet_limit": 25,
"floating_ip_limit": 5,
"email": "[email protected]",
"uuid": "b6fr89dbf6d9156cace5f3c78dc9851d957381ef",
"email_verified": true,
"status": "active",
"status_message": ""
}
}`
accountOutput = `
Email Droplet Limit Email Verified UUID Status
[email protected] 25 true b6fr89dbf6d9156cace5f3c78dc9851d957381ef active
`
ratelimitOutput = `
Limit Remaining Reset
200 199 %s
`
)