Commit bcb27a9

mo <mo.khan@gmail.com>
2019-06-16 03:54:43
start to implement the search controller
1 parent 5c0fbe2
Changed files (3)
app
controllers
views
scim
spec
requests
app/controllers/scim/v2/search_controller.rb
@@ -3,15 +3,22 @@
 module Scim
   module V2
     class SearchController < ::Scim::Controller
+      include Pageable
+
       def index
-        response.headers['Content-Type'] = 'application/scim+json'
-        render json: {
-          schemas: [Scim::Kit::V2::Messages::LIST_RESPONSE],
-          totalResults: 0,
-          itemsPerPage: 0,
-          startIndex: 1,
-          Resources: [],
-        }.to_json, status: :ok
+        @users = User.order(:created_at).scim_search(params[:filter])
+        @users = paginate(@users, page: page - 1, page_size: page_size)
+        render formats: :scim, status: :ok
+      end
+
+      private
+
+      def page
+        page_param(:startIndex, default: 0, bottom: 1, top: 100)
+      end
+
+      def page_size
+        page_param(:count, default: 25, bottom: 0, top: 25)
       end
     end
   end
app/views/scim/v2/search/index.scim.jbuilder
@@ -0,0 +1,11 @@
+# frozen_string_literal: true
+
+json.schemas [Scim::Kit::V2::Messages::LIST_RESPONSE]
+json.totalResults @users.total_count
+json.startIndex @users.page + 1
+json.itemsPerPage @users.page_size
+json.Resources do
+  json.array! @users do |user|
+    json.partial! user, as: :user
+  end
+end
spec/requests/scim/v2/search_spec.rb
@@ -18,7 +18,7 @@ describe '/scim/v1/.search' do
       {
         "schemas": [Scim::Kit::V2::Messages::SEARCH_REQUEST],
         "attributes": %w[displayName userName],
-        "filter": "displayName sw \"smith\"",
+        "filter": "userName sw \"#{user.email}\"",
         "startIndex": 1,
         "count": 10
       }
@@ -31,9 +31,10 @@ describe '/scim/v1/.search' do
     specify { expect(response.headers['Content-Type']).to eql('application/scim+json') }
     specify { expect(response.body).to be_present }
     specify { expect(json[:schemas]).to match_array([Scim::Kit::V2::Messages::LIST_RESPONSE]) }
-    specify { expect(json[:totalResults]).to be_zero }
-    specify { expect(json[:itemsPerPage]).to be_zero }
+    specify { expect(json[:totalResults]).to be(1) }
+    specify { expect(json[:itemsPerPage]).to be(10) }
     specify { expect(json[:startIndex]).to be(1) }
-    specify { expect(json[:Resources]).to be_empty }
+    specify { expect(json[:Resources].map { |x| x[:userName] }).to match_array([user.email]) }
+    specify { expect(json[:Resources].map { |x| x[:displayName] }).to match_array([user.email]) }
   end
 end