keycloak-admin 1.1.4 → 1.1.6

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 (101) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/Dockerfile +24 -24
  3. data/.github/workflows/ci.yml +80 -80
  4. data/.gitignore +9 -9
  5. data/.rspec +2 -2
  6. data/CHANGELOG.md +23 -0
  7. data/Dockerfile +12 -12
  8. data/Gemfile +3 -3
  9. data/Gemfile.lock +1 -1
  10. data/MIT-LICENSE +20 -20
  11. data/README.md +20 -1
  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 -148
  25. data/lib/keycloak-admin/client/identity_provider_client.rb +51 -51
  26. data/lib/keycloak-admin/client/organization_client.rb +245 -0
  27. data/lib/keycloak-admin/client/realm_client.rb +126 -122
  28. data/lib/keycloak-admin/client/role_client.rb +59 -59
  29. data/lib/keycloak-admin/client/role_mapper_client.rb +47 -47
  30. data/lib/keycloak-admin/client/token_client.rb +29 -29
  31. data/lib/keycloak-admin/client/user_client.rb +278 -266
  32. data/lib/keycloak-admin/configuration.rb +52 -52
  33. data/lib/keycloak-admin/representation/attack_detection_representation.rb +17 -17
  34. data/lib/keycloak-admin/representation/camel_json.rb +12 -12
  35. data/lib/keycloak-admin/representation/client_authz_permission_representation.rb +33 -33
  36. data/lib/keycloak-admin/representation/client_authz_policy_config_representation.rb +14 -14
  37. data/lib/keycloak-admin/representation/client_authz_policy_representation.rb +26 -26
  38. data/lib/keycloak-admin/representation/client_authz_resource_representation.rb +25 -25
  39. data/lib/keycloak-admin/representation/client_authz_scope_representation.rb +16 -16
  40. data/lib/keycloak-admin/representation/client_representation.rb +71 -71
  41. data/lib/keycloak-admin/representation/credential_representation.rb +71 -38
  42. data/lib/keycloak-admin/representation/federated_identity_representation.rb +15 -15
  43. data/lib/keycloak-admin/representation/group_representation.rb +21 -21
  44. data/lib/keycloak-admin/representation/identity_provider_mapper_representation.rb +19 -19
  45. data/lib/keycloak-admin/representation/identity_provider_representation.rb +71 -67
  46. data/lib/keycloak-admin/representation/impersonation_redirection_representation.rb +16 -16
  47. data/lib/keycloak-admin/representation/impersonation_representation.rb +43 -43
  48. data/lib/keycloak-admin/representation/member_representation.rb +11 -0
  49. data/lib/keycloak-admin/representation/organization_domain_representation.rb +18 -0
  50. data/lib/keycloak-admin/representation/organization_representation.rb +30 -0
  51. data/lib/keycloak-admin/representation/protocol_mapper_representation.rb +19 -19
  52. data/lib/keycloak-admin/representation/realm_representation.rb +14 -14
  53. data/lib/keycloak-admin/representation/representation.rb +23 -23
  54. data/lib/keycloak-admin/representation/role_representation.rb +19 -19
  55. data/lib/keycloak-admin/representation/session_representation.rb +22 -22
  56. data/lib/keycloak-admin/representation/token_representation.rb +39 -39
  57. data/lib/keycloak-admin/representation/user_representation.rb +47 -47
  58. data/lib/keycloak-admin/resource/base_role_containing_resource.rb +28 -28
  59. data/lib/keycloak-admin/resource/group_resource.rb +11 -11
  60. data/lib/keycloak-admin/resource/user_resource.rb +7 -7
  61. data/lib/keycloak-admin/version.rb +3 -3
  62. data/lib/keycloak-admin.rb +88 -84
  63. data/spec/client/attack_detection_client_spec.rb +102 -102
  64. data/spec/client/client_authz_permission_client_spec.rb +170 -170
  65. data/spec/client/client_authz_policy_client_spec.rb +169 -169
  66. data/spec/client/client_authz_resource_client_spec.rb +150 -150
  67. data/spec/client/client_authz_scope_client_spec.rb +133 -133
  68. data/spec/client/client_client_spec.rb +133 -133
  69. data/spec/client/client_role_mappings_client_spec.rb +82 -82
  70. data/spec/client/client_spec.rb +28 -28
  71. data/spec/client/configurable_token_client_spec.rb +34 -34
  72. data/spec/client/group_client_spec.rb +328 -328
  73. data/spec/client/identity_provider_client_spec.rb +92 -92
  74. data/spec/client/organization_client_spec.rb +595 -0
  75. data/spec/client/realm_client_spec.rb +155 -155
  76. data/spec/client/role_client_spec.rb +79 -79
  77. data/spec/client/role_mapper_client_spec.rb +113 -113
  78. data/spec/client/token_client_spec.rb +68 -68
  79. data/spec/client/user_client_spec.rb +418 -373
  80. data/spec/configuration_spec.rb +113 -113
  81. data/spec/integration/client_authorization_spec.rb +93 -93
  82. data/spec/representation/attack_detection_representation_spec.rb +15 -15
  83. data/spec/representation/client_authz_permission_representation_spec.rb +52 -52
  84. data/spec/representation/client_authz_policy_representation_spec.rb +46 -46
  85. data/spec/representation/client_authz_resource_representation_spec.rb +33 -33
  86. data/spec/representation/client_authz_scope_representation_spec.rb +18 -18
  87. data/spec/representation/client_representation_spec.rb +119 -119
  88. data/spec/representation/credential_representation_spec.rb +68 -0
  89. data/spec/representation/group_representation_spec.rb +22 -22
  90. data/spec/representation/identity_provider_mapper_representation_spec.rb +24 -24
  91. data/spec/representation/identity_provider_representation_spec.rb +113 -113
  92. data/spec/representation/impersonation_representation_spec.rb +163 -163
  93. data/spec/representation/organization_representation_spec.rb +64 -0
  94. data/spec/representation/protocol_mapper_representation_spec.rb +57 -57
  95. data/spec/representation/role_representation_spec.rb +37 -37
  96. data/spec/representation/session_representation_spec.rb +15 -15
  97. data/spec/representation/user_representation_spec.rb +15 -15
  98. data/spec/resource/group_resource_spec.rb +14 -14
  99. data/spec/resource/user_resource_spec.rb +14 -14
  100. data/spec/spec_helper.rb +37 -37
  101. metadata +13 -6
