keycloak-admin 1.1.1 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/Dockerfile +24 -0
  3. data/.github/workflows/ci.yml +83 -0
  4. data/CHANGELOG.md +12 -2
  5. data/Gemfile.lock +8 -8
  6. data/README.md +277 -4
  7. data/lib/keycloak-admin/client/client_authz_permission_client.rb +81 -0
  8. data/lib/keycloak-admin/client/client_authz_policy_client.rb +76 -0
  9. data/lib/keycloak-admin/client/client_authz_resource_client.rb +93 -0
  10. data/lib/keycloak-admin/client/client_authz_scope_client.rb +71 -0
  11. data/lib/keycloak-admin/client/group_client.rb +41 -13
  12. data/lib/keycloak-admin/client/realm_client.rb +16 -0
  13. data/lib/keycloak-admin/client/role_client.rb +12 -10
  14. data/lib/keycloak-admin/client/user_client.rb +1 -0
  15. data/lib/keycloak-admin/representation/client_authz_permission_representation.rb +34 -0
  16. data/lib/keycloak-admin/representation/client_authz_policy_config_representation.rb +15 -0
  17. data/lib/keycloak-admin/representation/client_authz_policy_representation.rb +27 -0
  18. data/lib/keycloak-admin/representation/client_authz_resource_representation.rb +26 -0
  19. data/lib/keycloak-admin/representation/client_authz_scope_representation.rb +17 -0
  20. data/lib/keycloak-admin/representation/group_representation.rb +9 -5
  21. data/lib/keycloak-admin/version.rb +1 -1
  22. data/lib/keycloak-admin.rb +9 -0
  23. data/spec/client/client_authz_permission_client_spec.rb +170 -0
  24. data/spec/client/client_authz_policy_client_spec.rb +170 -0
  25. data/spec/client/client_authz_resource_client_spec.rb +150 -0
  26. data/spec/client/client_authz_scope_client_spec.rb +134 -0
  27. data/spec/client/client_client_spec.rb +2 -2
  28. data/spec/client/client_role_mappings_client_spec.rb +2 -2
  29. data/spec/client/group_client_spec.rb +137 -15
  30. data/spec/client/identity_provider_client_spec.rb +1 -1
  31. data/spec/client/realm_client_spec.rb +4 -4
  32. data/spec/client/role_client_spec.rb +12 -16
  33. data/spec/client/role_mapper_client_spec.rb +1 -1
  34. data/spec/client/token_client_spec.rb +1 -1
  35. data/spec/client/user_client_spec.rb +5 -5
  36. data/spec/configuration_spec.rb +1 -1
  37. data/spec/integration/client_authorization_spec.rb +95 -0
  38. data/spec/representation/client_authz_permission_representation_spec.rb +52 -0
  39. data/spec/representation/client_authz_policy_representation_spec.rb +47 -0
  40. data/spec/representation/client_authz_resource_representation_spec.rb +33 -0
  41. data/spec/representation/client_authz_scope_representation_spec.rb +19 -0
  42. data/spec/representation/group_representation_spec.rb +7 -0
  43. metadata +23 -3
