Skip to content

Commit 3bd9fca

Browse files
committed
resourcetypes endpoints
1 parent 166b9b8 commit 3bd9fca

File tree

6 files changed

+172
-17
lines changed

6 files changed

+172
-17
lines changed

app/controllers/concerns/scim_rails/response.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def json_scim_response(object:, status: :ok, counts: nil)
2222
return
2323
when "configuration"
2424
response = ScimRails.config.config_schema
25+
when "resource_user"
26+
response = ScimRails.config.resource_user_schema
27+
when "resource_group"
28+
response = ScimRails.config.resource_group_schema
2529
end
2630

2731
render \
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module ScimRails
2+
class ScimResourceController < ScimRails::ApplicationController
3+
def resource_user
4+
ScimRails.config.before_scim_response.call(request.params) unless ScimRails.config.before_scim_response.nil?
5+
6+
ScimRails.config.after_scim_response.call(ScimRails.config.resource_user_schema, "RETRIEVED") unless ScimRails.config.after_scim_response.nil?
7+
8+
json_scim_response(object: nil)
9+
end
10+
11+
def resource_group
12+
ScimRails.config.before_scim_response.call(request.params) unless ScimRails.config.before_scim_response.nil?
13+
14+
ScimRails.config.after_scim_response.call(ScimRails.config.resource_group_schema, "RETRIEVED") unless ScimRails.config.after_scim_response.nil?
15+
16+
json_scim_response(object: nil)
17+
end
18+
end
19+
end

config/routes.rb

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
ScimRails::Engine.routes.draw do
2-
get 'scim/v2/Users', action: :index, controller: 'scim_users'
3-
post 'scim/v2/Users', action: :create, controller: 'scim_users'
4-
get 'scim/v2/Users/:id', action: :show, controller: 'scim_users'
5-
put 'scim/v2/Users/:id', action: :put_update, controller: 'scim_users'
6-
patch 'scim/v2/Users/:id', action: :patch_update, controller: 'scim_users'
7-
delete 'scim/v2/Users/:id', action: :delete, controller: 'scim_users'
8-
9-
get 'scim/v2/Groups', action: :index, controller: 'scim_groups'
10-
post 'scim/v2/Groups', action: :create, controller: 'scim_groups'
11-
get 'scim/v2/Groups/:id', action: :show, controller: 'scim_groups'
12-
put 'scim/v2/Groups/:id', action: :put_update, controller: 'scim_groups'
13-
patch 'scim/v2/Groups/:id', action: :patch_update, controller: 'scim_groups'
14-
delete 'scim/v2/Groups/:id', action: :delete, controller: 'scim_groups'
15-
16-
get 'scim/v2/ServiceProviderConfig', action: :configuration, controller: 'scim_service'
17-
get 'scim/v2/ServiceProviderConfigs', action: :configuration, controller: 'scim_service'
2+
get 'scim/v2/Users', action: :index, controller: 'scim_users'
3+
post 'scim/v2/Users', action: :create, controller: 'scim_users'
4+
get 'scim/v2/Users/:id', action: :show, controller: 'scim_users'
5+
put 'scim/v2/Users/:id', action: :put_update, controller: 'scim_users'
6+
patch 'scim/v2/Users/:id', action: :patch_update, controller: 'scim_users'
7+
delete 'scim/v2/Users/:id', action: :delete, controller: 'scim_users'
8+
9+
get 'scim/v2/Groups', action: :index, controller: 'scim_groups'
10+
post 'scim/v2/Groups', action: :create, controller: 'scim_groups'
11+
get 'scim/v2/Groups/:id', action: :show, controller: 'scim_groups'
12+
put 'scim/v2/Groups/:id', action: :put_update, controller: 'scim_groups'
13+
patch 'scim/v2/Groups/:id', action: :patch_update, controller: 'scim_groups'
14+
delete 'scim/v2/Groups/:id', action: :delete, controller: 'scim_groups'
15+
16+
get 'scim/v2/ServiceProviderConfig', action: :configuration, controller: 'scim_service'
17+
get 'scim/v2/ServiceProviderConfigs', action: :configuration, controller: 'scim_service'
18+
19+
get 'scim/v2/ResourceTypes/User', action: :resource_user, controller: 'scim_resource'
20+
get 'scim/v2/ResourceTypes/Group', action: :resource_group, controller: 'scim_resource'
1821
end