@@ -1,328 +1,328 @@
1
- RSpec.describe KeycloakAdmin::GroupClient do
2
- describe "#groups_url" do
3
- let(:realm_name) { "valid-realm" }
4
- let(:group_id) { nil }
5
-
6
- before(:each) do
7
- @built_url = KeycloakAdmin.realm(realm_name).groups.groups_url(group_id)
8
- end
9
-
10
- context "when group_id is not defined" do
11
- let(:group_id) { nil }
12
- it "return a proper url without group id" do
13
- expect(@built_url).to eq "http://auth.service.io/auth/admin/realms/valid-realm/groups"
14
- end
15
- end
16
-
17
- context "when group_id is defined" do
18
- let(:group_id) { "95985b21-d884-4bbd-b852-cb8cd365afc2" }
19
- it "return a proper url with the group id" do
20
- expect(@built_url).to eq "http://auth.service.io/auth/admin/realms/valid-realm/groups/95985b21-d884-4bbd-b852-cb8cd365afc2"
21
- end
22
- end
23
- end
24
-
25
- describe "#get" do
26
- let(:realm_name) { "valid-realm" }
27
-
28
- before(:each) do
29
- @group_client = KeycloakAdmin.realm(realm_name).groups
30
-
31
- stub_token_client
32
- allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '{"id":"test_group_id","name":"test_group_name"}'
33
- end
34
-
35
- it "get a group" do
36
- group = @group_client.get("test_group_id")
37
- expect(group.id).to eq "test_group_id"
38
- expect(group.name).to eq "test_group_name"
39
- end
40
-
41
- it "passes rest client options" do
42
- rest_client_options = {timeout: 10}
43
- allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
44
-
45
- expect(RestClient::Resource).to receive(:new).with(
46
- "http://auth.service.io/auth/admin/realms/valid-realm/groups/test_group_id", rest_client_options).and_call_original
47
-
48
- group = @group_client.get("test_group_id")
49
- expect(group.id).to eq "test_group_id"
50
- expect(group.name).to eq "test_group_name"
51
- end
52
- end
53
-
54
- describe "#list" do
55
- let(:realm_name) { "valid-realm" }
56
-
57
- before(:each) do
58
- @group_client = KeycloakAdmin.realm(realm_name).groups
59
-
60
- stub_token_client
61
- allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"id":"test_group_id","name":"test_group_name"}]'
62
- end
63
-
64
- it "lists groups" do
65
- groups = @group_client.list
66
- expect(groups.length).to eq 1
67
- expect(groups[0].name).to eq "test_group_name"
68
- end
69
-
70
- it "passes rest client options" do
71
- rest_client_options = {timeout: 10}
72
- allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
73
-
74
- expect(RestClient::Resource).to receive(:new).with(
75
- "http://auth.service.io/auth/admin/realms/valid-realm/groups", rest_client_options).and_call_original
76
-
77
- groups = @group_client.list
78
- expect(groups.length).to eq 1
79
- expect(groups[0].name).to eq "test_group_name"
80
- end
81
- end
82
-
83
-
84
- describe "#children" do
85
- let(:realm_name) { "valid-realm" }
86
-
87
- before(:each) do
88
- @group_client = KeycloakAdmin.realm(realm_name).groups
89
-
90
- stub_token_client
91
- allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"id":"test_group_id","name":"test_group_name"}]'
92
- end
93
-
94
- it "lists children groups" do
95
- groups = @group_client.children("parent_group_id")
96
- expect(groups.length).to eq 1
97
- expect(groups[0].name).to eq "test_group_name"
98
- end
99
-
100
- it "passes rest client options" do
101
- rest_client_options = {timeout: 10}
102
- allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
103
-
104
- expect(RestClient::Resource).to receive(:new).with(
105
- "http://auth.service.io/auth/admin/realms/valid-realm/groups/parent_group_id/children", rest_client_options).and_call_original
106
-
107
- groups = @group_client.children("parent_group_id")
108
- expect(groups.length).to eq 1
109
- expect(groups[0].name).to eq "test_group_name"
110
- end
111
- end
112
-
113
- describe "#save" do
114
- let(:realm_name) { "valid-realm" }
115
-
116
- before(:each) do
117
- @group_client = KeycloakAdmin.realm(realm_name).groups
118
-
119
- stub_token_client
120
- end
121
-
122
- context "when the group does not exist" do
123
- let(:group) { KeycloakAdmin::GroupRepresentation.from_hash(
124
- "name" => "test_group_name"
125
- )}
126
-
127
- before do
128
- response = double
129
- allow(response).to receive(:headers).and_return(
130
- { location: 'http://auth.service.io/auth/admin/realms/valid-realm/groups/be061c48-6edd-4783-a726-1a57d4bfa22b' }
131
- )
132
-
133
- expect_any_instance_of(RestClient::Resource).to receive(:post).with(group.to_json, anything).and_return response
134
- end
135
-
136
- it "saves a group" do
137
- @group_client.save(group)
138
- end
139
-
140
- it "passes rest client options" do
141
- rest_client_options = {timeout: 10}
142
- allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
143
-
144
- expect(RestClient::Resource).to receive(:new).with(
145
- "http://auth.service.io/auth/admin/realms/valid-realm/groups", rest_client_options).and_call_original
146
-
147
- @group_client.save(group)
148
- end
149
- end
150
-
151
- context "when the group already exists" do
152
- let(:group) { KeycloakAdmin::GroupRepresentation.from_hash(
153
- "id" => "test_group_id",
154
- "name" => "test_group_name"
155
- )}
156
-
157
- before do
158
- response = double
159
- allow(response).to receive(:headers).and_return(
160
- { location: 'http://auth.service.io/auth/admin/realms/valid-realm/groups/be061c48-6edd-4783-a726-1a57d4bfa22b' }
161
- )
162
-
163
- expect_any_instance_of(RestClient::Resource).to receive(:put).with(group.to_json, anything).and_return response
164
- end
165
-
166
- it "saves a group" do
167
- @group_client.save(group)
168
- end
169
-
170
- it "passes rest client options" do
171
- rest_client_options = {timeout: 10}
172
- allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
173
-
174
- expect(RestClient::Resource).to receive(:new).with(
175
- "http://auth.service.io/auth/admin/realms/valid-realm/groups/test_group_id", rest_client_options).and_call_original
176
-
177
- @group_client.save(group)
178
- end
179
- end
180
- end
181
-
182
- describe "#create" do
183
- let(:realm_name) { "valid-realm" }
184
-
185
- before(:each) do
186
- @group_client = KeycloakAdmin.realm(realm_name).groups
187
-
188
- stub_token_client
189
- @response = double
190
- allow(@response).to receive(:headers).and_return(
191
- { location: 'http://auth.service.io/auth/admin/realms/valid-realm/groups/be061c48-6edd-4783-a726-1a57d4bfa22b' }
192
- )
193
- allow_any_instance_of(RestClient::Resource).to receive(:post).and_return @response
194
- end
195
-
196
- it "creates a group" do
197
- stub_net_http_res(Net::HTTPCreated, 201, 'Created')
198
-
199
- group_id = @group_client.create!("test_group_name")
200
- expect(group_id).to eq 'be061c48-6edd-4783-a726-1a57d4bfa22b'
201
- end
202
-
203
- it "detects unexpected response to create a group" do
204
- stub_net_http_res(Net::HTTPOK, 200, 'OK')
205
-
206
- expect{ @group_client.create!("test_group_name") }.to raise_error(
207
- 'Create method returned status OK (Code: 200); expected status: Created (201)'
208
- )
209
- end
210
- end
211
-
212
- describe "#create_subgroup!" do
213
- let(:realm_name) { "valid-realm" }
214
-
215
- before(:each) do
216
- @group_client = KeycloakAdmin.realm(realm_name).groups
217
-
218
- stub_token_client
219
- @response = double headers: {
220
- location: 'http://auth.service.io/auth/admin/realms/valid-realm/groups/7686af34-204c-4515-8122-78d19febbf6e'
221
- }
222
- allow_any_instance_of(RestClient::Resource).to receive(:post).and_return @response
223
- end
224
-
225
- it "creates a subgroup" do
226
- stub_net_http_res(Net::HTTPCreated, 201, 'Created')
227
-
228
- group_id = @group_client.create_subgroup!('be061c48-6edd-4783-a726-1a57d4bfa22b', 'subgroup-name')
229
- expect(group_id).to eq '7686af34-204c-4515-8122-78d19febbf6e'
230
- end
231
- end
232
-
233
- describe "#delete" do
234
- let(:realm_name) { "valid-realm" }
235
-
236
- before(:each) do
237
- @group_client = KeycloakAdmin.realm(realm_name).groups
238
-
239
- stub_token_client
240
- allow_any_instance_of(RestClient::Resource).to receive(:delete).and_return ''
241
- end
242
-
243
- it "deletes a group" do
244
- result = @group_client.delete("test_group_id")
245
- expect(result).to be(true)
246
- end
247
-
248
- it "raises a delete error" 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/groups/test_group_id", rest_client_options).and_raise("error")
254
-
255
- expect { @group_client.delete("test_group_id") }.to raise_error("error")
256
- end
257
- end
258
-
259
- describe '#get_realm_level_roles' do
260
- let(:realm_name) { 'valid-realm' }
261
- before(:each) do
262
- @group_client = KeycloakAdmin.realm(realm_name).groups
263
- stub_token_client
264
- allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"id":"role-id","name":"role-name"}]'
265
- end
266
-
267
- it 'gets all realm-level roles for a group' do
268
- roles = @group_client.get_realm_level_roles('test-group-id')
269
- expect(roles.length).to eq 1
270
- expect(roles[0].id).to eq 'role-id'
271
- expect(roles[0].name).to eq 'role-name'
272
- end
273
- end
274
-
275
- describe '#add_realm_level_role_name!' do
276
- let(:realm_name) { 'valid-realm' }
277
-
278
- before(:each) do
279
- @group_client = KeycloakAdmin.realm(realm_name).groups
280
-
281
- stub_token_client
282
- allow_any_instance_of(RestClient::Resource).to receive(:post).and_return ''
283
- end
284
-
285
- it 'adds a realm-level role to a group' do
286
- role_representation = double
287
- allow(role_representation).to receive(:name).and_return 'test-role-name'
288
-
289
- role_client = double
290
- allow(role_client).to receive(:get).with('test-role-name').and_return role_representation
291
- allow(KeycloakAdmin::RoleClient).to receive(:new).and_return role_client
292
-
293
- result = @group_client.add_realm_level_role_name!('test-group-id', 'test-role-name')
294
- expect(result).to eq role_representation
295
- end
296
- end
297
-
298
- describe '#remove_realm_level_role_name!' do
299
- let(:realm_name) { 'valid-realm' }
300
-
301
- before(:each) do
302
- @group_client = KeycloakAdmin.realm(realm_name).groups
303
-
304
- stub_token_client
305
- allow(RestClient::Request).to receive(:execute).and_return ''
306
- end
307
-
308
- it 'deletes a realm-level role from a group' do
309
- role_representation = double
310
- allow(role_representation).to receive(:name).and_return 'test-role-name'
311
-
312
- role_client = double
313
- allow(role_client).to receive(:get).with('test-role-name').and_return role_representation
314
- allow(KeycloakAdmin::RoleClient).to receive(:new).and_return role_client
315
-
316
- result = @group_client.remove_realm_level_role_name!('test-group-id', 'test-role-name')
317
- expect(result).to be(true)
318
- expect(RestClient::Request).to have_received(:execute).with(
319
- hash_including(
320
- url: "http://auth.service.io/auth/admin/realms/valid-realm/groups/test-group-id/role-mappings/realm",
321
- method: :delete,
322
- payload: @group_client.send(:create_payload, [role_representation]),
323
- headers: @group_client.send(:headers)
324
- )
325
- )
326
- end
327
- end
328
- end
1
+ RSpec.describe KeycloakAdmin::GroupClient do
2
+ describe "#groups_url" do
3
+ let(:realm_name) { "valid-realm" }
4
+ let(:group_id) { nil }
5
+
6
+ before(:each) do
7
+ @built_url = KeycloakAdmin.realm(realm_name).groups.groups_url(group_id)
8
+ end
9
+
10
+ context "when group_id is not defined" do
11
+ let(:group_id) { nil }
12
+ it "return a proper url without group id" do
13
+ expect(@built_url).to eq "http://auth.service.io/auth/admin/realms/valid-realm/groups"
14
+ end
15
+ end
16
+
17
+ context "when group_id is defined" do
18
+ let(:group_id) { "95985b21-d884-4bbd-b852-cb8cd365afc2" }
19
+ it "return a proper url with the group id" do
20
+ expect(@built_url).to eq "http://auth.service.io/auth/admin/realms/valid-realm/groups/95985b21-d884-4bbd-b852-cb8cd365afc2"
21
+ end
22
+ end
23
+ end
24
+
25
+ describe "#get" do
26
+ let(:realm_name) { "valid-realm" }
27
+
28
+ before(:each) do
29
+ @group_client = KeycloakAdmin.realm(realm_name).groups
30
+
31
+ stub_token_client
32
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '{"id":"test_group_id","name":"test_group_name"}'
33
+ end
34
+
35
+ it "get a group" do
36
+ group = @group_client.get("test_group_id")
37
+ expect(group.id).to eq "test_group_id"
38
+ expect(group.name).to eq "test_group_name"
39
+ end
40
+
41
+ it "passes rest client options" do
42
+ rest_client_options = {timeout: 10}
43
+ allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
44
+
45
+ expect(RestClient::Resource).to receive(:new).with(
46
+ "http://auth.service.io/auth/admin/realms/valid-realm/groups/test_group_id", rest_client_options).and_call_original
47
+
48
+ group = @group_client.get("test_group_id")
49
+ expect(group.id).to eq "test_group_id"
50
+ expect(group.name).to eq "test_group_name"
51
+ end
52
+ end
53
+
54
+ describe "#list" do
55
+ let(:realm_name) { "valid-realm" }
56
+
57
+ before(:each) do
58
+ @group_client = KeycloakAdmin.realm(realm_name).groups
59
+
60
+ stub_token_client
61
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"id":"test_group_id","name":"test_group_name"}]'
62
+ end
63
+
64
+ it "lists groups" do
65
+ groups = @group_client.list
66
+ expect(groups.length).to eq 1
67
+ expect(groups[0].name).to eq "test_group_name"
68
+ end
69
+
70
+ it "passes rest client options" do
71
+ rest_client_options = {timeout: 10}
72
+ allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
73
+
74
+ expect(RestClient::Resource).to receive(:new).with(
75
+ "http://auth.service.io/auth/admin/realms/valid-realm/groups", rest_client_options).and_call_original
76
+
77
+ groups = @group_client.list
78
+ expect(groups.length).to eq 1
79
+ expect(groups[0].name).to eq "test_group_name"
80
+ end
81
+ end
82
+
83
+
84
+ describe "#children" do
85
+ let(:realm_name) { "valid-realm" }
86
+
87
+ before(:each) do
88
+ @group_client = KeycloakAdmin.realm(realm_name).groups
89
+
90
+ stub_token_client
91
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"id":"test_group_id","name":"test_group_name"}]'
92
+ end
93
+
94
+ it "lists children groups" do
95
+ groups = @group_client.children("parent_group_id")
96
+ expect(groups.length).to eq 1
97
+ expect(groups[0].name).to eq "test_group_name"
98
+ end
99
+
100
+ it "passes rest client options" do
101
+ rest_client_options = {timeout: 10}
102
+ allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
103
+
104
+ expect(RestClient::Resource).to receive(:new).with(
105
+ "http://auth.service.io/auth/admin/realms/valid-realm/groups/parent_group_id/children", rest_client_options).and_call_original
106
+
107
+ groups = @group_client.children("parent_group_id")
108
+ expect(groups.length).to eq 1
109
+ expect(groups[0].name).to eq "test_group_name"
110
+ end
111
+ end
112
+
113
+ describe "#save" do
114
+ let(:realm_name) { "valid-realm" }
115
+
116
+ before(:each) do
117
+ @group_client = KeycloakAdmin.realm(realm_name).groups
118
+
119
+ stub_token_client
120
+ end
121
+
122
+ context "when the group does not exist" do
123
+ let(:group) { KeycloakAdmin::GroupRepresentation.from_hash(
124
+ "name" => "test_group_name"
125
+ )}
126
+
127
+ before do
128
+ response = double
129
+ allow(response).to receive(:headers).and_return(
130
+ { location: 'http://auth.service.io/auth/admin/realms/valid-realm/groups/be061c48-6edd-4783-a726-1a57d4bfa22b' }
131
+ )
132
+
133
+ expect_any_instance_of(RestClient::Resource).to receive(:post).with(group.to_json, anything).and_return response
134
+ end
135
+
136
+ it "saves a group" do
137
+ @group_client.save(group)
138
+ end
139
+
140
+ it "passes rest client options" do
141
+ rest_client_options = {timeout: 10}
142
+ allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
143
+
144
+ expect(RestClient::Resource).to receive(:new).with(
145
+ "http://auth.service.io/auth/admin/realms/valid-realm/groups", rest_client_options).and_call_original
146
+
147
+ @group_client.save(group)
148
+ end
149
+ end
150
+
151
+ context "when the group already exists" do
152
+ let(:group) { KeycloakAdmin::GroupRepresentation.from_hash(
153
+ "id" => "test_group_id",
154
+ "name" => "test_group_name"
155
+ )}
156
+
157
+ before do
158
+ response = double
159
+ allow(response).to receive(:headers).and_return(
160
+ { location: 'http://auth.service.io/auth/admin/realms/valid-realm/groups/be061c48-6edd-4783-a726-1a57d4bfa22b' }
161
+ )
162
+
163
+ expect_any_instance_of(RestClient::Resource).to receive(:put).with(group.to_json, anything).and_return response
164
+ end
165
+
166
+ it "saves a group" do
167
+ @group_client.save(group)
168
+ end
169
+
170
+ it "passes rest client options" do
171
+ rest_client_options = {timeout: 10}
172
+ allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
173
+
174
+ expect(RestClient::Resource).to receive(:new).with(
175
+ "http://auth.service.io/auth/admin/realms/valid-realm/groups/test_group_id", rest_client_options).and_call_original
176
+
177
+ @group_client.save(group)
178
+ end
179
+ end
180
+ end
181
+
182
+ describe "#create" do
183
+ let(:realm_name) { "valid-realm" }
184
+
185
+ before(:each) do
186
+ @group_client = KeycloakAdmin.realm(realm_name).groups
187
+
188
+ stub_token_client
189
+ @response = double
190
+ allow(@response).to receive(:headers).and_return(
191
+ { location: 'http://auth.service.io/auth/admin/realms/valid-realm/groups/be061c48-6edd-4783-a726-1a57d4bfa22b' }
192
+ )
193
+ allow_any_instance_of(RestClient::Resource).to receive(:post).and_return @response
194
+ end
195
+
196
+ it "creates a group" do
197
+ stub_net_http_res(Net::HTTPCreated, 201, 'Created')
198
+
199
+ group_id = @group_client.create!("test_group_name")
200
+ expect(group_id).to eq 'be061c48-6edd-4783-a726-1a57d4bfa22b'
201
+ end
202
+
203
+ it "detects unexpected response to create a group" do
204
+ stub_net_http_res(Net::HTTPOK, 200, 'OK')
205
+
206
+ expect{ @group_client.create!("test_group_name") }.to raise_error(
207
+ 'Create method returned status OK (Code: 200); expected status: Created (201)'
208
+ )
209
+ end
210
+ end
211
+
212
+ describe "#create_subgroup!" do
213
+ let(:realm_name) { "valid-realm" }
214
+
215
+ before(:each) do
216
+ @group_client = KeycloakAdmin.realm(realm_name).groups
217
+
218
+ stub_token_client
219
+ @response = double headers: {
220
+ location: 'http://auth.service.io/auth/admin/realms/valid-realm/groups/7686af34-204c-4515-8122-78d19febbf6e'
221
+ }
222
+ allow_any_instance_of(RestClient::Resource).to receive(:post).and_return @response
223
+ end
224
+
225
+ it "creates a subgroup" do
226
+ stub_net_http_res(Net::HTTPCreated, 201, 'Created')
227
+
228
+ group_id = @group_client.create_subgroup!('be061c48-6edd-4783-a726-1a57d4bfa22b', 'subgroup-name')
229
+ expect(group_id).to eq '7686af34-204c-4515-8122-78d19febbf6e'
230
+ end
231
+ end
232
+
233
+ describe "#delete" do
234
+ let(:realm_name) { "valid-realm" }
235
+
236
+ before(:each) do
237
+ @group_client = KeycloakAdmin.realm(realm_name).groups
238
+
239
+ stub_token_client
240
+ allow_any_instance_of(RestClient::Resource).to receive(:delete).and_return ''
241
+ end
242
+
243
+ it "deletes a group" do
244
+ result = @group_client.delete("test_group_id")
245
+ expect(result).to be(true)
246
+ end
247
+
248
+ it "raises a delete error" 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/groups/test_group_id", rest_client_options).and_raise("error")
254
+
255
+ expect { @group_client.delete("test_group_id") }.to raise_error("error")
256
+ end
257
+ end
258
+
259
+ describe '#get_realm_level_roles' do
260
+ let(:realm_name) { 'valid-realm' }
261
+ before(:each) do
262
+ @group_client = KeycloakAdmin.realm(realm_name).groups
263
+ stub_token_client
264
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"id":"role-id","name":"role-name"}]'
265
+ end
266
+
267
+ it 'gets all realm-level roles for a group' do
268
+ roles = @group_client.get_realm_level_roles('test-group-id')
269
+ expect(roles.length).to eq 1
270
+ expect(roles[0].id).to eq 'role-id'
271
+ expect(roles[0].name).to eq 'role-name'
272
+ end
273
+ end
274
+
275
+ describe '#add_realm_level_role_name!' do
276
+ let(:realm_name) { 'valid-realm' }
277
+
278
+ before(:each) do
279
+ @group_client = KeycloakAdmin.realm(realm_name).groups
280
+
281
+ stub_token_client
282
+ allow_any_instance_of(RestClient::Resource).to receive(:post).and_return ''
283
+ end
284
+
285
+ it 'adds a realm-level role to a group' do
286
+ role_representation = double
287
+ allow(role_representation).to receive(:name).and_return 'test-role-name'
288
+
289
+ role_client = double
290
+ allow(role_client).to receive(:get).with('test-role-name').and_return role_representation
291
+ allow(KeycloakAdmin::RoleClient).to receive(:new).and_return role_client
292
+
293
+ result = @group_client.add_realm_level_role_name!('test-group-id', 'test-role-name')
294
+ expect(result).to eq role_representation
295
+ end
296
+ end
297
+
298
+ describe '#remove_realm_level_role_name!' do
299
+ let(:realm_name) { 'valid-realm' }
300
+
301
+ before(:each) do
302
+ @group_client = KeycloakAdmin.realm(realm_name).groups
303
+
304
+ stub_token_client
305
+ allow(RestClient::Request).to receive(:execute).and_return ''
306
+ end
307
+
308
+ it 'deletes a realm-level role from a group' do
309
+ role_representation = double
310
+ allow(role_representation).to receive(:name).and_return 'test-role-name'
311
+
312
+ role_client = double
313
+ allow(role_client).to receive(:get).with('test-role-name').and_return role_representation
314
+ allow(KeycloakAdmin::RoleClient).to receive(:new).and_return role_client
315
+
316
+ result = @group_client.remove_realm_level_role_name!('test-group-id', 'test-role-name')
317
+ expect(result).to be(true)
318
+ expect(RestClient::Request).to have_received(:execute).with(
319
+ hash_including(
320
+ url: "http://auth.service.io/auth/admin/realms/valid-realm/groups/test-group-id/role-mappings/realm",
321
+ method: :delete,
322
+ payload: @group_client.send(:create_payload, [role_representation]),
323
+ headers: @group_client.send(:headers)
324
+ )
325
+ )
326
+ end
327
+ end
328
+ end