blob: fe73d19898749894d04e387b3d33dd68ac0a94d3 [file] [log] [blame]
Jamie Hannaford4e7c7202014-10-30 11:46:21 +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 "github.com/rackspace/gophercloud/openstack/identity/v2/tenants"
12 "github.com/rackspace/gophercloud/openstack/identity/v2/users"
13 "github.com/rackspace/gophercloud/pagination"
14 th "github.com/rackspace/gophercloud/testhelper"
15)
16
17func TestUsers(t *testing.T) {
18 client := authenticatedClient(t)
19
20 tenantID := findTenant(t, client)
21
22 userID := createUser(t, client, tenantID)
23
24 listUsers(t, client)
25
26 getUser(t, client, userID)
27
28 updateUser(t, client, userID)
29
30 listUserRoles(t, client, tenantID, userID)
31
32 deleteUser(t, client, userID)
33}
34
35func findTenant(t *testing.T, client *gophercloud.ServiceClient) string {
36 var tenantID string
37 err := tenants.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
38 tenantList, err := tenants.ExtractTenants(page)
39 th.AssertNoErr(t, err)
40
41 for _, t := range tenantList {
42 tenantID = t.ID
43 break
44 }
45
46 return true, nil
47 })
48 th.AssertNoErr(t, err)
49
50 return tenantID
51}
52
53func createUser(t *testing.T, client *gophercloud.ServiceClient, tenantID string) string {
54 t.Log("Creating user")
55
56 opts := users.CreateOpts{
57 Name: tools.RandomString("user_", 5),
58 Enabled: users.Disabled,
59 TenantID: tenantID,
60 Email: "new_user@foo.com",
61 }
62
63 user, err := users.Create(client, opts).Extract()
64 th.AssertNoErr(t, err)
65 t.Logf("Created user %s on tenant %s", user.ID, tenantID)
66
67 return user.ID
68}
69
70func listUsers(t *testing.T, client *gophercloud.ServiceClient) {
71 err := users.List(client).EachPage(func(page pagination.Page) (bool, error) {
72 userList, err := users.ExtractUsers(page)
73 th.AssertNoErr(t, err)
74
75 for _, user := range userList {
76 t.Logf("Listing user: ID [%s] Name [%s] Email [%s] Enabled? [%s]",
77 user.ID, user.Name, user.Email, strconv.FormatBool(user.Enabled))
78 }
79
80 return true, nil
81 })
82
83 th.AssertNoErr(t, err)
84}
85
86func getUser(t *testing.T, client *gophercloud.ServiceClient, userID string) {
87 _, err := users.Get(client, userID).Extract()
88 th.AssertNoErr(t, err)
89 t.Logf("Getting user %s", userID)
90}
91
92func updateUser(t *testing.T, client *gophercloud.ServiceClient, userID string) {
93 opts := users.UpdateOpts{Name: tools.RandomString("new_name", 5), Email: "new@foo.com"}
94 user, err := users.Update(client, userID, opts).Extract()
95 th.AssertNoErr(t, err)
96 t.Logf("Updated user %s: Name [%s] Email [%s]", userID, user.Name, user.Email)
97}
98
99func listUserRoles(t *testing.T, client *gophercloud.ServiceClient, tenantID, userID string) {
100 count := 0
101 err := users.ListRoles(client, tenantID, userID).EachPage(func(page pagination.Page) (bool, error) {
102 count++
103
104 roleList, err := users.ExtractRoles(page)
105 th.AssertNoErr(t, err)
106
107 t.Logf("Listing roles for user %s", userID)
108
109 for _, r := range roleList {
110 t.Logf("- %s (%s)", r.Name, r.ID)
111 }
112
113 return true, nil
114 })
115
116 if count == 0 {
117 t.Logf("No roles for user %s", userID)
118 }
119
120 th.AssertNoErr(t, err)
121}
122
123func deleteUser(t *testing.T, client *gophercloud.ServiceClient, userID string) {
124 res := users.Delete(client, userID)
125 th.AssertNoErr(t, res.Err)
126 t.Logf("Deleted user %s", userID)
127}