2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
describe SearchController do
|
|
|
|
|
2014-09-02 17:15:08 +08:00
|
|
|
context "integration" do
|
|
|
|
before do
|
2016-12-22 10:13:14 +08:00
|
|
|
SearchIndexer.enable
|
2014-09-02 17:15:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can search correctly" do
|
|
|
|
my_post = Fabricate(:post, raw: 'this is my really awesome post')
|
|
|
|
xhr :get, :query, term: 'awesome', include_blurb: true
|
|
|
|
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-09-02 17:15:08 +08:00
|
|
|
data = JSON.parse(response.body)
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(data['posts'][0]['id']).to eq(my_post.id)
|
|
|
|
expect(data['posts'][0]['blurb']).to eq('this is my really awesome post')
|
|
|
|
expect(data['topics'][0]['id']).to eq(my_post.topic_id)
|
2014-09-02 17:15:08 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-14 01:34:31 +08:00
|
|
|
context "#query" do
|
|
|
|
it "logs the search term" do
|
|
|
|
SiteSetting.log_search_queries = true
|
|
|
|
xhr :get, :query, term: 'wookie'
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(SearchLog.where(term: 'wookie')).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't log when disabled" do
|
|
|
|
SiteSetting.log_search_queries = false
|
|
|
|
xhr :get, :query, term: 'wookie'
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(SearchLog.where(term: 'wookie')).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "#show" do
|
|
|
|
it "logs the search term" do
|
|
|
|
SiteSetting.log_search_queries = true
|
|
|
|
xhr :get, :show, q: 'bantha'
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(SearchLog.where(term: 'bantha')).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't log when disabled" do
|
|
|
|
SiteSetting.log_search_queries = false
|
|
|
|
xhr :get, :show, q: 'bantha'
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(SearchLog.where(term: 'bantha')).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-02 17:15:08 +08:00
|
|
|
|
2013-05-25 02:03:45 +08:00
|
|
|
let(:search_context) { {type: 'user', id: 'eviltrout'} }
|
|
|
|
|
2017-07-14 01:34:31 +08:00
|
|
|
pending "basics" do
|
2014-08-29 03:42:29 +08:00
|
|
|
let(:guardian) { Guardian.new }
|
|
|
|
let(:search) { mock() }
|
2013-05-23 02:36:14 +08:00
|
|
|
|
2014-08-29 03:42:29 +08:00
|
|
|
before do
|
|
|
|
Guardian.stubs(:new).returns(guardian)
|
|
|
|
end
|
2013-05-23 02:36:14 +08:00
|
|
|
|
2014-08-29 03:42:29 +08:00
|
|
|
it 'performs the query' do
|
|
|
|
Search.expects(:new).with('test', guardian: guardian).returns(search)
|
|
|
|
search.expects(:execute)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-08-29 03:42:29 +08:00
|
|
|
xhr :get, :query, term: 'test'
|
|
|
|
end
|
2013-05-23 02:36:14 +08:00
|
|
|
|
2014-08-29 03:42:29 +08:00
|
|
|
it 'performs the query with a filter' do
|
|
|
|
Search.expects(:new).with('test', guardian: guardian, type_filter: 'topic').returns(search)
|
|
|
|
search.expects(:execute)
|
2013-05-23 02:36:14 +08:00
|
|
|
|
2014-08-29 03:42:29 +08:00
|
|
|
xhr :get, :query, term: 'test', type_filter: 'topic'
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-08-29 03:42:29 +08:00
|
|
|
it "performs the query and returns results including blurbs" do
|
|
|
|
Search.expects(:new).with('test', guardian: guardian, include_blurbs: true).returns(search)
|
|
|
|
search.expects(:execute)
|
2014-04-12 04:07:39 +08:00
|
|
|
|
2014-08-29 03:42:29 +08:00
|
|
|
xhr :get, :query, term: 'test', include_blurbs: 'true'
|
|
|
|
end
|
2014-04-12 04:07:39 +08:00
|
|
|
|
2014-08-29 03:42:29 +08:00
|
|
|
it 'performs the query with a filter and passes through search_for_id' do
|
|
|
|
Search.expects(:new).with('test', guardian: guardian, search_for_id: true, type_filter: 'topic').returns(search)
|
|
|
|
search.expects(:execute)
|
|
|
|
|
|
|
|
xhr :get, :query, term: 'test', type_filter: 'topic', search_for_id: true
|
|
|
|
end
|
2014-04-12 04:07:39 +08:00
|
|
|
end
|
|
|
|
|
2014-08-29 03:42:29 +08:00
|
|
|
|
2017-07-14 01:34:31 +08:00
|
|
|
pending "search context" do
|
2013-05-25 02:03:45 +08:00
|
|
|
|
|
|
|
it "raises an error with an invalid context type" do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect {
|
2013-05-25 02:03:45 +08:00
|
|
|
xhr :get, :query, term: 'test', search_context: {type: 'security', id: 'hole'}
|
2015-01-10 01:04:02 +08:00
|
|
|
}.to raise_error(Discourse::InvalidParameters)
|
2013-05-25 02:03:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error with a missing id" do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect {
|
2013-05-25 02:03:45 +08:00
|
|
|
xhr :get, :query, term: 'test', search_context: {type: 'user'}
|
2015-01-10 01:04:02 +08:00
|
|
|
}.to raise_error(Discourse::InvalidParameters)
|
2013-05-25 02:03:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "with a user" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
it "raises an error if the user can't see the context" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(user).returns(false)
|
|
|
|
xhr :get, :query, term: 'test', search_context: {type: 'user', id: user.username}
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).not_to be_success
|
2013-05-25 02:03:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'performs the query with a search context' do
|
|
|
|
guardian = Guardian.new
|
|
|
|
Guardian.stubs(:new).returns(guardian)
|
|
|
|
|
|
|
|
search = mock()
|
|
|
|
Search.expects(:new).with('test', guardian: guardian, search_context: user).returns(search)
|
|
|
|
search.expects(:execute)
|
|
|
|
|
|
|
|
xhr :get, :query, term: 'test', search_context: {type: 'user', id: user.username}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|