PageRenderTime 140ms CodeModel.GetById 60ms RepoModel.GetById 0ms app.codeStats 0ms

/Godeps/_workspace/src/github.com/rackspace/gophercloud/acceptance/rackspace/identity/v2/user_test.go

https://gitlab.com/Red54/machine
Go | 93 lines | 68 code | 24 blank | 1 comment | 3 complexity | a590458cf349a8229849180a3f7c9be1 MD5 | raw file
  1. // +build acceptance identity
  2. package v2
  3. import (
  4. "strconv"
  5. "testing"
  6. "github.com/rackspace/gophercloud"
  7. "github.com/rackspace/gophercloud/acceptance/tools"
  8. os "github.com/rackspace/gophercloud/openstack/identity/v2/users"
  9. "github.com/rackspace/gophercloud/pagination"
  10. "github.com/rackspace/gophercloud/rackspace/identity/v2/users"
  11. th "github.com/rackspace/gophercloud/testhelper"
  12. )
  13. func TestUsers(t *testing.T) {
  14. client := authenticatedClient(t)
  15. userID := createUser(t, client)
  16. listUsers(t, client)
  17. getUser(t, client, userID)
  18. updateUser(t, client, userID)
  19. resetApiKey(t, client, userID)
  20. deleteUser(t, client, userID)
  21. }
  22. func createUser(t *testing.T, client *gophercloud.ServiceClient) string {
  23. t.Log("Creating user")
  24. opts := users.CreateOpts{
  25. Username: tools.RandomString("user_", 5),
  26. Enabled: os.Disabled,
  27. Email: "new_user@foo.com",
  28. }
  29. user, err := users.Create(client, opts).Extract()
  30. th.AssertNoErr(t, err)
  31. t.Logf("Created user %s", user.ID)
  32. return user.ID
  33. }
  34. func listUsers(t *testing.T, client *gophercloud.ServiceClient) {
  35. err := users.List(client).EachPage(func(page pagination.Page) (bool, error) {
  36. userList, err := os.ExtractUsers(page)
  37. th.AssertNoErr(t, err)
  38. for _, user := range userList {
  39. t.Logf("Listing user: ID [%s] Username [%s] Email [%s] Enabled? [%s]",
  40. user.ID, user.Username, user.Email, strconv.FormatBool(user.Enabled))
  41. }
  42. return true, nil
  43. })
  44. th.AssertNoErr(t, err)
  45. }
  46. func getUser(t *testing.T, client *gophercloud.ServiceClient, userID string) {
  47. _, err := users.Get(client, userID).Extract()
  48. th.AssertNoErr(t, err)
  49. t.Logf("Getting user %s", userID)
  50. }
  51. func updateUser(t *testing.T, client *gophercloud.ServiceClient, userID string) {
  52. opts := users.UpdateOpts{Username: tools.RandomString("new_name", 5), Email: "new@foo.com"}
  53. user, err := users.Update(client, userID, opts).Extract()
  54. th.AssertNoErr(t, err)
  55. t.Logf("Updated user %s: Username [%s] Email [%s]", userID, user.Username, user.Email)
  56. }
  57. func deleteUser(t *testing.T, client *gophercloud.ServiceClient, userID string) {
  58. res := users.Delete(client, userID)
  59. th.AssertNoErr(t, res.Err)
  60. t.Logf("Deleted user %s", userID)
  61. }
  62. func resetApiKey(t *testing.T, client *gophercloud.ServiceClient, userID string) {
  63. key, err := users.ResetAPIKey(client, userID).Extract()
  64. th.AssertNoErr(t, err)
  65. if key.APIKey == "" {
  66. t.Fatal("failed to reset API key for user")
  67. }
  68. t.Logf("Reset API key for user %s to %s", key.Username, key.APIKey)
  69. }