From da8bde4c0276c6093b1b50b7951b6fa7f34bb441 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Fri, 16 Jul 2021 10:49:57 +1000 Subject: [PATCH] chore: allow dashboard API to be paginated --- lib/pact_broker/api/resources/dashboard.rb | 5 +++- .../api/resources/pagination_methods.rb | 14 +++++++++++ lib/pact_broker/index/service.rb | 4 +-- .../api/resources/dashboard_spec.rb | 25 +++++++++++++++++-- 4 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 lib/pact_broker/api/resources/pagination_methods.rb diff --git a/lib/pact_broker/api/resources/dashboard.rb b/lib/pact_broker/api/resources/dashboard.rb index 92fe699a2..bec031c83 100644 --- a/lib/pact_broker/api/resources/dashboard.rb +++ b/lib/pact_broker/api/resources/dashboard.rb @@ -1,11 +1,14 @@ require "pact_broker/api/resources/base_resource" require "pact_broker/api/decorators/dashboard_decorator" require "pact_broker/api/decorators/dashboard_text_decorator" +require "pact_broker/api/resources/pagination_methods" module PactBroker module Api module Resources class Dashboard < BaseResource + include PaginationMethods + def content_types_provided [ ["application/hal+json", :to_json], @@ -32,7 +35,7 @@ def policy_name private def index_items - index_service.find_index_items_for_api(identifier_from_path) + index_service.find_index_items_for_api(identifier_from_path.merge(pagination_options)) end end end diff --git a/lib/pact_broker/api/resources/pagination_methods.rb b/lib/pact_broker/api/resources/pagination_methods.rb new file mode 100644 index 000000000..6e270ff18 --- /dev/null +++ b/lib/pact_broker/api/resources/pagination_methods.rb @@ -0,0 +1,14 @@ +module PactBroker + module Api + module Resources + module PaginationMethods + def pagination_options + { + page_number: request.query["pageNumber"]&.to_i, + page_size: request.query["pageSize"]&.to_i + }.compact + end + end + end + end +end diff --git a/lib/pact_broker/index/service.rb b/lib/pact_broker/index/service.rb index d31e0cb40..f17bb87c9 100644 --- a/lib/pact_broker/index/service.rb +++ b/lib/pact_broker/index/service.rb @@ -102,9 +102,9 @@ def self.consumer_version_tags(pact_publication, tags_option) end end - def self.find_index_items_for_api(consumer_name: nil, provider_name: nil, **_ignored) + def self.find_index_items_for_api(consumer_name: nil, provider_name: nil, page_number: nil, page_size: nil, **_ignored) latest_pp_ids = latest_pact_publication_ids - pact_publications = head_pact_publications(consumer_name: consumer_name, provider_name: provider_name, tags: true) + pact_publications = head_pact_publications(consumer_name: consumer_name, provider_name: provider_name, tags: true, page_number: page_number, page_size: page_size) .eager(:consumer) .eager(:provider) .eager(:pact_version) diff --git a/spec/lib/pact_broker/api/resources/dashboard_spec.rb b/spec/lib/pact_broker/api/resources/dashboard_spec.rb index 83d5a89ab..56937e29c 100644 --- a/spec/lib/pact_broker/api/resources/dashboard_spec.rb +++ b/spec/lib/pact_broker/api/resources/dashboard_spec.rb @@ -3,12 +3,33 @@ module PactBroker module Api module Resources - describe Dashboard do + before do + td.create_pact_with_verification("Foo1", "1", "Bar", "2") + .create_pact_with_verification("Foo2", "1", "Bar", "2") + .create_pact_with_verification("Foo3", "1", "Bar", "2") + .create_pact_with_verification("Foo4", "1", "Bar", "2") + end + + let(:response_body_hash) { JSON.parse(subject.body) } let(:path) { "/dashboard" } - subject { get path; last_response } + subject { get(path) } + + it { is_expected.to be_a_hal_json_success_response } + + it "returns a list of items" do + expect(response_body_hash["items"]).to be_a(Array) + end + + context "with pagination" do + subject { get(path, { pageNumber: 1, pageSize: 1 }) } + + it "only returns the items for the page" do + expect(response_body_hash["items"].size).to eq 1 + end + end end end end