Skip to content

Commit

Permalink
feat: add pagination parameter validation for paginated endpoints. (#626
Browse files Browse the repository at this point in the history
)

PACT-1101
  • Loading branch information
Inksprout authored and bethesque committed Mar 22, 2024
1 parent 35434a3 commit dc1b063
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 2 deletions.
12 changes: 12 additions & 0 deletions lib/pact_broker/api/resources/branch_versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def allowed_methods
["GET", "OPTIONS"]
end

def malformed_request?
super || request.get? && validation_errors_for_schema?(schema, request.query)
end

def resource_exists?
!!branch
end
Expand All @@ -41,6 +45,14 @@ def branch
def resource_title
"Versions for branch #{branch.name} of #{branch.pacticipant.name}"
end

private

def schema
if request.get?
PactBroker::Api::Contracts::PaginationQueryParamsSchema
end
end
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions lib/pact_broker/api/resources/dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def allowed_methods
["GET", "OPTIONS"]
end

def malformed_request?
super || (request.get? && validation_errors_for_schema?(schema, request.query))
end

def to_json
decorator_class(:dashboard_decorator).new(index_items).to_json(**decorator_options)
end
Expand All @@ -34,6 +38,12 @@ def policy_name

private

def schema
if request.get?
PactBroker::Api::Contracts::PaginationQueryParamsSchema
end
end

def index_items
index_service.find_index_items_for_api(**identifier_from_path.merge(pagination_options))
end
Expand Down
16 changes: 14 additions & 2 deletions lib/pact_broker/api/resources/pacticipants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ def allowed_methods
end

def malformed_request?
super || (request.post? && validation_errors_for_schema?)
if super
true
elsif request.post? && validation_errors_for_schema?
true
elsif request.get? && validation_errors_for_schema?(schema, request.query)
true
else
false
end
end

def request_body_required?
Expand Down Expand Up @@ -68,7 +76,11 @@ def policy_name
private

def schema
PactBroker::Api::Contracts::PacticipantCreateSchema
if request.get?
PactBroker::Api::Contracts::PaginationQueryParamsSchema
else
PactBroker::Api::Contracts::PacticipantCreateSchema
end
end

def eager_load_associations
Expand Down
12 changes: 12 additions & 0 deletions lib/pact_broker/api/resources/versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def allowed_methods
["GET", "OPTIONS"]
end

def malformed_request?
super || request.get? && validation_errors_for_schema?(schema, request.query)
end

def resource_exists?
!!pacticipant
end
Expand All @@ -33,6 +37,14 @@ def versions
def policy_name
:'versions::versions'
end

private

def schema
if request.get?
PactBroker::Api::Contracts::PaginationQueryParamsSchema
end
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/lib/pact_broker/api/resources/dashboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ module Resources
expect(response_body_hash["items"].size).to eq 1
end
end

context "with invalid pagination" do
subject { get(path, { pageNumber: -1, pageSize: -1 }) }

it_behaves_like "an invalid pagination params response"
end
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/lib/pact_broker/api/resources/pacticipants_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ module Resources
and_return(pacticipants)
expect(subject.status).to eq 200
end

context "with invalid pagination params" do
let(:query) do
{
"pageSize" => "0",
"pageNumber" => "0",
}
end

it_behaves_like "an invalid pagination params response"
end
end

describe "POST" do
Expand Down
13 changes: 13 additions & 0 deletions spec/support/shared_examples_for_responses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@
end
end

shared_examples_for "an invalid pagination params response" do
let(:response_body_hash) { JSON.parse(subject.body, symbolize_names: true) }

it "returns a a 400 status code" do
expect(subject.status).to eq 400
end

it "includes the parameter validation errors" do
expect(response_body_hash[:errors].has_key?(:pageNumber)).to be_truthy
expect(response_body_hash[:errors].has_key?(:pageSize)).to be_truthy
end
end

require "rspec/expectations"

RSpec::Matchers.define :be_a_hal_json_success_response do
Expand Down

0 comments on commit dc1b063

Please sign in to comment.