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')
|
2017-08-31 12:06:56 +08:00
|
|
|
|
|
|
|
get :query, params: {
|
|
|
|
term: 'awesome', include_blurb: true
|
|
|
|
}, format: :json
|
2014-09-02 17:15:08 +08:00
|
|
|
|
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
|
2017-07-15 01:56:58 +08:00
|
|
|
|
|
|
|
it 'performs the query with a type filter' do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
my_post = Fabricate(:post, raw: "#{user.username} is a cool person")
|
2017-08-31 12:06:56 +08:00
|
|
|
|
|
|
|
get :query, params: {
|
|
|
|
term: user.username, type_filter: 'topic'
|
|
|
|
}, format: :json
|
2017-07-15 01:56:58 +08:00
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
data = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(data['posts'][0]['id']).to eq(my_post.id)
|
|
|
|
expect(data['users']).to be_blank
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
get :query, params: {
|
|
|
|
term: user.username, type_filter: 'user'
|
|
|
|
}, format: :json
|
|
|
|
|
2017-07-15 01:56:58 +08:00
|
|
|
expect(response).to be_success
|
|
|
|
data = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(data['posts']).to be_blank
|
|
|
|
expect(data['users'][0]['id']).to eq(user.id)
|
|
|
|
end
|
|
|
|
|
2017-07-26 08:51:44 +08:00
|
|
|
context 'searching by topic id' do
|
|
|
|
it 'should not be restricted by minimum search term length' do
|
|
|
|
SiteSetting.min_search_term_length = 20000
|
|
|
|
|
|
|
|
post = Fabricate(:post)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
get :query, params: {
|
2017-07-26 08:51:44 +08:00
|
|
|
term: post.topic_id,
|
|
|
|
type_filter: 'topic',
|
|
|
|
search_for_id: true
|
2017-08-31 12:06:56 +08:00
|
|
|
}, format: :json
|
2017-07-26 08:51:44 +08:00
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
data = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(data['topics'][0]['id']).to eq(post.topic_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return the right result" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
my_post = Fabricate(:post, raw: "#{user.username} is a cool person")
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
get :query, params: {
|
2017-07-26 08:51:44 +08:00
|
|
|
term: my_post.topic_id,
|
|
|
|
type_filter: 'topic',
|
|
|
|
search_for_id: true
|
2017-08-31 12:06:56 +08:00
|
|
|
}, format: :json
|
2017-07-26 08:51:44 +08:00
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
data = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(data['topics'][0]['id']).to eq(my_post.topic_id)
|
2017-07-26 05:09:13 +08:00
|
|
|
end
|
2017-07-15 01:56:58 +08:00
|
|
|
end
|
2014-09-02 17:15:08 +08:00
|
|
|
end
|
|
|
|
|
2017-07-14 01:34:31 +08:00
|
|
|
context "#query" do
|
|
|
|
it "logs the search term" do
|
|
|
|
SiteSetting.log_search_queries = true
|
2017-08-31 12:06:56 +08:00
|
|
|
get :query, params: { term: 'wookie' }, format: :json
|
2017-07-17 23:57:13 +08:00
|
|
|
|
2017-07-14 01:34:31 +08:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(SearchLog.where(term: 'wookie')).to be_present
|
2017-07-17 23:57:13 +08:00
|
|
|
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
search_log_id = json['grouped_search_result']['search_log_id']
|
|
|
|
expect(search_log_id).to be_present
|
|
|
|
|
|
|
|
log = SearchLog.where(id: search_log_id).first
|
|
|
|
expect(log).to be_present
|
|
|
|
expect(log.term).to eq('wookie')
|
2017-07-14 01:34:31 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't log when disabled" do
|
|
|
|
SiteSetting.log_search_queries = false
|
2017-08-31 12:06:56 +08:00
|
|
|
get :query, params: { term: 'wookie' }, format: :json
|
2017-07-14 01:34:31 +08:00
|
|
|
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
|
2017-08-31 12:06:56 +08:00
|
|
|
get :show, params: { q: 'bantha' }, format: :json
|
2017-07-14 01:34:31 +08:00
|
|
|
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
|
2017-08-31 12:06:56 +08:00
|
|
|
get :show, params: { q: 'bantha' }, format: :json
|
2017-07-14 01:34:31 +08:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(SearchLog.where(term: 'bantha')).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-15 01:56:58 +08:00
|
|
|
context "search context" do
|
2013-05-25 02:03:45 +08:00
|
|
|
it "raises an error with an invalid context type" do
|
2017-08-31 12:06:56 +08:00
|
|
|
expect do
|
|
|
|
get :query, params: {
|
|
|
|
term: 'test', search_context: { type: 'security', id: 'hole' }
|
|
|
|
}, format: :json
|
|
|
|
end.to raise_error(Discourse::InvalidParameters)
|
2013-05-25 02:03:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error with a missing id" do
|
2017-08-31 12:06:56 +08:00
|
|
|
expect do
|
|
|
|
get :query,
|
|
|
|
params: { term: 'test', search_context: { type: 'user' } },
|
|
|
|
format: :json
|
|
|
|
end.to raise_error(Discourse::InvalidParameters)
|
2013-05-25 02:03:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "with a user" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2013-05-25 02:03:45 +08:00
|
|
|
it "raises an error if the user can't see the context" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(user).returns(false)
|
2017-08-31 12:06:56 +08:00
|
|
|
get :query, params: {
|
|
|
|
term: 'test', search_context: { type: 'user', id: user.username }
|
|
|
|
}, format: :json
|
|
|
|
|
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
|
2017-08-31 12:06:56 +08:00
|
|
|
get :query, params: {
|
|
|
|
term: 'test', search_context: { type: 'user', id: user.username }
|
|
|
|
}, format: :json
|
|
|
|
|
2017-07-15 01:56:58 +08:00
|
|
|
expect(response).to be_success
|
2013-05-25 02:03:45 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-15 01:56:58 +08:00
|
|
|
end
|
2013-05-25 02:03:45 +08:00
|
|
|
|
2017-07-18 03:42:32 +08:00
|
|
|
context "#click" do
|
|
|
|
it "doesn't work wthout the necessary parameters" do
|
2017-08-31 12:06:56 +08:00
|
|
|
expect do
|
|
|
|
post :click, format: :json
|
|
|
|
end.to raise_error(ActionController::ParameterMissing)
|
2017-07-18 03:42:32 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't record the click for a different user" do
|
|
|
|
log_in(:user)
|
|
|
|
|
|
|
|
_, search_log_id = SearchLog.log(
|
|
|
|
term: 'kitty',
|
|
|
|
search_type: :header,
|
|
|
|
user_id: -10,
|
|
|
|
ip_address: '127.0.0.1'
|
|
|
|
)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
post :click, params: {
|
|
|
|
search_log_id: search_log_id,
|
|
|
|
search_result_id: 12345,
|
|
|
|
search_result_type: 'topic'
|
|
|
|
}
|
2017-07-18 03:42:32 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
expect(response).to be_success
|
2017-11-29 01:54:27 +08:00
|
|
|
expect(SearchLog.find(search_log_id).search_result_id).to be_blank
|
2017-07-18 03:42:32 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "records the click for a logged in user" do
|
|
|
|
user = log_in(:user)
|
|
|
|
|
|
|
|
_, search_log_id = SearchLog.log(
|
2017-11-29 01:54:27 +08:00
|
|
|
term: 'foobar',
|
2017-07-18 03:42:32 +08:00
|
|
|
search_type: :header,
|
|
|
|
user_id: user.id,
|
|
|
|
ip_address: '127.0.0.1'
|
|
|
|
)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
post :click, params: {
|
|
|
|
search_log_id: search_log_id,
|
|
|
|
search_result_id: 12345,
|
2017-11-29 01:54:27 +08:00
|
|
|
search_result_type: 'user'
|
2017-08-31 12:06:56 +08:00
|
|
|
}, format: :json
|
2017-07-18 03:42:32 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
expect(response).to be_success
|
2017-11-29 01:54:27 +08:00
|
|
|
expect(SearchLog.find(search_log_id).search_result_id).to eq(12345)
|
|
|
|
expect(SearchLog.find(search_log_id).search_result_type).to eq(SearchLog.search_result_types[:user])
|
2017-07-18 03:42:32 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "records the click for an anonymous user" do
|
2017-08-31 12:06:56 +08:00
|
|
|
request.remote_addr = '192.168.0.1';
|
2017-07-18 03:42:32 +08:00
|
|
|
|
|
|
|
_, search_log_id = SearchLog.log(
|
|
|
|
term: 'kitty',
|
|
|
|
search_type: :header,
|
|
|
|
ip_address: '192.168.0.1'
|
|
|
|
)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
post :click, params: {
|
|
|
|
search_log_id: search_log_id,
|
|
|
|
search_result_id: 22222,
|
|
|
|
search_result_type: 'topic'
|
|
|
|
}, format: :json
|
2017-07-18 03:42:32 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
expect(response).to be_success
|
2017-11-29 01:54:27 +08:00
|
|
|
expect(SearchLog.find(search_log_id).search_result_id).to eq(22222)
|
|
|
|
expect(SearchLog.find(search_log_id).search_result_type).to eq(SearchLog.search_result_types[:topic])
|
2017-07-18 03:42:32 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't record the click for a different IP" do
|
|
|
|
request.stubs(:remote_ip).returns('192.168.0.2')
|
|
|
|
|
|
|
|
_, search_log_id = SearchLog.log(
|
|
|
|
term: 'kitty',
|
|
|
|
search_type: :header,
|
|
|
|
ip_address: '192.168.0.1'
|
|
|
|
)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
post :click, params: {
|
|
|
|
search_log_id: search_log_id,
|
|
|
|
search_result_id: 22222,
|
|
|
|
search_result_type: 'topic'
|
|
|
|
}
|
2017-07-18 03:42:32 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
expect(response).to be_success
|
2017-11-29 01:54:27 +08:00
|
|
|
expect(SearchLog.find(search_log_id).search_result_id).to be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it "records the click for search result type category" do
|
|
|
|
request.remote_addr = '192.168.0.1';
|
|
|
|
|
|
|
|
_, search_log_id = SearchLog.log(
|
|
|
|
term: 'dev',
|
|
|
|
search_type: :header,
|
|
|
|
ip_address: '192.168.0.1'
|
|
|
|
)
|
|
|
|
|
|
|
|
post :click, params: {
|
|
|
|
search_log_id: search_log_id,
|
|
|
|
search_result_id: 23456,
|
|
|
|
search_result_type: 'category'
|
|
|
|
}, format: :json
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(SearchLog.find(search_log_id).search_result_id).to eq(23456)
|
|
|
|
expect(SearchLog.find(search_log_id).search_result_type).to eq(SearchLog.search_result_types[:category])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "records the click for search result type tag" do
|
|
|
|
request.remote_addr = '192.168.0.1';
|
|
|
|
tag = Fabricate(:tag, name: 'test')
|
|
|
|
|
|
|
|
_, search_log_id = SearchLog.log(
|
|
|
|
term: 'test',
|
|
|
|
search_type: :header,
|
|
|
|
ip_address: '192.168.0.1'
|
|
|
|
)
|
|
|
|
|
|
|
|
post :click, params: {
|
|
|
|
search_log_id: search_log_id,
|
|
|
|
search_result_id: tag.name,
|
|
|
|
search_result_type: 'tag'
|
|
|
|
}, format: :json
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(SearchLog.find(search_log_id).search_result_id).to eq(tag.id)
|
|
|
|
expect(SearchLog.find(search_log_id).search_result_type).to eq(SearchLog.search_result_types[:tag])
|
2017-07-18 03:42:32 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|