github.com/gophercloud/gophercloud@v1.11.0/openstack/keymanager/v1/orders/testing/fixtures_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 "time" 8 9 "github.com/gophercloud/gophercloud/openstack/keymanager/v1/orders" 10 th "github.com/gophercloud/gophercloud/testhelper" 11 "github.com/gophercloud/gophercloud/testhelper/client" 12 ) 13 14 // ListResponse provides a single page of RESOURCE results. 15 const ListResponse = ` 16 { 17 "orders": [ 18 { 19 "created": "2018-06-22T05:05:43", 20 "creator_id": "5c70d99f4a8641c38f8084b32b5e5c0e", 21 "meta": { 22 "algorithm": "aes", 23 "bit_length": 256, 24 "expiration": null, 25 "mode": "cbc", 26 "name": null, 27 "payload_content_type": "application/octet-stream" 28 }, 29 "order_ref": "http://barbican:9311/v1/orders/46f73695-82bb-447a-bf96-6635f0fb0ce7", 30 "secret_ref": "http://barbican:9311/v1/secrets/22dfef44-1046-4549-a86d-95af462e8fa0", 31 "status": "ACTIVE", 32 "sub_status": "Unknown", 33 "sub_status_message": "Unknown", 34 "type": "key", 35 "updated": "2018-06-22T05:05:43" 36 }, 37 { 38 "created": "2018-06-22T05:08:15", 39 "creator_id": "5c70d99f4a8641c38f8084b32b5e5c0e", 40 "meta": { 41 "algorithm": "aes", 42 "bit_length": 256, 43 "expiration": null, 44 "mode": "cbc", 45 "name": null, 46 "payload_content_type": "application/octet-stream" 47 }, 48 "order_ref": "http://barbican:9311/v1/orders/07fba88b-3dcf-44e3-a4a3-0bad7f56f01c", 49 "secret_ref": "http://barbican:9311/v1/secrets/a31ad551-1aa5-4ba0-810e-0865163e0fa9", 50 "status": "ACTIVE", 51 "sub_status": "Unknown", 52 "sub_status_message": "Unknown", 53 "type": "key", 54 "updated": "2018-06-22T05:08:15" 55 } 56 ], 57 "total": 2 58 }` 59 60 // GetResponse provides a Get result. 61 const GetResponse = ` 62 { 63 "created": "2018-06-22T05:08:15", 64 "creator_id": "5c70d99f4a8641c38f8084b32b5e5c0e", 65 "meta": { 66 "algorithm": "aes", 67 "bit_length": 256, 68 "expiration": null, 69 "mode": "cbc", 70 "name": null, 71 "payload_content_type": "application/octet-stream" 72 }, 73 "order_ref": "http://barbican:9311/v1/orders/07fba88b-3dcf-44e3-a4a3-0bad7f56f01c", 74 "secret_ref": "http://barbican:9311/v1/secrets/a31ad551-1aa5-4ba0-810e-0865163e0fa9", 75 "status": "ACTIVE", 76 "sub_status": "Unknown", 77 "sub_status_message": "Unknown", 78 "type": "key", 79 "updated": "2018-06-22T05:08:15" 80 } 81 ` 82 83 // CreateRequest provides the input to a Create request. 84 const CreateRequest = ` 85 { 86 "meta": { 87 "algorithm": "aes", 88 "bit_length": 256, 89 "mode": "cbc", 90 "payload_content_type": "application/octet-stream" 91 }, 92 "type": "key" 93 }` 94 95 // FirstOrder is the first resource in the List request. 96 var FirstOrder = orders.Order{ 97 Created: time.Date(2018, 6, 22, 5, 5, 43, 0, time.UTC), 98 CreatorID: "5c70d99f4a8641c38f8084b32b5e5c0e", 99 Meta: orders.Meta{ 100 Algorithm: "aes", 101 BitLength: 256, 102 Mode: "cbc", 103 PayloadContentType: "application/octet-stream", 104 }, 105 OrderRef: "http://barbican:9311/v1/orders/46f73695-82bb-447a-bf96-6635f0fb0ce7", 106 SecretRef: "http://barbican:9311/v1/secrets/22dfef44-1046-4549-a86d-95af462e8fa0", 107 Status: "ACTIVE", 108 SubStatus: "Unknown", 109 SubStatusMessage: "Unknown", 110 Type: "key", 111 Updated: time.Date(2018, 6, 22, 5, 5, 43, 0, time.UTC), 112 } 113 114 // SecondOrder is the second resource in the List request. 115 var SecondOrder = orders.Order{ 116 Created: time.Date(2018, 6, 22, 5, 8, 15, 0, time.UTC), 117 CreatorID: "5c70d99f4a8641c38f8084b32b5e5c0e", 118 Meta: orders.Meta{ 119 Algorithm: "aes", 120 BitLength: 256, 121 Mode: "cbc", 122 PayloadContentType: "application/octet-stream", 123 }, 124 OrderRef: "http://barbican:9311/v1/orders/07fba88b-3dcf-44e3-a4a3-0bad7f56f01c", 125 SecretRef: "http://barbican:9311/v1/secrets/a31ad551-1aa5-4ba0-810e-0865163e0fa9", 126 Status: "ACTIVE", 127 SubStatus: "Unknown", 128 SubStatusMessage: "Unknown", 129 Type: "key", 130 Updated: time.Date(2018, 6, 22, 5, 8, 15, 0, time.UTC), 131 } 132 133 // ExpectedOrdersSlice is the slice of orders expected to be returned from ListResponse. 134 var ExpectedOrdersSlice = []orders.Order{FirstOrder, SecondOrder} 135 136 // HandleListOrdersSuccessfully creates an HTTP handler at `/orders` on the 137 // test handler mux that responds with a list of two orders. 138 func HandleListOrdersSuccessfully(t *testing.T) { 139 th.Mux.HandleFunc("/orders", func(w http.ResponseWriter, r *http.Request) { 140 th.TestMethod(t, r, "GET") 141 th.TestHeader(t, r, "Accept", "application/json") 142 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 143 144 w.Header().Set("Content-Type", "application/json") 145 w.WriteHeader(http.StatusOK) 146 fmt.Fprintf(w, ListResponse) 147 }) 148 } 149 150 // HandleGetOrderSuccessfully creates an HTTP handler at `/orders` on the 151 // test handler mux that responds with a single resource. 152 func HandleGetOrderSuccessfully(t *testing.T) { 153 th.Mux.HandleFunc("/orders/46f73695-82bb-447a-bf96-6635f0fb0ce7", func(w http.ResponseWriter, r *http.Request) { 154 th.TestMethod(t, r, "GET") 155 th.TestHeader(t, r, "Accept", "application/json") 156 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 157 158 w.Header().Set("Content-Type", "application/json") 159 w.WriteHeader(http.StatusOK) 160 fmt.Fprintf(w, GetResponse) 161 }) 162 } 163 164 // HandleCreateOrderSuccessfully creates an HTTP handler at `/orders` on the 165 // test handler mux that tests resource creation. 166 func HandleCreateOrderSuccessfully(t *testing.T) { 167 th.Mux.HandleFunc("/orders", func(w http.ResponseWriter, r *http.Request) { 168 th.TestMethod(t, r, "POST") 169 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 170 th.TestJSONRequest(t, r, CreateRequest) 171 172 w.WriteHeader(http.StatusAccepted) 173 fmt.Fprintf(w, GetResponse) 174 }) 175 } 176 177 // HandleDeleteOrderSuccessfully creates an HTTP handler at `/orders` on the 178 // test handler mux that tests resource deletion. 179 func HandleDeleteOrderSuccessfully(t *testing.T) { 180 th.Mux.HandleFunc("/orders/46f73695-82bb-447a-bf96-6635f0fb0ce7", func(w http.ResponseWriter, r *http.Request) { 181 th.TestMethod(t, r, "DELETE") 182 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 183 184 w.WriteHeader(http.StatusNoContent) 185 }) 186 }