github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/identity/v3/trusts/testing/fixtures_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 "time" 8 9 "github.com/vnpaycloud-console/gophercloud/v2/openstack/identity/v3/trusts" 10 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 11 "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client" 12 ) 13 14 const CreateRequest = ` 15 { 16 "trust": { 17 "expires_at": "2019-12-01T14:00:00Z", 18 "impersonation": false, 19 "allow_redelegation": true, 20 "project_id": "9b71012f5a4a4aef9193f1995fe159b2", 21 "roles": [ 22 { 23 "name": "member" 24 } 25 ], 26 "trustee_user_id": "ecb37e88cc86431c99d0332208cb6fbf", 27 "trustor_user_id": "959ed913a32c4ec88c041c98e61cbbc3" 28 } 29 } 30 ` 31 32 const CreateRequestNoExpire = ` 33 { 34 "trust": { 35 "impersonation": false, 36 "allow_redelegation": true, 37 "project_id": "9b71012f5a4a4aef9193f1995fe159b2", 38 "roles": [ 39 { 40 "name": "member" 41 } 42 ], 43 "trustee_user_id": "ecb37e88cc86431c99d0332208cb6fbf", 44 "trustor_user_id": "959ed913a32c4ec88c041c98e61cbbc3" 45 } 46 } 47 ` 48 49 const CreateResponse = ` 50 { 51 "trust": { 52 "expires_at": "2019-12-01T14:00:00.000000Z", 53 "id": "3422b7c113894f5d90665e1a79655e23", 54 "impersonation": false, 55 "redelegation_count": 10, 56 "project_id": "9b71012f5a4a4aef9193f1995fe159b2", 57 "remaining_uses": null, 58 "roles": [ 59 { 60 "id": "b627fca5-beb0-471a-9857-0e852b719e76", 61 "links": { 62 "self": "http://example.com/identity/v3/roles/b627fca5-beb0-471a-9857-0e852b719e76" 63 }, 64 "name": "member" 65 } 66 ], 67 "trustee_user_id": "ecb37e88cc86431c99d0332208cb6fbf", 68 "trustor_user_id": "959ed913a32c4ec88c041c98e61cbbc3" 69 } 70 } 71 ` 72 73 const CreateResponseNoExpire = ` 74 { 75 "trust": { 76 "id": "3422b7c113894f5d90665e1a79655e23", 77 "impersonation": false, 78 "redelegation_count": 10, 79 "project_id": "9b71012f5a4a4aef9193f1995fe159b2", 80 "remaining_uses": null, 81 "roles": [ 82 { 83 "id": "b627fca5-beb0-471a-9857-0e852b719e76", 84 "links": { 85 "self": "http://example.com/identity/v3/roles/b627fca5-beb0-471a-9857-0e852b719e76" 86 }, 87 "name": "member" 88 } 89 ], 90 "trustee_user_id": "ecb37e88cc86431c99d0332208cb6fbf", 91 "trustor_user_id": "959ed913a32c4ec88c041c98e61cbbc3" 92 } 93 } 94 ` 95 96 // GetOutput provides a Get result. 97 const GetResponse = ` 98 { 99 "trust": { 100 "id": "987fe8", 101 "expires_at": "2013-02-27T18:30:59.000000Z", 102 "impersonation": true, 103 "links": { 104 "self": "http://example.com/identity/v3/OS-TRUST/trusts/987fe8" 105 }, 106 "roles": [ 107 { 108 "id": "ed7b78", 109 "links": { 110 "self": "http://example.com/identity/v3/roles/ed7b78" 111 }, 112 "name": "member" 113 } 114 ], 115 "roles_links": { 116 "next": null, 117 "previous": null, 118 "self": "http://example.com/identity/v3/OS-TRUST/trusts/1ff900/roles" 119 }, 120 "project_id": "0f1233", 121 "trustee_user_id": "be34d1", 122 "trustor_user_id": "56ae32" 123 } 124 } 125 ` 126 127 // ListOutput provides a single page of Role results. 128 const ListResponse = ` 129 { 130 "trusts": [ 131 { 132 "id": "1ff900", 133 "expires_at": "2019-12-01T14:00:00.000000Z", 134 "impersonation": true, 135 "links": { 136 "self": "http://example.com/identity/v3/OS-TRUST/trusts/1ff900" 137 }, 138 "project_id": "0f1233", 139 "trustee_user_id": "86c0d5", 140 "trustor_user_id": "a0fdfd" 141 }, 142 { 143 "id": "f4513a", 144 "impersonation": false, 145 "links": { 146 "self": "http://example.com/identity/v3/OS-TRUST/trusts/f45513a" 147 }, 148 "project_id": "0f1233", 149 "trustee_user_id": "86c0d5", 150 "trustor_user_id": "3cd2ce" 151 } 152 ] 153 } 154 ` 155 156 const ListTrustRolesResponse = ` 157 { 158 "roles": [ 159 { 160 "id": "c1648e", 161 "links": { 162 "self": "http://example.com/identity/v3/roles/c1648e" 163 }, 164 "name": "manager" 165 }, 166 { 167 "id": "ed7b78", 168 "links": { 169 "self": "http://example.com/identity/v3/roles/ed7b78" 170 }, 171 "name": "member" 172 } 173 ] 174 } 175 ` 176 177 const GetTrustRoleResponse = ` 178 { 179 "role": { 180 "id": "c1648e", 181 "links": { 182 "self": "http://example.com/identity/v3/roles/c1648e" 183 }, 184 "name": "manager" 185 } 186 } 187 ` 188 189 var FirstRole = trusts.Role{ 190 ID: "c1648e", 191 Name: "manager", 192 } 193 194 var SecondRole = trusts.Role{ 195 ID: "ed7b78", 196 Name: "member", 197 } 198 199 var ExpectedTrustRolesSlice = []trusts.Role{FirstRole, SecondRole} 200 201 // HandleCreateTrust creates an HTTP handler at `/OS-TRUST/trusts` on the 202 // test handler mux that tests trust creation. 203 func HandleCreateTrust(t *testing.T) { 204 th.Mux.HandleFunc("/OS-TRUST/trusts", func(w http.ResponseWriter, r *http.Request) { 205 th.TestMethod(t, r, "POST") 206 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 207 th.TestJSONRequest(t, r, CreateRequest) 208 209 w.WriteHeader(http.StatusCreated) 210 _, err := fmt.Fprint(w, CreateResponse) 211 th.AssertNoErr(t, err) 212 }) 213 } 214 215 // HandleCreateTrustNoExpire creates an HTTP handler at `/OS-TRUST/trusts` on the 216 // test handler mux that tests trust creation. 217 func HandleCreateTrustNoExpire(t *testing.T) { 218 th.Mux.HandleFunc("/OS-TRUST/trusts", func(w http.ResponseWriter, r *http.Request) { 219 th.TestMethod(t, r, "POST") 220 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 221 th.TestJSONRequest(t, r, CreateRequestNoExpire) 222 223 w.WriteHeader(http.StatusCreated) 224 _, err := fmt.Fprint(w, CreateResponseNoExpire) 225 th.AssertNoErr(t, err) 226 }) 227 } 228 229 // HandleDeleteUserSuccessfully creates an HTTP handler at `/users` on the 230 // test handler mux that tests user deletion. 231 func HandleDeleteTrust(t *testing.T) { 232 th.Mux.HandleFunc("/OS-TRUST/trusts/3422b7c113894f5d90665e1a79655e23", func(w http.ResponseWriter, r *http.Request) { 233 th.TestMethod(t, r, "DELETE") 234 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 235 236 w.WriteHeader(http.StatusNoContent) 237 }) 238 } 239 240 // HandleGetTrustSuccessfully creates an HTTP handler at `/OS-TRUST/trusts` on the 241 // test handler mux that responds with a single trusts. 242 func HandleGetTrustSuccessfully(t *testing.T) { 243 th.Mux.HandleFunc("/OS-TRUST/trusts/987fe8", func(w http.ResponseWriter, r *http.Request) { 244 th.TestMethod(t, r, "GET") 245 th.TestHeader(t, r, "Accept", "application/json") 246 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 247 248 w.Header().Set("Content-Type", "application/json") 249 w.WriteHeader(http.StatusOK) 250 fmt.Fprint(w, GetResponse) 251 }) 252 } 253 254 var FirstTrust = trusts.Trust{ 255 ID: "1ff900", 256 Impersonation: true, 257 TrusteeUserID: "86c0d5", 258 TrustorUserID: "a0fdfd", 259 ProjectID: "0f1233", 260 ExpiresAt: time.Date(2019, 12, 01, 14, 00, 00, 0, time.UTC), 261 DeletedAt: time.Time{}, 262 } 263 264 var SecondTrust = trusts.Trust{ 265 ID: "f4513a", 266 Impersonation: false, 267 TrusteeUserID: "86c0d5", 268 TrustorUserID: "3cd2ce", 269 ProjectID: "0f1233", 270 ExpiresAt: time.Time{}, 271 DeletedAt: time.Time{}, 272 } 273 274 var CreatedTrust = trusts.Trust{ 275 ID: "3422b7c113894f5d90665e1a79655e23", 276 Impersonation: false, 277 TrusteeUserID: "ecb37e88cc86431c99d0332208cb6fbf", 278 TrustorUserID: "959ed913a32c4ec88c041c98e61cbbc3", 279 ProjectID: "9b71012f5a4a4aef9193f1995fe159b2", 280 ExpiresAt: time.Date(2019, 12, 01, 14, 00, 00, 0, time.UTC), 281 DeletedAt: time.Time{}, 282 RedelegationCount: 10, 283 Roles: []trusts.Role{ 284 { 285 ID: "b627fca5-beb0-471a-9857-0e852b719e76", 286 Name: "member", 287 }, 288 }, 289 } 290 291 var CreatedTrustNoExpire = trusts.Trust{ 292 ID: "3422b7c113894f5d90665e1a79655e23", 293 Impersonation: false, 294 TrusteeUserID: "ecb37e88cc86431c99d0332208cb6fbf", 295 TrustorUserID: "959ed913a32c4ec88c041c98e61cbbc3", 296 ProjectID: "9b71012f5a4a4aef9193f1995fe159b2", 297 DeletedAt: time.Time{}, 298 RedelegationCount: 10, 299 Roles: []trusts.Role{ 300 { 301 ID: "b627fca5-beb0-471a-9857-0e852b719e76", 302 Name: "member", 303 }, 304 }, 305 } 306 307 // ExpectedRolesSlice is the slice of roles expected to be returned from ListOutput. 308 var ExpectedTrustsSlice = []trusts.Trust{FirstTrust, SecondTrust} 309 310 // HandleListTrustsSuccessfully creates an HTTP handler at `/OS-TRUST/trusts` on the 311 // test handler mux that responds with a list of two trusts. 312 func HandleListTrustsSuccessfully(t *testing.T) { 313 th.Mux.HandleFunc("/OS-TRUST/trusts", func(w http.ResponseWriter, r *http.Request) { 314 th.TestMethod(t, r, "GET") 315 th.TestHeader(t, r, "Accept", "application/json") 316 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 317 318 w.Header().Set("Content-Type", "application/json") 319 w.WriteHeader(http.StatusOK) 320 fmt.Fprint(w, ListResponse) 321 }) 322 } 323 324 // HandleListTrustRolesSuccessfully creates an HTTP handler at `/OS-TRUST/trusts/987fe8/roles` on the 325 // test handler mux that responds with a list trust roles. 326 func HandleListTrustRolesSuccessfully(t *testing.T) { 327 th.Mux.HandleFunc("/OS-TRUST/trusts/987fe8/roles", func(w http.ResponseWriter, r *http.Request) { 328 th.TestMethod(t, r, "GET") 329 th.TestHeader(t, r, "Accept", "application/json") 330 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 331 332 w.Header().Set("Content-Type", "application/json") 333 w.WriteHeader(http.StatusOK) 334 fmt.Fprint(w, ListTrustRolesResponse) 335 }) 336 } 337 338 // HandleGetTrustRoleSuccessfully creates an HTTP handler at `/OS-TRUST/trusts/987fe8/roles/c1648e` on the 339 // test handler mux that responds with a trust role details. 340 func HandleGetTrustRoleSuccessfully(t *testing.T) { 341 th.Mux.HandleFunc("/OS-TRUST/trusts/987fe8/roles/c1648e", func(w http.ResponseWriter, r *http.Request) { 342 th.TestMethod(t, r, "GET") 343 th.TestHeader(t, r, "Accept", "application/json") 344 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 345 346 w.Header().Set("Content-Type", "application/json") 347 w.WriteHeader(http.StatusOK) 348 fmt.Fprint(w, GetTrustRoleResponse) 349 }) 350 } 351 352 // HandleCheckTrustRoleSuccessfully creates an HTTP handler at `/OS-TRUST/trusts/987fe8/roles/c1648e` on the 353 // test handler mux that responds with a list trust roles. 354 func HandleCheckTrustRoleSuccessfully(t *testing.T) { 355 th.Mux.HandleFunc("/OS-TRUST/trusts/987fe8/roles/c1648e", func(w http.ResponseWriter, r *http.Request) { 356 th.TestMethod(t, r, "HEAD") 357 th.TestHeader(t, r, "Accept", "application/json") 358 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 359 360 w.WriteHeader(http.StatusOK) 361 }) 362 }