-
Notifications
You must be signed in to change notification settings - Fork 303
/
reserved_ips_actions_test.go
167 lines (135 loc) · 4.51 KB
/
reserved_ips_actions_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
package godo
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestReservedIPsActions_Assign(t *testing.T) {
setup()
defer teardown()
dropletID := 12345
assignRequest := &ActionRequest{
"droplet_id": float64(dropletID), // encoding/json decodes numbers as floats
"type": "assign",
}
mux.HandleFunc("/v2/reserved_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, assignRequest) {
t.Errorf("Request body = %#v, expected %#v", v, assignRequest)
}
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})
assign, _, err := client.ReservedIPActions.Assign(ctx, "192.168.0.1", 12345)
if err != nil {
t.Errorf("ReservedIPsActions.Assign returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(assign, expected) {
t.Errorf("ReservedIPsActions.Assign returned %+v, expected %+v", assign, expected)
}
}
func TestReservedIPsActions_Unassign(t *testing.T) {
setup()
defer teardown()
unassignRequest := &ActionRequest{
"type": "unassign",
}
mux.HandleFunc("/v2/reserved_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, unassignRequest) {
t.Errorf("Request body = %+v, expected %+v", v, unassignRequest)
}
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})
action, _, err := client.ReservedIPActions.Unassign(ctx, "192.168.0.1")
if err != nil {
t.Errorf("ReservedIPsActions.Get returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(action, expected) {
t.Errorf("ReservedIPsActions.Get returned %+v, expected %+v", action, expected)
}
}
func TestReservedIPsActions_Get(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/reserved_ips/192.168.0.1/actions/456", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})
action, _, err := client.ReservedIPActions.Get(ctx, "192.168.0.1", 456)
if err != nil {
t.Errorf("ReservedIPsActions.Get returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(action, expected) {
t.Errorf("ReservedIPsActions.Get returned %+v, expected %+v", action, expected)
}
}
func TestReservedIPsActions_List(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/reserved_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprintf(w, `{"actions":[{"status":"in-progress"}]}`)
})
actions, _, err := client.ReservedIPActions.List(ctx, "192.168.0.1", nil)
if err != nil {
t.Errorf("ReservedIPsActions.List returned error: %v", err)
}
expected := []Action{{Status: "in-progress"}}
if !reflect.DeepEqual(actions, expected) {
t.Errorf("ReservedIPsActions.List returned %+v, expected %+v", actions, expected)
}
}
func TestReservedIPsActions_ListMultiplePages(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/reserved_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"actions":[{"status":"in-progress"}], "links":{"pages":{"next":"http://example.com/v2/reserved_ips/192.168.0.1/actions?page=2"}}}`)
})
_, resp, err := client.ReservedIPActions.List(ctx, "192.168.0.1", nil)
if err != nil {
t.Errorf("ReservedIPsActions.List returned error: %v", err)
}
checkCurrentPage(t, resp, 1)
}
func TestReservedIPsActions_ListPageByNumber(t *testing.T) {
setup()
defer teardown()
jBlob := `
{
"actions":[{"status":"in-progress"}],
"links":{
"pages":{
"next":"http://example.com/v2/reserved_ips/?page=3",
"prev":"http://example.com/v2/reserved_ips/?page=1",
"last":"http://example.com/v2/reserved_ips/?page=3",
"first":"http://example.com/v2/reserved_ips/?page=1"
}
}
}`
mux.HandleFunc("/v2/reserved_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})
opt := &ListOptions{Page: 2}
_, resp, err := client.ReservedIPActions.List(ctx, "192.168.0.1", opt)
if err != nil {
t.Errorf("ReservedIPsActions.List returned error: %v", err)
}
checkCurrentPage(t, resp, 2)
}