fixtures.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package testing
  2. import (
  3. "fmt"
  4. "net/http"
  5. "testing"
  6. "devel.mephi.ru/iacherepanov/openstack-gophercloud/openstack/identity/v3/domains"
  7. th "devel.mephi.ru/iacherepanov/openstack-gophercloud/testhelper"
  8. "devel.mephi.ru/iacherepanov/openstack-gophercloud/testhelper/client"
  9. )
  10. // ListOutput provides a single page of Domain results.
  11. const ListOutput = `
  12. {
  13. "links": {
  14. "next": null,
  15. "previous": null,
  16. "self": "http://example.com/identity/v3/domains"
  17. },
  18. "domains": [
  19. {
  20. "enabled": true,
  21. "id": "2844b2a08be147a08ef58317d6471f1f",
  22. "links": {
  23. "self": "http://example.com/identity/v3/domains/2844b2a08be147a08ef58317d6471f1f"
  24. },
  25. "name": "domain one",
  26. "description": "some description"
  27. },
  28. {
  29. "enabled": true,
  30. "id": "9fe1d3",
  31. "links": {
  32. "self": "https://example.com/identity/v3/domains/9fe1d3"
  33. },
  34. "name": "domain two"
  35. }
  36. ]
  37. }
  38. `
  39. // GetOutput provides a Get result.
  40. const GetOutput = `
  41. {
  42. "domain": {
  43. "enabled": true,
  44. "id": "9fe1d3",
  45. "links": {
  46. "self": "https://example.com/identity/v3/domains/9fe1d3"
  47. },
  48. "name": "domain two"
  49. }
  50. }
  51. `
  52. // CreateRequest provides the input to a Create request.
  53. const CreateRequest = `
  54. {
  55. "domain": {
  56. "name": "domain two"
  57. }
  58. }
  59. `
  60. // UpdateRequest provides the input to as Update request.
  61. const UpdateRequest = `
  62. {
  63. "domain": {
  64. "description": "Staging Domain"
  65. }
  66. }
  67. `
  68. // UpdateOutput provides an update result.
  69. const UpdateOutput = `
  70. {
  71. "domain": {
  72. "enabled": true,
  73. "id": "9fe1d3",
  74. "links": {
  75. "self": "https://example.com/identity/v3/domains/9fe1d3"
  76. },
  77. "name": "domain two",
  78. "description": "Staging Domain"
  79. }
  80. }
  81. `
  82. // FirstDomain is the first domain in the List request.
  83. var FirstDomain = domains.Domain{
  84. Enabled: true,
  85. ID: "2844b2a08be147a08ef58317d6471f1f",
  86. Links: map[string]interface{}{
  87. "self": "http://example.com/identity/v3/domains/2844b2a08be147a08ef58317d6471f1f",
  88. },
  89. Name: "domain one",
  90. Description: "some description",
  91. }
  92. // SecondDomain is the second domain in the List request.
  93. var SecondDomain = domains.Domain{
  94. Enabled: true,
  95. ID: "9fe1d3",
  96. Links: map[string]interface{}{
  97. "self": "https://example.com/identity/v3/domains/9fe1d3",
  98. },
  99. Name: "domain two",
  100. }
  101. // SecondDomainUpdated is how SecondDomain should look after an Update.
  102. var SecondDomainUpdated = domains.Domain{
  103. Enabled: true,
  104. ID: "9fe1d3",
  105. Links: map[string]interface{}{
  106. "self": "https://example.com/identity/v3/domains/9fe1d3",
  107. },
  108. Name: "domain two",
  109. Description: "Staging Domain",
  110. }
  111. // ExpectedDomainsSlice is the slice of domains expected to be returned from ListOutput.
  112. var ExpectedDomainsSlice = []domains.Domain{FirstDomain, SecondDomain}
  113. // HandleListDomainsSuccessfully creates an HTTP handler at `/domains` on the
  114. // test handler mux that responds with a list of two domains.
  115. func HandleListDomainsSuccessfully(t *testing.T) {
  116. th.Mux.HandleFunc("/domains", func(w http.ResponseWriter, r *http.Request) {
  117. th.TestMethod(t, r, "GET")
  118. th.TestHeader(t, r, "Accept", "application/json")
  119. th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
  120. w.Header().Set("Content-Type", "application/json")
  121. w.WriteHeader(http.StatusOK)
  122. fmt.Fprintf(w, ListOutput)
  123. })
  124. }
  125. // HandleGetDomainSuccessfully creates an HTTP handler at `/domains` on the
  126. // test handler mux that responds with a single domain.
  127. func HandleGetDomainSuccessfully(t *testing.T) {
  128. th.Mux.HandleFunc("/domains/9fe1d3", func(w http.ResponseWriter, r *http.Request) {
  129. th.TestMethod(t, r, "GET")
  130. th.TestHeader(t, r, "Accept", "application/json")
  131. th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
  132. w.Header().Set("Content-Type", "application/json")
  133. w.WriteHeader(http.StatusOK)
  134. fmt.Fprintf(w, GetOutput)
  135. })
  136. }
  137. // HandleCreateDomainSuccessfully creates an HTTP handler at `/domains` on the
  138. // test handler mux that tests domain creation.
  139. func HandleCreateDomainSuccessfully(t *testing.T) {
  140. th.Mux.HandleFunc("/domains", func(w http.ResponseWriter, r *http.Request) {
  141. th.TestMethod(t, r, "POST")
  142. th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
  143. th.TestJSONRequest(t, r, CreateRequest)
  144. w.WriteHeader(http.StatusCreated)
  145. fmt.Fprintf(w, GetOutput)
  146. })
  147. }
  148. // HandleDeleteDomainSuccessfully creates an HTTP handler at `/domains` on the
  149. // test handler mux that tests domain deletion.
  150. func HandleDeleteDomainSuccessfully(t *testing.T) {
  151. th.Mux.HandleFunc("/domains/9fe1d3", func(w http.ResponseWriter, r *http.Request) {
  152. th.TestMethod(t, r, "DELETE")
  153. th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
  154. w.WriteHeader(http.StatusNoContent)
  155. })
  156. }
  157. // HandleUpdateDomainSuccessfully creates an HTTP handler at `/domains` on the
  158. // test handler mux that tests domain update.
  159. func HandleUpdateDomainSuccessfully(t *testing.T) {
  160. th.Mux.HandleFunc("/domains/9fe1d3", func(w http.ResponseWriter, r *http.Request) {
  161. th.TestMethod(t, r, "PATCH")
  162. th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
  163. th.TestJSONRequest(t, r, UpdateRequest)
  164. w.WriteHeader(http.StatusOK)
  165. fmt.Fprintf(w, UpdateOutput)
  166. })
  167. }