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
......@@ -41,6 +41,12 @@ module SearchHelper
def find_project_for_blob(blob)
Project.find(blob['_parent'])
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
......
......@@ -27,7 +27,7 @@
Milestones
%span.badge
= @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') }
= link_to search_filter_path(scope: 'notes') do
Comments
......
......@@ -187,19 +187,21 @@ module Gitlab
end
def wiki_filter
blob_filter(:wiki_access_level)
blob_filter(:wiki_access_level, visible_for_guests: true)
end
def repository_filter
blob_filter(:repository_access_level)
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 =
if non_guest_project_ids == :any
if project_ids == :any
[{ exists: { field: "id" } }]
else
[{ terms: { id: non_guest_project_ids } }]
[{ terms: { id: project_ids } }]
end
if public_and_internal_projects
......
require 'spec_helper'
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(:auditor) {create :user, auditor: true }
let(:non_member) { create :user }
......@@ -130,11 +130,13 @@ describe 'GlobalSearch' do
Sidekiq::Testing.inline! do
create :issue, title: 'term', 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.repository.index_blobs
project.repository.index_commits
project.wiki.index_blobs
Gitlab::Elastic::Helper.refresh_index
end
......@@ -149,6 +151,7 @@ describe 'GlobalSearch' do
results = search(user, 'term')
expect(results.issues_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, 'add').commits_count).to eq(0)
end
......@@ -157,6 +160,7 @@ describe 'GlobalSearch' do
results = search(user, 'term')
expect(results.issues_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, 'add').commits_count).not_to eq(0)
end
......@@ -164,6 +168,7 @@ describe 'GlobalSearch' do
def expect_non_code_items_to_be_found(user)
results = search(guest, 'term')
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(search(guest, 'def').blobs_count).to eq(0)
expect(search(guest, 'add').commits_count).to eq(0)
......
......@@ -66,7 +66,7 @@ describe Gitlab::Elastic::ProjectSearchResults, lib: true do
it 'shows wiki for guests' do
project = create :empty_project, :public
guest = create :user
project.team << [guest, :guest]
project.add_guest(guest)
# Wiki
project.wiki.create_page('index_page', 'term')
......
......@@ -419,6 +419,14 @@ describe Gitlab::Elastic::SearchResults, lib: true do
expect(results.wiki_blobs_count).to eq 1
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
project_2 = create :project, :private
project_2.wiki.create_page('index_page', 'term')
......
......@@ -29,7 +29,7 @@ describe SearchService, services: true do
it 'returns the project for guests' do
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
......
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