blob: 04f837163abb4ffb8305c364286a7489e3639763 [file] [log] [blame]
Jamie Hannaford2a130242014-10-28 11:19:46 +01001package users
Jamie Hannaford929bd002014-10-29 11:14:25 +01002
3import (
4 "testing"
5
6 "github.com/rackspace/gophercloud/pagination"
7 th "github.com/rackspace/gophercloud/testhelper"
8 "github.com/rackspace/gophercloud/testhelper/client"
9)
10
11func TestList(t *testing.T) {
12 th.SetupHTTP()
13 defer th.TeardownHTTP()
14
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +010015 MockListUserResponse(t)
Jamie Hannaford929bd002014-10-29 11:14:25 +010016
17 count := 0
18
19 err := List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
20 count++
21 actual, err := ExtractUsers(page)
22 if err != nil {
23 t.Errorf("Failed to extract users: %v", err)
24 return false, err
25 }
26
27 expected := []User{
28 User{
29 ID: "u1000",
30 Name: "John Smith",
31 Username: "jqsmith",
32 Email: "john.smith@example.org",
33 Enabled: true,
34 TenantID: "12345",
35 },
36 User{
37 ID: "u1001",
38 Name: "Jane Smith",
39 Username: "jqsmith",
40 Email: "jane.smith@example.org",
41 Enabled: true,
42 TenantID: "12345",
43 },
44 }
45
46 th.CheckDeepEquals(t, expected, actual)
47
48 return true, nil
49 })
50
51 th.AssertNoErr(t, err)
52 th.AssertEquals(t, 1, count)
53}
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010054
55func TestCreateUser(t *testing.T) {
56 th.SetupHTTP()
57 defer th.TeardownHTTP()
58
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +010059 mockCreateUserResponse(t)
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010060
61 opts := CreateOpts{
62 Name: "new_user",
63 TenantID: "12345",
64 Enabled: Disabled,
65 Email: "new_user@foo.com",
66 }
67
68 user, err := Create(client.ServiceClient(), opts).Extract()
69
70 th.AssertNoErr(t, err)
71
72 expected := &User{
73 Name: "new_user",
74 ID: "c39e3de9be2d4c779f1dfd6abacc176d",
75 Email: "new_user@foo.com",
76 Enabled: false,
77 TenantID: "12345",
78 }
79
80 th.AssertDeepEquals(t, expected, user)
81}
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +010082
83func TestGetUser(t *testing.T) {
84 th.SetupHTTP()
85 defer th.TeardownHTTP()
86
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +010087 mockGetUserResponse(t)
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +010088
89 user, err := Get(client.ServiceClient(), "new_user").Extract()
90 th.AssertNoErr(t, err)
91
92 expected := &User{
93 Name: "new_user",
94 ID: "c39e3de9be2d4c779f1dfd6abacc176d",
95 Email: "new_user@foo.com",
96 Enabled: false,
97 TenantID: "12345",
98 }
99
100 th.AssertDeepEquals(t, expected, user)
101}
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100102
103func TestUpdateUser(t *testing.T) {
104 th.SetupHTTP()
105 defer th.TeardownHTTP()
106
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +0100107 mockUpdateUserResponse(t)
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100108
109 id := "c39e3de9be2d4c779f1dfd6abacc176d"
110 opts := UpdateOpts{
111 Name: "new_name",
112 Enabled: Enabled,
113 Email: "new_email@foo.com",
114 }
115
116 user, err := Update(client.ServiceClient(), id, opts).Extract()
117
118 th.AssertNoErr(t, err)
119
120 expected := &User{
121 Name: "new_name",
122 ID: id,
123 Email: "new_email@foo.com",
124 Enabled: true,
125 TenantID: "12345",
126 }
127
128 th.AssertDeepEquals(t, expected, user)
129}
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100130
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +0100131func TestDeleteUser(t *testing.T) {
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100132 th.SetupHTTP()
133 defer th.TeardownHTTP()
134
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +0100135 mockDeleteUserResponse(t)
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100136
137 res := Delete(client.ServiceClient(), "c39e3de9be2d4c779f1dfd6abacc176d")
138 th.AssertNoErr(t, res.Err)
139}
Jamie Hannaforde680e422014-10-29 14:55:57 +0100140
141func TestListingUserRoles(t *testing.T) {
142 th.SetupHTTP()
143 defer th.TeardownHTTP()
144
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +0100145 mockListRolesResponse(t)
Jamie Hannaforde680e422014-10-29 14:55:57 +0100146
147 tenantID := "1d8b6120dcc640fda4fc9194ffc80273"
148 userID := "c39e3de9be2d4c779f1dfd6abacc176d"
149
150 err := ListRoles(client.ServiceClient(), tenantID, userID).EachPage(func(page pagination.Page) (bool, error) {
151 actual, err := ExtractRoles(page)
152 th.AssertNoErr(t, err)
153
154 expected := []Role{
155 Role{ID: "9fe2ff9ee4384b1894a90878d3e92bab", Name: "foo_role"},
156 Role{ID: "1ea3d56793574b668e85960fbf651e13", Name: "admin"},
157 }
158
159 th.CheckDeepEquals(t, expected, actual)
160
161 return true, nil
162 })
163
164 th.AssertNoErr(t, err)
165}