blob: 65795785bff95c517f4d99a2ee072ae940664cef [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Jamie Hannaford929bd002014-10-29 11:14:25 +01002
3import (
4 "testing"
5
jrperritt8ce5e282016-04-13 14:31:01 -05006 "github.com/gophercloud/gophercloud"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02007 "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/identity/v2/users"
8 "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination"
9 th "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper"
10 "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper/client"
Jamie Hannaford929bd002014-10-29 11:14:25 +010011)
12
13func TestList(t *testing.T) {
14 th.SetupHTTP()
15 defer th.TeardownHTTP()
16
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +010017 MockListUserResponse(t)
Jamie Hannaford929bd002014-10-29 11:14:25 +010018
19 count := 0
20
jrperritt3d966162016-06-06 14:08:54 -050021 err := users.List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford929bd002014-10-29 11:14:25 +010022 count++
jrperritt3d966162016-06-06 14:08:54 -050023 actual, err := users.ExtractUsers(page)
Jon Perrittdb0ae142016-03-13 00:33:41 -060024 th.AssertNoErr(t, err)
Jamie Hannaford929bd002014-10-29 11:14:25 +010025
jrperritt3d966162016-06-06 14:08:54 -050026 expected := []users.User{
27 {
Jamie Hannaford929bd002014-10-29 11:14:25 +010028 ID: "u1000",
29 Name: "John Smith",
30 Username: "jqsmith",
31 Email: "john.smith@example.org",
32 Enabled: true,
33 TenantID: "12345",
34 },
jrperritt3d966162016-06-06 14:08:54 -050035 {
Jamie Hannaford929bd002014-10-29 11:14:25 +010036 ID: "u1001",
37 Name: "Jane Smith",
38 Username: "jqsmith",
39 Email: "jane.smith@example.org",
40 Enabled: true,
41 TenantID: "12345",
42 },
43 }
Jamie Hannaford929bd002014-10-29 11:14:25 +010044 th.CheckDeepEquals(t, expected, actual)
Jamie Hannaford929bd002014-10-29 11:14:25 +010045 return true, nil
46 })
Jamie Hannaford929bd002014-10-29 11:14:25 +010047 th.AssertNoErr(t, err)
48 th.AssertEquals(t, 1, count)
49}
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010050
51func TestCreateUser(t *testing.T) {
52 th.SetupHTTP()
53 defer th.TeardownHTTP()
54
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +010055 mockCreateUserResponse(t)
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010056
jrperritt3d966162016-06-06 14:08:54 -050057 opts := users.CreateOpts{
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010058 Name: "new_user",
59 TenantID: "12345",
Jon Perrittdb0ae142016-03-13 00:33:41 -060060 Enabled: gophercloud.Disabled,
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010061 Email: "new_user@foo.com",
62 }
63
jrperritt3d966162016-06-06 14:08:54 -050064 user, err := users.Create(client.ServiceClient(), opts).Extract()
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010065
66 th.AssertNoErr(t, err)
67
jrperritt3d966162016-06-06 14:08:54 -050068 expected := &users.User{
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010069 Name: "new_user",
70 ID: "c39e3de9be2d4c779f1dfd6abacc176d",
71 Email: "new_user@foo.com",
72 Enabled: false,
73 TenantID: "12345",
74 }
75
76 th.AssertDeepEquals(t, expected, user)
77}
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +010078
79func TestGetUser(t *testing.T) {
80 th.SetupHTTP()
81 defer th.TeardownHTTP()
82
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +010083 mockGetUserResponse(t)
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +010084
jrperritt3d966162016-06-06 14:08:54 -050085 user, err := users.Get(client.ServiceClient(), "new_user").Extract()
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +010086 th.AssertNoErr(t, err)
87
jrperritt3d966162016-06-06 14:08:54 -050088 expected := &users.User{
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +010089 Name: "new_user",
90 ID: "c39e3de9be2d4c779f1dfd6abacc176d",
91 Email: "new_user@foo.com",
92 Enabled: false,
93 TenantID: "12345",
94 }
95
96 th.AssertDeepEquals(t, expected, user)
97}
Jamie Hannaford7e04adf2014-10-29 13:47:58 +010098
99func TestUpdateUser(t *testing.T) {
100 th.SetupHTTP()
101 defer th.TeardownHTTP()
102
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +0100103 mockUpdateUserResponse(t)
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100104
105 id := "c39e3de9be2d4c779f1dfd6abacc176d"
jrperritt3d966162016-06-06 14:08:54 -0500106 opts := users.UpdateOpts{
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100107 Name: "new_name",
Jon Perrittdb0ae142016-03-13 00:33:41 -0600108 Enabled: gophercloud.Enabled,
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100109 Email: "new_email@foo.com",
110 }
111
jrperritt3d966162016-06-06 14:08:54 -0500112 user, err := users.Update(client.ServiceClient(), id, opts).Extract()
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100113
114 th.AssertNoErr(t, err)
115
jrperritt3d966162016-06-06 14:08:54 -0500116 expected := &users.User{
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100117 Name: "new_name",
118 ID: id,
119 Email: "new_email@foo.com",
120 Enabled: true,
121 TenantID: "12345",
122 }
123
124 th.AssertDeepEquals(t, expected, user)
125}
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100126
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +0100127func TestDeleteUser(t *testing.T) {
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100128 th.SetupHTTP()
129 defer th.TeardownHTTP()
130
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +0100131 mockDeleteUserResponse(t)
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100132
jrperritt3d966162016-06-06 14:08:54 -0500133 res := users.Delete(client.ServiceClient(), "c39e3de9be2d4c779f1dfd6abacc176d")
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100134 th.AssertNoErr(t, res.Err)
135}
Jamie Hannaforde680e422014-10-29 14:55:57 +0100136
137func TestListingUserRoles(t *testing.T) {
138 th.SetupHTTP()
139 defer th.TeardownHTTP()
140
Jamie Hannaford10cf2bd2014-10-30 12:21:14 +0100141 mockListRolesResponse(t)
Jamie Hannaforde680e422014-10-29 14:55:57 +0100142
143 tenantID := "1d8b6120dcc640fda4fc9194ffc80273"
144 userID := "c39e3de9be2d4c779f1dfd6abacc176d"
145
jrperritt3d966162016-06-06 14:08:54 -0500146 err := users.ListRoles(client.ServiceClient(), tenantID, userID).EachPage(func(page pagination.Page) (bool, error) {
147 actual, err := users.ExtractRoles(page)
Jamie Hannaforde680e422014-10-29 14:55:57 +0100148 th.AssertNoErr(t, err)
149
jrperritt3d966162016-06-06 14:08:54 -0500150 expected := []users.Role{
151 {ID: "9fe2ff9ee4384b1894a90878d3e92bab", Name: "foo_role"},
152 {ID: "1ea3d56793574b668e85960fbf651e13", Name: "admin"},
Jamie Hannaforde680e422014-10-29 14:55:57 +0100153 }
154
155 th.CheckDeepEquals(t, expected, actual)
156
157 return true, nil
158 })
159
160 th.AssertNoErr(t, err)
161}