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.

174 lines
4.3 KiB

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