Commit e0e399c4 authored by Mark Chao's avatar Mark Chao

ES: Fix snippet search

Ensure snippet STI subclasses to share the same `snippet` type,
to fix admin not ablet to find any snippet due to type being
`project_snippet`.

Filter non-admin search also by type so other models won't be searched
by accident in the future.
parent 92b943cb
...@@ -17,6 +17,10 @@ module Elastic ...@@ -17,6 +17,10 @@ module Elastic
search(query_hash) search(query_hash)
end end
def es_type
target.base_class.name.underscore
end
private private
def filter(query_hash, user) def filter(query_hash, user)
...@@ -31,7 +35,10 @@ module Elastic ...@@ -31,7 +35,10 @@ module Elastic
{ terms: { project_id: authorized_project_ids_for_user(user) } }, { terms: { project_id: authorized_project_ids_for_user(user) } },
{ {
bool: { bool: {
filter: { terms: { visibility_level: [Snippet::PUBLIC, Snippet::INTERNAL] } }, filter: [
{ terms: { visibility_level: [Snippet::PUBLIC, Snippet::INTERNAL] } },
{ term: { type: self.es_type } }
],
must_not: { exists: { field: 'project_id' } } must_not: { exists: { field: 'project_id' } }
} }
} }
...@@ -41,7 +48,10 @@ module Elastic ...@@ -41,7 +48,10 @@ module Elastic
else else
{ {
bool: { bool: {
filter: { term: { visibility_level: Snippet::PUBLIC } }, filter: [
{ term: { visibility_level: Snippet::PUBLIC } },
{ term: { type: self.es_type } }
],
must_not: { exists: { field: 'project_id' } } must_not: { exists: { field: 'project_id' } }
} }
} }
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::Elastic::SnippetSearchResults, :elastic do describe Gitlab::Elastic::SnippetSearchResults, :elastic do
let(:snippet) { create(:snippet, content: 'foo', file_name: 'foo') } let(:snippet) { create(:personal_snippet, content: 'foo', file_name: 'foo') }
let(:results) { described_class.new(snippet.author, 'foo') } let(:results) { described_class.new(snippet.author, 'foo') }
before do before do
...@@ -43,7 +43,7 @@ describe Gitlab::Elastic::SnippetSearchResults, :elastic do ...@@ -43,7 +43,7 @@ describe Gitlab::Elastic::SnippetSearchResults, :elastic do
end end
context 'when snippet is public' do context 'when snippet is public' do
let(:snippet) { create(:snippet, :public, content: 'foo', file_name: 'foo') } let(:snippet) { create(:personal_snippet, :public, content: 'foo', file_name: 'foo') }
it 'returns public snippet' do it 'returns public snippet' do
expect(results.snippet_titles_count).to eq(1) expect(results.snippet_titles_count).to eq(1)
...@@ -51,4 +51,14 @@ describe Gitlab::Elastic::SnippetSearchResults, :elastic do ...@@ -51,4 +51,14 @@ describe Gitlab::Elastic::SnippetSearchResults, :elastic do
end end
end end
end end
context 'when user has full_private_access' do
let(:user) { create(:admin) }
let(:results) { described_class.new(user, 'foo') }
it 'returns matched snippets' do
expect(results.snippet_titles_count).to eq(1)
expect(results.snippet_blobs_count).to eq(1)
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