@@ -0,0 +1,170 @@
1
+ RSpec.describe KeycloakAdmin::ClientAuthzPermissionClient do
2
+
3
+ describe "#initialize" do
4
+ let(:realm_name) { nil }
5
+ let(:type) { :scope }
6
+ before(:each) do
7
+ @realm = KeycloakAdmin.realm(realm_name)
8
+ end
9
+
10
+ context "when realm_name is defined" do
11
+ let(:realm_name) { "master" }
12
+ it "does not raise any error" do
13
+ expect {
14
+ @realm.authz_permissions("", type)
15
+ }.to_not raise_error
16
+ end
17
+ end
18
+
19
+ context "when realm_name is not defined" do
20
+ let(:realm_name) { nil }
21
+ it "raises any error" do
22
+ expect {
23
+ @realm.authz_permissions("", type)
24
+ }.to raise_error(ArgumentError)
25
+ end
26
+ end
27
+
28
+ context "when type is bad value" do
29
+ let(:realm_name) { "master" }
30
+ let(:type) { "bad-type" }
31
+ it "does not raise any error" do
32
+ expect {
33
+ @realm.authz_permissions("", type)
34
+ }.to raise_error
35
+ end
36
+ end
37
+ end
38
+
39
+ describe '#delete' do
40
+ let(:realm_name) { "valid-realm" }
41
+ let(:client_id) { "valid-client-id" }
42
+ let(:client_authz_permission) { KeycloakAdmin.realm(realm_name).authz_permissions(client_id, "resource") }
43
+ before(:each) do
44
+ stub_token_client
45
+ allow_any_instance_of(RestClient::Resource).to receive(:delete).and_return 'true'
46
+ end
47
+
48
+ it "deletes a permission" do
49
+ expect(client_authz_permission.delete("valid-permission-id")).to be_truthy
50
+ end
51
+ end
52
+
53
+ describe '#find_by' do
54
+ let(:realm_name) { "valid-realm" }
55
+ let(:client_id) { "valid-client-id" }
56
+ let(:client_authz_permission) { KeycloakAdmin.realm(realm_name).authz_permissions(client_id, "resource") }
57
+ before(:each) do
58
+ stub_token_client
59
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"id":"245ce612-ccdc-4426-8ea7-e0e29a718033","name":"Default Permission","description":"A permission that applies to the default resource type","type":"resource","logic":"POSITIVE","decisionStrategy":"UNANIMOUS","resourceType":"urn:dummy-client:resources:default"},{"id":"06a21e38-4e92-466d-8647-ffcd9c7b51c3","name":"delme policy","description":"delme polidy ","type":"resource","logic":"POSITIVE","decisionStrategy":"UNANIMOUS","resourceType":"asdfasdf"}]'
60
+ end
61
+
62
+ it "finds permissions" do
63
+ response = client_authz_permission.find_by("name", "resource", "scope")
64
+ expect(response[0].id).to eql "245ce612-ccdc-4426-8ea7-e0e29a718033"
65
+ expect(response[1].id).to eql "06a21e38-4e92-466d-8647-ffcd9c7b51c3"
66
+ end
67
+ end
68
+
69
+ describe '#create!' do
70
+ let(:realm_name) { "valid-realm" }
71
+ let(:client_id) { "valid-client-id" }
72
+ let(:client_authz_permission) { KeycloakAdmin.realm(realm_name).authz_permissions(client_id, "resource") }
73
+ before(:each) do
74
+ stub_token_client
75
+ allow_any_instance_of(RestClient::Resource).to receive(:post).and_return '{"id":"245ce612-ccdc-4426-8ea7-e0e29a718033","name":"Default Permission","description":"A permission that applies to the default resource type","type":"resource","logic":"POSITIVE","decisionStrategy":"UNANIMOUS","resourceType":"urn:dummy-client:resources:default"}'
76
+ end
77
+
78
+ it "creates a permission" do
79
+ response = client_authz_permission.create!("name", "description", "UNANIMOUS", "POSITIVE", [], [], [], "resource")
80
+ expect(response.id).to eql "245ce612-ccdc-4426-8ea7-e0e29a718033"
81
+ expect(response.name).to eql "Default Permission"
82
+ expect(response.description).to eql "A permission that applies to the default resource type"
83
+ expect(response.logic).to eql "POSITIVE"
84
+ expect(response.decision_strategy).to eql "UNANIMOUS"
85
+ expect(response.resource_type).to eql "urn:dummy-client:resources:default"
86
+ end
87
+ end
88
+
89
+ describe '#list' do
90
+ let(:realm_name) { "valid-realm" }
91
+ let(:client_id) { "valid-client-id" }
92
+ before(:each) do
93
+ @client_authz_permission = KeycloakAdmin.realm(realm_name).authz_permissions(client_id, "resource")
94
+ stub_token_client
95
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"id":"245ce612-ccdc-4426-8ea7-e0e29a718033","name":"Default Permission","description":"A permission that applies to the default resource type","type":"resource","logic":"POSITIVE","decisionStrategy":"UNANIMOUS","resourceType":"urn:dummy-client:resources:default"},{"id":"06a21e38-4e92-466d-8647-ffcd9c7b51c3","name":"delme policy","description":"delme polidy ","type":"resource","logic":"POSITIVE","decisionStrategy":"UNANIMOUS","resourceType":"asdfasdf"}]'
96
+
97
+ end
98
+
99
+ it "returns list of authz permissions" do
100
+ response = @client_authz_permission.list
101
+ expect(response.size).to eq 2
102
+ expect(response[0].id).to eq "245ce612-ccdc-4426-8ea7-e0e29a718033"
103
+ expect(response[0].name).to eq "Default Permission"
104
+ expect(response[0].description).to eq "A permission that applies to the default resource type"
105
+ expect(response[0].logic).to eq "POSITIVE"
106
+ expect(response[0].decision_strategy).to eq "UNANIMOUS"
107
+ expect(response[0].resource_type).to eq "urn:dummy-client:resources:default"
108
+ end
109
+ end
110
+
111
+ describe '#get' do
112
+ let(:realm_name) { "valid-realm" }
113
+ let(:client_id) { "valid-client-id" }
114
+ let(:client_authz_permission) { KeycloakAdmin.realm(realm_name).authz_permissions(client_id, "resource") }
115
+ before(:each) do
116
+ stub_token_client
117
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '{"id":"245ce612-ccdc-4426-8ea7-e0e29a718033","name":"Default Permission","description":"A permission that applies to the default resource type","type":"resource","logic":"POSITIVE","decisionStrategy":"UNANIMOUS","resourceType":"urn:dummy-client:resources:default"}'
118
+ end
119
+
120
+ it "gets a permission" do
121
+ response = client_authz_permission.get("245ce612-ccdc-4426-8ea7-e0e29a718033")
122
+ expect(response.id).to eql "245ce612-ccdc-4426-8ea7-e0e29a718033"
123
+ expect(response.name).to eql "Default Permission"
124
+ expect(response.description).to eql "A permission that applies to the default resource type"
125
+ expect(response.logic).to eql "POSITIVE"
126
+ expect(response.decision_strategy).to eql "UNANIMOUS"
127
+ expect(response.resource_type).to eql "urn:dummy-client:resources:default"
128
+ end
129
+ end
130
+
131
+ describe '#authz_permission_url' do
132
+ let(:realm_name) { "valid-realm" }
133
+ let(:client_id) { "valid-client-id" }
134
+ let(:resource_id){ "valid-resource-id" }
135
+ let(:type){ "resource" }
136
+ let(:client_authz_permission) { KeycloakAdmin.realm(realm_name).authz_permissions(client_id, type, resource_id) }
137
+ context 'when resource_id is nil' do
138
+ it "return a proper url" do
139
+ expect(client_authz_permission.authz_permission_url(client_id)).to eq "http://auth.service.io/auth/admin/realms/valid-realm/clients/valid-client-id/authz/resource-server/permission/"
140
+ end
141
+ end
142
+ context 'when resource_id is not nil' do
143
+ it "return a proper url" do
144
+ expect(client_authz_permission.authz_permission_url(client_id, resource_id)).to eq "http://auth.service.io/auth/admin/realms/valid-realm/clients/valid-client-id/authz/resource-server/resource/valid-resource-id/permissions"
145
+ end
146
+ end
147
+ context 'when id is not nil' do
148
+ it "return a proper url" do
149
+ expect(client_authz_permission.authz_permission_url(client_id, nil, :resource, "valid-permission-id")).to eq "http://auth.service.io/auth/admin/realms/valid-realm/clients/valid-client-id/authz/resource-server/permission/resource/valid-permission-id"
150
+ end
151
+ end
152
+ context 'when resource_id and id are nil' do
153
+ it "return a proper url" do
154
+ expect(client_authz_permission.authz_permission_url(client_id, nil, :resource)).to eq "http://auth.service.io/auth/admin/realms/valid-realm/clients/valid-client-id/authz/resource-server/permission/resource"
155
+ end
156
+ end
157
+ end
158
+
159
+ describe '#authz_permission_url' do
160
+ let(:realm_name) { "valid-realm" }
161
+ let(:client_id) { "valid-client-id" }
162
+ before(:each) do
163
+ @client_authz_permission = KeycloakAdmin.realm(realm_name).authz_permissions(client_id, "resource")
164
+ end
165
+
166
+ it "return a proper url" do
167
+ expect(@client_authz_permission.authz_permission_url(client_id)).to eq "http://auth.service.io/auth/admin/realms/valid-realm/clients/valid-client-id/authz/resource-server/permission/"
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,170 @@
1
+ RSpec.describe KeycloakAdmin::ClientAuthzPolicyClient do
2
+
3
+ describe '#initialize' do
4
+ let(:realm_name) { nil }
5
+ let(:type) { :role }
6
+ before(:each) do
7
+ @realm = KeycloakAdmin.realm(realm_name)
8
+ end
9
+
10
+ context "when realm_name is defined" do
11
+ let(:realm_name) { "master" }
12
+ it "does not raise any error" do
13
+ expect {
14
+ @realm.authz_policies("", type)
15
+ }.to_not raise_error
16
+ end
17
+ end
18
+
19
+ context "when realm_name is not defined" do
20
+ let(:realm_name) { nil }
21
+ it "raises any error" do
22
+ expect {
23
+ @realm.authz_policies("", type)
24
+ }.to raise_error(ArgumentError)
25
+ end
26
+ end
27
+
28
+ context "when type is bad value" do
29
+ let(:realm_name) { "master" }
30
+ let(:type) { "bad-type" }
31
+ it "does not raise any error" do
32
+ expect {
33
+ @realm.authz_policies("", type)
34
+ }.to raise_error
35
+ end
36
+ end
37
+ end
38
+
39
+ describe '#create!' do
40
+ let(:realm_name) { "valid-realm" }
41
+ let(:client_id) { "valid-client-id" }
42
+ let(:type) { :role }
43
+ let(:name) { "policy name" }
44
+ let(:description) { "policy description" }
45
+ let(:logic) { "POSITIVE" }
46
+ let(:decision_strategy) { "UNANIMOUS" }
47
+ let(:fetch_roles) { true }
48
+ let(:config) { { roles: [{ id: "1d305dbe-6379-4900-8e63-96541006160a", required: false }] } }
49
+ let(:client_authz_policy){ KeycloakAdmin.realm(realm_name).authz_policies(client_id, type) }
50
+
51
+ before(:each) do
52
+ stub_token_client
53
+ allow_any_instance_of(RestClient::Resource).to receive(:post).and_return '{"id":"234f6f33-ef03-4f3f-a8c0-ad7bca27b720","name":"policy name","description":"policy description","type":"role","logic":"POSITIVE","decisionStrategy":"UNANIMOUS","config":{"roles":"[{\"id\":\"1d305dbe-6379-4900-8e63-96541006160a\",\"required\":false}]"}}'
54
+ end
55
+
56
+ it "creates a new authz policy" do
57
+ response = client_authz_policy.create!(name, description, type, logic, decision_strategy, fetch_roles, config[:roles])
58
+ expect(response.id).to eq "234f6f33-ef03-4f3f-a8c0-ad7bca27b720"
59
+ expect(response.name).to eq "policy name"
60
+ expect(response.type).to eq "role"
61
+ expect(response.logic).to eq "POSITIVE"
62
+ expect(response.decision_strategy).to eq "UNANIMOUS"
63
+ expect(response.config.roles[0].id).to eq "1d305dbe-6379-4900-8e63-96541006160a"
64
+ end
65
+ end
66
+
67
+ describe '#get' do
68
+ let(:realm_name) { "valid-realm" }
69
+ let(:client_id) { "valid-client-id" }
70
+ let(:type) { :role }
71
+ let(:policy_id) { "234f6f33-ef03-4f3f-a8c0-ad7bca27b720" }
72
+ let(:client_authz_policy){ KeycloakAdmin.realm(realm_name).authz_policies(client_id, type) }
73
+ before(:each) do
74
+ stub_token_client
75
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '{"id":"234f6f33-ef03-4f3f-a8c0-ad7bca27b720","name":"policy name","description":"policy description","type":"role","logic":"POSITIVE","decisionStrategy":"UNANIMOUS","config":{"roles":"[{\"id\":\"1d305dbe-6379-4900-8e63-96541006160a\",\"required\":false}]"}}'
76
+ end
77
+
78
+ it "returns an authz policy" do
79
+ response = client_authz_policy.get(policy_id)
80
+ expect(response.id).to eq "234f6f33-ef03-4f3f-a8c0-ad7bca27b720"
81
+ expect(response.name).to eq "policy name"
82
+ expect(response.type).to eq "role"
83
+ expect(response.logic).to eq "POSITIVE"
84
+ expect(response.decision_strategy).to eq "UNANIMOUS"
85
+ expect(response.config.roles[0].id).to eq "1d305dbe-6379-4900-8e63-96541006160a"
86
+ end
87
+ end
88
+
89
+ describe '#find_by' do
90
+ let(:realm_name) { "valid-realm" }
91
+ let(:client_id) { "valid-client-id" }
92
+ let(:type) { :role }
93
+ let(:name) { "policy name" }
94
+ let(:client_authz_policy){ KeycloakAdmin.realm(realm_name).authz_policies(client_id, type) }
95
+ before(:each) do
96
+ stub_token_client
97
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"id":"234f6f33-ef03-4f3f-a8c0-ad7bca27b720","name":"policy name","description":"policy description","type":"role","logic":"POSITIVE","decisionStrategy":"UNANIMOUS","config":{"roles":"[{\"id\":\"1d305dbe-6379-4900-8e63-96541006160a\",\"required\":false}]"}}]'
98
+ end
99
+
100
+ it "returns list of authz policies" do
101
+ response = client_authz_policy.find_by(name, type)
102
+ expect(response.size).to eq 1
103
+ expect(response[0].id).to eq "234f6f33-ef03-4f3f-a8c0-ad7bca27b720"
104
+ expect(response[0].name).to eq "policy name"
105
+ expect(response[0].type).to eq "role"
106
+ expect(response[0].logic).to eq "POSITIVE"
107
+ expect(response[0].decision_strategy).to eq "UNANIMOUS"
108
+ expect(response[0].config.roles[0].id).to eq "1d305dbe-6379-4900-8e63-96541006160a"
109
+ end
110
+ end
111
+
112
+ describe '#delete' do
113
+ let(:realm_name) { "valid-realm" }
114
+ let(:client_id) { "valid-client-id" }
115
+ let(:type) { :role }
116
+ let(:policy_id) { "234f6f33-ef03-4f3f-a8c0-ad7bca27b720" }
117
+ before(:each) do
118
+ @client_authz_policy = KeycloakAdmin.realm(realm_name).authz_policies(client_id, type)
119
+ stub_token_client
120
+ allow_any_instance_of(RestClient::Resource).to receive(:delete).and_return 'true'
121
+ end
122
+
123
+ it "deletes an authz policy" do
124
+ response = @client_authz_policy.delete(policy_id)
125
+ expect(response).to eq true
126
+ end
127
+ end
128
+
129
+ describe '#list' do
130
+ let(:realm_name) { "valid-realm" }
131
+ let(:client_id) { "valid-client-id" }
132
+ let(:type) { :role }
133
+ before(:each) do
134
+ @client_authz_policy = KeycloakAdmin.realm(realm_name).authz_policies(client_id, type)
135
+ stub_token_client
136
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"id":"234f6f33-ef03-4f3f-a8c0-ad7bca27b720","name":"policy name","description":"policy description","type":"role","logic":"POSITIVE","decisionStrategy":"UNANIMOUS","config":{"roles":"[{\"id\":\"1d305dbe-6379-4900-8e63-96541006160a\",\"required\":false}]"}}]'
137
+ end
138
+
139
+ it "returns list of authz policies" do
140
+ response = @client_authz_policy.list
141
+ expect(response.size).to eq 1
142
+ expect(response[0].id).to eq "234f6f33-ef03-4f3f-a8c0-ad7bca27b720"
143
+ expect(response[0].name).to eq "policy name"
144
+ expect(response[0].type).to eq "role"
145
+ expect(response[0].logic).to eq "POSITIVE"
146
+ expect(response[0].decision_strategy).to eq "UNANIMOUS"
147
+ expect(response[0].config.roles[0].id).to eq "1d305dbe-6379-4900-8e63-96541006160a"
148
+ end
149
+ end
150
+
151
+ describe '#authz_policy_url' do
152
+ let(:realm_name) { "valid-realm" }
153
+ let(:client_id) { "valid-client-id" }
154
+ let(:type) { :role }
155
+ let(:client_authz_policy){ KeycloakAdmin.realm(realm_name).authz_policies(client_id, type) }
156
+
157
+ context 'when policy_id is nil' do
158
+ let(:policy_id) { nil }
159
+ it "return a proper url" do
160
+ expect(client_authz_policy.authz_policy_url(client_id, type, policy_id)).to eq "http://auth.service.io/auth/admin/realms/valid-realm/clients/valid-client-id/authz/resource-server/policy/role?permission=false"
161
+ end
162
+ end
163
+ context 'when policy_id is not nil' do
164
+ let(:policy_id) { "234f6f33-ef03-4f3f-a8c0-ad7bca27b720" }
165
+ it "return a proper url" do
166
+ expect(client_authz_policy.authz_policy_url(client_id, type, policy_id)).to eq "http://auth.service.io/auth/admin/realms/valid-realm/clients/valid-client-id/authz/resource-server/policy/role/234f6f33-ef03-4f3f-a8c0-ad7bca27b720"
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,150 @@
1
+ RSpec.describe KeycloakAdmin::ClientAuthzResourceClient 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 { @realm.attack_detections }.to_not raise_error
12
+ end
13
+ end
14
+ context "when realm_name is not defined" do
15
+ it "raise argument error" do
16
+ expect { @realm.attack_detections }.to raise_error(ArgumentError)
17
+ end
18
+ end
19
+ end
20
+
21
+ describe "#list" do
22
+ let(:realm_name) { "valid-realm" }
23
+ let(:client_id) { "valid-client-id" }
24
+ before(:each) do
25
+ @client_authz_resource = KeycloakAdmin.realm(realm_name).authz_resources(client_id)
26
+ stub_token_client
27
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"name":"Default Resource","type":"urn:delme-client-id:resources:default","owner":{"id":"d259b451-371b-432a-a526-3508f3a36f3b","name":"delme-client-id"},"ownerManagedAccess":false,"_id":"94643fe2-1973-4a36-8e1f-830ade186398","uris":["/*"]},{"name":"asdfasdf","type":"urn:delme-client-id:resources:default","owner":{"id":"d259b451-371b-432a-a526-3508f3a36f3b","name":"delme-client-id"},"ownerManagedAccess":true,"displayName":"asdfasdfasdfa","_id":"385966a2-14b9-4cc4-9539-5f2fe1008222","uris":["/*"],"icon_uri":"http://icon"}]'
28
+ end
29
+
30
+ it "returns list of authz scopes" do
31
+ response = @client_authz_resource.list
32
+ expect(response.size).to eq 2
33
+ expect(response[1].id).to eq "385966a2-14b9-4cc4-9539-5f2fe1008222"
34
+ expect(response[1].name).to eq "asdfasdf"
35
+ expect(response[1].type).to eq "urn:delme-client-id:resources:default"
36
+ expect(response[1].owner_managed_access).to be_truthy
37
+ end
38
+ end
39
+
40
+ describe '#get' do
41
+ let(:realm_name) { "valid-realm" }
42
+ let(:client_id) { "valid-client-id" }
43
+ let(:resource_id) { "valid-resource-id" }
44
+ let(:client_authz_resource) { KeycloakAdmin.realm(realm_name).authz_resources(client_id) }
45
+ before(:each) do
46
+ stub_token_client
47
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '{"name":"Default Resource","type":"urn:delme-client-id:resources:default","owner":{"id":"d259b451-371b-432a-a526-3508f3a36f3b","name":"delme-client-id"},"ownerManagedAccess":false,"_id":"94643fe2-1973-4a36-8e1f-830ade186398","uris":["/*"]}'
48
+ end
49
+
50
+ it "returns authz scope" do
51
+ response = client_authz_resource.get(resource_id)
52
+ expect(response.id).to eq "94643fe2-1973-4a36-8e1f-830ade186398"
53
+ expect(response.name).to eq "Default Resource"
54
+ expect(response.type).to eq "urn:delme-client-id:resources:default"
55
+ expect(response.owner_managed_access).to be_falsey
56
+ end
57
+ end
58
+
59
+ describe '#update' do
60
+ let(:realm_name) { "valid-realm" }
61
+ let(:client_id) { "valid-client-id" }
62
+ let(:resource_id) { "valid-resource-id" }
63
+ let(:client_authz_resource) { KeycloakAdmin.realm(realm_name).authz_resources(client_id) }
64
+ before(:each) do
65
+ stub_token_client
66
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '{"name":"Default Resource","type":"urn:delme-client-id:resources:default","owner":{"id":"d259b451-371b-432a-a526-3508f3a36f3b","name":"delme-client-id"},"ownerManagedAccess":false,"_id":"94643fe2-1973-4a36-8e1f-830ade186398","uris":["/*"]}'
67
+ allow_any_instance_of(RestClient::Resource).to receive(:put).and_return '{"name":"Default Resource","type":"urn:delme-client-id:resources:default","owner":{"id":"d259b451-371b-432a-a526-3508f3a36f3b","name":"delme-client-id"},"ownerManagedAccess":false,"_id":"94643fe2-1973-4a36-8e1f-830ade186398","uris":["/*"]}'
68
+ end
69
+
70
+ it "returns updated authz scope" do
71
+ response = client_authz_resource.update(resource_id, {name: "Default Resource", type: "urn:delme-client-id:resources:default", uris: ["/tmp/*"], owner_managed_access: false, display_name: "Default Resource", scopes: []})
72
+ expect(response.id).to eq "94643fe2-1973-4a36-8e1f-830ade186398"
73
+ expect(response.name).to eq "Default Resource"
74
+ expect(response.type).to eq "urn:delme-client-id:resources:default"
75
+ expect(response.owner_managed_access).to be_falsey
76
+ end
77
+ end
78
+
79
+ describe '#create!' do
80
+ let(:realm_name) { "valid-realm" }
81
+ let(:client_id) { "valid-client-id" }
82
+ before(:each) do
83
+ @client_authz_resource = KeycloakAdmin.realm(realm_name).authz_resources(client_id)
84
+ stub_token_client
85
+ allow_any_instance_of(RestClient::Resource).to receive(:post).and_return '{"name":"Default Resource","type":"urn:delme-client-id:resources:default","owner":{"id":"d259b451-371b-432a-a526-3508f3a36f3b","name":"delme-client-id"},"ownerManagedAccess":false,"_id":"94643fe2-1973-4a36-8e1f-830ade186398","uris":["/*"]}'
86
+ end
87
+
88
+ it "returns created authz scope" do
89
+ response = @client_authz_resource.create!("Default Resource", "urn:delme-client-id:resources:default", ["/tmp/*"], false, "Default Resource", [])
90
+ expect(response.id).to eq "94643fe2-1973-4a36-8e1f-830ade186398"
91
+ expect(response.name).to eq "Default Resource"
92
+ expect(response.type).to eq "urn:delme-client-id:resources:default"
93
+ expect(response.owner_managed_access).to be_falsey
94
+ end
95
+ end
96
+
97
+ describe "#find_by" do
98
+ let(:realm_name) { "valid-realm" }
99
+ let(:client_id) { "valid-client-id" }
100
+ let(:resource_id) { "valid-resource-id" }
101
+ let(:client_authz_resource) { KeycloakAdmin.realm(realm_name).authz_resources(client_id) }
102
+ before(:each) do
103
+ stub_token_client
104
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"name":"Default Resource","type":"urn:delme-client-id:resources:default","owner":{"id":"d259b451-371b-432a-a526-3508f3a36f3b","name":"delme-client-id"},"ownerManagedAccess":false,"_id":"94643fe2-1973-4a36-8e1f-830ade186398","uris":["/*"]}]'
105
+ end
106
+
107
+ it "returns list of authz scopes" do
108
+ response = client_authz_resource.find_by("Default Resource", "urn:delme-client-id:resources:default", ["/tmp/*"], false, "")
109
+ expect(response.size).to eq 1
110
+ expect(response[0].id).to eq "94643fe2-1973-4a36-8e1f-830ade186398"
111
+ expect(response[0].name).to eq "Default Resource"
112
+ expect(response[0].type).to eq "urn:delme-client-id:resources:default"
113
+ expect(response[0].owner_managed_access).to be_falsey
114
+ end
115
+ end
116
+
117
+ describe "#authz_scopes_url" do
118
+ let(:realm_name) { "valid-realm" }
119
+ let(:client_id) { "valid-client-id" }
120
+ let(:client_authz_resource){ KeycloakAdmin.realm(realm_name).authz_resources(client_id) }
121
+
122
+ context 'when resource_id is nil' do
123
+ it "return a proper url" do
124
+ expect(client_authz_resource.authz_resources_url(client_id)).to eq "http://auth.service.io/auth/admin/realms/valid-realm/clients/valid-client-id/authz/resource-server/resource"
125
+ end
126
+ end
127
+
128
+ context 'when resource_id is not nil' do
129
+ it "return a proper url" do
130
+ expect(client_authz_resource.authz_resources_url(client_id, "resource-id")).to eq "http://auth.service.io/auth/admin/realms/valid-realm/clients/valid-client-id/authz/resource-server/resource/resource-id"
131
+ end
132
+ end
133
+ end
134
+
135
+ describe "#delete" do
136
+ let(:realm_name) { "valid-realm" }
137
+ let(:client_id) { "valid-client-id" }
138
+ let(:resource_id) { "valid-resource-id" }
139
+ let(:client_authz_resource) { KeycloakAdmin.realm(realm_name).authz_resources(client_id) }
140
+ before(:each) do
141
+ stub_token_client
142
+ allow_any_instance_of(RestClient::Resource).to receive(:delete).and_return '{}'
143
+ end
144
+
145
+ it "returns true" do
146
+ response = client_authz_resource.delete(resource_id)
147
+ expect(response).to be_truthy
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,134 @@
1
+ RSpec.describe KeycloakAdmin::ClientAuthzScopeClient 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 { @realm.attack_detections }.to_not raise_error
12
+ end
13
+ end
14
+ context "when realm_name is not defined" do
15
+ it "raise argument error" do
16
+ expect { @realm.attack_detections }.to raise_error(ArgumentError)
17
+ end
18
+ end
19
+ end
20
+
21
+ describe "#create" do
22
+ let(:realm_name) { "valid-realm" }
23
+ let(:client_id) { "valid-client-id" }
24
+ before(:each) do
25
+ @client_authz_scope = KeycloakAdmin.realm(realm_name).authz_scopes(client_id)
26
+ stub_token_client
27
+ allow_any_instance_of(RestClient::Resource).to receive(:post).and_return '{"id":"c0779ce3-0900-4ea3-b1d6-b23e1f19c662","name":"GET","iconUri":"http://asdfasd1","displayName":"GET authz scope"}'
28
+ end
29
+
30
+ it "returns created authz scope" do
31
+ response = @client_authz_scope.create!("GET", "GET authz scope", "http://asdfasd1")
32
+ expect(response.id).to eq "c0779ce3-0900-4ea3-b1d6-b23e1f19c662"
33
+ expect(response.name).to eq "GET"
34
+ expect(response.display_name).to eq "GET authz scope"
35
+ expect(response.icon_uri).to eq "http://asdfasd1"
36
+ end
37
+ end
38
+
39
+ describe "#list" do
40
+ let(:realm_name) { "valid-realm" }
41
+ let(:client_id) { "valid-client-id" }
42
+ before(:each) do
43
+ @client_authz_scope = KeycloakAdmin.realm(realm_name).authz_scopes(client_id)
44
+ stub_token_client
45
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"id":"c0779ce3-0900-4ea3-b1d6-b23e1f19c662","name":"GET","iconUri":"http://asdfasd1","displayName":"GET authz scope"},{"id":"d0779ce3-0900-4ea3-b1d6-b23e1f19c662","name":"POST","iconUri":"http://asdfasd2","displayName":"POST authz scope"}]'
46
+ end
47
+
48
+ it "returns list of authz scopes" do
49
+ response = @client_authz_scope.list
50
+ expect(response.size).to eq 2
51
+ expect(response.first.id).to eq "c0779ce3-0900-4ea3-b1d6-b23e1f19c662"
52
+ expect(response.first.name).to eq "GET"
53
+ expect(response.first.display_name).to eq "GET authz scope"
54
+ expect(response.first.icon_uri).to eq "http://asdfasd1"
55
+ end
56
+ end
57
+
58
+
59
+ describe "#delete" do
60
+ let(:realm_name) { "valid-realm" }
61
+ let(:client_id) { "valid-client-id" }
62
+ let(:scope_id) { "valid-scope-id" }
63
+ before(:each) do
64
+ @client_authz_scope = KeycloakAdmin.realm(realm_name).authz_scopes(client_id)
65
+ stub_token_client
66
+ allow_any_instance_of(RestClient::Resource).to receive(:delete).and_return ""
67
+ end
68
+
69
+ it "returns true" do
70
+ response = @client_authz_scope.delete(scope_id)
71
+ expect(response).to eq true
72
+ end
73
+ end
74
+
75
+ describe '#get' do
76
+ let(:realm_name) { "valid-realm" }
77
+ let(:client_id) { "valid-client-id" }
78
+ let(:scope_id) { "valid-scope-id" }
79
+ before(:each) do
80
+ @client_authz_scope = KeycloakAdmin.realm(realm_name).authz_scopes(client_id)
81
+ stub_token_client
82
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '{"id":"c0779ce3-0900-4ea3-b1d6-b23e1f19c662","name":"GET","iconUri":"http://asdfasd1","displayName":"GET authz scope"}'
83
+ end
84
+
85
+ it "returns authz scope" do
86
+ response = @client_authz_scope.get(scope_id)
87
+ expect(response.id).to eq "c0779ce3-0900-4ea3-b1d6-b23e1f19c662"
88
+ expect(response.name).to eq "GET"
89
+ expect(response.display_name).to eq "GET authz scope"
90
+ expect(response.icon_uri).to eq "http://asdfasd1"
91
+ end
92
+ end
93
+
94
+ describe '#search' do
95
+ let(:realm_name) { "valid-realm" }
96
+ let(:client_id) { "valid-client-id" }
97
+ let(:name) { "GET" }
98
+ before(:each) do
99
+ @client_authz_scope = KeycloakAdmin.realm(realm_name).authz_scopes(client_id)
100
+ stub_token_client
101
+ allow_any_instance_of(RestClient::Resource).to receive(:get).and_return '[{"id":"c0779ce3-0900-4ea3-b1d6-b23e1f19c662","name":"GET","iconUri":"http://asdfasd1","displayName":"GET authz scope"}]'
102
+ end
103
+
104
+ it "returns list of authz scopes" do
105
+ response = @client_authz_scope.search(name)
106
+ expect(response.size).to eq 1
107
+ expect(response.first.id).to eq "c0779ce3-0900-4ea3-b1d6-b23e1f19c662"
108
+ expect(response.first.name).to eq "GET"
109
+ expect(response.first.display_name).to eq "GET authz scope"
110
+ expect(response.first.icon_uri).to eq "http://asdfasd1"
111
+ end
112
+ end
113
+
114
+ describe "#authz_scopes_url" do
115
+ let(:realm_name) { "valid-realm" }
116
+ let(:client_id) { "valid-client-id" }
117
+ let(:client_authz_scope) { KeycloakAdmin.realm(realm_name).authz_scopes(client_id) }
118
+ context 'resource_id is nil and id is nil' do
119
+ it "return a proper url" do
120
+ expect(client_authz_scope.authz_scopes_url(client_id)).to eq "http://auth.service.io/auth/admin/realms/valid-realm/clients/valid-client-id/authz/resource-server/scope"
121
+ end
122
+ end
123
+ context 'resource_id is not nil' do
124
+ it "return a proper url" do
125
+ expect(client_authz_scope.authz_scopes_url(client_id, "valid-resource-id")).to eq "http://auth.service.io/auth/admin/realms/valid-realm/clients/valid-client-id/authz/resource-server/resource/valid-resource-id/scopes"
126
+ end
127
+ end
128
+ context 'resource_id is nil and id is not nil' do
129
+ it "return a proper url" do
130
+ expect(client_authz_scope.authz_scopes_url(client_id, nil, "valid-id")).to eq "http://auth.service.io/auth/admin/realms/valid-realm/clients/valid-client-id/authz/resource-server/scope/valid-id"
131
+ end
132
+ end
133
+ end
134
+ end
@@ -82,7 +82,7 @@ RSpec.describe KeycloakAdmin::ClientClient do
82
82
  end
