Skip to content

Commit

Permalink
Merge pull request #305 from projectblacklight/bad_range_limit_params
Browse files Browse the repository at this point in the history
bad query params to #range_limit action should not result in uncaught exception
  • Loading branch information
seanaery authored Dec 6, 2024
2 parents 5bbb8db + c776941 commit 1239284
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/blacklight_range_limit/range_limit_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ def add_range_limit_params(solr_params)
# range_field, range_start, range_end
def fetch_specific_range_limit(solr_params)
field_key = blacklight_params[:range_field] # what field to fetch for

unless blacklight_params[:range_start].present? && blacklight_params[:range_start].kind_of?(String) &&
blacklight_params[:range_end].present? && blacklight_params[:range_end].kind_of?(String)
raise BlacklightRangeLimit::InvalidRange
end

start = blacklight_params[:range_start].to_i
finish = blacklight_params[:range_end].to_i

Expand All @@ -61,6 +67,9 @@ def fetch_specific_range_limit(solr_params)
solr_params[:rows] = 0

return solr_params
rescue BlacklightRangeLimit::InvalidRange
# This will make Rails return a 400
raise ActionController::BadRequest, "invalid range_start (#{blacklight_params[:range_start]}) or range_end (#{blacklight_params[:range_end]})"
end

# hacky polyfill for new Blacklight behavior we need, if we don't have it yet
Expand Down
61 changes: 61 additions & 0 deletions spec/controllers/range_limit_action_method_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'spec_helper'

RSpec.describe CatalogController, type: :controller do
# Note that ActionController::BadRequest is caught by rails and turned into a 400
# response, and ActionController::RoutingError is caught by raisl and turned into 404
describe "bad params" do
let (:facet_field) { "pub_date_si" }

it "without start param present raise BadRequest " do
expect {
get :range_limit, params: {
"range_field"=> facet_field,
"range_start"=>"1931"
}
}.to raise_error(ActionController::BadRequest)
end

it "without end param raise BadRequest " do
expect {
get :range_limit, params: {
"range_field"=> facet_field,
"range_start"=>"1931"
}
}.to raise_error(ActionController::BadRequest)
end

it "without either boundary raise BadRequest" do
expect {
get :range_limit, params: {
"range_field"=> facet_field,
}
}.to raise_error(ActionController::BadRequest)
end

it "without a range_field raise RoutingError" do
expect {
get :range_limit, params: {}
}.to raise_error(ActionController::RoutingError)
end

it "with params out of order raise BadRequest" do
expect {
get :range_limit, params: {
"range_field"=> facet_field,
"range_start"=>"1940",
"range_end"=>"1930"
}
}.to raise_error(ActionController::BadRequest)
end

it "with one of the params is an array raise BadRequest" do
expect {
get :range_limit, params: {
"range_field"=> facet_field,
"range_start"=>"1931",
"range_end"=>["1940"]
}
}.to raise_error(ActionController::BadRequest)
end
end
end

0 comments on commit 1239284

Please sign in to comment.