Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip out invalid characters from search + add tests for this #227

Merged
merged 2 commits into from
Aug 8, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ def self.index_tank

# retrieve docs from IndexTank
def self.search(query)
index_tank.search("#{query} OR title:#{query}", :fetch => 'timestamp,url,text,title', :snippet => 'text')
query = query.gsub(/\W+/,' ').strip

if query.present?
query << "*"
index_tank.search("#{query} OR title:#{query}", :fetch => 'timestamp,url,text,title', :snippet => 'text')
end
end

def index
Expand Down
36 changes: 36 additions & 0 deletions spec/requests/search_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "spec_helper"

describe "Search" do
context "When the user searches for" do
before(:each) do
visit '/'
@user = FactoryGirl.create(:student_sam)
login_with_oauth @user
click_button('searchSubmit')
end

it "nothing - page should render" do
current_path.should == search_index_path
end

it "valid string - page should show the results" do
fill_in "query", :with => "Search String"
click_button('searchSubmit')
page.should have_content('Your search for "Search String" returned 0 results')
end

describe "invalid string" do
it "like '-+_' - the page should not tank" do
fill_in "query", :with => "-+_"
click_button('searchSubmit')
current_path.should == search_index_path
page.should have_content('Search for: ')
end
it "like '-%20%3C/p%3E%20-' - the page should not tank" do
fill_in "query", :with => "-%20%3C/p%3E%20-"
click_button('searchSubmit')
page.should have_content('Your search for "-%20%3C/p%3E%20-" returned 0 results')
end
end
end
end