Spec coverage for missing q
param and error conditions in api/v2/search
controller (#27842)
This commit is contained in:
parent
a8473f582d
commit
e7c340a634
2 changed files with 75 additions and 4 deletions
|
@ -8,6 +8,11 @@ class Api::V2::SearchController < Api::BaseController
|
|||
before_action -> { authorize_if_got_token! :read, :'read:search' }
|
||||
before_action :validate_search_params!
|
||||
|
||||
with_options unless: :user_signed_in? do
|
||||
before_action :query_pagination_error, if: :pagination_requested?
|
||||
before_action :remote_resolve_error, if: :remote_resolve_requested?
|
||||
end
|
||||
|
||||
def index
|
||||
@search = Search.new(search_results)
|
||||
render json: @search, serializer: REST::SearchSerializer
|
||||
|
@ -21,12 +26,22 @@ class Api::V2::SearchController < Api::BaseController
|
|||
|
||||
def validate_search_params!
|
||||
params.require(:q)
|
||||
end
|
||||
|
||||
return if user_signed_in?
|
||||
def query_pagination_error
|
||||
render json: { error: 'Search queries pagination is not supported without authentication' }, status: 401
|
||||
end
|
||||
|
||||
return render json: { error: 'Search queries pagination is not supported without authentication' }, status: 401 if params[:offset].present?
|
||||
def remote_resolve_error
|
||||
render json: { error: 'Search queries that resolve remote resources are not supported without authentication' }, status: 401
|
||||
end
|
||||
|
||||
render json: { error: 'Search queries that resolve remote resources are not supported without authentication' }, status: 401 if truthy_param?(:resolve)
|
||||
def remote_resolve_requested?
|
||||
truthy_param?(:resolve)
|
||||
end
|
||||
|
||||
def pagination_requested?
|
||||
params[:offset].present?
|
||||
end
|
||||
|
||||
def search_results
|
||||
|
@ -34,7 +49,15 @@ class Api::V2::SearchController < Api::BaseController
|
|||
params[:q],
|
||||
current_account,
|
||||
limit_param(RESULTS_LIMIT),
|
||||
search_params.merge(resolve: truthy_param?(:resolve), exclude_unreviewed: truthy_param?(:exclude_unreviewed), following: truthy_param?(:following))
|
||||
combined_search_params
|
||||
)
|
||||
end
|
||||
|
||||
def combined_search_params
|
||||
search_params.merge(
|
||||
resolve: truthy_param?(:resolve),
|
||||
exclude_unreviewed: truthy_param?(:exclude_unreviewed),
|
||||
following: truthy_param?(:following)
|
||||
)
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue