2016-06-22 16:44:24 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Search::SnippetService do
|
2016-06-22 16:44:24 -04:00
|
|
|
let(:author) { create(:author) }
|
2016-06-22 18:29:40 -04:00
|
|
|
let(:project) { create(:empty_project) }
|
2016-06-22 16:44:24 -04:00
|
|
|
|
|
|
|
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) }
|
|
|
|
|
2016-06-22 18:29:40 -04:00
|
|
|
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') }
|
|
|
|
|
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
|
2016-06-22 16:44:24 -04:00
|
|
|
search = described_class.new(nil, search: 'password')
|
|
|
|
results = search.execute
|
|
|
|
|
2016-06-22 18:29:40 -04:00
|
|
|
expect(results.objects('snippet_blobs')).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
|
|
|
|
user = create(:user)
|
|
|
|
search = described_class.new(user, search: 'password')
|
|
|
|
results = search.execute
|
|
|
|
|
|
|
|
expect(results.objects('snippet_blobs')).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.team << [member, :developer]
|
|
|
|
search = described_class.new(member, search: 'password')
|
2016-06-22 16:44:24 -04:00
|
|
|
results = search.execute
|
|
|
|
|
2016-06-22 18:29:40 -04:00
|
|
|
expect(results.objects('snippet_blobs')).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
|
2016-06-22 16:44:24 -04:00
|
|
|
search = described_class.new(author, search: 'password')
|
|
|
|
results = search.execute
|
|
|
|
|
2016-06-22 18:29:40 -04:00
|
|
|
expect(results.objects('snippet_blobs')).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')
|
|
|
|
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]
|
2016-06-22 16:44:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|