blob: 0e036539cb0d7a64eb62bc51f36b758c96a9dddb [file] [log] [blame]
Jamie Hannaford2a130242014-10-28 11:19:46 +01001package users
Jamie Hannaford929bd002014-10-29 11:14:25 +01002
3import (
4 "testing"
5
Jon Perritt27249f42016-02-18 10:35:59 -06006 "github.com/gophercloud/gophercloud/pagination"
7 th "github.com/gophercloud/gophercloud/testhelper"
8 "github.com/gophercloud/gophercloud/testhelper/client"
Jon Perrittdb0ae142016-03-13 00:33:41 -06009 "github.com/jrperritt/gophercloud"
Jamie Hannaford929bd002014-10-29 11:14:25 +010010)
11
12func TestList(t *testing.T) {
13 th.SetupHTTP()
14 defer th.TeardownHTTP()
15
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +010016 MockListUserResponse(t)
Jamie Hannaford929bd002014-10-29 11:14:25 +010017
18 count := 0
19
20 err := List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
21 count++
22 actual, err := ExtractUsers(page)
Jon Perrittdb0ae142016-03-13 00:33:41 -060023 th.AssertNoErr(t, err)
Jamie Hannaford929bd002014-10-29 11:14:25 +010024
25 expected := []User{
26 User{
27 ID: "u1000",
28 Name: "John Smith",
29 Username: "jqsmith",
30 Email: "john.smith@example.org",
31 Enabled: true,
32 TenantID: "12345",
33 },
34 User{
35 ID: "u1001",
36 Name: "Jane Smith",
37 Username: "jqsmith",
38 Email: "jane.smith@example.org",
39 Enabled: true,
40 TenantID: "12345",
41 },
42 }
Jamie Hannaford929bd002014-10-29 11:14:25 +010043 th.CheckDeepEquals(t, expected, actual)
Jamie Hannaford929bd002014-10-29 11:14:25 +010044 return true, nil
45 })
Jamie Hannaford929bd002014-10-29 11:14:25 +010046 th.AssertNoErr(t, err)
47 th.AssertEquals(t, 1, count)
48}
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010049
50func TestCreateUser(t *testing.T) {
51 th.SetupHTTP()
52 defer th.TeardownHTTP()
53
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +010054 mockCreateUserResponse(t)
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010055
56 opts := CreateOpts{
57 Name: "new_user",
58 TenantID: "12345",
Jon Perrittdb0ae142016-03-13 00:33:41 -060059 Enabled: gophercloud.Disabled,
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010060 Email: "new_user@foo.com",
61 }
62
63 user, err := Create(client.ServiceClient(), opts).Extract()
64
65 th.AssertNoErr(t, err)
66
67 expected := &User{
68 Name: "new_user",
69 ID: "c39e3de9be2d4c779f1dfd6abacc176d",
70 Email: "new_user@foo.com",
71 Enabled: false,
72 TenantID: "12345",
73 }
74
75 th.AssertDeepEquals(t, expected, user)
76}
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +010077
78func TestGetUser(t *testing.T) {
79 th.SetupHTTP()
80 defer th.TeardownHTTP()
81
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +010082 mockGetUserResponse(t)
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +010083
84 user, err := Get(client.ServiceClient(), "new_user").Extract()
85 th.AssertNoErr(t, err)
86
87 expected := &User{
88 Name: "new_user",
89 ID: "c39e3de9be2d4c779f1dfd6abacc176d",
90 Email: "new_user@foo.com",
91 Enabled: false,
92 TenantID: "12345",
93 }
94
95 th.AssertDeepEquals(t, expected, user)
96}
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010097
98func TestUpdateUser(t *testing.T) {
99 th.SetupHTTP()
100 defer th.TeardownHTTP()
101
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +0100102 mockUpdateUserResponse(t)
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100103
104 id := "c39e3de9be2d4c779f1dfd6abacc176d"
105 opts := UpdateOpts{
106 Name: "new_name",
Jon Perrittdb0ae142016-03-13 00:33:41 -0600107 Enabled: gophercloud.Enabled,
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100108 Email: "new_email@foo.com",
109 }
110
111 user, err := Update(client.ServiceClient(), id, opts).Extract()
112
113 th.AssertNoErr(t, err)
114
115 expected := &User{
116 Name: "new_name",
117 ID: id,
118 Email: "new_email@foo.com",
119 Enabled: true,
120 TenantID: "12345",
121 }
122
123 th.AssertDeepEquals(t, expected, user)
124}
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100125
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +0100126func TestDeleteUser(t *testing.T) {
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100127 th.SetupHTTP()
128 defer th.TeardownHTTP()
129
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +0100130 mockDeleteUserResponse(t)
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100131
132 res := Delete(client.ServiceClient(), "c39e3de9be2d4c779f1dfd6abacc176d")
133 th.AssertNoErr(t, res.Err)
134}
Jamie Hannaforde680e422014-10-29 14:55:57 +0100135
136func TestListingUserRoles(t *testing.T) {
137 th.SetupHTTP()
138 defer th.TeardownHTTP()
139
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +0100140 mockListRolesResponse(t)
Jamie Hannaforde680e422014-10-29 14:55:57 +0100141
142 tenantID := "1d8b6120dcc640fda4fc9194ffc80273"
143 userID := "c39e3de9be2d4c779f1dfd6abacc176d"
144
145 err := ListRoles(client.ServiceClient(), tenantID, userID).EachPage(func(page pagination.Page) (bool, error) {
146 actual, err := ExtractRoles(page)
147 th.AssertNoErr(t, err)
148
149 expected := []Role{
150 Role{ID: "9fe2ff9ee4384b1894a90878d3e92bab", Name: "foo_role"},
151 Role{ID: "1ea3d56793574b668e85960fbf651e13", Name: "admin"},
152 }
153
154 th.CheckDeepEquals(t, expected, actual)
155
156 return true, nil
157 })
158
159 th.AssertNoErr(t, err)
160}