blob: 28c0c832befb7c3b30c5d35ab643a4770d100373 [file] [log] [blame]
Jamie Hannaford40bd5a62014-10-30 11:58:01 +01001// +build acceptance identity
2
3package v2
4
5import (
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
17func 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
Paul Querna266a7c22014-10-31 13:26:51 -070028 resetApiKey(t, client, userID)
29
Jamie Hannaford40bd5a62014-10-30 11:58:01 +010030 deleteUser(t, client, userID)
31}
32
33func createUser(t *testing.T, client *gophercloud.ServiceClient) string {
34 t.Log("Creating user")
35
36 opts := users.CreateOpts{
37 Username: tools.RandomString("user_", 5),
38 Enabled: os.Disabled,
39 Email: "new_user@foo.com",
40 }
41
42 user, err := users.Create(client, opts).Extract()
43 th.AssertNoErr(t, err)
44 t.Logf("Created user %s", user.ID)
45
46 return user.ID
47}
48
49func listUsers(t *testing.T, client *gophercloud.ServiceClient) {
50 err := users.List(client).EachPage(func(page pagination.Page) (bool, error) {
51 userList, err := os.ExtractUsers(page)
52 th.AssertNoErr(t, err)
53
54 for _, user := range userList {
55 t.Logf("Listing user: ID [%s] Username [%s] Email [%s] Enabled? [%s]",
56 user.ID, user.Username, user.Email, strconv.FormatBool(user.Enabled))
57 }
58
59 return true, nil
60 })
61
62 th.AssertNoErr(t, err)
63}
64
65func getUser(t *testing.T, client *gophercloud.ServiceClient, userID string) {
66 _, err := users.Get(client, userID).Extract()
67 th.AssertNoErr(t, err)
68 t.Logf("Getting user %s", userID)
69}
70
71func updateUser(t *testing.T, client *gophercloud.ServiceClient, userID string) {
72 opts := users.UpdateOpts{Username: tools.RandomString("new_name", 5), Email: "new@foo.com"}
73 user, err := users.Update(client, userID, opts).Extract()
74 th.AssertNoErr(t, err)
75 t.Logf("Updated user %s: Username [%s] Email [%s]", userID, user.Username, user.Email)
76}
77
78func deleteUser(t *testing.T, client *gophercloud.ServiceClient, userID string) {
79 res := users.Delete(client, userID)
80 th.AssertNoErr(t, res.Err)
81 t.Logf("Deleted user %s", userID)
82}
Paul Querna266a7c22014-10-31 13:26:51 -070083
84func resetApiKey(t *testing.T, client *gophercloud.ServiceClient, userID string) {
85 key, err := users.ResetAPIKey(client, userID).Extract()
86 th.AssertNoErr(t, err)
87
88 if key.APIKey == "" {
89 t.Fatal("failed to reset API key for user")
90 }
91
92 t.Logf("Reset API key for user %s to %s", key.Username, key.APIKey)
93}