2019-04-15 10:17:05 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-09 01:56:31 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-03 18:08:28 +00:00
|
|
|
RSpec.describe SearchController do
|
2019-04-09 15:38:58 +00:00
|
|
|
include ExternalAuthorizationServiceHelpers
|
|
|
|
|
2019-01-16 12:09:29 +00:00
|
|
|
let(:user) { create(:user) }
|
2016-12-09 01:56:31 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
shared_examples_for 'when the user cannot read cross project' do |action, params|
|
2019-06-26 23:19:59 +00:00
|
|
|
before do
|
2019-07-15 17:59:57 +00:00
|
|
|
allow(Ability).to receive(:allowed?).and_call_original
|
|
|
|
allow(Ability).to receive(:allowed?)
|
|
|
|
.with(user, :read_cross_project, :global) { false }
|
2019-06-26 23:19:59 +00:00
|
|
|
end
|
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
it 'blocks access without a project_id' do
|
|
|
|
get action, params: params
|
2019-04-24 17:12:20 +00:00
|
|
|
|
2020-01-27 12:08:35 +00:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2019-04-24 17:12:20 +00:00
|
|
|
end
|
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
it 'allows access with a project_id' do
|
|
|
|
get action, params: params.merge(project_id: create(:project, :public).id)
|
2019-04-24 17:12:20 +00:00
|
|
|
|
2020-01-27 12:08:35 +00:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-04-24 17:12:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
shared_examples_for 'with external authorization service enabled' do |action, params|
|
|
|
|
let(:project) { create(:project, namespace: user.namespace) }
|
|
|
|
let(:note) { create(:note_on_issue, project: project) }
|
2019-06-27 07:00:01 +00:00
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
before do
|
|
|
|
enable_external_authorization_service_check
|
2019-06-27 07:00:01 +00:00
|
|
|
end
|
2016-12-09 01:56:31 +00:00
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
it 'renders a 403 when no project is given' do
|
|
|
|
get action, params: params
|
2016-12-09 01:56:31 +00:00
|
|
|
|
2020-01-27 12:08:35 +00:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2017-12-11 14:21:06 +00:00
|
|
|
end
|
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
it 'renders a 200 when a project was set' do
|
|
|
|
get action, params: params.merge(project_id: project.id)
|
2017-12-11 14:21:06 +00:00
|
|
|
|
2020-01-27 12:08:35 +00:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-12-11 14:21:06 +00:00
|
|
|
end
|
2019-07-15 17:59:57 +00:00
|
|
|
end
|
2017-12-11 14:21:06 +00:00
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
describe 'GET #show' do
|
|
|
|
it_behaves_like 'when the user cannot read cross project', :show, { search: 'hello' } do
|
|
|
|
it 'still allows accessing the search page' do
|
|
|
|
get :show
|
2017-12-11 14:21:06 +00:00
|
|
|
|
2020-01-27 12:08:35 +00:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-07-15 17:59:57 +00:00
|
|
|
end
|
2017-12-11 14:21:06 +00:00
|
|
|
end
|
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
it_behaves_like 'with external authorization service enabled', :show, { search: 'hello' }
|
2017-12-11 14:21:06 +00:00
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
context 'uses the right partials depending on scope' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
render_views
|
|
|
|
|
2019-10-02 00:06:26 +00:00
|
|
|
let_it_be(:project) { create(:project, :public, :repository, :wiki_repo) }
|
2017-12-11 14:21:06 +00:00
|
|
|
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
2019-07-15 17:59:57 +00:00
|
|
|
expect(::Gitlab::GitalyClient).to receive(:allow_ref_name_caching).and_call_original
|
2017-06-14 18:18:56 +00:00
|
|
|
end
|
2016-12-09 01:56:31 +00:00
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
subject { get(:show, params: { project_id: project.id, scope: scope, search: 'merge' }) }
|
2016-12-09 01:56:31 +00:00
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
where(:partial, :scope) do
|
|
|
|
'_blob' | :blobs
|
|
|
|
'_wiki_blob' | :wiki_blobs
|
|
|
|
'_commit' | :commits
|
|
|
|
end
|
2016-12-09 01:56:31 +00:00
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
with_them do
|
|
|
|
it do
|
|
|
|
project_wiki = create(:project_wiki, project: project, user: user)
|
2020-04-27 09:09:51 +00:00
|
|
|
create(:wiki_page, wiki: project_wiki, title: 'merge', content: 'merge')
|
2019-07-15 17:59:57 +00:00
|
|
|
|
|
|
|
expect(subject).to render_template("search/results/#{partial}")
|
|
|
|
end
|
2016-12-09 01:56:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
context 'global search' do
|
2019-12-26 21:07:49 +00:00
|
|
|
using RSpec::Parameterized::TableSyntax
|
2019-07-15 17:59:57 +00:00
|
|
|
render_views
|
2016-12-09 01:56:31 +00:00
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
it 'omits pipeline status from load' do
|
|
|
|
project = create(:project, :public)
|
|
|
|
expect(Gitlab::Cache::Ci::ProjectPipelineStatus).not_to receive(:load_in_batch_for_projects)
|
|
|
|
|
|
|
|
get :show, params: { scope: 'projects', search: project.name }
|
2016-12-09 01:56:31 +00:00
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
expect(assigns[:search_objects].first).to eq project
|
|
|
|
end
|
2019-12-26 21:07:49 +00:00
|
|
|
|
|
|
|
context 'check search term length' do
|
|
|
|
let(:search_queries) do
|
2020-01-14 18:08:31 +00:00
|
|
|
char_limit = SearchService::SEARCH_CHAR_LIMIT
|
|
|
|
term_limit = SearchService::SEARCH_TERM_LIMIT
|
2019-12-26 21:07:49 +00:00
|
|
|
{
|
|
|
|
chars_under_limit: ('a' * (char_limit - 1)),
|
|
|
|
chars_over_limit: ('a' * (char_limit + 1)),
|
|
|
|
terms_under_limit: ('abc ' * (term_limit - 1)),
|
|
|
|
terms_over_limit: ('abc ' * (term_limit + 1))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-01-14 18:08:31 +00:00
|
|
|
where(:string_name, :expectation) do
|
|
|
|
:chars_under_limit | :not_to_set_flash
|
|
|
|
:chars_over_limit | :set_chars_flash
|
|
|
|
:terms_under_limit | :not_to_set_flash
|
|
|
|
:terms_over_limit | :set_terms_flash
|
2019-12-26 21:07:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it do
|
|
|
|
get :show, params: { scope: 'projects', search: search_queries[string_name] }
|
|
|
|
|
|
|
|
case expectation
|
|
|
|
when :not_to_set_flash
|
|
|
|
expect(controller).not_to set_flash[:alert]
|
|
|
|
when :set_chars_flash
|
|
|
|
expect(controller).to set_flash[:alert].to(/characters/)
|
|
|
|
when :set_terms_flash
|
|
|
|
expect(controller).to set_flash[:alert].to(/terms/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-12-09 01:56:31 +00:00
|
|
|
end
|
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
it 'finds issue comments' do
|
|
|
|
project = create(:project, :public)
|
|
|
|
note = create(:note_on_issue, project: project)
|
2016-12-09 01:56:31 +00:00
|
|
|
|
2018-12-17 22:52:17 +00:00
|
|
|
get :show, params: { project_id: project.id, scope: 'notes', search: note.note }
|
2016-12-09 01:56:31 +00:00
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
expect(assigns[:search_objects].first).to eq note
|
2016-12-09 01:56:31 +00:00
|
|
|
end
|
2019-04-09 15:38:58 +00:00
|
|
|
|
2020-08-28 12:10:37 +00:00
|
|
|
it_behaves_like 'tracking unique hll events', :show do
|
|
|
|
let(:request_params) { { scope: 'projects', search: 'term' } }
|
|
|
|
let(:target_id) { 'i_search_total' }
|
|
|
|
end
|
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
context 'on restricted projects' do
|
|
|
|
context 'when signed out' do
|
|
|
|
before do
|
|
|
|
sign_out(user)
|
|
|
|
end
|
2019-04-09 15:38:58 +00:00
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
it "doesn't expose comments on issues" do
|
|
|
|
project = create(:project, :public, :issues_private)
|
|
|
|
note = create(:note_on_issue, project: project)
|
2019-04-09 15:38:58 +00:00
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
get :show, params: { project_id: project.id, scope: 'notes', search: note.note }
|
2019-04-09 15:38:58 +00:00
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
expect(assigns[:search_objects].count).to eq(0)
|
|
|
|
end
|
2019-04-09 15:38:58 +00:00
|
|
|
end
|
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
it "doesn't expose comments on merge_requests" do
|
|
|
|
project = create(:project, :public, :merge_requests_private)
|
|
|
|
note = create(:note_on_merge_request, project: project)
|
|
|
|
|
2019-04-09 15:38:58 +00:00
|
|
|
get :show, params: { project_id: project.id, scope: 'notes', search: note.note }
|
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
expect(assigns[:search_objects].count).to eq(0)
|
2019-04-09 15:38:58 +00:00
|
|
|
end
|
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
it "doesn't expose comments on snippets" do
|
|
|
|
project = create(:project, :public, :snippets_private)
|
|
|
|
note = create(:note_on_project_snippet, project: project)
|
2019-04-09 15:38:58 +00:00
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
get :show, params: { project_id: project.id, scope: 'notes', search: note.note }
|
|
|
|
|
|
|
|
expect(assigns[:search_objects].count).to eq(0)
|
2019-04-09 15:38:58 +00:00
|
|
|
end
|
2019-07-15 17:59:57 +00:00
|
|
|
end
|
|
|
|
end
|
2019-04-09 15:38:58 +00:00
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
describe 'GET #count' do
|
|
|
|
it_behaves_like 'when the user cannot read cross project', :count, { search: 'hello', scope: 'projects' }
|
|
|
|
it_behaves_like 'with external authorization service enabled', :count, { search: 'hello', scope: 'projects' }
|
2019-04-09 15:38:58 +00:00
|
|
|
|
2019-07-15 17:59:57 +00:00
|
|
|
it 'returns the result count for the given term and scope' do
|
|
|
|
create(:project, :public, name: 'hello world')
|
|
|
|
create(:project, :public, name: 'foo bar')
|
|
|
|
|
|
|
|
get :count, params: { search: 'hello', scope: 'projects' }
|
|
|
|
|
2020-01-27 12:08:35 +00:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-07-15 17:59:57 +00:00
|
|
|
expect(json_response).to eq({ 'count' => '1' })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error if search term is missing' do
|
|
|
|
expect do
|
|
|
|
get :count, params: { scope: 'projects' }
|
|
|
|
end.to raise_error(ActionController::ParameterMissing)
|
2019-04-09 15:38:58 +00:00
|
|
|
end
|
2019-07-15 17:59:57 +00:00
|
|
|
|
|
|
|
it 'raises an error if search scope is missing' do
|
|
|
|
expect do
|
|
|
|
get :count, params: { search: 'hello' }
|
|
|
|
end.to raise_error(ActionController::ParameterMissing)
|
|
|
|
end
|
|
|
|
end
|
2020-07-10 09:09:01 +00:00
|
|
|
|
|
|
|
describe 'GET #autocomplete' do
|
|
|
|
it_behaves_like 'when the user cannot read cross project', :autocomplete, { term: 'hello' }
|
|
|
|
it_behaves_like 'with external authorization service enabled', :autocomplete, { term: 'hello' }
|
|
|
|
end
|
2020-07-30 12:09:33 +00:00
|
|
|
|
|
|
|
describe '#append_info_to_payload' do
|
|
|
|
it 'appends search metadata for logging' do
|
|
|
|
last_payload = nil
|
|
|
|
original_append_info_to_payload = controller.method(:append_info_to_payload)
|
|
|
|
|
|
|
|
expect(controller).to receive(:append_info_to_payload) do |payload|
|
|
|
|
original_append_info_to_payload.call(payload)
|
|
|
|
last_payload = payload
|
|
|
|
end
|
|
|
|
|
|
|
|
get :show, params: { scope: 'issues', search: 'hello world', group_id: '123', project_id: '456' }
|
|
|
|
|
|
|
|
expect(last_payload[:metadata]['meta.search.group_id']).to eq('123')
|
|
|
|
expect(last_payload[:metadata]['meta.search.project_id']).to eq('456')
|
|
|
|
expect(last_payload[:metadata]['meta.search.search']).to eq('hello world')
|
|
|
|
expect(last_payload[:metadata]['meta.search.scope']).to eq('issues')
|
|
|
|
end
|
|
|
|
end
|
2016-12-09 01:56:31 +00:00
|
|
|
end
|