Commit ee82c56

mo <mo@mokhan.ca>
2019-05-09 20:32:41
update pagination to be 1-based
```text Table 6 describes the URL pagination parameters. +------------+----------------------------+-------------------------+ | Parameter | Description | Default | +------------+----------------------------+-------------------------+ | startIndex | The 1-based index of the | 1 | | | first query result. A | | | | value less than 1 SHALL be | | | | interpreted as 1. | | | | | | | count | Non-negative integer. | None. When specified, | | | Specifies the desired | the service provider | | | maximum number of query | MUST NOT return more | | | results per page, e.g., | results than specified, | | | 10. A negative value | although it MAY return | | | SHALL be interpreted as | fewer results. If | | | "0". A value of "0" | unspecified, the | | | indicates that no resource | maximum number of | | | results are to be returned | results is set by the | | | except for "totalResults". | service provider. | +------------+----------------------------+-------------------------+ ```
1 parent e19a625
Changed files (2)
app
controllers
spec
requests
app/controllers/scim/v2/users_controller.rb
@@ -10,10 +10,10 @@ module Scim
       end
 
       def index
-        @page = params.fetch(:startIndex, 0).to_i
+        @page = params.fetch(:startIndex, 1).to_i
         @page_size = params.fetch(:count, Scim::V2::UsersController::PAGE_SIZE).to_i
         @total = User.count
-        @users = User.offset(@page).limit(@page_size)
+        @users = @page_size >= 0 ? User.offset(@page - 1).limit(@page_size) : User.none
         render formats: :scim, status: :ok
       end
 
spec/requests/scim/v2/users_spec.rb
@@ -136,15 +136,31 @@ describe '/scim/v2/users' do
       let(:json) { JSON.parse(response.body, symbolize_names: true) }
       let!(:users) { create_list(:user, 10) }
 
-      before { get "/scim/v2/users", params: { startIndex: 1, count: 1 }, headers: headers }
+      context "when requesting the first page of results (1-based index)" do
+        before { get "/scim/v2/users", params: { startIndex: 1, count: 1 }, headers: headers }
+
+        specify { expect(response).to have_http_status(:ok) }
+        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(users.count + 1) }
+        specify { expect(json[:startIndex]).to eql(1) }
+        specify { expect(json[:itemsPerPage]).to eql(1) }
+        specify { expect(json[:Resources][0][:id]).to eql(User.offset(0).limit(1).pluck(:id).first) }
+      end
 
-      specify { expect(response).to have_http_status(:ok) }
-      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(users.count + 1) }
-      specify { expect(json[:startIndex]).to eql(1) }
-      specify { expect(json[:itemsPerPage]).to eql(1) }
+      context "when requesting a count of 0 results" do
+        before { get "/scim/v2/users", params: { count: 0 }, headers: headers }
+
+        specify { expect(response).to have_http_status(:ok) }
+        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(users.count + 1) }
+        specify { expect(json[:startIndex]).to eql(1) }
+        specify { expect(json[:itemsPerPage]).to eql(0) }
+        specify { expect(json[:Resources]).to be_empty }
+      end
     end
 
     xcontext "when fetching specific attributes" do