keycloak-admin 1.1.3 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/Dockerfile +24 -24
  3. data/.github/workflows/ci.yml +80 -83
  4. data/.gitignore +9 -9
  5. data/.rspec +2 -2
  6. data/CHANGELOG.md +194 -188
  7. data/Dockerfile +12 -12
  8. data/Gemfile +3 -3
  9. data/Gemfile.lock +51 -49
  10. data/MIT-LICENSE +20 -20
  11. data/README.md +748 -748
  12. data/bin/console +9 -9
  13. data/keycloak-admin.gemspec +24 -24
  14. data/lib/keycloak-admin/client/attack_detection_client.rb +41 -41
  15. data/lib/keycloak-admin/client/client.rb +56 -56
  16. data/lib/keycloak-admin/client/client_authz_permission_client.rb +80 -80
  17. data/lib/keycloak-admin/client/client_authz_policy_client.rb +75 -75
  18. data/lib/keycloak-admin/client/client_authz_resource_client.rb +92 -92
  19. data/lib/keycloak-admin/client/client_authz_scope_client.rb +70 -70
  20. data/lib/keycloak-admin/client/client_client.rb +71 -71
  21. data/lib/keycloak-admin/client/client_role_client.rb +20 -20
  22. data/lib/keycloak-admin/client/client_role_mappings_client.rb +32 -32
  23. data/lib/keycloak-admin/client/configurable_token_client.rb +35 -35
  24. data/lib/keycloak-admin/client/group_client.rb +148 -131
  25. data/lib/keycloak-admin/client/identity_provider_client.rb +51 -51
  26. data/lib/keycloak-admin/client/realm_client.rb +122 -122
  27. data/lib/keycloak-admin/client/role_client.rb +59 -59
  28. data/lib/keycloak-admin/client/role_mapper_client.rb +47 -45
  29. data/lib/keycloak-admin/client/token_client.rb +29 -29
  30. data/lib/keycloak-admin/client/user_client.rb +266 -266
  31. data/lib/keycloak-admin/configuration.rb +52 -52
  32. data/lib/keycloak-admin/representation/attack_detection_representation.rb +17 -17
  33. data/lib/keycloak-admin/representation/camel_json.rb +12 -12
  34. data/lib/keycloak-admin/representation/client_authz_permission_representation.rb +33 -33
  35. data/lib/keycloak-admin/representation/client_authz_policy_config_representation.rb +14 -14
  36. data/lib/keycloak-admin/representation/client_authz_policy_representation.rb +26 -26
  37. data/lib/keycloak-admin/representation/client_authz_resource_representation.rb +25 -25
  38. data/lib/keycloak-admin/representation/client_authz_scope_representation.rb +16 -16
  39. data/lib/keycloak-admin/representation/client_representation.rb +71 -71
  40. data/lib/keycloak-admin/representation/credential_representation.rb +38 -38
  41. data/lib/keycloak-admin/representation/federated_identity_representation.rb +15 -15
  42. data/lib/keycloak-admin/representation/group_representation.rb +21 -21
  43. data/lib/keycloak-admin/representation/identity_provider_mapper_representation.rb +19 -19
  44. data/lib/keycloak-admin/representation/identity_provider_representation.rb +67 -67
  45. data/lib/keycloak-admin/representation/impersonation_redirection_representation.rb +16 -16
  46. data/lib/keycloak-admin/representation/impersonation_representation.rb +43 -43
  47. data/lib/keycloak-admin/representation/protocol_mapper_representation.rb +19 -19
  48. data/lib/keycloak-admin/representation/realm_representation.rb +14 -14
  49. data/lib/keycloak-admin/representation/representation.rb +23 -23
  50. data/lib/keycloak-admin/representation/role_representation.rb +19 -19
  51. data/lib/keycloak-admin/representation/session_representation.rb +22 -22
  52. data/lib/keycloak-admin/representation/token_representation.rb +39 -39
  53. data/lib/keycloak-admin/representation/user_representation.rb +47 -47
  54. data/lib/keycloak-admin/resource/base_role_containing_resource.rb +28 -28
  55. data/lib/keycloak-admin/resource/group_resource.rb +11 -11
  56. data/lib/keycloak-admin/resource/user_resource.rb +7 -7
  57. data/lib/keycloak-admin/version.rb +3 -3
  58. data/lib/keycloak-admin.rb +84 -84
  59. data/spec/client/attack_detection_client_spec.rb +102 -102
  60. data/spec/client/client_authz_permission_client_spec.rb +170 -170
  61. data/spec/client/client_authz_policy_client_spec.rb +169 -169
  62. data/spec/client/client_authz_resource_client_spec.rb +150 -150
  63. data/spec/client/client_authz_scope_client_spec.rb +133 -133
  64. data/spec/client/client_client_spec.rb +133 -133
  65. data/spec/client/client_role_mappings_client_spec.rb +82 -82
  66. data/spec/client/client_spec.rb +28 -28
  67. data/spec/client/configurable_token_client_spec.rb +34 -34
  68. data/spec/client/group_client_spec.rb +328 -258
  69. data/spec/client/identity_provider_client_spec.rb +92 -92
  70. data/spec/client/realm_client_spec.rb +155 -155
  71. data/spec/client/role_client_spec.rb +79 -79
  72. data/spec/client/role_mapper_client_spec.rb +113 -68
  73. data/spec/client/token_client_spec.rb +68 -68
  74. data/spec/client/user_client_spec.rb +373 -373
  75. data/spec/configuration_spec.rb +113 -113
  76. data/spec/integration/client_authorization_spec.rb +93 -95
  77. data/spec/representation/attack_detection_representation_spec.rb +15 -15
  78. data/spec/representation/client_authz_permission_representation_spec.rb +52 -52
  79. data/spec/representation/client_authz_policy_representation_spec.rb +46 -46
  80. data/spec/representation/client_authz_resource_representation_spec.rb +33 -33
  81. data/spec/representation/client_authz_scope_representation_spec.rb +18 -18
  82. data/spec/representation/client_representation_spec.rb +119 -119
  83. data/spec/representation/group_representation_spec.rb +22 -22
  84. data/spec/representation/identity_provider_mapper_representation_spec.rb +24 -24
  85. data/spec/representation/identity_provider_representation_spec.rb +113 -113
  86. data/spec/representation/impersonation_representation_spec.rb +163 -163
  87. data/spec/representation/protocol_mapper_representation_spec.rb +57 -57
  88. data/spec/representation/role_representation_spec.rb +37 -37
  89. data/spec/representation/session_representation_spec.rb +15 -15
  90. data/spec/representation/user_representation_spec.rb +15 -15
  91. data/spec/resource/group_resource_spec.rb +14 -14
  92. data/spec/resource/user_resource_spec.rb +14 -14
  93. data/spec/spec_helper.rb +37 -37
  94. metadata +10 -10
