keycloak-admin 0.7.0 → 0.7.1

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 (32) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/CHANGELOG.md +48 -0
  4. data/Gemfile.lock +1 -1
  5. data/README.md +109 -2
  6. data/lib/keycloak-admin.rb +9 -0
  7. data/lib/keycloak-admin/client/client.rb +11 -3
  8. data/lib/keycloak-admin/client/client_client.rb +24 -0
  9. data/lib/keycloak-admin/client/client_role_mappings_client.rb +20 -0
  10. data/lib/keycloak-admin/client/group_client.rb +46 -0
  11. data/lib/keycloak-admin/client/realm_client.rb +50 -0
  12. data/lib/keycloak-admin/client/role_client.rb +32 -0
  13. data/lib/keycloak-admin/client/user_client.rb +9 -2
  14. data/lib/keycloak-admin/representation/camel_json.rb +1 -1
  15. data/lib/keycloak-admin/representation/client_representation.rb +16 -0
  16. data/lib/keycloak-admin/representation/group_representation.rb +15 -0
  17. data/lib/keycloak-admin/representation/realm_representation.rb +14 -0
  18. data/lib/keycloak-admin/representation/representation.rb +4 -0
  19. data/lib/keycloak-admin/representation/role_representation.rb +17 -0
  20. data/lib/keycloak-admin/representation/user_representation.rb +13 -13
  21. data/lib/keycloak-admin/resource/user_resource.rb +18 -0
  22. data/lib/keycloak-admin/version.rb +1 -1
  23. data/spec/client/client_client_spec.rb +53 -0
  24. data/spec/client/client_role_mappings_client_spec.rb +48 -0
  25. data/spec/client/group_client_spec.rb +125 -0
  26. data/spec/client/realm_client_spec.rb +108 -0
  27. data/spec/client/role_client_spec.rb +83 -0
  28. data/spec/client/user_client_spec.rb +105 -14
  29. data/spec/representation/user_representation_spec.rb +15 -0
  30. data/spec/resource/user_resource_spec.rb +14 -0
  31. data/spec/spec_helper.rb +7 -0
  32. metadata +18 -2
@@ -44,4 +44,112 @@ RSpec.describe KeycloakAdmin::RealmClient do
44
44
  end
45
45
  end
46
46
  end
