From e7e4074856c71af7e9dca640f4473886e3d29f3b Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Thu, 20 Dec 2018 21:43:14 +0530 Subject: [PATCH] FIX: raises an error if q param is empty in search page --- app/controllers/search_controller.rb | 4 ++-- spec/requests/search_controller_spec.rb | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 76a21ca6bc7..b1fc8b0f5a2 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -9,8 +9,8 @@ class SearchController < ApplicationController end def show - @search_term = params.require(:q) - raise Discourse::InvalidParameters.new(:q) if @search_term.length < SiteSetting.min_search_term_length + @search_term = params[:q] + raise Discourse::InvalidParameters.new(:q) if @search_term.present? && @search_term.length < SiteSetting.min_search_term_length search_args = { type_filter: 'topic', diff --git a/spec/requests/search_controller_spec.rb b/spec/requests/search_controller_spec.rb index 16ce9261fae..ca3e20fa848 100644 --- a/spec/requests/search_controller_spec.rb +++ b/spec/requests/search_controller_spec.rb @@ -127,6 +127,11 @@ describe SearchController do end context "#show" do + it "doesn't raise an error when search term not specified" do + get "/search" + expect(response.status).to eq(200) + end + it "raises an error when the search term length is less than required" do get "/search.json", params: { q: 'ba' } expect(response.status).to eq(400)