-
Notifications
You must be signed in to change notification settings - Fork 3
/
objects_users_test.go
143 lines (119 loc) · 4 KB
/
objects_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
package cloudscale
import (
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestObjectsUser_Create(t *testing.T) {
setup()
defer teardown()
ObjectsUserRequest := &ObjectsUserRequest{
DisplayName: "TestBucket",
TaggedResourceRequest: TaggedResourceRequest{
&TagMap{
"tag": "one",
"other": "tag",
},
},
}
mux.HandleFunc("/v1/objects-users", func(w http.ResponseWriter, r *http.Request) {
expected := map[string]interface{}{
"display_name": "TestBucket",
"tags": map[string]interface{}{
"tag": "one",
"other": "tag",
},
}
var v map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
if !reflect.DeepEqual(v, expected) {
t.Errorf("Request body\n got=%#v\nwant=%#v", v, expected)
}
jsonStr := `{
"href": "https://api.cloudscale.ch/v1/objects-users/6fe39134bf4178747eebc429f82cfafdd08891d4279d0d899bc4012db1db6a15",
"id": "6fe39134bf4178747eebc429f82cfafdd08891d4279d0d899bc4012db1db6a15",
"display_name": "alan",
"keys": [{
"access_key": "0ZTAIBKSGYBRHQ09G11W",
"secret_key": "bn2ufcwbIa0ARLc5CLRSlVaCfFxPHOpHmjKiH34T"
}],
"tags": {
"tag": "one",
"other": "tag"
}
}`
fmt.Fprintf(w, jsonStr)
})
objectsUser, err := client.ObjectsUsers.Create(ctx, ObjectsUserRequest)
if err != nil {
t.Errorf("ObjectsUser.Create returned error: %v", err)
return
}
if id := objectsUser.ID; id != "6fe39134bf4178747eebc429f82cfafdd08891d4279d0d899bc4012db1db6a15" {
t.Errorf("expected id '%s', received '%s'", objectsUser.ID, id)
}
}
func TestObjectsUser_Get(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v1/objects-users/6fe39134bf4178747eebc429f82cfafdd08891d4279d0d899bc4012db1db6a15", func(w http.ResponseWriter, r *http.Request) {
testHTTPMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"id": "6fe39134bf4178747eebc429f82cfafdd08891d4279d0d899bc4012db1db6a15"}`)
})
objectUser, err := client.ObjectsUsers.Get(ctx, "6fe39134bf4178747eebc429f82cfafdd08891d4279d0d899bc4012db1db6a15")
if err != nil {
t.Errorf("ObjectsUser.Get returned error: %v", err)
}
expected := &ObjectsUser{ID: "6fe39134bf4178747eebc429f82cfafdd08891d4279d0d899bc4012db1db6a15"}
if !reflect.DeepEqual(objectUser, expected) {
t.Errorf("ObjectsUser.Get\n got=%#v\nwant=%#v", objectUser, expected)
}
}
func TestObjectsUser_Delete(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v1/objects-users/6fe39134bf4178747eebc429f82cfafdd08891d4279d0d899bc4012db1db6a15", func(w http.ResponseWriter, r *http.Request) {
testHTTPMethod(t, r, http.MethodDelete)
})
err := client.ObjectsUsers.Delete(ctx, "6fe39134bf4178747eebc429f82cfafdd08891d4279d0d899bc4012db1db6a15")
if err != nil {
t.Errorf("ObjectsUser.Delete returned error: %v", err)
}
}
func TestObjectsUser_List(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v1/objects-users", func(w http.ResponseWriter, r *http.Request) {
testHTTPMethod(t, r, http.MethodGet)
fmt.Fprint(w, `[{"id": "6fe39134bf4178747eebc429f82cfafdd08891d4279d0d899bc4012db1db6a15"}]`)
})
objectUsers, err := client.ObjectsUsers.List(ctx)
if err != nil {
t.Errorf("ObjectsUser.List returned error: %v", err)
}
expected := []ObjectsUser{{ID: "6fe39134bf4178747eebc429f82cfafdd08891d4279d0d899bc4012db1db6a15"}}
if !reflect.DeepEqual(objectUsers, expected) {
t.Errorf("ObjectsUser.List\n got=%#v\nwant=%#v", objectUsers, expected)
}
}
func TestObjectsUser_Update(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v1/objects-users/6fe39134bf4178747eebc429f82cfafdd08891d4279d0d899bc4012db1db6a15", func(w http.ResponseWriter, r *http.Request) {
testHTTPMethod(t, r, http.MethodPatch)
})
userID := "6fe39134bf4178747eebc429f82cfafdd08891d4279d0d899bc4012db1db6a15"
req := &ObjectsUserRequest{
DisplayName: "new_name",
}
err := client.ObjectsUsers.Update(context.TODO(), userID, req)
if err != nil {
t.Errorf("ObjectsUser.Update returned error: %v", err)
}
}