From 1762c0dadf3a6197641624be7a1709009a9ef88e Mon Sep 17 00:00:00 2001 From: Kyle Werner Date: Tue, 12 Feb 2019 16:29:55 -0500 Subject: [PATCH] Allow the create endpoint to find and update users if they are already in the system instead of throwing an error --- app/controllers/scim_rails/scim_users_controller.rb | 8 +++++++- spec/controllers/scim_rails/scim_users_controller_spec.rb | 22 +++++++++++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/app/controllers/scim_rails/scim_users_controller.rb b/app/controllers/scim_rails/scim_users_controller.rb index 2e6f96c..a42845b 100644 --- a/app/controllers/scim_rails/scim_users_controller.rb +++ b/app/controllers/scim_rails/scim_users_controller.rb @@ -27,7 +27,13 @@ module ScimRails end def create - user = @company.public_send(ScimRails.config.scim_users_scope).create!(permitted_user_params) + username_key = ScimRails.config.queryable_user_attributes[:userName] + username_create_hash = Hash.new + username_create_hash[username_key] = permitted_user_params[username_key] + user = @company + .public_send(ScimRails.config.scim_users_scope) + .find_or_create_by(username_create_hash) + user.update!(permitted_user_params) update_status(user) unless put_active_param.nil? json_scim_response(object: user, status: :created) end diff --git a/spec/controllers/scim_rails/scim_users_controller_spec.rb b/spec/controllers/scim_rails/scim_users_controller_spec.rb index 1ca7b5b..e7fe5a7 100644 --- a/spec/controllers/scim_rails/scim_users_controller_spec.rb +++ b/spec/controllers/scim_rails/scim_users_controller_spec.rb @@ -11,7 +11,7 @@ RSpec.describe ScimRails::ScimUsersController, type: :controller do context "when unauthorized" do it "returns scim+json content type" do get :index - + expect(response.content_type).to eq "application/scim+json, application/json" end @@ -37,7 +37,7 @@ RSpec.describe ScimRails::ScimUsersController, type: :controller do it "returns scim+json content type" do get :index - + expect(response.content_type).to eq "application/scim+json, application/json" end @@ -146,7 +146,7 @@ RSpec.describe ScimRails::ScimUsersController, type: :controller do context "when unauthorized" do it "returns scim+json content type" do get :show, params: { id: 1 } - + expect(response.content_type).to eq "application/scim+json, application/json" end @@ -172,7 +172,7 @@ RSpec.describe ScimRails::ScimUsersController, type: :controller do it "returns scim+json content type" do get :show, params: { id: 1 } - + expect(response.content_type).to eq "application/scim+json, application/json" end @@ -207,7 +207,7 @@ RSpec.describe ScimRails::ScimUsersController, type: :controller do context "when unauthorized" do it "returns scim+json content type" do post :create - + expect(response.content_type).to eq "application/scim+json, application/json" end @@ -305,7 +305,7 @@ RSpec.describe ScimRails::ScimUsersController, type: :controller do expect(company.users.count).to eq 0 end - it "returns 409 if user already exists" do + it "returns 201 if user already exists" do create(:user, email: "new@example.com", company: company) post :create, params: { @@ -320,7 +320,7 @@ RSpec.describe ScimRails::ScimUsersController, type: :controller do ] } - expect(response.status).to eq 409 + expect(response.status).to eq 201 expect(company.users.count).to eq 1 end @@ -355,7 +355,7 @@ RSpec.describe ScimRails::ScimUsersController, type: :controller do context "when unauthorized" do it "returns scim+json content type" do put :put_update, params: { id: 1 } - + expect(response.content_type).to eq "application/scim+json, application/json" end @@ -383,7 +383,7 @@ RSpec.describe ScimRails::ScimUsersController, type: :controller do it "returns scim+json content type" do put :put_update, params: put_params - + expect(response.content_type).to eq "application/scim+json, application/json" end @@ -450,7 +450,7 @@ RSpec.describe ScimRails::ScimUsersController, type: :controller do context "when unauthorized" do it "returns scim+json content type" do patch :patch_update, params: patch_params(id: 1) - + expect(response.content_type).to eq "application/scim+json, application/json" end @@ -478,7 +478,7 @@ RSpec.describe ScimRails::ScimUsersController, type: :controller do it "returns scim+json content type" do patch :patch_update, params: patch_params(id: 1) - + expect(response.content_type).to eq "application/scim+json, application/json" end -- libgit2 0.21.0