debian-mirror-gitlab/spec/services/search/snippet_service_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

82 lines
3.3 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2016-08-24 12:49:21 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Search::SnippetService do
2020-05-24 23:13:21 +05:30
let_it_be(:author) { create(:author) }
let_it_be(:project) { create(:project, :public) }
2016-08-24 12:49:21 +05:30
2020-05-24 23:13:21 +05:30
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-08-24 12:49:21 +05:30
2020-05-24 23:13:21 +05:30
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-08-24 12:49:21 +05:30
describe '#execute' do
context 'unauthenticated' do
it 'returns public snippets only' do
2020-05-24 23:13:21 +05:30
search = described_class.new(nil, search: 'bar')
2016-08-24 12:49:21 +05:30
results = search.execute
2020-05-24 23:13:21 +05:30
expect(results.objects('snippet_titles')).to match_array [public_snippet, project_public_snippet]
2016-08-24 12:49:21 +05:30
end
end
context 'authenticated' do
it 'returns only public & internal snippets for regular users' do
2020-05-24 23:13:21 +05:30
search = described_class.new(user, search: 'bar')
2016-08-24 12:49:21 +05:30
results = search.execute
2020-05-24 23:13:21 +05:30
expect(results.objects('snippet_titles')).to match_array [public_snippet, internal_snippet, project_public_snippet, project_internal_snippet]
2016-08-24 12:49:21 +05:30
end
it 'returns public, internal snippets and project private snippets for project members' do
2020-05-24 23:13:21 +05:30
project.add_developer(user)
search = described_class.new(user, search: 'bar')
2016-08-24 12:49:21 +05:30
results = search.execute
2020-05-24 23:13:21 +05:30
expect(results.objects('snippet_titles')).to match_array [public_snippet, internal_snippet, project_public_snippet, project_internal_snippet, project_private_snippet]
2016-08-24 12:49:21 +05:30
end
it 'returns public, internal and private snippets where user is the author' do
2020-05-24 23:13:21 +05:30
search = described_class.new(author, search: 'bar')
2016-08-24 12:49:21 +05:30
results = search.execute
2020-05-24 23:13:21 +05:30
expect(results.objects('snippet_titles')).to match_array [public_snippet, internal_snippet, private_snippet, project_public_snippet, project_internal_snippet]
2016-08-24 12:49:21 +05:30
end
2021-01-29 00:20:46 +05:30
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-08-24 12:49:21 +05:30
2021-01-29 00:20:46 +05:30
expect(results.objects('snippet_titles')).to match_array [public_snippet, internal_snippet, project_public_snippet, project_internal_snippet]
end
2016-08-24 12:49:21 +05:30
end
end
end
2020-05-24 23:13:21 +05:30
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-08-24 12:49:21 +05:30
end