diff --git a/audiences/lib/audiences/scim/resources_query.rb b/audiences/lib/audiences/scim/resources_query.rb index 739b4992..945566ee 100644 --- a/audiences/lib/audiences/scim/resources_query.rb +++ b/audiences/lib/audiences/scim/resources_query.rb @@ -31,19 +31,30 @@ def resources end def next_page? - start_index = response.fetch("startIndex", 1) - per_page = response["itemsPerPage"].to_i - total_results = response["totalResults"].to_i - start_index + per_page <= total_results end + def start_index + response.fetch("startIndex", 1) + end + + def per_page + response["itemsPerPage"].to_i + end + + def total_results + response["totalResults"].to_i + end + + def next_index + start_index + per_page + end + def next_page return unless next_page? - current_page = @query_options.fetch(:page, 1) ResourcesQuery.new(@client, resource_type: @resource_type, **@query_options, - page: current_page + 1) + startIndex: next_index) end private diff --git a/audiences/spec/dummy/config/initializers/audiences.rb b/audiences/spec/dummy/config/initializers/audiences.rb index 1361032d..a8523ea4 100644 --- a/audiences/spec/dummy/config/initializers/audiences.rb +++ b/audiences/spec/dummy/config/initializers/audiences.rb @@ -2,7 +2,7 @@ Audiences::Scim.client = Audiences::Scim::Client.new( uri: ENV.fetch("SCIM_V2_API", "http://example.com/scim/v2/"), - headers: { "Authorization" => "Bearer 123456789" } + headers: { "Authorization" => ENV.fetch("SCIM_AUTHORIZATION", "Bearer 123456789") } ) Audiences::Scim.defaults[:Users] = { attributes: "id,displayName,photos" } diff --git a/audiences/spec/lib/audiences/scim/resources_query_spec.rb b/audiences/spec/lib/audiences/scim/resources_query_spec.rb index 0d227b73..ab29e6d2 100644 --- a/audiences/spec/lib/audiences/scim/resources_query_spec.rb +++ b/audiences/spec/lib/audiences/scim/resources_query_spec.rb @@ -40,7 +40,7 @@ ) allow(client).to( receive(:perform_request) - .with(method: :Get, path: :Users, query: { page: 2 }) + .with(method: :Get, path: :Users, query: { startIndex: 3 }) .and_return("Resources" => [ { "id" => 333, "displayName" => "John Doe the 3rd" }, { "id" => 444, "displayName" => "John Doe the 4th" }, @@ -51,7 +51,7 @@ ) allow(client).to( receive(:perform_request) - .with(method: :Get, path: :Users, query: { page: 3 }) + .with(method: :Get, path: :Users, query: { startIndex: 5 }) .and_return("Resources" => [ { "id" => 555, "displayName" => "John Doe the 5th" }, ], @@ -139,19 +139,19 @@ query = Audiences::Scim::ResourcesQuery.new(client, resource_type: :Users, filters: "displayName eq John") next_page = query.next_page - expect(next_page.query_options.delete(:page)).to eql 2 + expect(next_page.query_options.delete(:startIndex)).to eql 26 expect(query.query_options).to eql(next_page.query_options) end it "is nil when there is no next page" do allow(client).to( receive(:perform_request) - .with(method: :Get, path: :Users, query: { page: 2 }) + .with(method: :Get, path: :Users, query: { startIndex: 26 }) .and_return("totalResults" => 40, "startIndex" => 26, "itemsPerPage" => 25) ) - query = Audiences::Scim::ResourcesQuery.new(client, resource_type: :Users, page: 2) + query = Audiences::Scim::ResourcesQuery.new(client, resource_type: :Users, startIndex: 26) expect(query.next_page?).to be false expect(query.next_page).to be_nil