1package testing
2
3import (
4	"testing"
5
6	"github.com/gophercloud/gophercloud/openstack/identity/v3/groups"
7	"github.com/gophercloud/gophercloud/openstack/identity/v3/projects"
8	"github.com/gophercloud/gophercloud/openstack/identity/v3/users"
9	"github.com/gophercloud/gophercloud/pagination"
10	th "github.com/gophercloud/gophercloud/testhelper"
11	"github.com/gophercloud/gophercloud/testhelper/client"
12)
13
14func TestListUsers(t *testing.T) {
15	th.SetupHTTP()
16	defer th.TeardownHTTP()
17	HandleListUsersSuccessfully(t)
18
19	count := 0
20	err := users.List(client.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) {
21		count++
22
23		actual, err := users.ExtractUsers(page)
24		th.AssertNoErr(t, err)
25
26		th.CheckDeepEquals(t, ExpectedUsersSlice, actual)
27
28		return true, nil
29	})
30	th.AssertNoErr(t, err)
31	th.CheckEquals(t, count, 1)
32}
33
34func TestListUsersAllPages(t *testing.T) {
35	th.SetupHTTP()
36	defer th.TeardownHTTP()
37	HandleListUsersSuccessfully(t)
38
39	allPages, err := users.List(client.ServiceClient(), nil).AllPages()
40	th.AssertNoErr(t, err)
41	actual, err := users.ExtractUsers(allPages)
42	th.AssertNoErr(t, err)
43	th.CheckDeepEquals(t, ExpectedUsersSlice, actual)
44	th.AssertEquals(t, ExpectedUsersSlice[0].Extra["email"], "glance@localhost")
45	th.AssertEquals(t, ExpectedUsersSlice[1].Extra["email"], "jsmith@example.com")
46}
47
48func TestListUsersFiltersCheck(t *testing.T) {
49	type test struct {
50		filterName string
51		wantErr    bool
52	}
53	tests := []test{
54		{"foo__contains", false},
55		{"foo", true},
56		{"foo_contains", true},
57		{"foo__", true},
58		{"__foo", true},
59	}
60
61	var listOpts users.ListOpts
62	for _, _test := range tests {
63		listOpts.Filters = map[string]string{_test.filterName: "bar"}
64		_, err := listOpts.ToUserListQuery()
65
66		if !_test.wantErr {
67			th.AssertNoErr(t, err)
68		} else {
69			switch _t := err.(type) {
70			case nil:
71				t.Fatal("error expected but got a nil")
72			case users.InvalidListFilter:
73			default:
74				t.Fatalf("unexpected error type: [%T]", _t)
75			}
76		}
77	}
78}
79
80func TestGetUser(t *testing.T) {
81	th.SetupHTTP()
82	defer th.TeardownHTTP()
83	HandleGetUserSuccessfully(t)
84
85	actual, err := users.Get(client.ServiceClient(), "9fe1d3").Extract()
86	th.AssertNoErr(t, err)
87	th.CheckDeepEquals(t, SecondUser, *actual)
88	th.AssertEquals(t, SecondUser.Extra["email"], "jsmith@example.com")
89}
90
91func TestCreateUser(t *testing.T) {
92	th.SetupHTTP()
93	defer th.TeardownHTTP()
94	HandleCreateUserSuccessfully(t)
95
96	iTrue := true
97	createOpts := users.CreateOpts{
98		Name:             "jsmith",
99		DomainID:         "1789d1",
100		Enabled:          &iTrue,
101		Password:         "secretsecret",
102		DefaultProjectID: "263fd9",
103		Options: map[users.Option]interface{}{
104			users.IgnorePasswordExpiry: true,
105			users.MultiFactorAuthRules: []interface{}{
106				[]string{"password", "totp"},
107				[]string{"password", "custom-auth-method"},
108			},
109		},
110		Extra: map[string]interface{}{
111			"email": "jsmith@example.com",
112		},
113	}
114
115	actual, err := users.Create(client.ServiceClient(), createOpts).Extract()
116	th.AssertNoErr(t, err)
117	th.CheckDeepEquals(t, SecondUser, *actual)
118}
119
120func TestCreateNoOptionsUser(t *testing.T) {
121	th.SetupHTTP()
122	defer th.TeardownHTTP()
123	HandleCreateNoOptionsUserSuccessfully(t)
124
125	iTrue := true
126	createOpts := users.CreateOpts{
127		Name:             "jsmith",
128		DomainID:         "1789d1",
129		Enabled:          &iTrue,
130		Password:         "secretsecret",
131		DefaultProjectID: "263fd9",
132		Extra: map[string]interface{}{
133			"email": "jsmith@example.com",
134		},
135	}
136
137	actual, err := users.Create(client.ServiceClient(), createOpts).Extract()
138	th.AssertNoErr(t, err)
139	th.CheckDeepEquals(t, SecondUserNoOptions, *actual)
140}
141
142func TestUpdateUser(t *testing.T) {
143	th.SetupHTTP()
144	defer th.TeardownHTTP()
145	HandleUpdateUserSuccessfully(t)
146
147	iFalse := false
148	updateOpts := users.UpdateOpts{
149		Enabled: &iFalse,
150		Options: map[users.Option]interface{}{
151			users.MultiFactorAuthRules: nil,
152		},
153		Extra: map[string]interface{}{
154			"disabled_reason": "DDOS",
155		},
156	}
157
158	actual, err := users.Update(client.ServiceClient(), "9fe1d3", updateOpts).Extract()
159	th.AssertNoErr(t, err)
160	th.CheckDeepEquals(t, SecondUserUpdated, *actual)
161}
162
163func TestChangeUserPassword(t *testing.T) {
164	th.SetupHTTP()
165	defer th.TeardownHTTP()
166	HandleChangeUserPasswordSuccessfully(t)
167
168	changePasswordOpts := users.ChangePasswordOpts{
169		OriginalPassword: "secretsecret",
170		Password:         "new_secretsecret",
171	}
172
173	res := users.ChangePassword(client.ServiceClient(), "9fe1d3", changePasswordOpts)
174	th.AssertNoErr(t, res.Err)
175}
176
177func TestDeleteUser(t *testing.T) {
178	th.SetupHTTP()
179	defer th.TeardownHTTP()
180	HandleDeleteUserSuccessfully(t)
181
182	res := users.Delete(client.ServiceClient(), "9fe1d3")
183	th.AssertNoErr(t, res.Err)
184}
185
186func TestListUserGroups(t *testing.T) {
187	th.SetupHTTP()
188	defer th.TeardownHTTP()
189	HandleListUserGroupsSuccessfully(t)
190	allPages, err := users.ListGroups(client.ServiceClient(), "9fe1d3").AllPages()
191	th.AssertNoErr(t, err)
192	actual, err := groups.ExtractGroups(allPages)
193	th.AssertNoErr(t, err)
194	th.CheckDeepEquals(t, ExpectedGroupsSlice, actual)
195}
196
197func TestAddToGroup(t *testing.T) {
198	th.SetupHTTP()
199	defer th.TeardownHTTP()
200	HandleAddToGroupSuccessfully(t)
201	res := users.AddToGroup(client.ServiceClient(), "ea167b", "9fe1d3")
202	th.AssertNoErr(t, res.Err)
203}
204
205func TestIsMemberOfGroup(t *testing.T) {
206	th.SetupHTTP()
207	defer th.TeardownHTTP()
208	HandleIsMemberOfGroupSuccessfully(t)
209	ok, err := users.IsMemberOfGroup(client.ServiceClient(), "ea167b", "9fe1d3").Extract()
210	th.AssertNoErr(t, err)
211	th.AssertEquals(t, true, ok)
212}
213
214func TestRemoveFromGroup(t *testing.T) {
215	th.SetupHTTP()
216	defer th.TeardownHTTP()
217	HandleRemoveFromGroupSuccessfully(t)
218	res := users.RemoveFromGroup(client.ServiceClient(), "ea167b", "9fe1d3")
219	th.AssertNoErr(t, res.Err)
220}
221
222func TestListUserProjects(t *testing.T) {
223	th.SetupHTTP()
224	defer th.TeardownHTTP()
225	HandleListUserProjectsSuccessfully(t)
226	allPages, err := users.ListProjects(client.ServiceClient(), "9fe1d3").AllPages()
227	th.AssertNoErr(t, err)
228	actual, err := projects.ExtractProjects(allPages)
229	th.AssertNoErr(t, err)
230	th.CheckDeepEquals(t, ExpectedProjectsSlice, actual)
231}
232
233func TestListInGroup(t *testing.T) {
234	th.SetupHTTP()
235	defer th.TeardownHTTP()
236	HandleListInGroupSuccessfully(t)
237
238	iTrue := true
239	listOpts := users.ListOpts{
240		Enabled: &iTrue,
241	}
242
243	allPages, err := users.ListInGroup(client.ServiceClient(), "ea167b", listOpts).AllPages()
244	th.AssertNoErr(t, err)
245	actual, err := users.ExtractUsers(allPages)
246	th.AssertNoErr(t, err)
247	th.CheckDeepEquals(t, ExpectedUsersSlice, actual)
248}
249