47
+
48
+ describe "#list" do
49
+ before(:each) do
50
+ @realm_client = KeycloakAdmin.realm('master')
51
+
52
+ stub_token_client
53
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"id":"test_realm","realm":"test_realm"}]'
54
+ end
55
+
56
+ it "lists realms" do
57
+ realms = @realm_client.list
58
+ expect(realms.length).to eq 1
59
+ expect(realms[0].realm).to eq "test_realm"
60
+ end
61
+
62
+ it "passes rest client options" do
63
+ rest_client_options = {verify_ssl: OpenSSL::SSL::VERIFY_NONE}
64
+ allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
65
+
66
+ expect(RestClient::Resource).to receive(:new).with(
67
+ "http://auth.service.io/auth/admin/realms", rest_client_options).and_call_original
68
+
69
+ realms = @realm_client.list
70
+ expect(realms.length).to eq 1
71
+ expect(realms[0].realm).to eq "test_realm"
72
+ end
73
+ end
74
+
75
+ describe "#delete" do
76
+ let(:realm_name) { "valid-realm" }
77
+
78
+ before(:each) do
79
+ @realm_client = KeycloakAdmin.realm(realm_name)
80
+
81
+ stub_token_client
82
+ allow_any_instance_of(RestClient::Resource).to receive(:delete)
83
+ end
84
+
85
+ it "delete realm" do
86
+ expect(@realm_client.delete).to be_truthy
87
+ end
88
+
89
+ it "passes rest client options" do
90
+ rest_client_options = {verify_ssl: OpenSSL::SSL::VERIFY_NONE}
91
+ allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
92
+
93
+ expect(RestClient::Resource).to receive(:new).with(
94
+ "http://auth.service.io/auth/admin/realms/valid-realm", rest_client_options).and_call_original
95
+
96
+ expect(@realm_client.delete).to be_truthy
97
+ end
98
+ end
99
+
100
+ describe "#save" do
101
+ let(:realm_name) { "valid-realm" }
102
+ let(:realm) { KeycloakAdmin::RealmRepresentation.from_hash(
103
+ "id" => realm_name,
104
+ "realm" => realm_name
105
+ )}
106
+
107
+ before(:each) do
108
+ @realm_client = KeycloakAdmin.realm(nil)
109
+
110
+ stub_token_client
111
+
112
+ expect_any_instance_of(RestClient::Resource).to receive(:post).with(realm.to_json, anything)
113
+ end
114
+
115
+ it "saves a realm" do
116
+ @realm_client.save(realm)
117
+ end
118
+
119
+ it "passes rest client options" do
120
+ rest_client_options = {verify_ssl: OpenSSL::SSL::VERIFY_NONE}
121
+ allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
122
+
123
+ expect(RestClient::Resource).to receive(:new).with(
124
+ "http://auth.service.io/auth/admin/realms", rest_client_options).and_call_original
125
+
126
+ @realm_client.save(realm)
127
+ end
128
+ end
129
+
130
+ describe "#update" do
131
+ let(:realm_name) { "valid-realm" }
132
+ let(:realm_json) { { smtpServer: { host: 'test_host' } } }
133
+
134
+ before(:each) do
135
+ @realm_client = KeycloakAdmin.realm(realm_name)
136
+
137
+ stub_token_client
138
+ expect_any_instance_of(RestClient::Resource).to receive(:put).with(realm_json.to_json, anything)
139
+ end
140
+
141
+ it "updates a realm" do
142
+ @realm_client.update(realm_json)
143
+ end
144
+
145
+ it "passes rest client options" do
146
+ rest_client_options = {verify_ssl: OpenSSL::SSL::VERIFY_NONE}
147
+ allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
148
+
149
+ expect(RestClient::Resource).to receive(:new).with(
150
+ "http://auth.service.io/auth/admin/realms/valid-realm", rest_client_options).and_call_original
151
+
152
+ @realm_client.update(realm_json)
153
+ end
154
+ end
47
155
  end
@@ -0,0 +1,83 @@
1
+ RSpec.describe KeycloakAdmin::RoleClient do
2
+ describe "#roles_url" do
3
+ let(:realm_name) { "valid-realm" }
4
+ let(:role_id) { nil }
5
+
6
+ before(:each) do
7
+ @built_url = KeycloakAdmin.realm(realm_name).roles.roles_url(role_id)
8
+ end
9
+
10
+ context "when role_id is not defined" do
11
+ let(:role_id) { nil }
12
+ it "return a proper url without role id" do
13
+ expect(@built_url).to eq "http://auth.service.io/auth/admin/realms/valid-realm/roles"
14
+ end
15
+ end
16
+
17
+ context "when role_id is defined" do
18
+ let(:role_id) { "95985b21-d884-4bbd-b852-cb8cd365afc2" }
19
+ it "return a proper url with the role id" do
20
+ expect(@built_url).to eq "http://auth.service.io/auth/admin/realms/valid-realm/roles/95985b21-d884-4bbd-b852-cb8cd365afc2"
21
+ end
22
+ end
23
+ end
24
+
25
+ describe "#list" do
26
+ let(:realm_name) { "valid-realm" }
27
+
28
+ before(:each) do
29
+ @role_client = KeycloakAdmin.realm(realm_name).roles
30
+
31
+ stub_token_client
32
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"id":"test_role_id","name":"test_role_name"}]'
33
+ end
34
+
35
+ it "lists roles" do
36
+ roles = @role_client.list
37
+ expect(roles.length).to eq 1
38
+ expect(roles[0].name).to eq "test_role_name"
39
+ end
40
+
41
+ it "passes rest client options" do
42
+ rest_client_options = {verify_ssl: OpenSSL::SSL::VERIFY_NONE}
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/roles", rest_client_options).and_call_original
47
+
48
+ roles = @role_client.list
49
+ expect(roles.length).to eq 1
50
+ expect(roles[0].name).to eq "test_role_name"
51
+ end
52
+ end
53
+
54
+ describe "#save" do
55
+ let(:realm_name) { "valid-realm" }
56
+ let(:role) { KeycloakAdmin::RoleRepresentation.from_hash(
57
+ "name" => "test_role_name",
58
+ "composite" => false,
59
+ "clientRole" => false
60
+ )}
61
+
62
+ before(:each) do
63
+ @role_client = KeycloakAdmin.realm(realm_name).roles
64
+
65
+ stub_token_client
66
+ expect_any_instance_of(RestClient::Resource).to receive(:post).with(role.to_json, anything)
67
+ end
68
+
69
+ it "saves a role" do
70
+ @role_client.save(role)
71
+ end
72
+
73
+ it "passes rest client options" do
74
+ rest_client_options = {verify_ssl: OpenSSL::SSL::VERIFY_NONE}
75
+ allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
76
+
77
+ expect(RestClient::Resource).to receive(:new).with(
78
+ "http://auth.service.io/auth/admin/realms/valid-realm/roles", rest_client_options).and_call_original
79
+
80
+ @role_client.save(role)
81
+ end
82
+ end
83
+ end
@@ -97,15 +97,42 @@ RSpec.describe KeycloakAdmin::TokenClient do
97
97
  end
