Skip to content

Commit

Permalink
fix(controllers): RHICOMPL-1849 limit=0
Browse files Browse the repository at this point in the history
handling of limits equal to or below zero
  • Loading branch information
xmicha82 authored and akofink committed Aug 16, 2021
1 parent 60c7ae1 commit 5824c32
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/controllers/concerns/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
# Default options and configuration for pagination
module Pagination
extend ActiveSupport::Concern

ParamType = ActionController::Parameters

included do
def pagination_limit
params[:limit].present? ? params[:limit].to_i : 10
params.permit(limit: ParamType.integer & ParamType.gt(0))[:limit] || 10
end

def pagination_offset
Expand Down
28 changes: 28 additions & 0 deletions test/controllers/concerns/metadata_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,33 @@ def authenticate
assert_match(/offset=#{Profile.canonical(false).count}/,
json_body['links']['last'])
end

should 'return invalid parameter if limit=0' do
get profiles_url, params: { limit: 0 }
assert_response 422
assert_equal('Invalid parameter: limit must be greater than 0',
json_body['errors'][0])
end

should 'return invalid parameter if limit<0' do
get profiles_url, params: { limit: -256 }
assert_response 422
assert_equal('Invalid parameter: limit must be greater than 0',
json_body['errors'][0])
end

should 'return invalid parameter if limit is float' do
get profiles_url, params: { limit: 15.12 }
assert_response 422
assert_equal('Invalid parameter: limit must be an integer',
json_body['errors'][0])
end

should 'return invalid parameter if limit is string' do
get profiles_url, params: { limit: '15.12' }
assert_response 422
assert_equal('Invalid parameter: limit must be an integer',
json_body['errors'][0])
end
end
end

0 comments on commit 5824c32

Please sign in to comment.