Commit 2b0f6dce authored by Mark Chao's avatar Mark Chao

ES: Wrap wiki search results as FoundWikiPage

This is similar to FoundBlob, but allows different policy checks.
Delegation is used instead of inheritance because it allows reuse of
SearchResults.parse_search_result.
parent f8731fa3
......@@ -8,6 +8,12 @@ module Elastic
def es_type
'wiki_blob'
end
def elastic_search_as_wiki_page(*args)
elastic_search_as_found_blob(*args).map! do |blob|
Gitlab::Search::FoundWikiPage.new(blob)
end
end
end
end
end
......@@ -8,6 +8,13 @@ module Elastic
delegate :project, to: :target
delegate :id, to: :project, prefix: true
# @return [Kaminari::PaginatableArray]
def elastic_search_as_wiki_page(query, page: 1, per: 20, options: {})
options = repository_specific_options(options)
self.class.elastic_search_as_wiki_page(query, page: page, per: per, options: options)
end
private
def repository_id
......
# frozen_string_literal: true
require 'spec_helper'
describe Elastic::Latest::ProjectWikiClassProxy do
let_it_be(:project) { create(:project, :wiki_repo) }
subject { described_class.new(project.wiki.repository) }
describe '#elastic_search_as_wiki_page', :elastic do
let_it_be(:page) { create(:wiki_page, wiki: project.wiki) }
before do
stub_ee_application_setting(elasticsearch_search: true, elasticsearch_indexing: true)
Gitlab::Elastic::Indexer.new(project, wiki: true).run
Gitlab::Elastic::Helper.refresh_index
end
it 'returns FoundWikiPage', :sidekiq_inline do
results = subject.elastic_search_as_wiki_page('*')
expect(results.size).to eq(1)
expect(results).to all(be_a(Gitlab::Search::FoundWikiPage))
result = results.first
expect(result.path).to eq(page.path)
expect(result.startline).to eq(1)
expect(result.data).to include(page.content)
expect(result.project).to eq(project)
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Elastic::Latest::ProjectWikiInstanceProxy do
let_it_be(:project) { create(:project, :wiki_repo) }
subject { described_class.new(project.wiki) }
describe '#elastic_search_as_wiki_page' do
let(:params) do
{
page: 2,
per: 30,
options: { foo: :bar }
}
end
it 'provides repository_id if not provided' do
expected_params = params.deep_dup
expected_params[:options][:repository_id] = "wiki_#{project.id}"
expect(subject.class).to receive(:elastic_search_as_wiki_page).with('foo', expected_params)
subject.elastic_search_as_wiki_page('foo', params)
end
it 'uses provided repository_id' do
params[:options][:repository_id] = "wiki_63"
expect(subject.class).to receive(:elastic_search_as_wiki_page).with('foo', params)
subject.elastic_search_as_wiki_page('foo', params)
end
end
end
# frozen_string_literal: true
# - rendering by using data purely from Elasticsearch and does not trigger Gitaly calls.
# - allows policy check
module Gitlab
module Search
class FoundWikiPage < SimpleDelegator
attr_reader :wiki
def self.declarative_policy_class
'WikiPagePolicy'
end
# @param found_blob [Gitlab::Search::FoundBlob]
def initialize(found_blob)
super
@wiki = found_blob.project.wiki
end
def to_ability_name
'wiki_page'
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::Search::FoundWikiPage do
let(:project) { create(:project, :public, :repository) }
describe 'policy' do
let(:project) { build(:project, :repository) }
let(:found_blob) { Gitlab::Search::FoundBlob.new(project: project) }
subject { described_class.new(found_blob) }
it 'works with policy' do
expect(Ability.allowed?(project.creator, :read_wiki_page, subject)).to be_truthy
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