98
98
  end
99
99
 
100
+ describe "#save" do
101
+ let(:realm_name) { "valid-realm" }
102
+ let(:user) { KeycloakAdmin::UserRepresentation.from_hash(
103
+ "username" => "test_username",
104
+ "createdTimestamp" => Time.now.to_i,
105
+ )}
106
+
107
+ before(:each) do
108
+ @user_client = KeycloakAdmin.realm(realm_name).users
109
+
110
+ stub_token_client
111
+ allow_any_instance_of(RestClient::Resource).to receive(:post)
112
+ end
113
+
114
+ it "saves a user" do
115
+ expect(@user_client.save(user)).to eq user
116
+ end
117
+
118
+ it "passes rest client options" do
119
+ rest_client_options = {verify_ssl: OpenSSL::SSL::VERIFY_NONE}
120
+ allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
121
+
122
+ expect(RestClient::Resource).to receive(:new).with(
123
+ "http://auth.service.io/auth/admin/realms/valid-realm/users", rest_client_options).and_call_original
124
+
125
+ expect(@user_client.save(user)).to eq user
126
+ end
127
+ end
128
+
100
129
  describe "#get" do
101
130
  let(:realm_name) { "valid-realm" }
131
+
102
132
  before(:each) do
103
- @user_client = KeycloakAdmin.realm("a_realm").users
133
+ @user_client = KeycloakAdmin.realm(realm_name).users
104
134
 
105
- allow_any_instance_of(KeycloakAdmin::TokenClient).to receive(:get).and_return KeycloakAdmin::TokenRepresentation.new(
106
- 'test_access_token', 'token_type', 'expires_in', 'refresh_token',
107
- 'refresh_expires_in', 'id_token', 'not_before_policy', 'session_state'
108
- )
135
+ stub_token_client
109
136
  allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '{"username":"test_username","createdTimestamp":1559347200}'
110
137
  end
111
138
 
@@ -119,27 +146,91 @@ RSpec.describe KeycloakAdmin::TokenClient do
119
146
  allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
120
147
 
121
148
  expect(RestClient::Resource).to receive(:new).with(
122
- "http://auth.service.io/auth/admin/realms/a_realm/users/test_user_id", rest_client_options).and_call_original
149
+ "http://auth.service.io/auth/admin/realms/valid-realm/users/test_user_id", rest_client_options).and_call_original
123
150
 
124
151
  user = @user_client.get('test_user_id')
125
152
  expect(user.username).to eq 'test_username'
126
153
  end
127
154
  end
128
155
 
