Jamie Hannaford | 40bd5a6 | 2014-10-30 11:58:01 +0100 | [diff] [blame^] | 1 | // +build acceptance identity |
| 2 | |
| 3 | package v2 |
| 4 | |
| 5 | import ( |
| 6 | "strconv" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/rackspace/gophercloud" |
| 10 | "github.com/rackspace/gophercloud/acceptance/tools" |
| 11 | os "github.com/rackspace/gophercloud/openstack/identity/v2/users" |
| 12 | "github.com/rackspace/gophercloud/pagination" |
| 13 | "github.com/rackspace/gophercloud/rackspace/identity/v2/users" |
| 14 | th "github.com/rackspace/gophercloud/testhelper" |
| 15 | ) |
| 16 | |
| 17 | func TestUsers(t *testing.T) { |
| 18 | client := authenticatedClient(t) |
| 19 | |
| 20 | userID := createUser(t, client) |
| 21 | |
| 22 | listUsers(t, client) |
| 23 | |
| 24 | getUser(t, client, userID) |
| 25 | |
| 26 | updateUser(t, client, userID) |
| 27 | |
| 28 | deleteUser(t, client, userID) |
| 29 | } |
| 30 | |
| 31 | func createUser(t *testing.T, client *gophercloud.ServiceClient) string { |
| 32 | t.Log("Creating user") |
| 33 | |
| 34 | opts := users.CreateOpts{ |
| 35 | Username: tools.RandomString("user_", 5), |
| 36 | Enabled: os.Disabled, |
| 37 | Email: "new_user@foo.com", |
| 38 | } |
| 39 | |
| 40 | user, err := users.Create(client, opts).Extract() |
| 41 | th.AssertNoErr(t, err) |
| 42 | t.Logf("Created user %s", user.ID) |
| 43 | |
| 44 | return user.ID |
| 45 | } |
| 46 | |
| 47 | func listUsers(t *testing.T, client *gophercloud.ServiceClient) { |
| 48 | err := users.List(client).EachPage(func(page pagination.Page) (bool, error) { |
| 49 | userList, err := os.ExtractUsers(page) |
| 50 | th.AssertNoErr(t, err) |
| 51 | |
| 52 | for _, user := range userList { |
| 53 | t.Logf("Listing user: ID [%s] Username [%s] Email [%s] Enabled? [%s]", |
| 54 | user.ID, user.Username, user.Email, strconv.FormatBool(user.Enabled)) |
| 55 | } |
| 56 | |
| 57 | return true, nil |
| 58 | }) |
| 59 | |
| 60 | th.AssertNoErr(t, err) |
| 61 | } |
| 62 | |
| 63 | func getUser(t *testing.T, client *gophercloud.ServiceClient, userID string) { |
| 64 | _, err := users.Get(client, userID).Extract() |
| 65 | th.AssertNoErr(t, err) |
| 66 | t.Logf("Getting user %s", userID) |
| 67 | } |
| 68 | |
| 69 | func updateUser(t *testing.T, client *gophercloud.ServiceClient, userID string) { |
| 70 | opts := users.UpdateOpts{Username: tools.RandomString("new_name", 5), Email: "new@foo.com"} |
| 71 | user, err := users.Update(client, userID, opts).Extract() |
| 72 | th.AssertNoErr(t, err) |
| 73 | t.Logf("Updated user %s: Username [%s] Email [%s]", userID, user.Username, user.Email) |
| 74 | } |
| 75 | |
| 76 | func deleteUser(t *testing.T, client *gophercloud.ServiceClient, userID string) { |
| 77 | res := users.Delete(client, userID) |
| 78 | th.AssertNoErr(t, res.Err) |
| 79 | t.Logf("Deleted user %s", userID) |
| 80 | } |