83
83
 
84
84
  it "passes rest client options" do
85
- rest_client_options = {verify_ssl: OpenSSL::SSL::VERIFY_NONE}
85
+ rest_client_options = {timeout: 10}
86
86
  allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
87
87
 
88
88
  expect(RestClient::Resource).to receive(:new).with(
@@ -123,7 +123,7 @@ RSpec.describe KeycloakAdmin::ClientClient do
123
123
  end
124
124
 
125
125
  it "passes rest client options" do
126
- rest_client_options = {verify_ssl: OpenSSL::SSL::VERIFY_NONE}
126
+ rest_client_options = {timeout: 10}
127
127
  allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
128
128
  expect(RestClient::Resource).to receive(:new).with(
129
129
  "http://auth.service.io/auth/admin/realms/valid-realm/clients/95b45037-3980-404c-ba12-784fa1baf2c2", rest_client_options).and_call_original
@@ -32,7 +32,7 @@ RSpec.describe KeycloakAdmin::ClientRoleMappingsClient do
32
32
  end
33
33
 
34
34
  it "passes rest client options" do
35
- rest_client_options = {verify_ssl: OpenSSL::SSL::VERIFY_NONE}
35
+ rest_client_options = {timeout: 10}
36
36
  allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
37
37
 
38
38
  expect(RestClient::Resource).to receive(:new).with(
@@ -70,7 +70,7 @@ RSpec.describe KeycloakAdmin::ClientRoleMappingsClient do
70
70
  end
71
71
 
72
72
  it "passes rest client options" do
73
- rest_client_options = {verify_ssl: OpenSSL::SSL::VERIFY_NONE}
73
+ rest_client_options = {timeout: 10}
74
74
  allow_any_instance_of(KeycloakAdmin::Configuration).to receive(:rest_client_options).and_return rest_client_options
75
75
 
76
76
  expect(RestClient::Resource).to receive(:new).with(