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.

172 lines
4.1 KiB

  1. package cos
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "net/http"
  7. "reflect"
  8. "testing"
  9. )
  10. func TestBucketService_GetACL(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. "acl": "",
  17. }
  18. testFormValues(t, r, vs)
  19. fmt.Fprint(w, `<AccessControlPolicy>
  20. <Owner>
  21. <ID>qcs::cam::uin/100000760461:uin/100000760461</ID>
  22. <DisplayName>qcs::cam::uin/100000760461:uin/100000760461</DisplayName>
  23. </Owner>
  24. <AccessControlList>
  25. <Grant>
  26. <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="RootAccount">
  27. <ID>qcs::cam::uin/100000760461:uin/100000760461</ID>
  28. <DisplayName>qcs::cam::uin/100000760461:uin/100000760461</DisplayName>
  29. </Grantee>
  30. <Permission>FULL_CONTROL</Permission>
  31. </Grant>
  32. <Grant>
  33. <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="RootAccount">
  34. <ID>qcs::cam::uin/100000760461:uin/100000760461</ID>
  35. <DisplayName>qcs::cam::uin/100000760461:uin/100000760461</DisplayName>
  36. </Grantee>
  37. <Permission>READ</Permission>
  38. </Grant>
  39. </AccessControlList>
  40. </AccessControlPolicy>`)
  41. })
  42. ref, _, err := client.Bucket.GetACL(context.Background())
  43. if err != nil {
  44. t.Fatalf("Bucket.GetACL returned error: %v", err)
  45. }
  46. want := &BucketGetACLResult{
  47. XMLName: xml.Name{Local: "AccessControlPolicy"},
  48. Owner: &Owner{
  49. ID: "qcs::cam::uin/100000760461:uin/100000760461",
  50. DisplayName: "qcs::cam::uin/100000760461:uin/100000760461",
  51. },
  52. AccessControlList: []ACLGrant{
  53. {
  54. Grantee: &ACLGrantee{
  55. Type: "RootAccount",
  56. ID: "qcs::cam::uin/100000760461:uin/100000760461",
  57. DisplayName: "qcs::cam::uin/100000760461:uin/100000760461",
  58. },
  59. Permission: "FULL_CONTROL",
  60. },
  61. {
  62. Grantee: &ACLGrantee{
  63. Type: "RootAccount",
  64. ID: "qcs::cam::uin/100000760461:uin/100000760461",
  65. DisplayName: "qcs::cam::uin/100000760461:uin/100000760461",
  66. },
  67. Permission: "READ",
  68. },
  69. },
  70. }
  71. if !reflect.DeepEqual(ref, want) {
  72. t.Errorf("Bucket.GetACL returned %+v, want %+v", ref, want)
  73. }
  74. }
  75. func TestBucketService_PutACL_with_header_opt(t *testing.T) {
  76. setup()
  77. defer teardown()
  78. opt := &BucketPutACLOptions{
  79. Header: &ACLHeaderOptions{
  80. XCosACL: "private",
  81. },
  82. }
  83. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  84. testMethod(t, r, http.MethodPut)
  85. vs := values{
  86. "acl": "",
  87. }
  88. testFormValues(t, r, vs)
  89. testHeader(t, r, "x-cos-acl", "private")
  90. want := 0
  91. v, _ := r.Body.Read([]byte{})
  92. if !reflect.DeepEqual(v, want) {
  93. t.Errorf("Bucket.PutACL request body: %#v, want %#v", v, want)
  94. }
  95. })
  96. _, err := client.Bucket.PutACL(context.Background(), opt)
  97. if err != nil {
  98. t.Fatalf("Bucket.PutACL returned error: %v", err)
  99. }
  100. }
  101. func TestBucketService_PutACL_with_body_opt(t *testing.T) {
  102. setup()
  103. defer teardown()
  104. opt := &BucketPutACLOptions{
  105. Body: &ACLXml{
  106. Owner: &Owner{
  107. ID: "qcs::cam::uin/100000760461:uin/100000760461",
  108. DisplayName: "qcs::cam::uin/100000760461:uin/100000760461",
  109. },
  110. AccessControlList: []ACLGrant{
  111. {
  112. Grantee: &ACLGrantee{
  113. Type: "RootAccount",
  114. ID: "qcs::cam::uin/100000760461:uin/100000760461",
  115. DisplayName: "qcs::cam::uin/100000760461:uin/100000760461",
  116. },
  117. Permission: "FULL_CONTROL",
  118. },
  119. {
  120. Grantee: &ACLGrantee{
  121. Type: "RootAccount",
  122. ID: "qcs::cam::uin/100000760461:uin/100000760461",
  123. DisplayName: "qcs::cam::uin/100000760461:uin/100000760461",
  124. },
  125. Permission: "READ",
  126. },
  127. },
  128. },
  129. }
  130. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  131. v := new(ACLXml)
  132. xml.NewDecoder(r.Body).Decode(v)
  133. testMethod(t, r, http.MethodPut)
  134. vs := values{
  135. "acl": "",
  136. }
  137. testFormValues(t, r, vs)
  138. testHeader(t, r, "x-cos-acl", "")
  139. want := opt.Body
  140. want.XMLName = xml.Name{Local: "AccessControlPolicy"}
  141. if !reflect.DeepEqual(v, want) {
  142. t.Errorf("Bucket.PutACL request body: %+v, want %+v", v, want)
  143. }
  144. })
  145. _, err := client.Bucket.PutACL(context.Background(), opt)
  146. if err != nil {
  147. t.Fatalf("Bucket.PutACL returned error: %v", err)
  148. }
  149. }