Commit 768d21654a135130a28278c46cf413dbe65f8c81
1 parent
fc60c391
Exists in
spb-stable
and in
3 other branches
Better search login for global context. Improved tests
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing
2 changed files
with
41 additions
and
35 deletions
Show diff stats
app/contexts/search/global_context.rb
... | ... | @@ -11,22 +11,17 @@ module Search |
11 | 11 | query = Shellwords.shellescape(query) if query.present? |
12 | 12 | return result unless query.present? |
13 | 13 | |
14 | - | |
15 | - projects = current_user.authorized_projects | |
16 | - | |
17 | - if params[:group_id].present? | |
18 | - group = Group.find_by_id(params[:group_id]) | |
19 | - projects = projects.where(namespace_id: group.id) if group | |
20 | - end | |
21 | - | |
14 | + authorized_projects_ids = [] | |
15 | + authorized_projects_ids += current_user.authorized_projects.pluck(:id) if current_user | |
16 | + authorized_projects_ids += Project.public_or_internal_only(current_user).pluck(:id) | |
17 | + | |
18 | + group = Group.find_by_id(params[:group_id]) if params[:group_id].present? | |
19 | + projects = Project.where(id: authorized_projects_ids) | |
20 | + projects = projects.where(namespace_id: group.id) if group | |
21 | + projects = projects.search(query) | |
22 | 22 | project_ids = projects.pluck(:id) |
23 | 23 | |
24 | - visibility_levels = if current_user | |
25 | - [Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PUBLIC] | |
26 | - else | |
27 | - [Gitlab::VisibilityLevel::PUBLIC] | |
28 | - end | |
29 | - result[:projects] = Project.where("projects.id in (?) OR projects.visibility_level in (?)", project_ids, visibility_levels).search(query).limit(20) | |
24 | + result[:projects] = projects.limit(20) | |
30 | 25 | result[:merge_requests] = MergeRequest.in_projects(project_ids).search(query).order('updated_at DESC').limit(20) |
31 | 26 | result[:issues] = Issue.where(project_id: project_ids).search(query).order('updated_at DESC').limit(20) |
32 | 27 | result[:total_results] = %w(projects issues merge_requests).sum { |items| result[items.to_sym].size } | ... | ... |
spec/contexts/search_context_spec.rb
1 | 1 | require 'spec_helper' |
2 | 2 | |
3 | -describe SearchContext do | |
3 | +describe 'Search::GlobalContext' do | |
4 | 4 | let(:found_namespace) { create(:namespace, name: 'searchable namespace', path:'another_thing') } |
5 | 5 | let(:user) { create(:user, namespace: found_namespace) } |
6 | 6 | let!(:found_project) { create(:project, name: 'searchable_project', creator_id: user.id, namespace: found_namespace, visibility_level: Gitlab::VisibilityLevel::PRIVATE) } |
7 | 7 | |
8 | 8 | let(:unfound_namespace) { create(:namespace, name: 'unfound namespace', path: 'yet_something_else') } |
9 | 9 | let!(:unfound_project) { create(:project, name: 'unfound_project', creator_id: user.id, namespace: unfound_namespace, visibility_level: Gitlab::VisibilityLevel::PRIVATE) } |
10 | - | |
10 | + | |
11 | 11 | let(:internal_namespace) { create(:namespace, path: 'something_internal',name: 'searchable internal namespace') } |
12 | 12 | let(:internal_user) { create(:user, namespace: internal_namespace) } |
13 | 13 | let!(:internal_project) { create(:project, name: 'searchable_internal_project', creator_id: internal_user.id, namespace: internal_namespace, visibility_level: Gitlab::VisibilityLevel::INTERNAL) } |
14 | - | |
14 | + | |
15 | 15 | let(:public_namespace) { create(:namespace, path: 'something_public',name: 'searchable public namespace') } |
16 | 16 | let(:public_user) { create(:user, namespace: public_namespace) } |
17 | 17 | let!(:public_project) { create(:project, name: 'searchable_public_project', creator_id: public_user.id, namespace: public_namespace, visibility_level: Gitlab::VisibilityLevel::PUBLIC) } |
18 | 18 | |
19 | 19 | describe '#execute' do |
20 | - it 'public projects should be searchable' do | |
21 | - context = SearchContext.new([found_project.id], nil, {search_code: false, search: "searchable"}) | |
22 | - results = context.execute | |
23 | - results[:projects].should == [found_project, public_project] | |
20 | + context 'unauthenticated' do | |
21 | + it 'should return public projects only' do | |
22 | + context = Search::GlobalContext.new(nil, search: "searchable") | |
23 | + results = context.execute | |
24 | + results[:projects].should have(1).items | |
25 | + results[:projects].should include(public_project) | |
26 | + end | |
24 | 27 | end |
25 | 28 | |
26 | - it 'internal projects should be searchable' do | |
27 | - context = SearchContext.new([found_project.id], user, {search_code: false, search: "searchable"}) | |
28 | - results = context.execute | |
29 | - # can't seem to rely on the return order, so check this way | |
30 | - #subject { results[:projects] } | |
31 | - results[:projects].should have(3).items | |
32 | - results[:projects].should include(found_project) | |
33 | - results[:projects].should include(internal_project) | |
34 | - results[:projects].should include(public_project) | |
35 | - end | |
29 | + context 'authenticated' do | |
30 | + it 'should return public, internal and private projects' do | |
31 | + context = Search::GlobalContext.new(user, search: "searchable") | |
32 | + results = context.execute | |
33 | + results[:projects].should have(3).items | |
34 | + results[:projects].should include(public_project) | |
35 | + results[:projects].should include(found_project) | |
36 | + results[:projects].should include(internal_project) | |
37 | + end | |
38 | + | |
39 | + it 'should return only public & internal projects' do | |
40 | + context = Search::GlobalContext.new(internal_user, search: "searchable") | |
41 | + results = context.execute | |
42 | + results[:projects].should have(2).items | |
43 | + results[:projects].should include(internal_project) | |
44 | + results[:projects].should include(public_project) | |
45 | + end | |
36 | 46 | |
37 | - it 'namespace name should be searchable' do | |
38 | - context = SearchContext.new([found_project.id], user, {search_code: false, search: "searchable namespace"}) | |
39 | - results = context.execute | |
40 | - results[:projects].should == [found_project] | |
47 | + it 'namespace name should be searchable' do | |
48 | + context = Search::GlobalContext.new(user, search: "searchable namespace") | |
49 | + results = context.execute | |
50 | + results[:projects].should == [found_project] | |
51 | + end | |
41 | 52 | end |
42 | 53 | end |
43 | 54 | end | ... | ... |