2019-12-21 20:55:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
RSpec.describe SnippetsFinder do
|
2019-07-07 11:18:12 +05:30
|
|
|
include ExternalAuthorizationServiceHelpers
|
2018-03-17 18:26:18 +05:30
|
|
|
include Gitlab::Allowable
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
describe '#initialize' do
|
|
|
|
it 'raises ArgumentError when a project and author are given' do
|
|
|
|
user = build(:user)
|
|
|
|
project = build(:project)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
expect { described_class.new(user, author: user, project: project) }
|
|
|
|
.to raise_error(ArgumentError)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
describe '#execute' do
|
2019-12-21 20:55:43 +05:30
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:admin) { create(:admin) }
|
|
|
|
let_it_be(:group) { create(:group, :public) }
|
|
|
|
let_it_be(:project) { create(:project, :public, group: group) }
|
|
|
|
|
|
|
|
let_it_be(:private_personal_snippet) { create(:personal_snippet, :private, author: user) }
|
|
|
|
let_it_be(:internal_personal_snippet) { create(:personal_snippet, :internal, author: user) }
|
|
|
|
let_it_be(:public_personal_snippet) { create(:personal_snippet, :public, author: user) }
|
|
|
|
|
|
|
|
let_it_be(:private_project_snippet) { create(:project_snippet, :private, project: project) }
|
|
|
|
let_it_be(:internal_project_snippet) { create(:project_snippet, :internal, project: project) }
|
|
|
|
let_it_be(:public_project_snippet) { create(:project_snippet, :public, project: project) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
context 'filter by scope' do
|
|
|
|
it "returns all snippets for 'all' scope" do
|
|
|
|
snippets = described_class.new(user, scope: :all).execute
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
expect(snippets).to contain_exactly(
|
|
|
|
private_personal_snippet, internal_personal_snippet, public_personal_snippet,
|
|
|
|
internal_project_snippet, public_project_snippet
|
|
|
|
)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it "returns all snippets for 'are_private' scope" do
|
|
|
|
snippets = described_class.new(user, scope: :are_private).execute
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(snippets).to contain_exactly(private_personal_snippet)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it "returns all snippets for 'are_internal' scope" do
|
|
|
|
snippets = described_class.new(user, scope: :are_internal).execute
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
expect(snippets).to contain_exactly(internal_personal_snippet, internal_project_snippet)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
it "returns all snippets for 'are_public' scope" do
|
2019-07-07 11:18:12 +05:30
|
|
|
snippets = described_class.new(user, scope: :are_public).execute
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
expect(snippets).to contain_exactly(public_personal_snippet, public_project_snippet)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
2019-05-30 16:15:17 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
context 'filter by author' do
|
2020-01-01 13:55:28 +05:30
|
|
|
context 'when the author is a User object' do
|
|
|
|
it 'returns all public and internal snippets' do
|
|
|
|
snippets = described_class.new(create(:user), author: user).execute
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(snippets).to contain_exactly(internal_personal_snippet, public_personal_snippet)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the author is the User id' do
|
|
|
|
it 'returns all public and internal snippets' do
|
|
|
|
snippets = described_class.new(create(:user), author: user.id).execute
|
|
|
|
|
|
|
|
expect(snippets).to contain_exactly(internal_personal_snippet, public_personal_snippet)
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'returns internal snippets' do
|
|
|
|
snippets = described_class.new(user, author: user, scope: :are_internal).execute
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(snippets).to contain_exactly(internal_personal_snippet)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'returns private snippets' do
|
|
|
|
snippets = described_class.new(user, author: user, scope: :are_private).execute
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(snippets).to contain_exactly(private_personal_snippet)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'returns public snippets' do
|
|
|
|
snippets = described_class.new(user, author: user, scope: :are_public).execute
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(snippets).to contain_exactly(public_personal_snippet)
|
|
|
|
end
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'returns all snippets' do
|
|
|
|
snippets = described_class.new(user, author: user).execute
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(snippets).to contain_exactly(private_personal_snippet, internal_personal_snippet, public_personal_snippet)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'returns only public snippets if unauthenticated user' do
|
|
|
|
snippets = described_class.new(nil, author: user).execute
|
2019-05-30 16:15:17 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(snippets).to contain_exactly(public_personal_snippet)
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'returns all snippets for an admin' do
|
|
|
|
snippets = described_class.new(admin, author: user).execute
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(snippets).to contain_exactly(private_personal_snippet, internal_personal_snippet, public_personal_snippet)
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
context 'when author is not valid' do
|
|
|
|
it 'returns quickly' do
|
2020-04-22 19:07:51 +05:30
|
|
|
finder = described_class.new(admin, author: non_existing_record_id)
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
expect(finder).not_to receive(:init_collection)
|
|
|
|
expect(Snippet).to receive(:none).and_call_original
|
|
|
|
expect(finder.execute).to be_empty
|
|
|
|
end
|
|
|
|
end
|
2019-05-30 16:15:17 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
context 'filter by project' do
|
|
|
|
context 'when project is a Project object' do
|
|
|
|
it 'returns public personal and project snippets for unauthorized user' do
|
|
|
|
snippets = described_class.new(nil, project: project).execute
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(snippets).to contain_exactly(public_project_snippet)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project is a Project id' do
|
|
|
|
it 'returns public personal and project snippets for unauthorized user' do
|
|
|
|
snippets = described_class.new(nil, project: project.id).execute
|
|
|
|
|
|
|
|
expect(snippets).to contain_exactly(public_project_snippet)
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'returns public and internal snippets for non project members' do
|
|
|
|
snippets = described_class.new(user, project: project).execute
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(snippets).to contain_exactly(internal_project_snippet, public_project_snippet)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'returns public snippets for non project members' do
|
|
|
|
snippets = described_class.new(user, project: project, scope: :are_public).execute
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(snippets).to contain_exactly(public_project_snippet)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'returns internal snippets for non project members' do
|
|
|
|
snippets = described_class.new(user, project: project, scope: :are_internal).execute
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(snippets).to contain_exactly(internal_project_snippet)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'does not return private snippets for non project members' do
|
|
|
|
snippets = described_class.new(user, project: project, scope: :are_private).execute
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(snippets).to be_empty
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'returns all snippets for project members' do
|
|
|
|
project.add_developer(user)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
snippets = described_class.new(user, project: project).execute
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(snippets).to contain_exactly(private_project_snippet, internal_project_snippet, public_project_snippet)
|
|
|
|
end
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'returns private snippets for project members' do
|
|
|
|
project.add_developer(user)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
snippets = described_class.new(user, project: project, scope: :are_private).execute
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(snippets).to contain_exactly(private_project_snippet)
|
|
|
|
end
|
2019-05-30 16:15:17 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'returns all snippets for an admin' do
|
|
|
|
snippets = described_class.new(admin, project: project).execute
|
2019-05-30 16:15:17 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(snippets).to contain_exactly(private_project_snippet, internal_project_snippet, public_project_snippet)
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
context 'filter by author' do
|
|
|
|
let!(:other_user) { create(:user) }
|
|
|
|
let!(:other_private_project_snippet) { create(:project_snippet, :private, project: project, author: other_user) }
|
|
|
|
let!(:other_internal_project_snippet) { create(:project_snippet, :internal, project: project, author: other_user) }
|
|
|
|
let!(:other_public_project_snippet) { create(:project_snippet, :public, project: project, author: other_user) }
|
|
|
|
|
|
|
|
it 'returns all snippets for project members' do
|
|
|
|
project.add_developer(user)
|
|
|
|
|
|
|
|
snippets = described_class.new(user, author: other_user).execute
|
|
|
|
|
|
|
|
expect(snippets)
|
|
|
|
.to contain_exactly(
|
|
|
|
other_private_project_snippet,
|
|
|
|
other_internal_project_snippet,
|
|
|
|
other_public_project_snippet
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
context 'when project is not valid' do
|
|
|
|
it 'returns quickly' do
|
2020-04-22 19:07:51 +05:30
|
|
|
finder = described_class.new(admin, project: non_existing_record_id)
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
expect(finder).not_to receive(:init_collection)
|
|
|
|
expect(Snippet).to receive(:none).and_call_original
|
|
|
|
expect(finder.execute).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'filter by snippet type' do
|
|
|
|
context 'when filtering by only_personal snippet' do
|
|
|
|
it 'returns only personal snippet' do
|
|
|
|
snippets = described_class.new(admin, only_personal: true).execute
|
|
|
|
|
|
|
|
expect(snippets).to contain_exactly(private_personal_snippet,
|
|
|
|
internal_personal_snippet,
|
|
|
|
public_personal_snippet)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when filtering by only_project snippet' do
|
|
|
|
it 'returns only project snippet' do
|
|
|
|
snippets = described_class.new(admin, only_project: true).execute
|
|
|
|
|
|
|
|
expect(snippets).to contain_exactly(private_project_snippet,
|
|
|
|
internal_project_snippet,
|
|
|
|
public_project_snippet)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'filtering by ids' do
|
|
|
|
it 'returns only personal snippet' do
|
|
|
|
snippets = described_class.new(
|
|
|
|
admin, ids: [private_personal_snippet.id,
|
|
|
|
internal_personal_snippet.id]
|
|
|
|
).execute
|
|
|
|
|
|
|
|
expect(snippets).to contain_exactly(private_personal_snippet, internal_personal_snippet)
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'explore snippets' do
|
|
|
|
it 'returns only public personal snippets for unauthenticated users' do
|
|
|
|
snippets = described_class.new(nil, explore: true).execute
|
|
|
|
|
|
|
|
expect(snippets).to contain_exactly(public_personal_snippet)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'also returns internal personal snippets for authenticated users' do
|
|
|
|
snippets = described_class.new(user, explore: true).execute
|
|
|
|
|
|
|
|
expect(snippets).to contain_exactly(
|
|
|
|
internal_personal_snippet, public_personal_snippet
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns all personal snippets for admins' do
|
|
|
|
snippets = described_class.new(admin, explore: true).execute
|
|
|
|
|
|
|
|
expect(snippets).to contain_exactly(
|
|
|
|
private_personal_snippet, internal_personal_snippet, public_personal_snippet
|
|
|
|
)
|
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the user cannot read cross project' do
|
|
|
|
before do
|
|
|
|
allow(Ability).to receive(:allowed?).and_call_original
|
|
|
|
allow(Ability).to receive(:allowed?).with(user, :read_cross_project) { false }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns only personal snippets when the user cannot read cross project' do
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(described_class.new(user).execute).to contain_exactly(private_personal_snippet, internal_personal_snippet, public_personal_snippet)
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
context 'when project snippets are disabled' do
|
|
|
|
it 'returns quickly' do
|
|
|
|
disabled_snippets_project = create(:project, :snippets_disabled)
|
|
|
|
finder = described_class.new(user, project: disabled_snippets_project.id)
|
|
|
|
|
|
|
|
expect(finder).not_to receive(:init_collection)
|
|
|
|
expect(Snippet).to receive(:none).and_call_original
|
|
|
|
expect(finder.execute).to be_empty
|
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2018-12-13 13:39:08 +05:30
|
|
|
|
|
|
|
it_behaves_like 'snippet visibility'
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
context 'external authorization' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
let!(:snippet) { create(:project_snippet, :public, project: project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a finder with external authorization service' do
|
|
|
|
let!(:subject) { create(:project_snippet, project: project) }
|
|
|
|
let(:project_params) { { project: project } }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the result if the external service allows access' do
|
|
|
|
external_service_allow_access(user, project)
|
|
|
|
|
|
|
|
results = described_class.new(user, project: project).execute
|
|
|
|
|
|
|
|
expect(results).to contain_exactly(snippet)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not include any results if the external service denies access' do
|
|
|
|
external_service_deny_access(user, project)
|
|
|
|
|
|
|
|
results = described_class.new(user, project: project).execute
|
|
|
|
|
|
|
|
expect(results).to be_empty
|
|
|
|
end
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|