2019-04-11 12:17:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-22 20:44:24 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 15:08:50 +00:00
|
|
|
RSpec.describe Search::SnippetService do
|
2020-04-29 09:10:04 +00:00
|
|
|
let_it_be(:author) { create(:author) }
|
|
|
|
let_it_be(:project) { create(:project, :public) }
|
2016-06-22 20:44:24 +00:00
|
|
|
|
2020-04-29 09:10:04 +00:00
|
|
|
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) }
|
2016-06-22 20:44:24 +00:00
|
|
|
|
2020-04-29 09:10:04 +00:00
|
|
|
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) }
|
2016-06-22 22:29:40 +00:00
|
|
|
|
2016-06-22 20:44:24 +00:00
|
|
|
describe '#execute' do
|
|
|
|
context 'unauthenticated' do
|
2016-06-22 22:29:40 +00:00
|
|
|
it 'returns public snippets only' do
|
2020-04-29 09:10:04 +00:00
|
|
|
search = described_class.new(nil, search: 'bar')
|
2016-06-22 20:44:24 +00:00
|
|
|
results = search.execute
|
|
|
|
|
2020-04-29 09:10:04 +00:00
|
|
|
expect(results.objects('snippet_titles')).to match_array [public_snippet, project_public_snippet]
|
2016-06-22 20:44:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authenticated' do
|
2016-06-22 22:29:40 +00:00
|
|
|
it 'returns only public & internal snippets for regular users' do
|
2020-04-29 09:10:04 +00:00
|
|
|
search = described_class.new(user, search: 'bar')
|
2016-06-22 22:29:40 +00:00
|
|
|
results = search.execute
|
|
|
|
|
2020-04-29 09:10:04 +00:00
|
|
|
expect(results.objects('snippet_titles')).to match_array [public_snippet, internal_snippet, project_public_snippet, project_internal_snippet]
|
2016-06-22 22:29:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns public, internal snippets and project private snippets for project members' do
|
2020-04-29 09:10:04 +00:00
|
|
|
project.add_developer(user)
|
|
|
|
search = described_class.new(user, search: 'bar')
|
2016-06-22 20:44:24 +00:00
|
|
|
results = search.execute
|
|
|
|
|
2020-04-29 09:10:04 +00:00
|
|
|
expect(results.objects('snippet_titles')).to match_array [public_snippet, internal_snippet, project_public_snippet, project_internal_snippet, project_private_snippet]
|
2016-06-22 20:44:24 +00:00
|
|
|
end
|
|
|
|
|
2016-06-22 22:29:40 +00:00
|
|
|
it 'returns public, internal and private snippets where user is the author' do
|
2020-04-29 09:10:04 +00:00
|
|
|
search = described_class.new(author, search: 'bar')
|
2016-06-22 20:44:24 +00:00
|
|
|
results = search.execute
|
|
|
|
|
2020-04-29 09:10:04 +00:00
|
|
|
expect(results.objects('snippet_titles')).to match_array [public_snippet, internal_snippet, private_snippet, project_public_snippet, project_internal_snippet]
|
2016-06-22 22:29:40 +00:00
|
|
|
end
|
|
|
|
|
2020-11-11 12:09:06 +00:00
|
|
|
context 'when admin mode is enabled', :enable_admin_mode do
|
|
|
|
it 'returns all snippets when user is admin' do
|
|
|
|
admin = create(:admin)
|
|
|
|
search = described_class.new(admin, search: 'bar')
|
|
|
|
results = search.execute
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
context 'when admin mode is disabled' do
|
|
|
|
it 'returns only public & internal snippets when user is admin' do
|
|
|
|
admin = create(:admin)
|
|
|
|
search = described_class.new(admin, search: 'bar')
|
|
|
|
results = search.execute
|
2016-06-22 22:29:40 +00:00
|
|
|
|
2020-11-11 12:09:06 +00:00
|
|
|
expect(results.objects('snippet_titles')).to match_array [public_snippet, internal_snippet, project_public_snippet, project_internal_snippet]
|
|
|
|
end
|
2016-06-22 20:44:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-29 09:10:04 +00:00
|
|
|
|
|
|
|
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
|
2016-06-22 20:44:24 +00:00
|
|
|
end
|