Commit bc5cf0b7 authored by Igor Drozdov's avatar Igor Drozdov

Merge branch 'vij-snippet-blob-search-removal' into 'master'

Snippet blob search removal

See merge request gitlab-org/gitlab!29917
parents e28099b0 e4f2472c
......@@ -5,7 +5,6 @@ class SearchController < ApplicationController
include SearchHelper
include RendersCommits
before_action :override_snippet_scope, only: :show
around_action :allow_gitaly_ref_name_caching
skip_before_action :authenticate_user!
......@@ -104,14 +103,4 @@ class SearchController < ApplicationController
Gitlab::UsageDataCounters::SearchCounter.increment_navbar_searches_count
end
# Disallow web snippet_blobs search as we migrate snippet
# from database-backed storage to git repository-based,
# and searching across multiple git repositories is not feasible.
#
# TODO: after 13.0 refactor this into Search::SnippetService
# See https://gitlab.com/gitlab-org/gitlab/issues/208882
def override_snippet_scope
params[:scope] = 'snippet_titles' if params[:snippets] == 'true'
end
end
......@@ -341,17 +341,6 @@ class Snippet < ApplicationRecord
fuzzy_search(query, [:title, :description, :file_name])
end
# Searches for snippets with matching content.
#
# This method uses ILIKE on PostgreSQL and LIKE on MySQL.
#
# query - The search query as a String.
#
# Returns an ActiveRecord::Relation.
def search_code(query)
fuzzy_search(query, [:content])
end
def parent_class
::Project
end
......
......@@ -7,7 +7,7 @@ module Search
end
def scope
@scope ||= %w[snippet_titles].delete(params[:scope]) { 'snippet_blobs' }
@scope ||= 'snippet_titles'
end
end
end
......
......@@ -11,11 +11,5 @@ module Elastic
# see https://gitlab.com/gitlab-org/gitlab/issues/11850
::Gitlab::CurrentSettings.elasticsearch_indexing?
end
included do
class << self
delegate :elastic_search_code, to: :__elasticsearch__
end
end
end
end
......@@ -183,12 +183,6 @@ module Elastic
indexes :last_activity_at, type: :date
indexes :last_pushed_at, type: :date
### SNIPPETS
indexes :file_name, type: :text,
index_options: 'positions'
indexes :content, type: :text,
index_options: 'positions'
### REPOSITORIES
indexes :blob do
indexes :type, type: :keyword
......
......@@ -4,14 +4,7 @@ module Elastic
module Latest
class SnippetClassProxy < ApplicationClassProxy
def elastic_search(query, options: {})
query_hash = basic_query_hash(%w(title description file_name), query)
query_hash = filter(query_hash, options)
search(query_hash, options)
end
def elastic_search_code(query, options: {})
query_hash = basic_query_hash(%w(content), query)
query_hash = basic_query_hash(%w(title description), query)
query_hash = filter(query_hash, options)
search(query_hash, options)
......
......@@ -3,8 +3,6 @@
module Elastic
module Latest
class SnippetInstanceProxy < ApplicationInstanceProxy
MAX_INDEX_SIZE = 1.megabyte
def as_indexed_json(options = {})
# We don't use as_json(only: ...) because it calls all virtual and serialized attributes
# https://gitlab.com/gitlab-org/gitlab/issues/349
......@@ -13,8 +11,6 @@ module Elastic
[
:id,
:title,
:file_name,
:content,
:description,
:created_at,
:updated_at,
......@@ -25,10 +21,6 @@ module Elastic
data[attr.to_s] = safely_read_attribute_for_elasticsearch(attr)
end
if data['content'].bytesize > MAX_INDEX_SIZE
data['content'] = data['content'].mb_chars.limit(MAX_INDEX_SIZE).to_s # rubocop: disable CodeReuse/ActiveRecord
end
data.merge(generic_attributes)
end
......
......@@ -6,51 +6,27 @@ module Gitlab
def objects(scope, page = 1)
page = (page || 1).to_i
case scope
when 'snippet_titles'
eager_load(snippet_titles, page, eager: { project: [:route, :namespace] })
when 'snippet_blobs'
eager_load(snippet_blobs, page, eager: { project: [:route, :namespace] })
end
eager_load(snippet_titles, page, eager: { project: [:route, :namespace] })
end
def formatted_count(scope)
case scope
when 'snippet_titles'
snippet_titles_count.to_s
when 'snippet_blobs'
snippet_blobs_count.to_s
else
super
end
snippet_titles_count.to_s
end
def snippet_titles_count
limited_snippet_titles_count
end
def snippet_blobs_count
limited_snippet_blobs_count
end
private
def snippet_titles
Snippet.elastic_search(query, options: base_options)
end
def snippet_blobs
Snippet.elastic_search_code(query, options: base_options)
end
def limited_snippet_titles_count
@limited_snippet_titles_count ||= snippet_titles.total_count
end
def limited_snippet_blobs_count
@limited_snippet_blobs_count ||= snippet_blobs.total_count
end
def paginated_objects(relation, page)
super.records
end
......
......@@ -8,110 +8,126 @@ describe 'Snippet elastic search', :js, :elastic, :aggregate_failures, :sidekiq_
let(:authorized_project) { create(:project, namespace: authorized_user.namespace) }
before do
skip('Snippet content search will be disabled indefinitely')
stub_ee_application_setting(elasticsearch_search: true, elasticsearch_indexing: true)
authorized_project.add_maintainer(authorized_user)
create(:personal_snippet, :public, content: 'public personal snippet')
create(:project_snippet, :public, content: 'public project snippet', project: public_project)
create(:personal_snippet, :public, title: 'public personal snippet', description: 'a public personal snippet description')
create(:project_snippet, :public, title: 'public project snippet', description: 'a public project snippet description', project: public_project)
create(:personal_snippet, :internal, content: 'internal personal snippet')
create(:project_snippet, :internal, content: 'internal project snippet', project: public_project)
create(:personal_snippet, :internal, title: 'internal personal snippet', description: 'a internal personal snippet description')
create(:project_snippet, :internal, title: 'internal project snippet', description: 'a internal project snippet description', project: public_project)
create(:personal_snippet, :private, content: 'private personal snippet')
create(:project_snippet, :private, content: 'private project snippet', project: public_project)
create(:personal_snippet, :private, title: 'private personal snippet', description: 'a private personal snippet description')
create(:project_snippet, :private, title: 'private project snippet', description: 'a private project snippet description', project: public_project)
create(:personal_snippet, :private, content: 'authorized personal snippet', author: authorized_user)
create(:project_snippet, :private, content: 'authorized project snippet', project: authorized_project)
create(:personal_snippet, :private, title: 'authorized personal snippet', description: 'an authorized personal snippet description', author: authorized_user)
create(:project_snippet, :private, title: 'authorized project snippet', description: 'an authorized project snippet description', project: authorized_project)
ensure_elasticsearch_index!
sign_in(current_user) if current_user
visit explore_snippets_path
submit_search('snippet')
end
# TODO: Reenable support for public/internal project snippets
# https://gitlab.com/gitlab-org/gitlab/issues/35760
context 'as anonymous user' do
let(:current_user) { nil }
shared_examples 'expected snippet search results' do
context 'as anonymous user' do
let(:current_user) { nil }
it 'finds only public snippets' do
within('.results') do
expect(page).to have_content('public personal snippet')
expect(page).not_to have_content('public project snippet')
it 'finds only public snippets' do
within('.results') do
expect(page).to have_content('public personal snippet')
expect(page).not_to have_content('public project snippet')
expect(page).not_to have_content('internal personal snippet')
expect(page).not_to have_content('internal project snippet')
expect(page).not_to have_content('internal personal snippet')
expect(page).not_to have_content('internal project snippet')
expect(page).not_to have_content('authorized personal snippet')
expect(page).not_to have_content('authorized project snippet')
expect(page).not_to have_content('authorized personal snippet')
expect(page).not_to have_content('authorized project snippet')
expect(page).not_to have_content('private personal snippet')
expect(page).not_to have_content('private project snippet')
expect(page).not_to have_content('private personal snippet')
expect(page).not_to have_content('private project snippet')
end
end
end
end
context 'as logged in user' do
let(:current_user) { create(:user) }
context 'as logged in user' do
let(:current_user) { create(:user) }
it 'finds only public and internal snippets' do
within('.results') do
expect(page).to have_content('public personal snippet')
expect(page).not_to have_content('public project snippet')
it 'finds only public and internal snippets' do
within('.results') do
expect(page).to have_content('public personal snippet')
expect(page).not_to have_content('public project snippet')
expect(page).to have_content('internal personal snippet')
expect(page).not_to have_content('internal project snippet')
expect(page).to have_content('internal personal snippet')
expect(page).not_to have_content('internal project snippet')
expect(page).not_to have_content('private personal snippet')
expect(page).not_to have_content('private project snippet')
expect(page).not_to have_content('private personal snippet')
expect(page).not_to have_content('private project snippet')
expect(page).not_to have_content('authorized personal snippet')
expect(page).not_to have_content('authorized project snippet')
expect(page).not_to have_content('authorized personal snippet')
expect(page).not_to have_content('authorized project snippet')
end
end
end
end
context 'as authorized user' do
let(:current_user) { authorized_user }
context 'as authorized user' do
let(:current_user) { authorized_user }
it 'finds only public, internal, and authorized private snippets' do
within('.results') do
expect(page).to have_content('public personal snippet')
expect(page).not_to have_content('public project snippet')
it 'finds only public, internal, and authorized private snippets' do
within('.results') do
expect(page).to have_content('public personal snippet')
expect(page).not_to have_content('public project snippet')
expect(page).to have_content('internal personal snippet')
expect(page).not_to have_content('internal project snippet')
expect(page).to have_content('internal personal snippet')
expect(page).not_to have_content('internal project snippet')
expect(page).not_to have_content('private personal snippet')
expect(page).not_to have_content('private project snippet')
expect(page).not_to have_content('private personal snippet')
expect(page).not_to have_content('private project snippet')
expect(page).to have_content('authorized personal snippet')
expect(page).to have_content('authorized project snippet')
expect(page).to have_content('authorized personal snippet')
expect(page).to have_content('authorized project snippet')
end
end
end
end
context 'as administrator' do
let(:current_user) { create(:admin) }
context 'as administrator' do
let(:current_user) { create(:admin) }
it 'finds all snippets' do
within('.results') do
expect(page).to have_content('public personal snippet')
expect(page).to have_content('public project snippet')
it 'finds all snippets' do
within('.results') do
expect(page).to have_content('public personal snippet')
expect(page).to have_content('public project snippet')
expect(page).to have_content('internal personal snippet')
expect(page).to have_content('internal project snippet')
expect(page).to have_content('internal personal snippet')
expect(page).to have_content('internal project snippet')
expect(page).to have_content('private personal snippet')
expect(page).to have_content('private project snippet')
expect(page).to have_content('private personal snippet')
expect(page).to have_content('private project snippet')
expect(page).to have_content('authorized personal snippet')
expect(page).to have_content('authorized project snippet')
expect(page).to have_content('authorized personal snippet')
expect(page).to have_content('authorized project snippet')
end
end
end
end
context 'when searching titles' do
before do
submit_search('snippet')
end
it_behaves_like 'expected snippet search results'
end
context 'when searching descriptions' do
before do
submit_search('snippet description')
end
it_behaves_like 'expected snippet search results'
end
end
......@@ -2,7 +2,7 @@
require 'spec_helper'
describe Gitlab::SnippetSearchResults do
let!(:snippet) { create(:snippet, content: 'foo', file_name: 'foo') }
let_it_be(:snippet) { create(:snippet, title: 'foo', description: 'foo') }
let(:user) { snippet.author }
let(:com_value) { true }
let(:flag_enabled) { true }
......
......@@ -12,9 +12,7 @@ describe Elastic::Latest::SnippetInstanceProxy do
expect(subject.as_indexed_json.with_indifferent_access).to include(
id: snippet.id,
title: snippet.title,
file_name: snippet.file_name,
description: snippet.description,
content: snippet.content,
created_at: snippet.created_at,
updated_at: snippet.updated_at,
project_id: snippet.project_id,
......
......@@ -3,7 +3,7 @@
require 'spec_helper'
describe Gitlab::Elastic::SnippetSearchResults, :elastic, :sidekiq_might_not_need_inline do
let(:snippet) { create(:personal_snippet, content: 'foo', file_name: 'foo') }
let(:snippet) { create(:personal_snippet, title: 'foo', description: 'foo') }
let(:results) { described_class.new(snippet.author, 'foo', []) }
before do
......@@ -19,18 +19,11 @@ describe Gitlab::Elastic::SnippetSearchResults, :elastic, :sidekiq_might_not_nee
end
end
describe '#snippet_blobs_count' do
it 'returns the amount of matched snippet blobs' do
expect(results.snippet_blobs_count).to eq(1)
end
end
context 'when user is not author' do
let(:results) { described_class.new(create(:user), 'foo', []) }
it 'returns nothing' do
expect(results.snippet_titles_count).to eq(0)
expect(results.snippet_blobs_count).to eq(0)
end
end
......@@ -39,15 +32,13 @@ describe Gitlab::Elastic::SnippetSearchResults, :elastic, :sidekiq_might_not_nee
it 'returns nothing' do
expect(results.snippet_titles_count).to eq(0)
expect(results.snippet_blobs_count).to eq(0)
end
context 'when snippet is public' do
let(:snippet) { create(:personal_snippet, :public, content: 'foo', file_name: 'foo') }
let(:snippet) { create(:personal_snippet, :public, title: 'foo', description: 'foo') }
it 'returns public snippet' do
expect(results.snippet_titles_count).to eq(1)
expect(results.snippet_blobs_count).to eq(1)
end
end
end
......@@ -61,7 +52,6 @@ describe Gitlab::Elastic::SnippetSearchResults, :elastic, :sidekiq_might_not_nee
context 'admin mode disabled' do
it 'returns nothing' do
expect(results.snippet_titles_count).to eq(0)
expect(results.snippet_blobs_count).to eq(0)
end
end
......@@ -73,18 +63,7 @@ describe Gitlab::Elastic::SnippetSearchResults, :elastic, :sidekiq_might_not_nee
it 'returns matched snippets' do
expect(results.snippet_titles_count).to eq(1)
expect(results.snippet_blobs_count).to eq(1)
end
end
end
context 'when content is too long' do
let(:content) { "abc" + (" " * Elastic::Latest::SnippetInstanceProxy::MAX_INDEX_SIZE) + "xyz" }
let(:snippet) { create(:personal_snippet, :public, content: content) }
it 'indexes up to a limit' do
expect(described_class.new(nil, 'abc', []).snippet_blobs_count).to eq(1)
expect(described_class.new(nil, 'xyz', []).snippet_blobs_count).to eq(0)
end
end
end
......@@ -17,100 +17,13 @@ describe Snippet, :elastic do
expect(snippet.use_elasticsearch?).to eq(false)
end
context 'searching snippets by code' do
let!(:author) { create(:user) }
let!(:project) { create(:project) }
let!(:public_snippet) { create(:snippet, :public, content: 'password: XXX') }
let!(:internal_snippet) { create(:snippet, :internal, content: 'password: XXX') }
let!(:private_snippet) { create(:snippet, :private, content: 'password: XXX', author: author) }
let!(:project_public_snippet) { create(:snippet, :public, project: project, content: 'password: 123') }
let!(:project_internal_snippet) { create(:snippet, :internal, project: project, content: 'password: 456') }
let!(:project_private_snippet) { create(:snippet, :private, project: project, content: 'password: 789') }
before do
ensure_elasticsearch_index!
end
it 'returns only public snippets when user is blank', :sidekiq_might_not_need_inline do
result = described_class.elastic_search_code('password', options: { current_user: nil })
expect(result.total_count).to eq(1)
expect(result.records).to match_array [public_snippet]
end
it 'returns only public and internal personal snippets for non-members', :sidekiq_might_not_need_inline do
non_member = create(:user)
result = described_class.elastic_search_code('password', options: { current_user: non_member })
expect(result.total_count).to eq(2)
expect(result.records).to match_array [public_snippet, internal_snippet]
end
it 'returns public, internal snippets, and project private snippets for project members', :sidekiq_might_not_need_inline do
member = create(:user)
project.add_developer(member)
result = described_class.elastic_search_code('password', options: { current_user: member })
expect(result.total_count).to eq(5)
expect(result.records).to match_array [public_snippet, internal_snippet, project_public_snippet, project_internal_snippet, project_private_snippet]
end
it 'returns private snippets where the user is the author', :sidekiq_might_not_need_inline do
result = described_class.elastic_search_code('password', options: { current_user: author })
expect(result.total_count).to eq(3)
expect(result.records).to match_array [public_snippet, internal_snippet, private_snippet]
end
it 'supports advanced search syntax', :sidekiq_might_not_need_inline do
member = create(:user)
project.add_reporter(member)
result = described_class.elastic_search_code('password +(123 | 789)', options: { current_user: member })
expect(result.total_count).to eq(2)
expect(result.records).to match_array [project_public_snippet, project_private_snippet]
end
[:admin, :auditor].each do |user_type|
it "returns all snippets for #{user_type}", :sidekiq_might_not_need_inline do
superuser = create(user_type)
result = described_class.elastic_search_code('password', options: { current_user: superuser })
expect(result.total_count).to eq(6)
expect(result.records).to match_array [public_snippet, internal_snippet, private_snippet, project_public_snippet, project_internal_snippet, project_private_snippet]
end
end
describe 'when the user cannot read cross project' do
before do
allow(Ability).to receive(:allowed?).and_call_original
end
it 'returns public, internal snippets, but not project snippets', :sidekiq_might_not_need_inline do
member = create(:user)
project.add_developer(member)
expect(Ability).to receive(:allowed?).with(member, :read_cross_project) { false }
result = described_class.elastic_search_code('password', options: { current_user: member })
expect(result.records).to match_array [public_snippet, internal_snippet]
end
end
end
it 'searches snippets by title and file_name' do
it 'searches snippets by title and description' do
user = create(:user)
Sidekiq::Testing.inline! do
create(:snippet, :public, title: 'home')
create(:snippet, :private, title: 'home 1')
create(:snippet, :public, file_name: 'index.php')
create(:snippet, :public, description: 'a test snippet')
create(:snippet)
ensure_elasticsearch_index!
......@@ -119,7 +32,7 @@ describe Snippet, :elastic do
options = { current_user: user }
expect(described_class.elastic_search('home', options: options).total_count).to eq(1)
expect(described_class.elastic_search('index.php', options: options).total_count).to eq(1)
expect(described_class.elastic_search('test snippet', options: options).total_count).to eq(1)
end
it 'returns json with all needed elements' do
......@@ -128,8 +41,6 @@ describe Snippet, :elastic do
expected_hash = snippet.attributes.extract!(
'id',
'title',
'file_name',
'content',
'created_at',
'description',
'updated_at',
......
......@@ -59,7 +59,6 @@ RSpec.shared_examples 'EE search service shared examples' do |normal_results, el
scopes = if elasticsearch_results == ::Gitlab::Elastic::SnippetSearchResults
%w[
snippet_titles
snippet_blobs
]
else
%w[
......
......@@ -12,35 +12,17 @@ module Gitlab
end
def objects(scope, page = nil)
case scope
when 'snippet_titles'
paginated_objects(snippet_titles, page)
when 'snippet_blobs'
paginated_objects(snippet_blobs, page)
else
super(scope, nil, false)
end
paginated_objects(snippet_titles, page)
end
def formatted_count(scope)
case scope
when 'snippet_titles'
formatted_limited_count(limited_snippet_titles_count)
when 'snippet_blobs'
formatted_limited_count(limited_snippet_blobs_count)
else
super
end
formatted_limited_count(limited_snippet_titles_count)
end
def limited_snippet_titles_count
@limited_snippet_titles_count ||= limited_count(snippet_titles)
end
def limited_snippet_blobs_count
@limited_snippet_blobs_count ||= limited_count(snippet_blobs)
end
private
# rubocop: disable CodeReuse/ActiveRecord
......@@ -56,14 +38,6 @@ module Gitlab
snippets.search(query)
end
def snippet_blobs
snippets.search_code(query)
end
def default_scope
'snippet_blobs'
end
def paginated_objects(relation, page)
relation.page(page).per(per_page)
end
......
......@@ -140,14 +140,6 @@ describe SearchController do
end
end
context 'snippet search' do
it 'forces title search' do
get :show, params: { scope: 'snippet_blobs', snippets: 'true', search: 'foo' }
expect(assigns[:scope]).to eq('snippet_titles')
end
end
it 'finds issue comments' do
project = create(:project, :public)
note = create(:note_on_issue, project: project)
......
......@@ -5,7 +5,7 @@ require 'spec_helper'
describe Gitlab::SnippetSearchResults do
include SearchHelpers
let!(:snippet) { create(:snippet, content: 'foo', file_name: 'foo') }
let_it_be(:snippet) { create(:snippet, content: 'foo', file_name: 'foo') }
let(:results) { described_class.new(snippet.author, 'foo') }
describe '#snippet_titles_count' do
......@@ -14,27 +14,10 @@ describe Gitlab::SnippetSearchResults do
end
end
describe '#snippet_blobs_count' do
it 'returns the amount of matched snippet blobs' do
expect(results.limited_snippet_blobs_count).to eq(1)
end
end
describe '#formatted_count' do
using RSpec::Parameterized::TableSyntax
where(:scope, :count_method, :expected) do
'snippet_titles' | :limited_snippet_titles_count | max_limited_count
'snippet_blobs' | :limited_snippet_blobs_count | max_limited_count
'projects' | :limited_projects_count | max_limited_count
'unknown' | nil | nil
end
with_them do
it 'returns the expected formatted count' do
expect(results).to receive(count_method).and_return(1234) if count_method
expect(results.formatted_count(scope)).to eq(expected)
end
it 'returns the expected formatted count' do
expect(results).to receive(:limited_snippet_titles_count).and_return(1234)
expect(results.formatted_count('snippet_titles')).to eq(max_limited_count)
end
end
end
......@@ -180,22 +180,6 @@ describe Snippet do
end
end
describe '.search_code' do
let(:snippet) { create(:snippet, content: 'class Foo; end') }
it 'returns snippets with matching content' do
expect(described_class.search_code(snippet.content)).to eq([snippet])
end
it 'returns snippets with partially matching content' do
expect(described_class.search_code('class')).to eq([snippet])
end
it 'returns snippets with matching content regardless of the casing' do
expect(described_class.search_code('FOO')).to eq([snippet])
end
end
describe 'when default snippet visibility set to internal' do
using RSpec::Parameterized::TableSyntax
......
......@@ -3,59 +3,67 @@
require 'spec_helper'
describe Search::SnippetService do
let(:author) { create(:author) }
let(:project) { create(:project, :public) }
let_it_be(:author) { create(:author) }
let_it_be(:project) { create(:project, :public) }
let!(:public_snippet) { create(:snippet, :public, content: 'password: XXX') }
let!(:internal_snippet) { create(:snippet, :internal, content: 'password: XXX') }
let!(:private_snippet) { create(:snippet, :private, content: 'password: XXX', author: author) }
let_it_be(:public_snippet) { create(:snippet, :public, title: 'Foo Bar Title') }
let_it_be(:internal_snippet) { create(:snippet, :internal, title: 'Foo Bar Title') }
let_it_be(:private_snippet) { create(:snippet, :private, title: 'Foo Bar Title', author: author) }
let!(:project_public_snippet) { create(:snippet, :public, project: project, content: 'password: XXX') }
let!(:project_internal_snippet) { create(:snippet, :internal, project: project, content: 'password: XXX') }
let!(:project_private_snippet) { create(:snippet, :private, project: project, content: 'password: XXX') }
let_it_be(:project_public_snippet) { create(:snippet, :public, project: project, title: 'Foo Bar Title') }
let_it_be(:project_internal_snippet) { create(:snippet, :internal, project: project, title: 'Foo Bar Title') }
let_it_be(:project_private_snippet) { create(:snippet, :private, project: project, title: 'Foo Bar Title') }
let_it_be(:user) { create(:user) }
describe '#execute' do
context 'unauthenticated' do
it 'returns public snippets only' do
search = described_class.new(nil, search: 'password')
search = described_class.new(nil, search: 'bar')
results = search.execute
expect(results.objects('snippet_blobs')).to match_array [public_snippet, project_public_snippet]
expect(results.objects('snippet_titles')).to match_array [public_snippet, project_public_snippet]
end
end
context 'authenticated' do
it 'returns only public & internal snippets for regular users' do
user = create(:user)
search = described_class.new(user, search: 'password')
search = described_class.new(user, search: 'bar')
results = search.execute
expect(results.objects('snippet_blobs')).to match_array [public_snippet, internal_snippet, project_public_snippet, project_internal_snippet]
expect(results.objects('snippet_titles')).to match_array [public_snippet, internal_snippet, project_public_snippet, project_internal_snippet]
end
it 'returns public, internal snippets and project private snippets for project members' do
member = create(:user)
project.add_developer(member)
search = described_class.new(member, search: 'password')
project.add_developer(user)
search = described_class.new(user, search: 'bar')
results = search.execute
expect(results.objects('snippet_blobs')).to match_array [public_snippet, internal_snippet, project_public_snippet, project_internal_snippet, project_private_snippet]
expect(results.objects('snippet_titles')).to match_array [public_snippet, internal_snippet, project_public_snippet, project_internal_snippet, project_private_snippet]
end
it 'returns public, internal and private snippets where user is the author' do
search = described_class.new(author, search: 'password')
search = described_class.new(author, search: 'bar')
results = search.execute
expect(results.objects('snippet_blobs')).to match_array [public_snippet, internal_snippet, private_snippet, project_public_snippet, project_internal_snippet]
expect(results.objects('snippet_titles')).to match_array [public_snippet, internal_snippet, private_snippet, project_public_snippet, project_internal_snippet]
end
it 'returns all snippets when user is admin' do
admin = create(:admin)
search = described_class.new(admin, search: 'password')
search = described_class.new(admin, search: 'bar')
results = search.execute
expect(results.objects('snippet_blobs')).to match_array [public_snippet, internal_snippet, private_snippet, project_public_snippet, project_internal_snippet, project_private_snippet]
expect(results.objects('snippet_titles')).to match_array [public_snippet, internal_snippet, private_snippet, project_public_snippet, project_internal_snippet, project_private_snippet]
end
end
end
describe '#scope' do
it 'always scopes to snippet_titles' do
search = described_class.new(user, search: 'bar')
expect(search.scope).to eq 'snippet_titles'
end
end
end
......@@ -151,7 +151,7 @@ describe SearchService do
it 'returns the default scope' do
scope = described_class.new(user, snippets: 'true', scope: 'projects').scope
expect(scope).to eq 'snippet_blobs'
expect(scope).to eq 'snippet_titles'
end
end
......@@ -159,7 +159,7 @@ describe SearchService do
it 'returns the default scope' do
scope = described_class.new(user, snippets: 'true').scope
expect(scope).to eq 'snippet_blobs'
expect(scope).to eq 'snippet_titles'
end
end
end
......@@ -222,7 +222,7 @@ describe SearchService do
search_results = described_class.new(
user,
snippets: 'true',
search: snippet.content).search_results
search: snippet.title).search_results
expect(search_results).to be_a Gitlab::SnippetSearchResults
end
......@@ -270,7 +270,7 @@ describe SearchService do
search_objects = described_class.new(
user,
snippets: 'true',
search: snippet.content).search_objects
search: snippet.title).search_objects
expect(search_objects.first).to eq snippet
end
......@@ -383,7 +383,7 @@ describe SearchService do
let(:readable) { create(:project_snippet, project: accessible_project) }
let(:unreadable) { create(:project_snippet, project: inaccessible_project) }
let(:unredacted_results) { ar_relation(ProjectSnippet, readable, unreadable) }
let(:scope) { 'snippet_blobs' }
let(:scope) { 'snippet_titles' }
it 'redacts the inaccessible snippet' do
expect(result).to contain_exactly(readable)
......@@ -394,7 +394,7 @@ describe SearchService do
let(:readable) { create(:personal_snippet, :private, author: user) }
let(:unreadable) { create(:personal_snippet, :private) }
let(:unredacted_results) { ar_relation(PersonalSnippet, readable, unreadable) }
let(:scope) { 'snippet_blobs' }
let(:scope) { 'snippet_titles' }
it 'redacts the inaccessible snippet' do
expect(result).to contain_exactly(readable)
......
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