forked from zemirco/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users_test.go
299 lines (223 loc) · 6.75 KB
/
users_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package keycloak
import (
"context"
"net/http"
"strings"
"testing"
)
// create a new user.
func createUser(t *testing.T, k *Keycloak, realm string, username string) string {
t.Helper()
user := &User{
Enabled: Bool(true),
Username: String(username),
Email: String(username + "@email.com"),
FirstName: String("first"),
LastName: String("last"),
}
res, err := k.Users.Create(context.Background(), realm, user)
if err != nil {
t.Errorf("Users.Create returned error: %v", err)
}
parts := strings.Split(res.Header.Get("Location"), "/")
userID := parts[len(parts)-1]
return userID
}
func TestUsersService_Create(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
ctx := context.Background()
user := &User{
Enabled: Bool(true),
Username: String("username"),
Email: String("[email protected]"),
FirstName: String("first"),
LastName: String("last"),
}
res, err := k.Users.Create(ctx, realm, user)
if err != nil {
t.Errorf("Users.Create returned error: %v", err)
}
if res.StatusCode != http.StatusCreated {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusCreated)
}
}
func TestUsersService_Delete(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
userID := createUser(t, k, realm, "john")
res, err := k.Users.Delete(context.Background(), realm, userID)
if err != nil {
t.Errorf("Users.Delete returned error: %v", err)
}
if res.StatusCode != http.StatusNoContent {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusNoContent)
}
}
func TestUsersService_List(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
createUser(t, k, realm, "john")
createUser(t, k, realm, "mark")
users, res, err := k.Users.List(context.Background(), realm)
if err != nil {
t.Errorf("Users.List returned error: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusOK)
}
// it includes the master realm
if len(users) != 2 {
t.Errorf("got: %d, want: %d", len(users), 2)
}
}
func TestUsersService_GetByUsername(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
createUser(t, k, realm, "john")
users, res, err := k.Users.GetByUsername(context.Background(), realm, "john")
if err != nil {
t.Errorf("Users.GetByUsername returned error: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusOK)
}
// it includes the master realm
if len(users) != 1 {
t.Errorf("got: %d, want: %d", len(users), 1)
}
if *users[0].Username != "john" {
t.Errorf("got: %s, want: %s", *users[0].Username, "john")
}
}
func TestUsersService_ResetPassword(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
userID := createUser(t, k, realm, "john")
ctx := context.Background()
credential := &Credential{
Type: String("password"),
Value: String("mypassword"),
Temporary: Bool(false),
}
res, err := k.Users.ResetPassword(ctx, realm, userID, credential)
if err != nil {
t.Errorf("Users.ResetPassword returned error: %v", err)
}
if res.StatusCode != http.StatusNoContent {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusNoContent)
}
}
func TestUsersService_JoinGroup(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
userID := createUser(t, k, realm, "john")
groupID := createGroup(t, k, realm, "group")
ctx := context.Background()
res, err := k.Users.JoinGroup(ctx, realm, userID, groupID)
if err != nil {
t.Errorf("Users.JoinGroup returned error: %v", err)
}
if res.StatusCode != http.StatusNoContent {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusNoContent)
}
}
func TestUsersService_LeaveGroup(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
userID := createUser(t, k, realm, "john")
groupID := createGroup(t, k, realm, "group")
ctx := context.Background()
res, err := k.Users.JoinGroup(ctx, realm, userID, groupID)
if err != nil {
t.Errorf("Users.JoinGroup returned error: %v", err)
}
if res.StatusCode != http.StatusNoContent {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusNoContent)
}
res, err = k.Users.LeaveGroup(ctx, realm, userID, groupID)
if err != nil {
t.Errorf("Users.LeaveGroup returned error: %v", err)
}
if res.StatusCode != http.StatusNoContent {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusNoContent)
}
}
func TestUsersService_AddRealmRoles(t *testing.T) {
k := client(t)
realm := "first"
roleName := "role"
createRealm(t, k, realm)
createRealmRole(t, k, realm, roleName)
userID := createUser(t, k, realm, "user")
ctx := context.Background()
// get role in order to assign it
role, _, err := k.RealmRoles.GetByName(ctx, realm, roleName)
if err != nil {
t.Errorf("RealmRoles.GetByName returned error: %v", err)
}
roles := []*Role{role}
res, err := k.Users.AddRealmRoles(ctx, realm, userID, roles)
if err != nil {
t.Errorf("Users.AddRealmRoles returned error: %v", err)
}
if res.StatusCode != http.StatusNoContent {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusNoContent)
}
}
func TestUsersService_RemoveRealmRoles(t *testing.T) {
k := client(t)
realm := "first"
roleName := "role"
createRealm(t, k, realm)
createRealmRole(t, k, realm, roleName)
userID := createUser(t, k, realm, "user")
ctx := context.Background()
// get role in order to assign it
role, _, err := k.RealmRoles.GetByName(ctx, realm, roleName)
if err != nil {
t.Errorf("RealmRoles.GetByName returned error: %v", err)
}
roles := []*Role{role}
if _, err := k.Users.AddRealmRoles(ctx, realm, userID, roles); err != nil {
t.Errorf("Users.AddRealmRoles returned error: %v", err)
}
res, err := k.Users.RemoveRealmRoles(ctx, realm, userID, roles)
if err != nil {
t.Errorf("Users.RemoveRealmRoles returned error: %v", err)
}
if res.StatusCode != http.StatusNoContent {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusNoContent)
}
}
func TestUsersService_AddClientRoles(t *testing.T) {
k := client(t)
realm := "first"
createRealm(t, k, realm)
clientID := createClient(t, k, realm, "client")
createClientRole(t, k, realm, clientID, "role")
userID := createUser(t, k, realm, "user")
ctx := context.Background()
// get role in order to assign it
role, _, err := k.ClientRoles.Get(ctx, realm, clientID, "role")
if err != nil {
t.Errorf("ClientRoles.Get returned error: %v", err)
}
roles := []*Role{role}
res, err := k.Users.AddClientRoles(ctx, realm, userID, clientID, roles)
if err != nil {
t.Errorf("Users.AddClientRoles returned error: %v", err)
}
if res.StatusCode != http.StatusNoContent {
t.Errorf("got: %d, want: %d", res.StatusCode, http.StatusNoContent)
}
}
func TestUsersService_RemoveClientRoles(t *testing.T) {
}