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