@@ -1,373 +1,373 @@
1
- RSpec.describe KeycloakAdmin::TokenClient do
2
- describe "#initialize" do
3
- let(:realm_name) { nil }
4
- before(:each) do
5
- @realm = KeycloakAdmin.realm(realm_name)
6
- end
7
-
8
- context "when realm_name is defined" do
9
- let(:realm_name) { "master" }
10
- it "does not raise any error" do
11
- expect {
12
- @realm.users
13
- }.to_not raise_error
14
- end
15
- end
16
-
17
- context "when realm_name is not defined" do
18
- let(:realm_name) { nil }
19
- it "raises any error" do
20
- expect {
21
- @realm.users
22
- }.to raise_error(ArgumentError)
23
- end
24
- end
25
- end
26
-
27
- describe "#users_url" do
28
- let(:realm_name) { "valid-realm" }
29
- let(:user_id) { nil }
30
-
31
- before(:each) do
32
- @built_url = KeycloakAdmin.realm(realm_name).users.users_url(user_id)
33
- end
34
-
35
- context "when user_id is not defined" do
36
- let(:user_id) { nil }
37
- it "return a proper url without user id" do
38
- expect(@built_url).to eq "http://auth.service.io/auth/admin/realms/valid-realm/users"
39
- end
40
- end
41
-
42
- context "when user_id is defined" do
43
- let(:user_id) { "95985b21-d884-4bbd-b852-cb8cd365afc2" }
44
- it "return a proper url with the user id" do
45
- expect(@built_url).to eq "http://auth.service.io/auth/admin/realms/valid-realm/users/95985b21-d884-4bbd-b852-cb8cd365afc2"
46
- end
47
- end
48
- end
49
-
50
- describe "#reset_password_url" do
51
- let(:realm_name) { "valid-realm" }
52
- let(:user_id) { nil }
53
-
54
- before(:each) do
55
- @client = KeycloakAdmin.realm(realm_name).users
56
- end
57
-
58
- context "when user_id is not defined" do
59
- let(:user_id) { nil }
60
- it "raises an error" do
61
- expect {
62
- @client.reset_password_url(user_id)
63
- }.to raise_error(ArgumentError)
64
- end
65
- end
66
-
67
- context "when user_id is defined" do
68
- let(:user_id) { 42 }
69
- it "return a proper url" do
70
- expect(@client.reset_password_url(user_id)).to eq "http://auth.service.io/auth/admin/realms/valid-realm/users/42/reset-password"
71
- end
72
- end
73
- end
74
-
75
- describe "#execute_actions_email_url" do
76
- let(:realm_name) { "valid-realm" }
77
- let(:user_id) { nil }
78
-
79
- before(:each) do
80
- @client = KeycloakAdmin.realm(realm_name).users
81
- end
82
-
83
- context "when user_id is not defined" do
84
- let(:user_id) { nil }
85
- it "raises an error" do
86
- expect {
87
- @client.execute_actions_email_url(user_id)
88
- }.to raise_error(ArgumentError)
89
- end
90
- end
91
-
92
- context "when user_id is defined" do
93
- let(:user_id) { 42 }
94
- it "return a proper url" do
95
- expect(@client.execute_actions_email_url(user_id)).to eq "http://auth.service.io/auth/admin/realms/valid-realm/users/42/execute-actions-email"
96
- end
97
- end
98
- end
99
-
100
- describe "#impersonation_url" do
101
- let(:realm_name) { "valid-realm" }
102
- let(:user_id) { nil }
103
-
104
- before(:each) do
105
- @client = KeycloakAdmin.realm(realm_name).users
106
- end
107
-
108
- context "when user_id is not defined" do
109
- let(:user_id) { nil }
110
- it "raises an error" do
111
- expect {
112
- @client.impersonation_url(user_id)
113
- }.to raise_error(ArgumentError)
114
- end
115
- end
116
-
117
- context "when user_id is defined" do
118
- let(:user_id) { 42 }
119
- it "return a proper url" do
120
- expect(@client.impersonation_url(user_id)).to eq "http://auth.service.io/auth/admin/realms/valid-realm/users/42/impersonation"
121
- end
122
- end
123
- end
124
-
125
- describe "#save" do
126
- let(:realm_name) { "valid-realm" }
127
- let(:user) { KeycloakAdmin::UserRepresentation.from_hash(
128
- "username" => "test_username",
129
- "createdTimestamp" => Time.now.to_i,
130
- )}
131
-
132
- before(:each) do
133
- @user_client = KeycloakAdmin.realm(realm_name).users
134
-
135
- stub_token_client
136
- allow_any_instance_of(RestClient::Resource).to receive(:post)
137
- end
138
-
139
- it "saves a user" do
140
- expect(@user_client.save(user)).to eq user
141
- end
142
-
143
- it "passes rest client options" do
144
- rest_client_options = {timeout: 10}
145
- allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
146
-
147
- expect(RestClient::Resource).to receive(:new).with(
148
- "http://auth.service.io/auth/admin/realms/valid-realm/users", rest_client_options).and_call_original
149
-
150
- expect(@user_client.save(user)).to eq user
151
- end
152
- end
153
-
154
- describe "#get" do
155
- let(:realm_name) { "valid-realm" }
156
-
157
- before(:each) do
158
- @user_client = KeycloakAdmin.realm(realm_name).users
159
-
160
- stub_token_client
161
- allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '{"username":"test_username","createdTimestamp":1559347200, "requiredActions":["CONFIGURE_TOTP"], "totp": true}'
162
- end
163
-
164
- it "parses the response" do
165
- user = @user_client.get('test_user_id')
166
- expect(user.username).to eq 'test_username'
167
- end
168
-
169
- it "passes rest client options" do
170
- rest_client_options = {timeout: 10}
171
- allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
172
-
173
- expect(RestClient::Resource).to receive(:new).with(
174
- "http://auth.service.io/auth/admin/realms/valid-realm/users/test_user_id", rest_client_options).and_call_original
175
-
176
- user = @user_client.get('test_user_id')
177
- expect(user.username).to eq 'test_username'
178
- expect(user.totp).to be true
179
- expect(user.required_actions).to eq ["CONFIGURE_TOTP"]
180
- end
181
- end
182
-
183
- describe "#search" do
184
- let(:realm_name) { "valid-realm" }
185
- let(:user) { KeycloakAdmin::UserRepresentation.from_hash(
186
- "username" => "test_username",
187
- "createdTimestamp" => Time.now.to_i,
188
- )}
189
-
190
- before(:each) do
191
- @user_client = KeycloakAdmin.realm(realm_name).users
192
-
193
- stub_token_client
194
- allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"username":"test_username","createdTimestamp":1559347200}]'
195
- end
196
-
197
- it "finds a user using a string" do
198
- users = @user_client.search("test_username")
199
- expect(users.length).to eq 1
200
- expect(users[0].username).to eq "test_username"
201
- end
202
-
203
- it "finds a user using nil does not fail" do
204
- users = @user_client.search(nil)
205
- expect(users.length).to eq 1
206
- expect(users[0].username).to eq "test_username"
207
- end
208
-
209
- it "finds a user using a hash" do
210
- users = @user_client.search({ search: "test_username"})
211
- expect(users.length).to eq 1
212
- expect(users[0].username).to eq "test_username"
213
- end
214
-
215
- it "passes rest client options" do
216
- rest_client_options = {timeout: 10}
217
- allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
218
-
219
- expect(RestClient::Resource).to receive(:new).with(
220
- "http://auth.service.io/auth/admin/realms/valid-realm/users", rest_client_options).and_call_original
221
-
222
- users = @user_client.search("test_username")
223
- expect(users.length).to eq 1
224
- expect(users[0].username).to eq "test_username"
225
- end
226
- end
227
-
228
- describe "#list" do
229
- let(:realm_name) { "valid-realm" }
230
- let(:user) { KeycloakAdmin::UserRepresentation.from_hash(
231
- "username" => "test_username",
232
- "createdTimestamp" => Time.now.to_i,
233
- )}
234
-
235
- before(:each) do
236
- @user_client = KeycloakAdmin.realm(realm_name).users
237
-
238
- stub_token_client
239
- allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"username":"test_username","createdTimestamp":1559347200}]'
240
- end
241
-
242
- it "lists users" do
243
- users = @user_client.list
244
- expect(users.length).to eq 1
245
- expect(users[0].username).to eq "test_username"
246
- end
247
-
248
- it "passes rest client options" do
249
- rest_client_options = {timeout: 10}
250
- allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
251
-
252
- expect(RestClient::Resource).to receive(:new).with(
253
- "http://auth.service.io/auth/admin/realms/valid-realm/users", rest_client_options).and_call_original
254
-
255
- users = @user_client.list
256
- expect(users.length).to eq 1
257
- expect(users[0].username).to eq "test_username"
258
- end
259
- end
260
-
261
- describe "#delete" do
262
- let(:realm_name) { "valid-realm" }
263
-
264
- before(:each) do
265
- @user_client = KeycloakAdmin.realm(realm_name).users
266
-
267
- stub_token_client
268
- allow_any_instance_of(RestClient::Resource).to receive(:delete)
269
- end
270
-
271
- it "does not fail" do
272
- expect(@user_client.delete('test_user_id')).to be_truthy
273
- end
274
-
275
- it "passes rest client options" do
276
- rest_client_options = {timeout: 10}
277
- allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
278
-
279
- expect(RestClient::Resource).to receive(:new).with(
280
- "http://auth.service.io/auth/admin/realms/valid-realm/users/test_user_id", rest_client_options).and_call_original
281
-
282
- @user_client.delete('test_user_id')
283
- end
284
- end
285
-
286
- describe '#update' do
287
- let(:realm_name) { 'valid-realm' }
288
- before(:each) do
289
- @user_client = KeycloakAdmin.realm(realm_name).users
290
-
291
- stub_token_client
292
- allow_any_instance_of(RestClient::Request).to receive(:execute).and_return "write a better test"
293
- end
294
-
295
- context 'when user_id is defined' do
296
- let(:user_id) { '95985b21-d884-4bbd-b852-cb8cd365afc2' }
297
-
298
- it 'updates the user details' do
299
- ## TODO use this expected payload to check whether it has been sent or not
300
- expected_payload = {
301
- method: :put,
302
- url: "http://auth.service.io/auth/admin/realms/valid-realm/users/95985b21-d884-4bbd-b852-cb8cd365afc2",
303
- payload: '{"firstName":"Test","enabled":false}',
304
- headers: {
305
- Authorization: "Bearer test_access_token",
306
- content_type: :json,
307
- accept: :json
308
- }
309
- }
310
- response = @user_client.update(user_id, { firstName: 'Test', enabled: false })
311
- expect(response).to eq "write a better test"
312
- end
313
- end
314
-
315
- context 'when user_id is not defined' do
316
- let(:user_id) { '95985b21-d884-4bbd-b852-cb8cd365afc2' }
317
-
318
- let(:user_id) { nil }
319
- it 'raise argument error' do
320
- expect { @user_client.update(user_id, { firstName: 'Test', enabled: false }) }.to raise_error(ArgumentError)
321
- end
322
- end
323
- end
324
-
325
- describe '#sessions' do
326
- let(:realm_name) { "valid-realm" }
327
-
328
- before(:each) do
329
- @user_client = KeycloakAdmin.realm(realm_name).users
330
- stub_token_client
331
- allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"id":"95985b21-d884-4bbd-b852-dsfsdfsd","username":"test_username", "ip_address":"0.0.0.0"}]'
332
- end
333
-
334
- context 'when user_id is defined' do
335
- let(:user_id) { '95985b21-d884-4bbd-b852-cb8cd365afc2' }
336
- it 'returns list of active sessions' do
337
- response = @user_client.sessions(user_id)
338
- expect(response[0].id).to eq '95985b21-d884-4bbd-b852-dsfsdfsd'
339
- end
340
- end
341
-
342
- context 'when user_id is not defined' do
343
- let(:user_id) { nil }
344
- it 'raise argument error' do
345
- expect { @user_client.sessions(user_id) }.to raise_error(ArgumentError)
346
- end
347
- end
348
- end
349
-
350
- describe '#logout' do
351
- let(:realm_name) { 'valid-realm' }
352
-
353
- before(:each) do
354
- @user_client = KeycloakAdmin.realm(realm_name).users
355
- stub_token_client
356
- allow_any_instance_of(RestClient::Request).to receive(:execute)
357
- end
358
-
359
- context 'when user_id is defined' do
360
- let(:user_id) { '95985b21-d884-4bbd-b852-cb8cd365afc2' }
361
- it 'logout user and return true' do
362
- expect(@user_client.logout(user_id)).to be_truthy
363
- end
364
- end
365
-
366
- context 'when user_id is not defined' do
367
- let(:user_id) { nil }
368
- it 'raise argument error' do
369
- expect { @user_client.logout(user_id) }.to raise_error(ArgumentError)
370
- end
371
- end
372
- end
373
- end
1
+ RSpec.describe KeycloakAdmin::TokenClient do
2
+ describe "#initialize" do
3
+ let(:realm_name) { nil }
4
+ before(:each) do
5
+ @realm = KeycloakAdmin.realm(realm_name)
6
+ end
7
+
8
+ context "when realm_name is defined" do
9
+ let(:realm_name) { "master" }
10
+ it "does not raise any error" do
11
+ expect {
12
+ @realm.users
13
+ }.to_not raise_error
14
+ end
15
+ end
16
+
17
+ context "when realm_name is not defined" do
18
+ let(:realm_name) { nil }
19
+ it "raises any error" do
20
+ expect {
21
+ @realm.users
22
+ }.to raise_error(ArgumentError)
23
+ end
24
+ end
25
+ end
26
+
27
+ describe "#users_url" do
28
+ let(:realm_name) { "valid-realm" }
29
+ let(:user_id) { nil }
30
+
31
+ before(:each) do
32
+ @built_url = KeycloakAdmin.realm(realm_name).users.users_url(user_id)
33
+ end
34
+
35
+ context "when user_id is not defined" do
36
+ let(:user_id) { nil }
37
+ it "return a proper url without user id" do
38
+ expect(@built_url).to eq "http://auth.service.io/auth/admin/realms/valid-realm/users"
39
+ end
40
+ end
41
+
42
+ context "when user_id is defined" do
43
+ let(:user_id) { "95985b21-d884-4bbd-b852-cb8cd365afc2" }
44
+ it "return a proper url with the user id" do
45
+ expect(@built_url).to eq "http://auth.service.io/auth/admin/realms/valid-realm/users/95985b21-d884-4bbd-b852-cb8cd365afc2"
46
+ end
47
+ end
48
+ end
49
+
50
+ describe "#reset_password_url" do
51
+ let(:realm_name) { "valid-realm" }
52
+ let(:user_id) { nil }
53
+
54
+ before(:each) do
55
+ @client = KeycloakAdmin.realm(realm_name).users
56
+ end
57
+
58
+ context "when user_id is not defined" do
59
+ let(:user_id) { nil }
60
+ it "raises an error" do
61
+ expect {
62
+ @client.reset_password_url(user_id)
63
+ }.to raise_error(ArgumentError)
64
+ end
65
+ end
66
+
67
+ context "when user_id is defined" do
68
+ let(:user_id) { 42 }
69
+ it "return a proper url" do
70
+ expect(@client.reset_password_url(user_id)).to eq "http://auth.service.io/auth/admin/realms/valid-realm/users/42/reset-password"
71
+ end
72
+ end
73
+ end
74
+
75
+ describe "#execute_actions_email_url" do
76
+ let(:realm_name) { "valid-realm" }
77
+ let(:user_id) { nil }
78
+
79
+ before(:each) do
80
+ @client = KeycloakAdmin.realm(realm_name).users
81
+ end
82
+
83
+ context "when user_id is not defined" do
84
+ let(:user_id) { nil }
85
+ it "raises an error" do
86
+ expect {
87
+ @client.execute_actions_email_url(user_id)
88
+ }.to raise_error(ArgumentError)
89
+ end
90
+ end
91
+
92
+ context "when user_id is defined" do
93
+ let(:user_id) { 42 }
94
+ it "return a proper url" do
95
+ expect(@client.execute_actions_email_url(user_id)).to eq "http://auth.service.io/auth/admin/realms/valid-realm/users/42/execute-actions-email"
96
+ end
97
+ end
98
+ end
99
+
100
+ describe "#impersonation_url" do
101
+ let(:realm_name) { "valid-realm" }
102
+ let(:user_id) { nil }
103
+
104
+ before(:each) do
105
+ @client = KeycloakAdmin.realm(realm_name).users
106
+ end
107
+
108
+ context "when user_id is not defined" do
109
+ let(:user_id) { nil }
110
+ it "raises an error" do
111
+ expect {
112
+ @client.impersonation_url(user_id)
113
+ }.to raise_error(ArgumentError)
114
+ end
115
+ end
116
+
117
+ context "when user_id is defined" do
118
+ let(:user_id) { 42 }
119
+ it "return a proper url" do
120
+ expect(@client.impersonation_url(user_id)).to eq "http://auth.service.io/auth/admin/realms/valid-realm/users/42/impersonation"
121
+ end
122
+ end
123
+ end
124
+
125
+ describe "#save" do
126
+ let(:realm_name) { "valid-realm" }
127
+ let(:user) { KeycloakAdmin::UserRepresentation.from_hash(
128
+ "username" => "test_username",
129
+ "createdTimestamp" => Time.now.to_i,
130
+ )}
131
+
132
+ before(:each) do
133
+ @user_client = KeycloakAdmin.realm(realm_name).users
134
+
135
+ stub_token_client
136
+ allow_any_instance_of(RestClient::Resource).to receive(:post)
137
+ end
138
+
139
+ it "saves a user" do
140
+ expect(@user_client.save(user)).to eq user
141
+ end
142
+
143
+ it "passes rest client options" do
144
+ rest_client_options = {timeout: 10}
145
+ allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
146
+
147
+ expect(RestClient::Resource).to receive(:new).with(
148
+ "http://auth.service.io/auth/admin/realms/valid-realm/users", rest_client_options).and_call_original
149
+
150
+ expect(@user_client.save(user)).to eq user
151
+ end
152
+ end
153
+
154
+ describe "#get" do
155
+ let(:realm_name) { "valid-realm" }
156
+
157
+ before(:each) do
158
+ @user_client = KeycloakAdmin.realm(realm_name).users
159
+
160
+ stub_token_client
161
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '{"username":"test_username","createdTimestamp":1559347200, "requiredActions":["CONFIGURE_TOTP"], "totp": true}'
162
+ end
163
+
164
+ it "parses the response" do
165
+ user = @user_client.get('test_user_id')
166
+ expect(user.username).to eq 'test_username'
167
+ end
168
+
169
+ it "passes rest client options" do
170
+ rest_client_options = {timeout: 10}
171
+ allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
172
+
173
+ expect(RestClient::Resource).to receive(:new).with(
174
+ "http://auth.service.io/auth/admin/realms/valid-realm/users/test_user_id", rest_client_options).and_call_original
175
+
176
+ user = @user_client.get('test_user_id')
177
+ expect(user.username).to eq 'test_username'
178
+ expect(user.totp).to be true
179
+ expect(user.required_actions).to eq ["CONFIGURE_TOTP"]
180
+ end
181
+ end
182
+
183
+ describe "#search" do
184
+ let(:realm_name) { "valid-realm" }
185
+ let(:user) { KeycloakAdmin::UserRepresentation.from_hash(
186
+ "username" => "test_username",
187
+ "createdTimestamp" => Time.now.to_i,
188
+ )}
189
+
190
+ before(:each) do
191
+ @user_client = KeycloakAdmin.realm(realm_name).users
192
+
193
+ stub_token_client
194
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"username":"test_username","createdTimestamp":1559347200}]'
195
+ end
196
+
197
+ it "finds a user using a string" do
198
+ users = @user_client.search("test_username")
199
+ expect(users.length).to eq 1
200
+ expect(users[0].username).to eq "test_username"
201
+ end
202
+
203
+ it "finds a user using nil does not fail" do
204
+ users = @user_client.search(nil)
205
+ expect(users.length).to eq 1
206
+ expect(users[0].username).to eq "test_username"
207
+ end
208
+
209
+ it "finds a user using a hash" do
210
+ users = @user_client.search({ search: "test_username"})
211
+ expect(users.length).to eq 1
212
+ expect(users[0].username).to eq "test_username"
213
+ end
214
+
215
+ it "passes rest client options" do
216
+ rest_client_options = {timeout: 10}
217
+ allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
218
+
219
+ expect(RestClient::Resource).to receive(:new).with(
220
+ "http://auth.service.io/auth/admin/realms/valid-realm/users", rest_client_options).and_call_original
221
+
222
+ users = @user_client.search("test_username")
223
+ expect(users.length).to eq 1
224
+ expect(users[0].username).to eq "test_username"
225
+ end
226
+ end
227
+
228
+ describe "#list" do
229
+ let(:realm_name) { "valid-realm" }
230
+ let(:user) { KeycloakAdmin::UserRepresentation.from_hash(
231
+ "username" => "test_username",
232
+ "createdTimestamp" => Time.now.to_i,
233
+ )}
234
+
235
+ before(:each) do
236
+ @user_client = KeycloakAdmin.realm(realm_name).users
237
+
238
+ stub_token_client
239
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"username":"test_username","createdTimestamp":1559347200}]'
240
+ end
241
+
242
+ it "lists users" do
243
+ users = @user_client.list
244
+ expect(users.length).to eq 1
245
+ expect(users[0].username).to eq "test_username"
246
+ end
247
+
248
+ it "passes rest client options" do
249
+ rest_client_options = {timeout: 10}
250
+ allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
251
+
252
+ expect(RestClient::Resource).to receive(:new).with(
253
+ "http://auth.service.io/auth/admin/realms/valid-realm/users", rest_client_options).and_call_original
254
+
255
+ users = @user_client.list
256
+ expect(users.length).to eq 1
257
+ expect(users[0].username).to eq "test_username"
258
+ end
259
+ end
260
+
261
+ describe "#delete" do
262
+ let(:realm_name) { "valid-realm" }
263
+
264
+ before(:each) do
265
+ @user_client = KeycloakAdmin.realm(realm_name).users
266
+
267
+ stub_token_client
268
+ allow_any_instance_of(RestClient::Resource).to receive(:delete)
269
+ end
270
+
271
+ it "does not fail" do
272
+ expect(@user_client.delete('test_user_id')).to be_truthy
273
+ end
274
+
275
+ it "passes rest client options" do
276
+ rest_client_options = {timeout: 10}
277
+ allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
278
+
279
+ expect(RestClient::Resource).to receive(:new).with(
280
+ "http://auth.service.io/auth/admin/realms/valid-realm/users/test_user_id", rest_client_options).and_call_original
281
+
282
+ @user_client.delete('test_user_id')
283
+ end
284
+ end
285
+
286
+ describe '#update' do
287
+ let(:realm_name) { 'valid-realm' }
288
+ before(:each) do
289
+ @user_client = KeycloakAdmin.realm(realm_name).users
290
+
291
+ stub_token_client
292
+ allow_any_instance_of(RestClient::Request).to receive(:execute).and_return "write a better test"
293
+ end
294
+
295
+ context 'when user_id is defined' do
296
+ let(:user_id) { '95985b21-d884-4bbd-b852-cb8cd365afc2' }
297
+
298
+ it 'updates the user details' do
299
+ ## TODO use this expected payload to check whether it has been sent or not
300
+ expected_payload = {
301
+ method: :put,
302
+ url: "http://auth.service.io/auth/admin/realms/valid-realm/users/95985b21-d884-4bbd-b852-cb8cd365afc2",
303
+ payload: '{"firstName":"Test","enabled":false}',
304
+ headers: {
305
+ Authorization: "Bearer test_access_token",
306
+ content_type: :json,
307
+ accept: :json
308
+ }
309
+ }
310
+ response = @user_client.update(user_id, { firstName: 'Test', enabled: false })
311
+ expect(response).to eq "write a better test"
312
+ end
313
+ end
314
+
315
+ context 'when user_id is not defined' do
316
+ let(:user_id) { '95985b21-d884-4bbd-b852-cb8cd365afc2' }
317
+
318
+ let(:user_id) { nil }
319
+ it 'raise argument error' do
320
+ expect { @user_client.update(user_id, { firstName: 'Test', enabled: false }) }.to raise_error(ArgumentError)
321
+ end
322
+ end
323
+ end
324
+
325
+ describe '#sessions' do
326
+ let(:realm_name) { "valid-realm" }
327
+
328
+ before(:each) do
329
+ @user_client = KeycloakAdmin.realm(realm_name).users
330
+ stub_token_client
331
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"id":"95985b21-d884-4bbd-b852-dsfsdfsd","username":"test_username", "ip_address":"0.0.0.0"}]'
332
+ end
333
+
334
+ context 'when user_id is defined' do
335
+ let(:user_id) { '95985b21-d884-4bbd-b852-cb8cd365afc2' }
336
+ it 'returns list of active sessions' do
337
+ response = @user_client.sessions(user_id)
338
+ expect(response[0].id).to eq '95985b21-d884-4bbd-b852-dsfsdfsd'
339
+ end
340
+ end
341
+
342
+ context 'when user_id is not defined' do
343
+ let(:user_id) { nil }
344
+ it 'raise argument error' do
345
+ expect { @user_client.sessions(user_id) }.to raise_error(ArgumentError)
346
+ end
347
+ end
348
+ end
349
+
350
+ describe '#logout' do
351
+ let(:realm_name) { 'valid-realm' }
352
+
353
+ before(:each) do
354
+ @user_client = KeycloakAdmin.realm(realm_name).users
355
+ stub_token_client
356
+ allow_any_instance_of(RestClient::Request).to receive(:execute)
357
+ end
358
+
359
+ context 'when user_id is defined' do
360
+ let(:user_id) { '95985b21-d884-4bbd-b852-cb8cd365afc2' }
361
+ it 'logout user and return true' do
362
+ expect(@user_client.logout(user_id)).to be_truthy
363
+ end
364
+ end
365
+
366
+ context 'when user_id is not defined' do
367
+ let(:user_id) { nil }
368
+ it 'raise argument error' do
369
+ expect { @user_client.logout(user_id) }.to raise_error(ArgumentError)
370
+ end
371
+ end
372
+ end
373
+ end