-
Notifications
You must be signed in to change notification settings - Fork 91
/
bucket_policy_test.go
121 lines (109 loc) · 2.83 KB
/
bucket_policy_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
package cos
import (
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestBucketService_GetPolicy(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
vs := values{
"policy": "",
}
testFormValues(t, r, vs)
fmt.Fprint(w, `{
"Statement": [
{
"Principal": {
"qcs": [
"qcs::cam::uin/100000000001:uin/100000000011"
]
},
"Effect": "allow",
"Action": [
"name/cos:GetBucket"
],
"Resource": [
"qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"
]
}
],
"version": "2.0"
}`)
})
res, _, err := client.Bucket.GetPolicy(context.Background())
if err != nil {
t.Fatalf("Bucket.GetPolicy returned error %v", err)
}
want := &BucketGetPolicyResult{
Statement: []BucketStatement{
{
Principal: map[string][]string{
"qcs": []string{"qcs::cam::uin/100000000001:uin/100000000011"},
},
Effect: "allow",
Action: []string{"name/cos:GetBucket"},
Resource: []string{"qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"},
},
},
Version: "2.0",
}
if !reflect.DeepEqual(res, want) {
t.Errorf("Bucket.GetPolicy returned %+v, want %+v", res, want)
}
}
func TestBucketService_PutPolicy(t *testing.T) {
setup()
defer teardown()
opt := &BucketPutPolicyOptions{
Statement: []BucketStatement{
{
Principal: map[string][]string{
"qcs": []string{"qcs::cam::uin/100000000001:uin/100000000011"},
},
Effect: "allow",
Action: []string{"name/cos:GetBucket"},
Resource: []string{"qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"},
},
},
Version: "2.0",
}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
vs := values{
"policy": "",
}
testFormValues(t, r, vs)
body := new(BucketPutPolicyOptions)
json.NewDecoder(r.Body).Decode(body)
want := opt
if !reflect.DeepEqual(body, want) {
t.Errorf("Bucket.PutPolicy request\n body: %+v\n, want %+v\n", body, want)
}
})
_, err := client.Bucket.PutPolicy(context.Background(), opt)
if err != nil {
t.Fatalf("Bucket.PutPolicy returned error: %v", err)
}
}
func TestBucketService_DeletePolicy(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
vs := values{
"policy": "",
}
testFormValues(t, r, vs)
w.WriteHeader(http.StatusNoContent)
})
_, err := client.Bucket.DeletePolicy(context.Background())
if err != nil {
t.Fatalf("Bucket.DeletePolicy returned error: %v", err)
}
}