Commit 768d2165 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Better search login for global context. Improved tests

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent fc60c391
...@@ -11,22 +11,17 @@ module Search ...@@ -11,22 +11,17 @@ module Search
query = Shellwords.shellescape(query) if query.present? query = Shellwords.shellescape(query) if query.present?
return result unless query.present? return result unless query.present?
authorized_projects_ids = []
projects = current_user.authorized_projects authorized_projects_ids += current_user.authorized_projects.pluck(:id) if current_user
authorized_projects_ids += Project.public_or_internal_only(current_user).pluck(:id)
if params[:group_id].present?
group = Group.find_by_id(params[:group_id]) group = Group.find_by_id(params[:group_id]) if params[:group_id].present?
projects = projects.where(namespace_id: group.id) if group projects = Project.where(id: authorized_projects_ids)
end projects = projects.where(namespace_id: group.id) if group
projects = projects.search(query)
project_ids = projects.pluck(:id) project_ids = projects.pluck(:id)
visibility_levels = if current_user result[:projects] = projects.limit(20)
[Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PUBLIC]
else
[Gitlab::VisibilityLevel::PUBLIC]
end
result[:projects] = Project.where("projects.id in (?) OR projects.visibility_level in (?)", project_ids, visibility_levels).search(query).limit(20)
result[:merge_requests] = MergeRequest.in_projects(project_ids).search(query).order('updated_at DESC').limit(20) result[:merge_requests] = MergeRequest.in_projects(project_ids).search(query).order('updated_at DESC').limit(20)
result[:issues] = Issue.where(project_id: project_ids).search(query).order('updated_at DESC').limit(20) result[:issues] = Issue.where(project_id: project_ids).search(query).order('updated_at DESC').limit(20)
result[:total_results] = %w(projects issues merge_requests).sum { |items| result[items.to_sym].size } result[:total_results] = %w(projects issues merge_requests).sum { |items| result[items.to_sym].size }
......
require 'spec_helper' require 'spec_helper'
describe SearchContext do describe 'Search::GlobalContext' do
let(:found_namespace) { create(:namespace, name: 'searchable namespace', path:'another_thing') } let(:found_namespace) { create(:namespace, name: 'searchable namespace', path:'another_thing') }
let(:user) { create(:user, namespace: found_namespace) } let(:user) { create(:user, namespace: found_namespace) }
let!(:found_project) { create(:project, name: 'searchable_project', creator_id: user.id, namespace: found_namespace, visibility_level: Gitlab::VisibilityLevel::PRIVATE) } let!(:found_project) { create(:project, name: 'searchable_project', creator_id: user.id, namespace: found_namespace, visibility_level: Gitlab::VisibilityLevel::PRIVATE) }
let(:unfound_namespace) { create(:namespace, name: 'unfound namespace', path: 'yet_something_else') } let(:unfound_namespace) { create(:namespace, name: 'unfound namespace', path: 'yet_something_else') }
let!(:unfound_project) { create(:project, name: 'unfound_project', creator_id: user.id, namespace: unfound_namespace, visibility_level: Gitlab::VisibilityLevel::PRIVATE) } let!(:unfound_project) { create(:project, name: 'unfound_project', creator_id: user.id, namespace: unfound_namespace, visibility_level: Gitlab::VisibilityLevel::PRIVATE) }
let(:internal_namespace) { create(:namespace, path: 'something_internal',name: 'searchable internal namespace') } let(:internal_namespace) { create(:namespace, path: 'something_internal',name: 'searchable internal namespace') }
let(:internal_user) { create(:user, namespace: internal_namespace) } let(:internal_user) { create(:user, namespace: internal_namespace) }
let!(:internal_project) { create(:project, name: 'searchable_internal_project', creator_id: internal_user.id, namespace: internal_namespace, visibility_level: Gitlab::VisibilityLevel::INTERNAL) } let!(:internal_project) { create(:project, name: 'searchable_internal_project', creator_id: internal_user.id, namespace: internal_namespace, visibility_level: Gitlab::VisibilityLevel::INTERNAL) }
let(:public_namespace) { create(:namespace, path: 'something_public',name: 'searchable public namespace') } let(:public_namespace) { create(:namespace, path: 'something_public',name: 'searchable public namespace') }
let(:public_user) { create(:user, namespace: public_namespace) } let(:public_user) { create(:user, namespace: public_namespace) }
let!(:public_project) { create(:project, name: 'searchable_public_project', creator_id: public_user.id, namespace: public_namespace, visibility_level: Gitlab::VisibilityLevel::PUBLIC) } let!(:public_project) { create(:project, name: 'searchable_public_project', creator_id: public_user.id, namespace: public_namespace, visibility_level: Gitlab::VisibilityLevel::PUBLIC) }
describe '#execute' do describe '#execute' do
it 'public projects should be searchable' do context 'unauthenticated' do
context = SearchContext.new([found_project.id], nil, {search_code: false, search: "searchable"}) it 'should return public projects only' do
results = context.execute context = Search::GlobalContext.new(nil, search: "searchable")
results[:projects].should == [found_project, public_project] results = context.execute
results[:projects].should have(1).items
results[:projects].should include(public_project)
end
end end
it 'internal projects should be searchable' do context 'authenticated' do
context = SearchContext.new([found_project.id], user, {search_code: false, search: "searchable"}) it 'should return public, internal and private projects' do
results = context.execute context = Search::GlobalContext.new(user, search: "searchable")
# can't seem to rely on the return order, so check this way results = context.execute
#subject { results[:projects] } results[:projects].should have(3).items
results[:projects].should have(3).items results[:projects].should include(public_project)
results[:projects].should include(found_project) results[:projects].should include(found_project)
results[:projects].should include(internal_project) results[:projects].should include(internal_project)
results[:projects].should include(public_project) end
end
it 'should return only public & internal projects' do
context = Search::GlobalContext.new(internal_user, search: "searchable")
results = context.execute
results[:projects].should have(2).items
results[:projects].should include(internal_project)
results[:projects].should include(public_project)
end
it 'namespace name should be searchable' do it 'namespace name should be searchable' do
context = SearchContext.new([found_project.id], user, {search_code: false, search: "searchable namespace"}) context = Search::GlobalContext.new(user, search: "searchable namespace")
results = context.execute results = context.execute
results[:projects].should == [found_project] results[:projects].should == [found_project]
end
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment