discourse/spec/components/search_spec.rb

239 lines
6.6 KiB
Ruby
Raw Normal View History

# encoding: utf-8
2013-02-05 13:16:51 -06:00
require 'spec_helper'
require 'search'
describe Search do
before do
ActiveRecord::Base.observers.enable :search_observer
end
2013-02-05 13:16:51 -06:00
def first_of_type(results, type)
return nil if results.blank?
results.each do |r|
return r[:results].first if r[:type] == type
end
nil
end
2013-02-25 10:42:20 -06:00
context 'post indexing observer' do
before do
2013-02-05 13:16:51 -06:00
@category = Fabricate(:category, name: 'america')
@topic = Fabricate(:topic, title: 'sam saffron test topic', category: @category)
2013-02-05 13:16:51 -06:00
@post = Fabricate(:post, topic: @topic, raw: 'this <b>fun test</b> <img src="bla" title="my image">')
@indexed = Topic.exec_sql("select search_data from posts_search where id = #{@post.id}").first["search_data"]
end
2013-02-25 10:42:20 -06:00
it "should include body in index" do
2013-02-05 13:16:51 -06:00
@indexed.should =~ /fun/
end
2013-02-25 10:42:20 -06:00
it "should include title in index" do
2013-02-05 13:16:51 -06:00
@indexed.should =~ /sam/
end
2013-02-25 10:42:20 -06:00
it "should include category in index" do
2013-02-05 13:16:51 -06:00
@indexed.should =~ /america/
end
2013-02-25 10:42:20 -06:00
it "should pick up on title updates" do
@topic.title = "harpi is the new title"
2013-02-05 13:16:51 -06:00
@topic.save!
@indexed = Topic.exec_sql("select search_data from posts_search where id = #{@post.id}").first["search_data"]
@indexed.should =~ /harpi/
end
end
2013-02-25 10:42:20 -06:00
context 'user indexing observer' do
before do
2013-02-05 13:16:51 -06:00
@user = Fabricate(:user, username: 'fred', name: 'bob jones')
@indexed = User.exec_sql("select search_data from users_search where id = #{@user.id}").first["search_data"]
end
2013-02-25 10:42:20 -06:00
it "should pick up on username" do
2013-02-05 13:16:51 -06:00
@indexed.should =~ /fred/
end
2013-02-25 10:42:20 -06:00
it "should pick up on name" do
2013-02-05 13:16:51 -06:00
@indexed.should =~ /jone/
end
end
2013-02-25 10:42:20 -06:00
context 'category indexing observer' do
before do
2013-02-05 13:16:51 -06:00
@category = Fabricate(:category, name: 'america')
@indexed = Topic.exec_sql("select search_data from categories_search where id = #{@category.id}").first["search_data"]
end
2013-02-25 10:42:20 -06:00
it "should pick up on name" do
2013-02-05 13:16:51 -06:00
@indexed.should =~ /america/
end
end
it 'returns something blank on a nil search' do
ActiveRecord::Base.expects(:exec_sql).never
Search.new(nil).execute.should be_blank
2013-02-05 13:16:51 -06:00
end
it 'does not search when the search term is too small' do
ActiveRecord::Base.expects(:exec_sql).never
Search.new('evil', min_search_term_length: 5).execute.should be_blank
end
2013-02-05 13:16:51 -06:00
it 'escapes non alphanumeric characters' do
Search.new('foo :!$);}]>@\#\"\'').execute.should be_blank # There are at least three levels of sanitation for Search.query!
2013-02-05 13:16:51 -06:00
end
it 'works when given two terms with spaces' do
lambda { Search.new('evil trout').execute }.should_not raise_error
2013-02-05 13:16:51 -06:00
end
context 'users' do
let!(:user) { Fabricate(:user) }
let(:result) { first_of_type( Search.new('bruce', type_filter: 'user').execute, 'user') }
2013-02-05 13:16:51 -06:00
it 'returns a result' do
result.should be_present
end
it 'has the display name as the title' do
result['title'].should == user.username
end
it 'has the avatar_template is there so it can hand it to the client' do
result['avatar_template'].should_not be_nil
end
it 'has a url for the record' do
result['url'].should == "/users/#{user.username_lower}"
end
end
context 'topics' do
2013-05-12 19:48:32 -05:00
let(:topic) { Fabricate(:topic) }
2013-02-05 13:16:51 -06:00
2013-02-25 10:42:20 -06:00
context 'searching the OP' do
let!(:post) { Fabricate(:post, topic: topic, user: topic.user) }
let(:result) { first_of_type(Search.new('hello', type_filter: 'topic').execute, 'topic') }
2013-02-05 13:16:51 -06:00
2013-05-12 19:48:32 -05:00
it 'returns a result correctly' do
2013-02-05 13:16:51 -06:00
result.should be_present
2013-05-12 19:48:32 -05:00
result['title'].should == topic.title
result['url'].should == topic.relative_url
2013-02-05 13:16:51 -06:00
end
2013-05-13 16:04:41 -05:00
end
context "search for a topic by id" do
let(:result) { first_of_type(Search.new(topic.id, type_filter: 'topic').execute, 'topic') }
2013-05-13 16:04:41 -05:00
it 'returns the topic' do
result.should be_present
result['title'].should == topic.title
result['url'].should == topic.relative_url
end
end
2013-02-05 13:16:51 -06:00
2013-05-13 16:04:41 -05:00
context "search for a topic by url" do
let(:result) { first_of_type(Search.new(topic.relative_url, type_filter: 'topic').execute, 'topic') }
2013-05-13 16:04:41 -05:00
it 'returns the topic' do
result.should be_present
result['title'].should == topic.title
result['url'].should == topic.relative_url
end
2013-05-12 19:48:32 -05:00
end
context 'security' do
let!(:post) { Fabricate(:post, topic: topic, user: topic.user) }
def result(current_user)
first_of_type(Search.new('hello', guardian: current_user).execute, 'topic')
2013-02-05 13:16:51 -06:00
end
2013-05-12 19:48:32 -05:00
it 'secures results correctly' do
category = Fabricate(:category)
topic.category_id = category.id
topic.save
category.deny(:all)
category.allow(Group[:staff])
category.save
result(nil).should_not be_present
result(Fabricate(:user)).should_not be_present
result(Fabricate(:admin)).should be_present
2013-02-25 10:42:20 -06:00
end
2013-02-05 13:16:51 -06:00
end
end
context 'cyrillic topic' do
let!(:cyrillic_topic) { Fabricate(:topic) do
user
title { sequence(:title) { |i| "Тестовая запись #{i}" } }
end
}
let!(:post) {Fabricate(:post, topic: cyrillic_topic, user: cyrillic_topic.user)}
let(:result) { first_of_type(Search.new('запись').execute, 'topic') }
it 'finds something when given cyrillic query' do
result.should be_present
end
end
2013-02-05 13:16:51 -06:00
context 'categories' do
let!(:category) { Fabricate(:category) }
def result
first_of_type(Search.new('amazing').execute, 'category')
end
2013-02-05 13:16:51 -06:00
2013-05-12 19:48:32 -05:00
it 'returns the correct result' do
r = result
r.should be_present
r['title'].should == category.name
r['url'].should == "/category/#{category.slug}"
category.deny(:all)
category.save
result.should_not be_present
2013-02-25 10:42:20 -06:00
end
2013-02-05 13:16:51 -06:00
end
context 'type_filter' do
let!(:user) { Fabricate(:user, username: 'amazing', email: 'amazing@amazing.com') }
let!(:category) { Fabricate(:category, name: 'amazing category', user: user) }
2013-02-25 10:42:20 -06:00
2013-02-05 13:16:51 -06:00
context 'user filter' do
let(:results) { Search.new('amazing', type_filter: 'user').execute }
2013-02-05 13:16:51 -06:00
it "returns a user result" do
results.detect {|r| r[:type] == 'user'}.should be_present
results.detect {|r| r[:type] == 'category'}.should be_blank
end
end
context 'category filter' do
let(:results) { Search.new('amazing', type_filter: 'category').execute }
2013-02-05 13:16:51 -06:00
2013-05-21 03:56:04 -05:00
it "returns a category result" do
2013-02-05 13:16:51 -06:00
results.detect {|r| r[:type] == 'user'}.should be_blank
results.detect {|r| r[:type] == 'category'}.should be_present
end
end
end
end