Commit 6995550

mokha <mokha@cisco.com>
2018-01-24 02:59:19
add delete endpoint.
1 parent 0521359
Changed files (4)
app
spec
requests
routing
app/controllers/scim/v2/users_controller.rb
@@ -11,7 +11,7 @@ module Scim
       end
 
       def show
-        user = User.find_by(uuid: params[:id])
+        user = User.find_by!(uuid: params[:id])
         response.headers['Content-Type'] = 'application/scim+json'
         response.headers['Location'] = scim_v2_users_url(user)
         render json: user.to_scim(self).to_json, status: :ok
@@ -36,6 +36,11 @@ module Scim
         render json: user.to_scim(self).to_json, status: :ok
       end
 
+      def destroy
+        user = User.find_by(uuid: params[:id])
+        user.destroy!
+      end
+
       private
 
       def user_params
app/controllers/scim/controller.rb
@@ -1,10 +1,19 @@
 module Scim
   class Controller < ApplicationController
     protect_from_forgery with: :null_session
+    rescue_from ActiveRecord::RecordNotFound, with: :not_found
 
     private
 
     def authenticate!
     end
+
+    def not_found
+      render json: {
+        schemas: [Scim::Shady::Messages::ERROR],
+        detail: "Resource #{params[:id]} not found",
+        status: "404",
+      }.to_json, status: :not_found
+    end
   end
 end
spec/requests/scim/v2/users_spec.rb
@@ -73,7 +73,7 @@ describe '/scim/v2/users' do
     end
   end
 
-  describe "PUT /scim/v1/users" do
+  describe "PUT /scim/v2/users" do
     let(:user) { create(:user) }
     let(:new_email) { FFaker::Internet.email }
 
@@ -98,4 +98,21 @@ describe '/scim/v2/users' do
       expect(json[:emails]).to match_array([value: new_email, type: 'work', primary: true])
     end
   end
+
+  describe "DELETE /scim/v2/users/:id" do
+    let(:user) { create(:user) }
+
+    it 'deletes the user' do
+      delete "/scim/v2/users/#{user.uuid}", headers: headers
+      expect(response).to have_http_status(:no_content)
+
+      get "/scim/v2/users/#{user.uuid}", headers: headers
+      expect(response).to have_http_status(:not_found)
+      expect(response.body).to be_present
+      json = JSON.parse(response.body, symbolize_names: true)
+      expect(json[:schemas]).to match_array([Scim::Shady::Messages::ERROR])
+      expect(json[:detail]).to eql("Resource #{user.uuid} not found")
+      expect(json[:status]).to eql("404")
+    end
+  end
 end
spec/routing/scim_spec.rb
@@ -2,21 +2,21 @@ require "rails_helper"
 
 describe "/scim" do
   let(:id) { SecureRandom.uuid }
-  it { expect(get: "scim/v2/users/#{id}").to route_to(controller: "scim/v2/users", action: "show", id: id, format: "json") }
-  it { expect(post: "scim/v2/users").to route_to(controller: "scim/v2/users", action: "create", format: "json") }
-  it { expect(put: "scim/v2/users/#{id}").to route_to(controller: "scim/v2/users", action: "update", id: id, format: "json") }
-  it { expect(patch: "scim/v2/users/#{id}").to route_to(controller: "scim/v2/users", action: "update", id: id, format: "json") }
-  it { expect(delete: "scim/v2/users/#{id}").to route_to(controller: "scim/v2/users", action: "destroy", id: id, format: "json") }
+  it { expect(get: "scim/v2/users/#{id}").to route_to(controller: "scim/v2/users", action: "show", id: id, format: :scim) }
+  it { expect(post: "scim/v2/users").to route_to(controller: "scim/v2/users", action: "create", format: :scim) }
+  it { expect(put: "scim/v2/users/#{id}").to route_to(controller: "scim/v2/users", action: "update", id: id, format: :scim) }
+  it { expect(patch: "scim/v2/users/#{id}").to route_to(controller: "scim/v2/users", action: "update", id: id, format: :scim) }
+  it { expect(delete: "scim/v2/users/#{id}").to route_to(controller: "scim/v2/users", action: "destroy", id: id, format: :scim) }
 
-  #it { expect(get: "scim/v2/groups/#{id}").to route_to(controller: "scim/v2/groups", action: "show", id: id, format: "json") }
-  #it { expect(post: "scim/v2/groups").to route_to(controller: "scim/v2/groups", action: "create", format: "json") }
-  #it { expect(put: "scim/v2/groups/#{id}").to route_to(controller: "scim/v2/groups", action: "update", id: id, format: "json") }
-  #it { expect(patch: "scim/v2/groups/#{id}").to route_to(controller: "scim/v2/groups", action: "update", id: id, format: "json") }
-  #it { expect(delete: "scim/v2/groups/#{id}").to route_to(controller: "scim/v2/groups", action: "destroy", id: id, format: "json") }
+  #it { expect(get: "scim/v2/groups/#{id}").to route_to(controller: "scim/v2/groups", action: "show", id: id, format: :scim) }
+  #it { expect(post: "scim/v2/groups").to route_to(controller: "scim/v2/groups", action: "create", format: :scim) }
+  #it { expect(put: "scim/v2/groups/#{id}").to route_to(controller: "scim/v2/groups", action: "update", id: id, format: :scim) }
+  #it { expect(patch: "scim/v2/groups/#{id}").to route_to(controller: "scim/v2/groups", action: "update", id: id, format: :scim) }
+  #it { expect(delete: "scim/v2/groups/#{id}").to route_to(controller: "scim/v2/groups", action: "destroy", id: id, format: :scim) }
 
   #it { expect(get: "/scim/v2/me").to route_to(controller: 'hi') }
-  it { expect(get: "scim/v2/ServiceProviderConfig").to route_to(controller: "scim/v2/service_providers", action: "index", format: "json") }
-  it { expect(get: "scim/v2/ResourceTypes").to route_to(controller: "scim/v2/resource_types", action: "index", format: "json") }
-  it { expect(get: "scim/v2/schemas").to route_to(controller: "scim/v2/schemas", action: "index", format: "json") }
-  #it { expect(post: "scim/v2/bulk").to route_to(controller: "scim/v2/bulk", action: "update", format: "json") }
+  it { expect(get: "scim/v2/ServiceProviderConfig").to route_to(controller: "scim/v2/service_providers", action: "index", format: :scim) }
+  it { expect(get: "scim/v2/ResourceTypes").to route_to(controller: "scim/v2/resource_types", action: "index", format: :scim) }
+  it { expect(get: "scim/v2/schemas").to route_to(controller: "scim/v2/schemas", action: "index", format: :scim) }
+  #it { expect(post: "scim/v2/bulk").to route_to(controller: "scim/v2/bulk", action: "update", format: :scim) }
 end