156
+ describe "#search" do
157
+ let(:realm_name) { "valid-realm" }
158
+ let(:user) { KeycloakAdmin::UserRepresentation.from_hash(
159
+ "username" => "test_username",
160
+ "createdTimestamp" => Time.now.to_i,
161
+ )}
162
+
163
+ before(:each) do
164
+ @user_client = KeycloakAdmin.realm(realm_name).users
165
+
166
+ stub_token_client
167
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"username":"test_username","createdTimestamp":1559347200}]'
168
+ end
169
+
170
+ it "finds a user" do
171
+ users = @user_client.search("test_username")
172
+ expect(users.length).to eq 1
173
+ expect(users[0].username).to eq "test_username"
174
+ end
175
+
176
+ it "passes rest client options" do
177
+ rest_client_options = {verify_ssl: OpenSSL::SSL::VERIFY_NONE}
178
+ allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
179
+
180
+ expect(RestClient::Resource).to receive(:new).with(
181
+ "http://auth.service.io/auth/admin/realms/valid-realm/users", rest_client_options).and_call_original
182
+
183
+ users = @user_client.search("test_username")
184
+ expect(users.length).to eq 1
185
+ expect(users[0].username).to eq "test_username"
186
+ end
187
+ end
188
+
189
+ describe "#list" do
190
+ let(:realm_name) { "valid-realm" }
191
+ let(:user) { KeycloakAdmin::UserRepresentation.from_hash(
192
+ "username" => "test_username",
193
+ "createdTimestamp" => Time.now.to_i,
194
+ )}
195
+
196
+ before(:each) do
197
+ @user_client = KeycloakAdmin.realm(realm_name).users
198
+
199
+ stub_token_client
200
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"username":"test_username","createdTimestamp":1559347200}]'
201
+ end
202
+
203
+ it "lists users" do
204
+ users = @user_client.list
205
+ expect(users.length).to eq 1
206
+ expect(users[0].username).to eq "test_username"
207
+ end
208
+
209
+ it "passes rest client options" do
210
+ rest_client_options = {verify_ssl: OpenSSL::SSL::VERIFY_NONE}
211
+ allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
212
+
213
+ expect(RestClient::Resource).to receive(:new).with(
214
+ "http://auth.service.io/auth/admin/realms/valid-realm/users", rest_client_options).and_call_original
215
+
216
+ users = @user_client.list
217
+ expect(users.length).to eq 1
218
+ expect(users[0].username).to eq "test_username"
219
+ end
220
+ end
221
+
129
222
  describe "#delete" do
130
223
  let(:realm_name) { "valid-realm" }
224
+
131
225
  before(:each) do
132
- @user_client = KeycloakAdmin.realm("a_realm").users
226
+ @user_client = KeycloakAdmin.realm(realm_name).users
133
227
 
134
- allow_any_instance_of(KeycloakAdmin::TokenClient).to receive(:get).and_return KeycloakAdmin::TokenRepresentation.new(
135
- 'test_access_token', 'token_type', 'expires_in', 'refresh_token',
136
- 'refresh_expires_in', 'id_token', 'not_before_policy', 'session_state'
137
- )
228
+ stub_token_client
138
229
  allow_any_instance_of(RestClient::Resource).to receive(:delete)
139
230
  end
140
231
 
141
- it "parses the response" do
142
- @user_client.delete('test_user_id')
232
+ it "does not fail" do
233
+ expect(@user_client.delete('test_user_id')).to be_truthy
143
234
  end
144
235
 
145
236
  it "passes rest client options" do
@@ -147,7 +238,7 @@ RSpec.describe KeycloakAdmin::TokenClient do
147
238
  allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
148
239
 
149
240
  expect(RestClient::Resource).to receive(:new).with(
150
- "http://auth.service.io/auth/admin/realms/a_realm/users/test_user_id", rest_client_options).and_call_original
241
+ "http://auth.service.io/auth/admin/realms/valid-realm/users/test_user_id", rest_client_options).and_call_original
151
242
 
152
243
  @user_client.delete('test_user_id')
153
244
  end
