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