lib/scim_rails/config.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ class Config
4747
:before_scim_response,
4848
:after_scim_response,
4949
:scim_attribute_type_mappings,
50-
:config_schema
50+
:config_schema,
51+
:resource_user_schema,
52+
:resource_group_schema
5153

5254
def initialize
5355
@basic_auth_model = "Company"
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
require "spec_helper"
2+
3+
RSpec.describe ScimRails::ScimResourceController, type: :controller do
4+
include AuthHelper
5+
6+
routes { ScimRails::Engine.routes }
7+
8+
describe "resource_user" do
9+
let(:company) { create(:company) }
10+
11+
context "when unauthorized" do
12+
it "returns scim+json content type" do
13+
get :resource_user
14+
15+
expect(response.content_type).to eq("application/scim+json")
16+
end
17+
18+
it "fails with no credentials" do
19+
get :resource_user
20+
21+
expect(response.status).to eq(401)
22+
end
23+
24+
it "fails with invalid credentials" do
25+
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials("unauthorized","123456")
26+
27+
get :resource_user
28+
29+
expect(response.status).to eq(401)
30+
end
31+
end
32+
33+
context "when authorized" do
34+
let(:body) { JSON.parse(response.body) }
35+
36+
before :each do
37+
http_login(company)
38+
end
39+
40+
it "returns scim+json content type" do
41+
get :resource_user
42+
43+
expect(response.content_type).to eq("application/scim+json")
44+
end
45+
46+
it "is successful with valid credentials" do
47+
get :resource_user
48+
49+
expect(response.status).to eq(200)
50+
end
51+
52+
it "successfully returns the configuration of the app" do
53+
get :resource_user
54+
55+
expect(body.deep_symbolize_keys).to eq(ScimRails.config.resource_user_schema)
56+
end
57+
end
58+
end
59+
60+
describe "resource_group" do
61+
let(:company) { create(:company) }
62+
63+
context "when unauthorized" do
64+
it "returns scim+json content type" do
65+
get :resource_group
66+
67+
expect(response.content_type).to eq("application/scim+json")
68+
end
69+
70+
it "fails with no credentials" do
71+
get :resource_group
72+
73+
expect(response.status).to eq(401)
74+
end
75+
76+
it "fails with invalid credentials" do
77+
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials("unauthorized","123456")
78+
79+
get :resource_group
80+
81+
expect(response.status).to eq(401)
82+
end
83+
end
84+
85+
context "when authorized" do
86+
let(:body) { JSON.parse(response.body) }
87+
88+
before :each do
89+
http_login(company)
90+
end
91+
92+
it "returns scim+json content type" do
93+
get :resource_group
94+
95+
expect(response.content_type).to eq("application/scim+json")
96+
end
97+
98+
it "is successful with valid credentials" do
99+
get :resource_group
100+
101+
expect(response.status).to eq(200)
102+
end
103+
104+
it "successfully returns the configuration of the app" do
105+
get :resource_group
106+
107+
expect(body.deep_symbolize_keys).to eq(ScimRails.config.resource_group_schema)
108+
end
109+
end
110+
end
111+
end

spec/support/scim_rails_config.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,20 @@
152152
}
153153
]
154154
}
155+
156+
config.resource_user_schema = {
157+
schemas: ["urn:ietf:params:scim:schemas:core:2.0:ResourceType"],
158+
id: "User",
159+
name: "User",
160+
endpoint: "/Users",
161+
schema: "urn:ietf:params:scim:schemas:core:2.0:User"
162+
}
163+
164+
config.resource_group_schema = {
165+
schemas: ["urn:ietf:params:scim:schemas:core:2.0:ResourceType"],
166+
id: "Group",
167+
name: "Group",
168+
endpoint: "/Groups",
169+
schema: "urn:ietf:params:scim:schemas:core:2.0:Group"
170+
}
155171
end

0 commit comments

Comments
 (0)