fixtures.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package testing
  2. import (
  3. "fmt"
  4. "net/http"
  5. "testing"
  6. "devel.mephi.ru/iacherepanov/openstack-gophercloud/openstack/identity/v3/policies"
  7. th "devel.mephi.ru/iacherepanov/openstack-gophercloud/testhelper"
  8. fake "devel.mephi.ru/iacherepanov/openstack-gophercloud/testhelper/client"
  9. )
  10. // ListOutput provides a single page of Policy results.
  11. const ListOutput = `
  12. {
  13. "links": {
  14. "next": null,
  15. "previous": null,
  16. "self": "http://example.com/identity/v3/policies"
  17. },
  18. "policies": [
  19. {
  20. "type": "text/plain",
  21. "id": "2844b2a08be147a08ef58317d6471f1f",
  22. "links": {
  23. "self": "http://example.com/identity/v3/policies/2844b2a08be147a08ef58317d6471f1f"
  24. },
  25. "blob": "'foo_user': 'role:compute-user'"
  26. },
  27. {
  28. "type": "application/json",
  29. "id": "b49884da9d31494ea02aff38d4b4e701",
  30. "links": {
  31. "self": "http://example.com/identity/v3/policies/b49884da9d31494ea02aff38d4b4e701"
  32. },
  33. "blob": "{'bar_user': 'role:network-user'}",
  34. "description": "policy for bar_user"
  35. }
  36. ]
  37. }
  38. `
  39. // ListWithFilterOutput provides a single page of filtered Policy results.
  40. const ListWithFilterOutput = `
  41. {
  42. "links": {
  43. "next": null,
  44. "previous": null,
  45. "self": "http://example.com/identity/v3/policies"
  46. },
  47. "policies": [
  48. {
  49. "type": "application/json",
  50. "id": "b49884da9d31494ea02aff38d4b4e701",
  51. "links": {
  52. "self": "http://example.com/identity/v3/policies/b49884da9d31494ea02aff38d4b4e701"
  53. },
  54. "blob": "{'bar_user': 'role:network-user'}",
  55. "description": "policy for bar_user"
  56. }
  57. ]
  58. }
  59. `
  60. // GetOutput provides a Get result.
  61. const GetOutput = `
  62. {
  63. "policy": {
  64. "type": "application/json",
  65. "id": "b49884da9d31494ea02aff38d4b4e701",
  66. "links": {
  67. "self": "http://example.com/identity/v3/policies/b49884da9d31494ea02aff38d4b4e701"
  68. },
  69. "blob": "{'bar_user': 'role:network-user'}",
  70. "description": "policy for bar_user"
  71. }
  72. }
  73. `
  74. // CreateRequest provides the input to a Create request.
  75. const CreateRequest = `
  76. {
  77. "policy": {
  78. "blob": "{'bar_user': 'role:network-user'}",
  79. "description": "policy for bar_user",
  80. "type": "application/json"
  81. }
  82. }
  83. `
  84. // UpdateRequest provides the input to as Update request.
  85. const UpdateRequest = `
  86. {
  87. "policy": {
  88. "description": "updated policy for bar_user"
  89. }
  90. }
  91. `
  92. // UpdateOutput provides an update result.
  93. const UpdateOutput = `
  94. {
  95. "policy": {
  96. "type": "application/json",
  97. "id": "b49884da9d31494ea02aff38d4b4e701",
  98. "links": {
  99. "self": "http://example.com/identity/v3/policies/b49884da9d31494ea02aff38d4b4e701"
  100. },
  101. "blob": "{'bar_user': 'role:network-user'}",
  102. "description": "updated policy for bar_user"
  103. }
  104. }
  105. `
  106. // FirstPolicy is the first policy in the List request.
  107. var FirstPolicy = policies.Policy{
  108. ID: "2844b2a08be147a08ef58317d6471f1f",
  109. Blob: "'foo_user': 'role:compute-user'",
  110. Type: "text/plain",
  111. Links: map[string]interface{}{
  112. "self": "http://example.com/identity/v3/policies/2844b2a08be147a08ef58317d6471f1f",
  113. },
  114. Extra: map[string]interface{}{},
  115. }
  116. // SecondPolicy is the second policy in the List request.
  117. var SecondPolicy = policies.Policy{
  118. ID: "b49884da9d31494ea02aff38d4b4e701",
  119. Blob: "{'bar_user': 'role:network-user'}",
  120. Type: "application/json",
  121. Links: map[string]interface{}{
  122. "self": "http://example.com/identity/v3/policies/b49884da9d31494ea02aff38d4b4e701",
  123. },
  124. Extra: map[string]interface{}{
  125. "description": "policy for bar_user",
  126. },
  127. }
  128. // SecondPolicyUpdated is the policy in the Update request.
  129. var SecondPolicyUpdated = policies.Policy{
  130. ID: "b49884da9d31494ea02aff38d4b4e701",
  131. Blob: "{'bar_user': 'role:network-user'}",
  132. Type: "application/json",
  133. Links: map[string]interface{}{
  134. "self": "http://example.com/identity/v3/policies/b49884da9d31494ea02aff38d4b4e701",
  135. },
  136. Extra: map[string]interface{}{
  137. "description": "updated policy for bar_user",
  138. },
  139. }
  140. // ExpectedPoliciesSlice is the slice of policies expected to be returned from ListOutput.
  141. var ExpectedPoliciesSlice = []policies.Policy{FirstPolicy, SecondPolicy}
  142. // HandleListPoliciesSuccessfully creates an HTTP handler at `/policies` on the
  143. // test handler mux that responds with a list of two policies.
  144. func HandleListPoliciesSuccessfully(t *testing.T) {
  145. th.Mux.HandleFunc("/policies", func(w http.ResponseWriter, r *http.Request) {
  146. th.TestMethod(t, r, "GET")
  147. th.TestHeader(t, r, "Accept", "application/json")
  148. th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
  149. w.Header().Set("Content-Type", "application/json")
  150. w.WriteHeader(http.StatusOK)
  151. switch r.URL.Query().Get("type") {
  152. case "":
  153. fmt.Fprintf(w, ListOutput)
  154. case "application/json":
  155. fmt.Fprintf(w, ListWithFilterOutput)
  156. default:
  157. w.WriteHeader(http.StatusBadRequest)
  158. }
  159. })
  160. }
  161. // HandleCreatePolicySuccessfully creates an HTTP handler at `/policies` on the
  162. // test handler mux that tests policy creation.
  163. func HandleCreatePolicySuccessfully(t *testing.T) {
  164. th.Mux.HandleFunc("/policies", func(w http.ResponseWriter, r *http.Request) {
  165. th.TestMethod(t, r, "POST")
  166. th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
  167. th.TestJSONRequest(t, r, CreateRequest)
  168. w.WriteHeader(http.StatusCreated)
  169. fmt.Fprintf(w, GetOutput)
  170. })
  171. }
  172. // HandleGetPolicySuccessfully creates an HTTP handler at `/policies` on the
  173. // test handler mux that responds with a single policy.
  174. func HandleGetPolicySuccessfully(t *testing.T) {
  175. th.Mux.HandleFunc("/policies/b49884da9d31494ea02aff38d4b4e701",
  176. func(w http.ResponseWriter, r *http.Request) {
  177. th.TestMethod(t, r, "GET")
  178. th.TestHeader(t, r, "Accept", "application/json")
  179. th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
  180. w.Header().Set("Content-Type", "application/json")
  181. w.WriteHeader(http.StatusOK)
  182. fmt.Fprintf(w, GetOutput)
  183. },
  184. )
  185. }
  186. // HandleUpdatePolicySuccessfully creates an HTTP handler at `/policies` on the
  187. // test handler mux that tests role update.
  188. func HandleUpdatePolicySuccessfully(t *testing.T) {
  189. th.Mux.HandleFunc("/policies/b49884da9d31494ea02aff38d4b4e701",
  190. func(w http.ResponseWriter, r *http.Request) {
  191. th.TestMethod(t, r, "PATCH")
  192. th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
  193. th.TestJSONRequest(t, r, UpdateRequest)
  194. w.WriteHeader(http.StatusOK)
  195. fmt.Fprintf(w, UpdateOutput)
  196. },
  197. )
  198. }
  199. // HandleDeletePolicySuccessfully creates an HTTP handler at `/policies` on the
  200. // test handler mux that tests policy deletion.
  201. func HandleDeletePolicySuccessfully(t *testing.T) {
  202. th.Mux.HandleFunc("/policies/9fe1d3", func(w http.ResponseWriter, r *http.Request) {
  203. th.TestMethod(t, r, "DELETE")
  204. th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
  205. w.WriteHeader(http.StatusNoContent)
  206. })
  207. }