You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
2.8 KiB

  1. package cos
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "reflect"
  8. "testing"
  9. )
  10. func TestBucketService_GetPolicy(t *testing.T) {
  11. setup()
  12. defer teardown()
  13. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  14. testMethod(t, r, "GET")
  15. vs := values{
  16. "policy": "",
  17. }
  18. testFormValues(t, r, vs)
  19. fmt.Fprint(w, `{
  20. "Statement": [
  21. {
  22. "Principal": {
  23. "qcs": [
  24. "qcs::cam::uin/100000000001:uin/100000000011"
  25. ]
  26. },
  27. "Effect": "allow",
  28. "Action": [
  29. "name/cos:GetBucket"
  30. ],
  31. "Resource": [
  32. "qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"
  33. ]
  34. }
  35. ],
  36. "version": "2.0"
  37. }`)
  38. })
  39. res, _, err := client.Bucket.GetPolicy(context.Background())
  40. if err != nil {
  41. t.Fatalf("Bucket.GetPolicy returned error %v", err)
  42. }
  43. want := &BucketGetPolicyResult{
  44. Statement: []BucketStatement{
  45. {
  46. Principal: map[string][]string{
  47. "qcs": []string{"qcs::cam::uin/100000000001:uin/100000000011"},
  48. },
  49. Effect: "allow",
  50. Action: []string{"name/cos:GetBucket"},
  51. Resource: []string{"qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"},
  52. },
  53. },
  54. Version: "2.0",
  55. }
  56. if !reflect.DeepEqual(res, want) {
  57. t.Errorf("Bucket.GetPolicy returned %+v, want %+v", res, want)
  58. }
  59. }
  60. func TestBucketService_PutPolicy(t *testing.T) {
  61. setup()
  62. defer teardown()
  63. opt := &BucketPutPolicyOptions{
  64. Statement: []BucketStatement{
  65. {
  66. Principal: map[string][]string{
  67. "qcs": []string{"qcs::cam::uin/100000000001:uin/100000000011"},
  68. },
  69. Effect: "allow",
  70. Action: []string{"name/cos:GetBucket"},
  71. Resource: []string{"qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*"},
  72. },
  73. },
  74. Version: "2.0",
  75. }
  76. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  77. testMethod(t, r, "PUT")
  78. vs := values{
  79. "policy": "",
  80. }
  81. testFormValues(t, r, vs)
  82. body := new(BucketPutPolicyOptions)
  83. json.NewDecoder(r.Body).Decode(body)
  84. want := opt
  85. if !reflect.DeepEqual(body, want) {
  86. t.Errorf("Bucket.PutPolicy request\n body: %+v\n, want %+v\n", body, want)
  87. }
  88. })
  89. _, err := client.Bucket.PutPolicy(context.Background(), opt)
  90. if err != nil {
  91. t.Fatalf("Bucket.PutPolicy returned error: %v", err)
  92. }
  93. }
  94. func TestBucketService_DeletePolicy(t *testing.T) {
  95. setup()
  96. defer teardown()
  97. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  98. testMethod(t, r, http.MethodDelete)
  99. vs := values{
  100. "policy": "",
  101. }
  102. testFormValues(t, r, vs)
  103. w.WriteHeader(http.StatusNoContent)
  104. })
  105. _, err := client.Bucket.DeletePolicy(context.Background())
  106. if err != nil {
  107. t.Fatalf("Bucket.DeletePolicy returned error: %v", err)
  108. }
  109. }