-
Notifications
You must be signed in to change notification settings - Fork 5
/
users_test.go
389 lines (333 loc) · 8.77 KB
/
users_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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package oneandone
import (
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"strings"
"sync"
"testing"
"time"
)
var (
set_user sync.Once
getc_user sync.Once
tuname string
tudesc string
tumail string
tu_id string
cur_user *User
test_user *User
)
// Helper functions
func create_user() *User {
rand.Seed(time.Now().UnixNano())
r := rand.Intn(999999)
tuname = fmt.Sprintf("testuser_%d", r)
tudesc = fmt.Sprintf("testuser_%d description", r)
tumail = "[email protected]"
req := UserRequest{
Name: tuname,
Description: tudesc,
Password: fmt.Sprintf("Ss&)Hg3@&9!hJ&5%d", r),
Email: tumail,
}
fmt.Printf("Creating new user '%s'...\n", tuname)
u_id, u, err := api.CreateUser(&req)
if err != nil {
fmt.Printf("Unable to create a user. Error: %s", err.Error())
return nil
}
if u_id == "" || u.Id == "" {
fmt.Printf("Unable to create user '%s'.", tuname)
return nil
}
return u
}
func get_my_pub_ip() string {
res, err := http.Get("http://echoip.com/")
if err != nil {
return ""
}
defer res.Body.Close()
ip, err := ioutil.ReadAll(res.Body)
if err != nil {
return ""
}
return string(ip)
}
func set_test_users() {
test_user = create_user()
}
func get_current_user() {
res, _ := api.ListUsers()
if res != nil {
for _, u := range res {
if Token == u.Api.Key {
cur_user = &u
break
}
}
}
}
// /users tests
func TestCreateUser(t *testing.T) {
t.Skip("TestCreateUser is skipped at the moment.")
set_user.Do(set_test_users)
if test_user == nil {
t.Errorf("CreateUser failed.")
return
}
if !strings.Contains(test_user.Name, tuname) {
t.Errorf("Wrong user name.")
}
if test_user.Description != tudesc {
t.Errorf("Wrong user description.")
}
if test_user.Email != tumail {
t.Errorf("Wrong user email.")
}
}
func TestListUsers(t *testing.T) {
// set_user.Do(set_test_users)
fmt.Println("Listing all users...")
res, err := api.ListUsers()
if err != nil {
t.Errorf("ListUsers failed. Error: " + err.Error())
}
if len(res) == 0 {
t.Errorf("No user found.")
}
for _, u := range res {
if Token == u.Api.Key {
cur_user = &u
break
}
}
res, err = api.ListUsers(1, 3, "name", "", "id,name")
if err != nil {
t.Errorf("ListUsers with parameter options failed. Error: " + err.Error())
return
}
if len(res) == 0 {
t.Errorf("No user found.")
}
if len(res) != 3 {
t.Errorf("Wrong number of objects per page.")
}
for i, _ := range res {
if res[i].Id == "" || res[i].Name == "" || res[i].State != "" ||
res[i].Api != nil || res[i].Role != nil {
t.Errorf("Filtering parameters failed.")
}
if i < len(res)-1 {
if res[i].Name > res[i+1].Name {
t.Errorf("Sorting list of users failed.")
}
}
}
// Test for error response
res, err = api.ListUsers("", nil, 10, "15", "")
if res != nil || err == nil {
t.Errorf("ListUsers failed to handle incorrect argument type.")
}
res, err = api.ListUsers(0, 0, "", cur_user.Name, "")
if err != nil {
t.Errorf("ListUsers with parameter options failed. Error: " + err.Error())
return
}
if len(res) != 1 {
t.Errorf("Search parameter failed.")
}
if res[0].Name != cur_user.Name {
t.Errorf("Search parameter failed.")
}
}
func TestGetUser(t *testing.T) {
getc_user.Do(get_current_user)
fmt.Printf("Getting user '%s'...\n", cur_user.Name)
u, err := api.GetUser(cur_user.Id)
if err != nil {
t.Errorf("GetUser failed. Error: " + err.Error())
} else {
if u.Id != cur_user.Id {
t.Errorf("Wrong user ID.")
}
}
}
func TestGetCurrentUserPermissions(t *testing.T) {
getc_user.Do(get_current_user)
fmt.Printf("Getting current user permissions ...\n")
p, err := api.GetCurrentUserPermissions()
if err != nil {
t.Errorf("GetCurrentUserPermissions failed. Error: " + err.Error())
} else {
if p == nil || p.Backups == nil || p.Firewalls == nil || p.Images == nil || p.Invoice == nil ||
p.IPs == nil || p.LoadBalancers == nil || p.Logs == nil || p.MonitorCenter == nil ||
p.MonitorPolicies == nil || p.PrivateNetworks == nil || p.Roles == nil || p.Servers == nil ||
p.SharedStorage == nil || p.Usages == nil || p.Users == nil || p.VPNs == nil {
t.Errorf("Some permissions objects are missing.")
}
}
}
func TestGetUserApi(t *testing.T) {
getc_user.Do(get_current_user)
fmt.Printf("Getting API data of user '%s'...\n", cur_user.Name)
ua, err := api.GetUserApi(cur_user.Id)
if err != nil {
t.Errorf("GetUserApi failed. Error: " + err.Error())
} else {
if ua.Key != cur_user.Api.Key {
t.Errorf("Wrong user key.")
}
if !ua.Active {
t.Errorf("Wrong user active state.")
}
}
}
func TestModifyUserApi(t *testing.T) {
getc_user.Do(get_current_user)
fmt.Printf("Modify API state of user '%s'...\n", cur_user.Name)
// Just making sure that the request pass.
// TODO: test with active=false once the REST functionality is completed.
u, err := api.ModifyUserApi(cur_user.Id, true)
if err != nil {
t.Errorf("ModifyUserApi failed. Error: " + err.Error())
} else {
if u.Api.Key != cur_user.Api.Key {
t.Errorf("Wrong user key.")
}
if !u.Api.Active {
t.Errorf("Wrong user active state.")
}
}
}
func TestGetUserApiKey(t *testing.T) {
getc_user.Do(get_current_user)
fmt.Printf("Getting API data of user '%s'...\n", cur_user.Name)
key, err := api.GetUserApiKey(cur_user.Id)
if err != nil {
t.Errorf("GetUserApiKey failed. Error: " + err.Error())
} else {
if key.Key != cur_user.Api.Key {
t.Errorf("Wrong user key.")
}
}
}
func TestAddUserApiAlowedIps(t *testing.T) {
getc_user.Do(get_current_user)
fmt.Printf("Adding API allowed IPs to user '%s'...\n", cur_user.Name)
my_ip := get_my_pub_ip()
if my_ip == "" {
fmt.Println("Not able to obtain its own public IP. Skipping the test.")
return
}
ips := []string{my_ip, "192.168.7.77", "10.81.12.101"}
u, err := api.AddUserApiAlowedIps(cur_user.Id, ips)
if err != nil {
t.Errorf("AddUserApiAlowedIps failed. Error: " + err.Error())
} else {
if len(u.Api.AllowedIps) != 3 {
t.Errorf("Unable to add API allowed IPs to the user.")
}
for _, a := range u.Api.AllowedIps {
if a != my_ip && a != "192.168.7.77" && a != "10.81.12.101" {
t.Errorf("Wrong IP added to user's API allowed list.")
}
}
}
}
func TestListUserApiAllowedIps(t *testing.T) {
getc_user.Do(get_current_user)
fmt.Printf("Listing API allowed IPs to user '%s'...\n", cur_user.Name)
ips, err := api.ListUserApiAllowedIps(cur_user.Id)
if err != nil {
t.Errorf("ListUserApiAllowedIps failed. Error: " + err.Error())
} else {
if len(ips) != 3 {
t.Errorf("Wrong number of API allowed IPs found.")
}
}
}
func TestRemoveUserApiAllowedIp(t *testing.T) {
getc_user.Do(get_current_user)
fmt.Printf("Removing API allowed IPs to user '%s'...\n", cur_user.Name)
my_ip := get_my_pub_ip()
if my_ip == "" {
fmt.Println("Not able to obtain its own public IP. Skipping the test.")
return
}
ips := []string{"192.168.7.77", "10.81.12.101", my_ip}
for _, ip := range ips {
_, err := api.RemoveUserApiAllowedIp(cur_user.Id, ip)
if err != nil {
t.Errorf("RemoveUserApiAllowedIp failed. Error: " + err.Error())
}
}
u, _ := api.GetUser(cur_user.Id)
if len(u.Api.AllowedIps) != 0 {
t.Errorf("RemoveUserApiAllowedIp failed.")
}
}
func TestRenewUserApiKey(t *testing.T) {
t.Skip("TestRenewUserApiKey is skipped at the moment.")
getc_user.Do(get_current_user)
fmt.Printf("Renewing API key of user '%s'...\n", cur_user.Name)
u, err := api.RenewUserApiKey(cur_user.Id)
if err != nil {
t.Errorf("RenewUserApiKey failed. Error: " + err.Error())
} else {
if u.Api.Key == cur_user.Api.Key {
t.Errorf("Unable to renew user key.")
} else {
cur_user = u
api = New(u.Api.Key, BaseUrl)
SetToken(u.Api.Key)
setEnvironmentVar("ONEANDONE_TOKEN", u.Api.Key)
}
}
}
func TestModifyUser(t *testing.T) {
getc_user.Do(get_current_user)
fmt.Printf("Modifying user '%s'...\n", cur_user.Name)
rand.Seed(time.Now().UnixNano())
r := rand.Intn(999999)
new_pass := fmt.Sprintf("%d^&*bbYhgf%djv;HF", r, r)
new_desc := tudesc + "_updated"
new_mail := "[email protected]"
req := UserRequest{
Description: new_desc,
Email: new_mail,
Password: new_pass,
// State: "DISABLED",
}
u, err := api.ModifyUser(cur_user.Id, &req)
if err != nil {
t.Errorf("ModifyUser failed. Error: " + err.Error())
return
}
if u.Description != new_desc {
t.Errorf("User description not updated.")
}
if u.Email != new_mail {
t.Errorf("User email not updated.")
}
}
func TestDeleteUser(t *testing.T) {
t.Skip("TestDeleteUser is skipped at the moment.")
getc_user.Do(get_current_user)
fmt.Printf("Deleting user '%s'...\n", cur_user.Name)
u, err := api.DeleteUser(cur_user.Id)
if err != nil {
t.Errorf("DeleteUser failed. Error: " + err.Error())
return
}
if u != nil {
u, err = api.GetUser(u.Id)
if u != nil {
t.Errorf("Unable to delete the user.")
} else {
cur_user = nil
}
}
}