@@ -0,0 +1,15 @@
1
+ RSpec.describe KeycloakAdmin::UserRepresentation do
2
+ describe "#to_json" do
3
+ before(:each) do
4
+ @user = KeycloakAdmin::UserRepresentation.from_hash(
5
+ "username" => "test_username",
6
+ "createdTimestamp" => Time.at(1559836000).to_i,
7
+ "enabled" => true
8
+ )
9
+ end
10
+
11
+ it "can convert to json" do
12
+ expect(@user.to_json).to eq '{"id":null,"createdTimestamp":1559836000,"origin":null,"username":"test_username","email":null,"enabled":true,"emailVerified":null,"firstName":null,"lastName":null,"attributes":null,"credentials":[]}'
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ RSpec.describe KeycloakAdmin::UserResource do
2
+ describe "#resource_url" do
3
+ let(:realm_name) { "valid-realm" }
4
+ let(:user_id) { "95985b21-d884-4bbd-b852-cb8cd365afc2" }
5
+
6
+ before(:each) do
7
+ @built_url = KeycloakAdmin.realm(realm_name).user(user_id).resource_url
8
+ end
9
+
10
+ it "return a proper url" do
11
+ expect(@built_url).to eq "http://auth.service.io/auth/admin/realms/valid-realm/users/95985b21-d884-4bbd-b852-cb8cd365afc2"
12
+ end
13
+ end
14
+ end
@@ -20,3 +20,10 @@ RSpec.configure do |config|
20
20
 
21
21
  configure
22
22
  end
23
+
24
+ def stub_token_client
25
+ allow_any_instance_of(KeycloakAdmin::TokenClient).to receive(:get).and_return KeycloakAdmin::TokenRepresentation.new(
26
+ 'test_access_token', 'token_type', 'expires_in', 'refresh_token',
27
+ 'refresh_expires_in', 'id_token', 'not_before_policy', 'session_state'
28
+ )
29
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: keycloak-admin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lorent Lempereur
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-06 00:00:00.000000000 Z
11
+ date: 2019-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http-cookie
@@ -81,6 +81,7 @@ extra_rdoc_files: []
81
81
  files:
82
82
  - ".gitignore"
83
83
  - ".rspec"
84
+ - CHANGELOG.md
84
85
  - Dockerfile
85
86
  - Gemfile
86
87
  - Gemfile.lock
@@ -89,26 +90,41 @@ files:
89
90
  - keycloak-admin.gemspec
90
91
  - lib/keycloak-admin.rb
91
92
  - lib/keycloak-admin/client/client.rb
93
+ - lib/keycloak-admin/client/client_client.rb
94
+ - lib/keycloak-admin/client/client_role_mappings_client.rb
92
95
  - lib/keycloak-admin/client/configurable_token_client.rb
96
+ - lib/keycloak-admin/client/group_client.rb
93
97
  - lib/keycloak-admin/client/realm_client.rb
98
+ - lib/keycloak-admin/client/role_client.rb
94
99
  - lib/keycloak-admin/client/token_client.rb
95
100
  - lib/keycloak-admin/client/user_client.rb
96
101
  - lib/keycloak-admin/configuration.rb
97
102
  - lib/keycloak-admin/representation/camel_json.rb
103
+ - lib/keycloak-admin/representation/client_representation.rb
98
104
  - lib/keycloak-admin/representation/credential_representation.rb
105
+ - lib/keycloak-admin/representation/group_representation.rb
99
106
  - lib/keycloak-admin/representation/impersonation_redirection_representation.rb
100
107
  - lib/keycloak-admin/representation/impersonation_representation.rb
108
+ - lib/keycloak-admin/representation/realm_representation.rb
101
109
  - lib/keycloak-admin/representation/representation.rb
110
+ - lib/keycloak-admin/representation/role_representation.rb
102
111
  - lib/keycloak-admin/representation/token_representation.rb
103
112
  - lib/keycloak-admin/representation/user_representation.rb
113
+ - lib/keycloak-admin/resource/user_resource.rb
104
114
  - lib/keycloak-admin/version.rb
115
+ - spec/client/client_client_spec.rb
116
+ - spec/client/client_role_mappings_client_spec.rb
105
117
  - spec/client/client_spec.rb
106
118
  - spec/client/configurable_token_client_spec.rb
119
+ - spec/client/group_client_spec.rb
107
120
  - spec/client/realm_client_spec.rb
121
+ - spec/client/role_client_spec.rb
108
122
  - spec/client/token_client_spec.rb
109
123
  - spec/client/user_client_spec.rb
110
124
  - spec/configuration_spec.rb
111
125
  - spec/representation/impersonation_representation_spec.rb
126
+ - spec/representation/user_representation_spec.rb
127
+ - spec/resource/user_resource_spec.rb
112
128
  - spec/spec_helper.rb
113
129
  homepage: https://github.com/looorent/keycloak-admin-ruby
114
130
  licenses: