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