Skip to content

Commit

Permalink
Fix audiences resources pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
xjunior committed May 14, 2024
1 parent 2d3cb6b commit 024e1cb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
23 changes: 17 additions & 6 deletions audiences/lib/audiences/scim/resources_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion audiences/spec/dummy/config/initializers/audiences.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
10 changes: 5 additions & 5 deletions audiences/spec/lib/audiences/scim/resources_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand All @@ -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" },
],
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 024e1cb

Please sign in to comment.