github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v3/users/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" 10 "github.com/gophercloud/gophercloud/openstack/identity/v3/groups" 11 "github.com/gophercloud/gophercloud/openstack/identity/v3/projects" 12 "github.com/gophercloud/gophercloud/openstack/identity/v3/users" 13 th "github.com/gophercloud/gophercloud/testhelper" 14 "github.com/gophercloud/gophercloud/testhelper/client" 15 ) 16 17 // ListOutput provides a single page of User results. 18 const ListOutput = ` 19 { 20 "links": { 21 "next": null, 22 "previous": null, 23 "self": "http://example.com/identity/v3/users" 24 }, 25 "users": [ 26 { 27 "domain_id": "default", 28 "enabled": true, 29 "id": "2844b2a08be147a08ef58317d6471f1f", 30 "links": { 31 "self": "http://example.com/identity/v3/users/2844b2a08be147a08ef58317d6471f1f" 32 }, 33 "name": "glance", 34 "password_expires_at": null, 35 "description": "some description", 36 "extra": { 37 "email": "glance@localhost" 38 } 39 }, 40 { 41 "default_project_id": "263fd9", 42 "domain_id": "1789d1", 43 "enabled": true, 44 "id": "9fe1d3", 45 "links": { 46 "self": "https://example.com/identity/v3/users/9fe1d3" 47 }, 48 "name": "jsmith", 49 "password_expires_at": "2016-11-06T15:32:17.000000", 50 "email": "jsmith@example.com", 51 "options": { 52 "ignore_password_expiry": true, 53 "multi_factor_auth_rules": [["password", "totp"], ["password", "custom-auth-method"]] 54 } 55 } 56 ] 57 } 58 ` 59 60 // GetOutput provides a Get result. 61 const GetOutput = ` 62 { 63 "user": { 64 "default_project_id": "263fd9", 65 "domain_id": "1789d1", 66 "enabled": true, 67 "id": "9fe1d3", 68 "links": { 69 "self": "https://example.com/identity/v3/users/9fe1d3" 70 }, 71 "name": "jsmith", 72 "password_expires_at": "2016-11-06T15:32:17.000000", 73 "email": "jsmith@example.com", 74 "options": { 75 "ignore_password_expiry": true, 76 "multi_factor_auth_rules": [["password", "totp"], ["password", "custom-auth-method"]] 77 } 78 } 79 } 80 ` 81 82 // GetOutputNoOptions provides a Get result of a user with no options. 83 const GetOutputNoOptions = ` 84 { 85 "user": { 86 "default_project_id": "263fd9", 87 "domain_id": "1789d1", 88 "enabled": true, 89 "id": "9fe1d3", 90 "links": { 91 "self": "https://example.com/identity/v3/users/9fe1d3" 92 }, 93 "name": "jsmith", 94 "password_expires_at": "2016-11-06T15:32:17.000000", 95 "email": "jsmith@example.com" 96 } 97 } 98 ` 99 100 // CreateRequest provides the input to a Create request. 101 const CreateRequest = ` 102 { 103 "user": { 104 "default_project_id": "263fd9", 105 "domain_id": "1789d1", 106 "enabled": true, 107 "name": "jsmith", 108 "password": "secretsecret", 109 "email": "jsmith@example.com", 110 "options": { 111 "ignore_password_expiry": true, 112 "multi_factor_auth_rules": [["password", "totp"], ["password", "custom-auth-method"]] 113 } 114 } 115 } 116 ` 117 118 // CreateNoOptionsRequest provides the input to a Create request with no Options. 119 const CreateNoOptionsRequest = ` 120 { 121 "user": { 122 "default_project_id": "263fd9", 123 "domain_id": "1789d1", 124 "enabled": true, 125 "name": "jsmith", 126 "password": "secretsecret", 127 "email": "jsmith@example.com" 128 } 129 } 130 ` 131 132 // UpdateRequest provides the input to an Update request. 133 const UpdateRequest = ` 134 { 135 "user": { 136 "enabled": false, 137 "disabled_reason": "DDOS", 138 "options": { 139 "multi_factor_auth_rules": null 140 } 141 } 142 } 143 ` 144 145 // UpdateOutput provides an update result. 146 const UpdateOutput = ` 147 { 148 "user": { 149 "default_project_id": "263fd9", 150 "domain_id": "1789d1", 151 "enabled": false, 152 "id": "9fe1d3", 153 "links": { 154 "self": "https://example.com/identity/v3/users/9fe1d3" 155 }, 156 "name": "jsmith", 157 "password_expires_at": "2016-11-06T15:32:17.000000", 158 "email": "jsmith@example.com", 159 "disabled_reason": "DDOS", 160 "options": { 161 "ignore_password_expiry": true 162 } 163 } 164 } 165 ` 166 167 // ChangePasswordRequest provides the input to a ChangePassword request. 168 const ChangePasswordRequest = ` 169 { 170 "user": { 171 "password": "new_secretsecret", 172 "original_password": "secretsecret" 173 } 174 } 175 ` 176 177 // ListGroupsOutput provides a ListGroups result. 178 const ListGroupsOutput = ` 179 { 180 "groups": [ 181 { 182 "description": "Developers cleared for work on all general projects", 183 "domain_id": "1789d1", 184 "id": "ea167b", 185 "links": { 186 "self": "https://example.com/identity/v3/groups/ea167b" 187 }, 188 "building": "Hilltop A", 189 "name": "Developers" 190 }, 191 { 192 "description": "Developers cleared for work on secret projects", 193 "domain_id": "1789d1", 194 "id": "a62db1", 195 "links": { 196 "self": "https://example.com/identity/v3/groups/a62db1" 197 }, 198 "name": "Secure Developers" 199 } 200 ], 201 "links": { 202 "self": "http://example.com/identity/v3/users/9fe1d3/groups", 203 "previous": null, 204 "next": null 205 } 206 } 207 ` 208 209 // ListProjectsOutput provides a ListProjects result. 210 const ListProjectsOutput = ` 211 { 212 "links": { 213 "next": null, 214 "previous": null, 215 "self": "http://localhost:5000/identity/v3/users/foobar/projects" 216 }, 217 "projects": [ 218 { 219 "description": "my first project", 220 "domain_id": "11111", 221 "enabled": true, 222 "id": "abcde", 223 "links": { 224 "self": "http://localhost:5000/identity/v3/projects/abcde" 225 }, 226 "name": "project 1", 227 "parent_id": "11111" 228 }, 229 { 230 "description": "my second project", 231 "domain_id": "22222", 232 "enabled": true, 233 "id": "bcdef", 234 "links": { 235 "self": "http://localhost:5000/identity/v3/projects/bcdef" 236 }, 237 "name": "project 2", 238 "parent_id": "22222" 239 } 240 ] 241 } 242 ` 243 244 // FirstUser is the first user in the List request. 245 var nilTime time.Time 246 var FirstUser = users.User{ 247 DomainID: "default", 248 Enabled: true, 249 ID: "2844b2a08be147a08ef58317d6471f1f", 250 Links: map[string]interface{}{ 251 "self": "http://example.com/identity/v3/users/2844b2a08be147a08ef58317d6471f1f", 252 }, 253 Name: "glance", 254 PasswordExpiresAt: nilTime, 255 Description: "some description", 256 Extra: map[string]interface{}{ 257 "email": "glance@localhost", 258 }, 259 } 260 261 // SecondUser is the second user in the List request. 262 var SecondUserPasswordExpiresAt, _ = time.Parse(gophercloud.RFC3339MilliNoZ, "2016-11-06T15:32:17.000000") 263 var SecondUser = users.User{ 264 DefaultProjectID: "263fd9", 265 DomainID: "1789d1", 266 Enabled: true, 267 ID: "9fe1d3", 268 Links: map[string]interface{}{ 269 "self": "https://example.com/identity/v3/users/9fe1d3", 270 }, 271 Name: "jsmith", 272 PasswordExpiresAt: SecondUserPasswordExpiresAt, 273 Extra: map[string]interface{}{ 274 "email": "jsmith@example.com", 275 }, 276 Options: map[string]interface{}{ 277 "ignore_password_expiry": true, 278 "multi_factor_auth_rules": []interface{}{ 279 []string{"password", "totp"}, 280 []string{"password", "custom-auth-method"}, 281 }, 282 }, 283 } 284 285 var SecondUserNoOptions = users.User{ 286 DefaultProjectID: "263fd9", 287 DomainID: "1789d1", 288 Enabled: true, 289 ID: "9fe1d3", 290 Links: map[string]interface{}{ 291 "self": "https://example.com/identity/v3/users/9fe1d3", 292 }, 293 Name: "jsmith", 294 PasswordExpiresAt: SecondUserPasswordExpiresAt, 295 Extra: map[string]interface{}{ 296 "email": "jsmith@example.com", 297 }, 298 } 299 300 // SecondUserUpdated is how SecondUser should look after an Update. 301 var SecondUserUpdated = users.User{ 302 DefaultProjectID: "263fd9", 303 DomainID: "1789d1", 304 Enabled: false, 305 ID: "9fe1d3", 306 Links: map[string]interface{}{ 307 "self": "https://example.com/identity/v3/users/9fe1d3", 308 }, 309 Name: "jsmith", 310 PasswordExpiresAt: SecondUserPasswordExpiresAt, 311 Extra: map[string]interface{}{ 312 "email": "jsmith@example.com", 313 "disabled_reason": "DDOS", 314 }, 315 Options: map[string]interface{}{ 316 "ignore_password_expiry": true, 317 }, 318 } 319 320 // ExpectedUsersSlice is the slice of users expected to be returned from ListOutput. 321 var ExpectedUsersSlice = []users.User{FirstUser, SecondUser} 322 323 var FirstGroup = groups.Group{ 324 Description: "Developers cleared for work on all general projects", 325 DomainID: "1789d1", 326 ID: "ea167b", 327 Links: map[string]interface{}{ 328 "self": "https://example.com/identity/v3/groups/ea167b", 329 }, 330 Extra: map[string]interface{}{ 331 "building": "Hilltop A", 332 }, 333 Name: "Developers", 334 } 335 336 var SecondGroup = groups.Group{ 337 Description: "Developers cleared for work on secret projects", 338 DomainID: "1789d1", 339 ID: "a62db1", 340 Links: map[string]interface{}{ 341 "self": "https://example.com/identity/v3/groups/a62db1", 342 }, 343 Extra: map[string]interface{}{}, 344 Name: "Secure Developers", 345 } 346 347 var ExpectedGroupsSlice = []groups.Group{FirstGroup, SecondGroup} 348 349 var FirstProject = projects.Project{ 350 Description: "my first project", 351 DomainID: "11111", 352 Enabled: true, 353 ID: "abcde", 354 Name: "project 1", 355 ParentID: "11111", 356 Extra: map[string]interface{}{ 357 "links": map[string]interface{}{"self": "http://localhost:5000/identity/v3/projects/abcde"}, 358 }, 359 } 360 361 var SecondProject = projects.Project{ 362 Description: "my second project", 363 DomainID: "22222", 364 Enabled: true, 365 ID: "bcdef", 366 Name: "project 2", 367 ParentID: "22222", 368 Extra: map[string]interface{}{ 369 "links": map[string]interface{}{"self": "http://localhost:5000/identity/v3/projects/bcdef"}, 370 }, 371 } 372 373 var ExpectedProjectsSlice = []projects.Project{FirstProject, SecondProject} 374 375 // HandleListUsersSuccessfully creates an HTTP handler at `/users` on the 376 // test handler mux that responds with a list of two users. 377 func HandleListUsersSuccessfully(t *testing.T) { 378 th.Mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) { 379 th.TestMethod(t, r, "GET") 380 th.TestHeader(t, r, "Accept", "application/json") 381 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 382 383 w.Header().Set("Content-Type", "application/json") 384 w.WriteHeader(http.StatusOK) 385 fmt.Fprintf(w, ListOutput) 386 }) 387 } 388 389 // HandleGetUserSuccessfully creates an HTTP handler at `/users` on the 390 // test handler mux that responds with a single user. 391 func HandleGetUserSuccessfully(t *testing.T) { 392 th.Mux.HandleFunc("/users/9fe1d3", func(w http.ResponseWriter, r *http.Request) { 393 th.TestMethod(t, r, "GET") 394 th.TestHeader(t, r, "Accept", "application/json") 395 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 396 397 w.Header().Set("Content-Type", "application/json") 398 w.WriteHeader(http.StatusOK) 399 fmt.Fprintf(w, GetOutput) 400 }) 401 } 402 403 // HandleCreateUserSuccessfully creates an HTTP handler at `/users` on the 404 // test handler mux that tests user creation. 405 func HandleCreateUserSuccessfully(t *testing.T) { 406 th.Mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) { 407 th.TestMethod(t, r, "POST") 408 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 409 th.TestJSONRequest(t, r, CreateRequest) 410 411 w.WriteHeader(http.StatusCreated) 412 fmt.Fprintf(w, GetOutput) 413 }) 414 } 415 416 // HandleCreateNoOptionsUserSuccessfully creates an HTTP handler at `/users` on the 417 // test handler mux that tests user creation. 418 func HandleCreateNoOptionsUserSuccessfully(t *testing.T) { 419 th.Mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) { 420 th.TestMethod(t, r, "POST") 421 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 422 th.TestJSONRequest(t, r, CreateNoOptionsRequest) 423 424 w.WriteHeader(http.StatusCreated) 425 fmt.Fprintf(w, GetOutputNoOptions) 426 }) 427 } 428 429 // HandleUpdateUserSuccessfully creates an HTTP handler at `/users` on the 430 // test handler mux that tests user update. 431 func HandleUpdateUserSuccessfully(t *testing.T) { 432 th.Mux.HandleFunc("/users/9fe1d3", func(w http.ResponseWriter, r *http.Request) { 433 th.TestMethod(t, r, "PATCH") 434 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 435 th.TestJSONRequest(t, r, UpdateRequest) 436 437 w.WriteHeader(http.StatusOK) 438 fmt.Fprintf(w, UpdateOutput) 439 }) 440 } 441 442 // HandleChangeUserPasswordSuccessfully creates an HTTP handler at `/users` on the 443 // test handler mux that tests change user password. 444 func HandleChangeUserPasswordSuccessfully(t *testing.T) { 445 th.Mux.HandleFunc("/users/9fe1d3/password", func(w http.ResponseWriter, r *http.Request) { 446 th.TestMethod(t, r, "POST") 447 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 448 th.TestJSONRequest(t, r, ChangePasswordRequest) 449 450 w.WriteHeader(http.StatusNoContent) 451 }) 452 } 453 454 // HandleDeleteUserSuccessfully creates an HTTP handler at `/users` on the 455 // test handler mux that tests user deletion. 456 func HandleDeleteUserSuccessfully(t *testing.T) { 457 th.Mux.HandleFunc("/users/9fe1d3", func(w http.ResponseWriter, r *http.Request) { 458 th.TestMethod(t, r, "DELETE") 459 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 460 461 w.WriteHeader(http.StatusNoContent) 462 }) 463 } 464 465 // HandleListUserGroupsSuccessfully creates an HTTP handler at /users/{userID}/groups 466 // on the test handler mux that respons with a list of two groups 467 func HandleListUserGroupsSuccessfully(t *testing.T) { 468 th.Mux.HandleFunc("/users/9fe1d3/groups", func(w http.ResponseWriter, r *http.Request) { 469 th.TestMethod(t, r, "GET") 470 th.TestHeader(t, r, "Accept", "application/json") 471 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 472 473 w.Header().Set("Content-Type", "application/json") 474 w.WriteHeader(http.StatusOK) 475 fmt.Fprintf(w, ListGroupsOutput) 476 }) 477 } 478 479 // HandleAddToGroupSuccessfully creates an HTTP handler at /groups/{groupID}/users/{userID} 480 // on the test handler mux that tests adding user to group. 481 func HandleAddToGroupSuccessfully(t *testing.T) { 482 th.Mux.HandleFunc("/groups/ea167b/users/9fe1d3", func(w http.ResponseWriter, r *http.Request) { 483 th.TestMethod(t, r, "PUT") 484 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 485 486 w.WriteHeader(http.StatusNoContent) 487 }) 488 } 489 490 // HandleIsMemberOfGroupSuccessfully creates an HTTP handler at /groups/{groupID}/users/{userID} 491 // on the test handler mux that tests checking whether user belongs to group. 492 func HandleIsMemberOfGroupSuccessfully(t *testing.T) { 493 th.Mux.HandleFunc("/groups/ea167b/users/9fe1d3", func(w http.ResponseWriter, r *http.Request) { 494 th.TestMethod(t, r, "HEAD") 495 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 496 497 w.WriteHeader(http.StatusNoContent) 498 }) 499 } 500 501 // HandleRemoveFromGroupSuccessfully creates an HTTP handler at /groups/{groupID}/users/{userID} 502 // on the test handler mux that tests removing user from group. 503 func HandleRemoveFromGroupSuccessfully(t *testing.T) { 504 th.Mux.HandleFunc("/groups/ea167b/users/9fe1d3", func(w http.ResponseWriter, r *http.Request) { 505 th.TestMethod(t, r, "DELETE") 506 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 507 508 w.WriteHeader(http.StatusNoContent) 509 }) 510 } 511 512 // HandleListUserProjectsSuccessfully creates an HTTP handler at /users/{userID}/projects 513 // on the test handler mux that respons wit a list of two projects 514 func HandleListUserProjectsSuccessfully(t *testing.T) { 515 th.Mux.HandleFunc("/users/9fe1d3/projects", func(w http.ResponseWriter, r *http.Request) { 516 th.TestMethod(t, r, "GET") 517 th.TestHeader(t, r, "Accept", "application/json") 518 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 519 520 w.Header().Set("Content-Type", "application/json") 521 w.WriteHeader(http.StatusOK) 522 fmt.Fprintf(w, ListProjectsOutput) 523 }) 524 } 525 526 // HandleListInGroupSuccessfully creates an HTTP handler at /groups/{groupID}/users 527 // on the test handler mux that response with a list of two users 528 func HandleListInGroupSuccessfully(t *testing.T) { 529 th.Mux.HandleFunc("/groups/ea167b/users", func(w http.ResponseWriter, r *http.Request) { 530 th.TestMethod(t, r, "GET") 531 th.TestHeader(t, r, "Accept", "application/json") 532 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 533 534 w.Header().Set("Content-Type", "application/json") 535 w.WriteHeader(http.StatusOK) 536 fmt.Fprintf(w, ListOutput) 537 }) 538 }