Commit d2ca426b authored by Valery Sizov's avatar Valery Sizov

Fix Elasticsearch search for wiki: guest should be able to search through wikis

parent 814d79d4
...@@ -42,6 +42,12 @@ module SearchHelper ...@@ -42,6 +42,12 @@ module SearchHelper
Project.find(blob['_parent']) Project.find(blob['_parent'])
end end
def show_notes_tab?
[:read_merge_request, :download_code, :read_issue, :read_project_snippet].any? do |ability|
can?(current_user, :read_merge_request, @project)
end
end
private private
# Autocomplete results for various settings pages # Autocomplete results for various settings pages
......
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
Milestones Milestones
%span.badge %span.badge
= @search_results.milestones_count = @search_results.milestones_count
- if can?(current_user, :read_merge_request, @project) || can?(current_user, :read_issue, @project) - if show_notes_tab?
%li{ class: active_when(@scope == 'notes') } %li{ class: active_when(@scope == 'notes') }
= link_to search_filter_path(scope: 'notes') do = link_to search_filter_path(scope: 'notes') do
Comments Comments
......
...@@ -187,19 +187,21 @@ module Gitlab ...@@ -187,19 +187,21 @@ module Gitlab
end end
def wiki_filter def wiki_filter
blob_filter(:wiki_access_level) blob_filter(:wiki_access_level, visible_for_guests: true)
end end
def repository_filter def repository_filter
blob_filter(:repository_access_level) blob_filter(:repository_access_level)
end end
def blob_filter(project_feature_name) def blob_filter(project_feature_name, visible_for_guests: false)
project_ids = visible_for_guests ? limit_project_ids : non_guest_project_ids
conditions = conditions =
if non_guest_project_ids == :any if project_ids == :any
[{ exists: { field: "id" } }] [{ exists: { field: "id" } }]
else else
[{ terms: { id: non_guest_project_ids } }] [{ terms: { id: project_ids } }]
end end
if public_and_internal_projects if public_and_internal_projects
......
require 'spec_helper' require 'spec_helper'
describe 'GlobalSearch' do describe 'GlobalSearch' do
let(:features) { %i(issues merge_requests repository builds) } let(:features) { %i(issues merge_requests repository builds wiki) }
let(:admin) { create :user, admin: true } let(:admin) { create :user, admin: true }
let(:auditor) {create :user, auditor: true } let(:auditor) {create :user, auditor: true }
let(:non_member) { create :user } let(:non_member) { create :user }
...@@ -130,11 +130,13 @@ describe 'GlobalSearch' do ...@@ -130,11 +130,13 @@ describe 'GlobalSearch' do
Sidekiq::Testing.inline! do Sidekiq::Testing.inline! do
create :issue, title: 'term', project: project create :issue, title: 'term', project: project
create :merge_request, title: 'term', target_project: project, source_project: project create :merge_request, title: 'term', target_project: project, source_project: project
project.wiki.create_page('index_page', 'term')
project.project_feature.update!(feature_settings) if feature_settings project.project_feature.update!(feature_settings) if feature_settings
project.repository.index_blobs project.repository.index_blobs
project.repository.index_commits project.repository.index_commits
project.wiki.index_blobs
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
end end
...@@ -149,6 +151,7 @@ describe 'GlobalSearch' do ...@@ -149,6 +151,7 @@ describe 'GlobalSearch' do
results = search(user, 'term') results = search(user, 'term')
expect(results.issues_count).to eq(0) expect(results.issues_count).to eq(0)
expect(results.merge_requests_count).to eq(0) expect(results.merge_requests_count).to eq(0)
expect(results.wiki_blobs_count).to eq(0)
expect(search(user, 'def').blobs_count).to eq(0) expect(search(user, 'def').blobs_count).to eq(0)
expect(search(user, 'add').commits_count).to eq(0) expect(search(user, 'add').commits_count).to eq(0)
end end
...@@ -157,6 +160,7 @@ describe 'GlobalSearch' do ...@@ -157,6 +160,7 @@ describe 'GlobalSearch' do
results = search(user, 'term') results = search(user, 'term')
expect(results.issues_count).not_to eq(0) expect(results.issues_count).not_to eq(0)
expect(results.merge_requests_count).not_to eq(0) expect(results.merge_requests_count).not_to eq(0)
expect(results.wiki_blobs_count).not_to eq(0)
expect(search(user, 'def').blobs_count).not_to eq(0) expect(search(user, 'def').blobs_count).not_to eq(0)
expect(search(user, 'add').commits_count).not_to eq(0) expect(search(user, 'add').commits_count).not_to eq(0)
end end
...@@ -164,6 +168,7 @@ describe 'GlobalSearch' do ...@@ -164,6 +168,7 @@ describe 'GlobalSearch' do
def expect_non_code_items_to_be_found(user) def expect_non_code_items_to_be_found(user)
results = search(guest, 'term') results = search(guest, 'term')
expect(results.issues_count).not_to eq(0) expect(results.issues_count).not_to eq(0)
expect(results.wiki_blobs_count).not_to eq(0)
expect(results.merge_requests_count).to eq(0) expect(results.merge_requests_count).to eq(0)
expect(search(guest, 'def').blobs_count).to eq(0) expect(search(guest, 'def').blobs_count).to eq(0)
expect(search(guest, 'add').commits_count).to eq(0) expect(search(guest, 'add').commits_count).to eq(0)
......
...@@ -66,7 +66,7 @@ describe Gitlab::Elastic::ProjectSearchResults, lib: true do ...@@ -66,7 +66,7 @@ describe Gitlab::Elastic::ProjectSearchResults, lib: true do
it 'shows wiki for guests' do it 'shows wiki for guests' do
project = create :empty_project, :public project = create :empty_project, :public
guest = create :user guest = create :user
project.team << [guest, :guest] project.add_guest(guest)
# Wiki # Wiki
project.wiki.create_page('index_page', 'term') project.wiki.create_page('index_page', 'term')
......
...@@ -419,6 +419,14 @@ describe Gitlab::Elastic::SearchResults, lib: true do ...@@ -419,6 +419,14 @@ describe Gitlab::Elastic::SearchResults, lib: true do
expect(results.wiki_blobs_count).to eq 1 expect(results.wiki_blobs_count).to eq 1
end end
it 'finds wiki blobs for guest' do
project_1.add_guest(user)
blobs = results.objects('wiki_blobs')
expect(blobs.first["_source"]["blob"]["content"]).to include("term")
expect(results.wiki_blobs_count).to eq 1
end
it 'finds wiki blobs from public projects only' do it 'finds wiki blobs from public projects only' do
project_2 = create :project, :private project_2 = create :project, :private
project_2.wiki.create_page('index_page', 'term') project_2.wiki.create_page('index_page', 'term')
......
...@@ -29,7 +29,7 @@ describe SearchService, services: true do ...@@ -29,7 +29,7 @@ describe SearchService, services: true do
it 'returns the project for guests' do it 'returns the project for guests' do
search_project = create :empty_project search_project = create :empty_project
search_project.team << [user, :guest] search_project.add_guest(user)
project = SearchService.new(user, project_id: search_project.id).project project = SearchService.new(user, project_id: search_project.id).project
......
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