forked from vultr/govultr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_test.go
174 lines (145 loc) · 3.81 KB
/
user_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
package govultr
import (
"fmt"
"net/http"
"reflect"
"testing"
)
func TestUserServiceHandler_Create(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/users", func(writer http.ResponseWriter, request *http.Request) {
response := `{"user": {"id": "564a1a88947b4","name": "Example User","email": "[email protected]","api_key": "aaavvvvvvbbbbbb","api_enabled": true,"acls": []}}`
fmt.Fprint(writer, response)
})
api := true
userReq := &UserReq{
Email: "[email protected]",
Name: "Example User",
APIEnabled: &api,
Password: "password",
}
user, _, err := client.User.Create(ctx, userReq)
if err != nil {
t.Errorf("User.Create returned %+v, expected %+v", err, nil)
}
expected := &User{
ID: "564a1a88947b4",
Name: "Example User",
Email: "[email protected]",
APIEnabled: BoolToBoolPtr(true),
APIKey: "aaavvvvvvbbbbbb",
ACL: []string{},
}
if !reflect.DeepEqual(user, expected) {
t.Errorf("User.Create returned %+v, expected %+v", user, expected)
}
}
func TestUserServiceHandler_Delete(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/users/123abc", func(writer http.ResponseWriter, request *http.Request) {
fmt.Fprint(writer)
})
err := client.User.Delete(ctx, "123abc")
if err != nil {
t.Errorf("User.Delete returned %+v, expected %+v", err, nil)
}
}
func TestUserServiceHandler_List(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/users", func(writer http.ResponseWriter, request *http.Request) {
response := `
{
"users": [
{
"id": "f255efc9700d9",
"name": "test api",
"email": "[email protected]",
"api_enabled": true,
"acls": []
}
],
"meta": {
"total": 1,
"links": {
"next": "thisismycusror",
"prev": ""
}
}
}
`
fmt.Fprint(writer, response)
})
options := &ListOptions{
PerPage: 1,
}
users, meta, _, err := client.User.List(ctx, options)
if err != nil {
t.Errorf("User.List returned error: %v", err)
}
expected := []User{
{
ID: "f255efc9700d9",
Name: "test api",
Email: "[email protected]",
APIEnabled: BoolToBoolPtr(true),
ACL: []string{},
},
}
if !reflect.DeepEqual(users, expected) {
t.Errorf("User.List users returned %+v, expected %+v", users, expected)
}
expectedMeta := &Meta{
Total: 1,
Links: &Links{
Next: "thisismycusror",
Prev: "",
},
}
if !reflect.DeepEqual(meta, expectedMeta) {
t.Errorf("User.List meta returned %+v, expected %+v", meta, expectedMeta)
}
}
func TestUserServiceHandler_Update(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/users/abc123", func(writer http.ResponseWriter, request *http.Request) {
fmt.Fprint(writer)
})
api := true
user := &UserReq{
Name: "Example User",
Password: "w1a4dcnst0n!",
Email: "[email protected]",
APIEnabled: &api,
ACL: []string{"support"},
}
err := client.User.Update(ctx, "abc123", user)
if err != nil {
t.Errorf("User.Update returned error: %+v", err)
}
}
func TestUserServiceHandler_Get(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/users/abc123", func(writer http.ResponseWriter, request *http.Request) {
response := `{"user": {"id": "f255efc9c69ac","name": "Unit Test","email": "[email protected]","api_enabled": true,"acls": []}}`
fmt.Fprint(writer, response)
})
user, _, err := client.User.Get(ctx, "abc123")
if err != nil {
t.Errorf("User.Get returned error: %v", err)
}
expected := &User{
ID: "f255efc9c69ac",
Name: "Unit Test",
Email: "[email protected]",
APIEnabled: BoolToBoolPtr(true),
ACL: []string{},
}
if !reflect.DeepEqual(user, expected) {
t.Errorf("User.List users returned %+v, expected %+v", user, expected)
}
}