2021-01-29 00:20:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe BulkImports::Groups::Loaders::GroupLoader do
|
|
|
|
describe '#load' do
|
2021-04-29 21:17:54 +05:30
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:bulk_import) { create(:bulk_import, user: user) }
|
|
|
|
let_it_be(:entity) { create(:bulk_import_entity, bulk_import: bulk_import) }
|
|
|
|
let_it_be(:tracker) { create(:bulk_import_tracker, entity: entity) }
|
|
|
|
let_it_be(:context) { BulkImports::Pipeline::Context.new(tracker) }
|
2021-09-30 23:02:18 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
let(:service_double) { instance_double(::Groups::CreateService) }
|
2021-12-11 22:18:48 +05:30
|
|
|
let(:data) { { 'path' => 'test' } }
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
subject { described_class.new }
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
context 'when path is missing' do
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { subject.load(context, {}) }.to raise_error(described_class::GroupCreationError, 'Path is missing')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when destination namespace is not a group' do
|
|
|
|
it 'raises an error' do
|
|
|
|
entity.update!(destination_namespace: user.namespace.path)
|
|
|
|
|
|
|
|
expect { subject.load(context, data) }.to raise_error(described_class::GroupCreationError, 'Destination is not a group')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when group exists' do
|
|
|
|
it 'raises an error' do
|
|
|
|
group1 = create(:group)
|
|
|
|
group2 = create(:group, parent: group1)
|
|
|
|
entity.update!(destination_namespace: group1.full_path)
|
|
|
|
data = { 'path' => group2.path }
|
|
|
|
|
|
|
|
expect { subject.load(context, data) }.to raise_error(described_class::GroupCreationError, 'Group exists')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are other group errors' do
|
|
|
|
it 'raises an error with those errors' do
|
|
|
|
group = ::Group.new
|
|
|
|
group.validate
|
|
|
|
expected_errors = group.errors.full_messages.to_sentence
|
|
|
|
|
|
|
|
expect(::Groups::CreateService)
|
|
|
|
.to receive(:new)
|
|
|
|
.with(context.current_user, data)
|
|
|
|
.and_return(service_double)
|
|
|
|
|
|
|
|
expect(service_double).to receive(:execute).and_return(group)
|
|
|
|
expect(entity).not_to receive(:update!)
|
|
|
|
|
|
|
|
expect { subject.load(context, data) }.to raise_error(described_class::GroupCreationError, expected_errors)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
context 'when user can create group' do
|
|
|
|
shared_examples 'calls Group Create Service to create a new group' do
|
|
|
|
it 'calls Group Create Service to create a new group' do
|
2021-12-11 22:18:48 +05:30
|
|
|
group_double = instance_double(::Group)
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
expect(::Groups::CreateService)
|
|
|
|
.to receive(:new)
|
|
|
|
.with(context.current_user, data)
|
|
|
|
.and_return(service_double)
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
expect(service_double).to receive(:execute).and_return(group_double)
|
|
|
|
expect(group_double).to receive(:errors).and_return([])
|
|
|
|
expect(entity).to receive(:update!).with(group: group_double)
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
subject.load(context, data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is no parent group' do
|
|
|
|
before do
|
|
|
|
allow(Ability).to receive(:allowed?).with(user, :create_group).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'calls Group Create Service to create a new group'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is parent group' do
|
|
|
|
let(:parent) { create(:group) }
|
2021-12-11 22:18:48 +05:30
|
|
|
let(:data) { { 'parent_id' => parent.id, 'path' => 'test' } }
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Ability).to receive(:allowed?).with(user, :create_subgroup, parent).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'calls Group Create Service to create a new group'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user cannot create group' do
|
|
|
|
shared_examples 'does not create new group' do
|
|
|
|
it 'does not create new group' do
|
|
|
|
expect(::Groups::CreateService).not_to receive(:new)
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
expect { subject.load(context, data) }.to raise_error(described_class::GroupCreationError, 'User not allowed to create group')
|
2021-01-29 00:20:46 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is no parent group' do
|
|
|
|
before do
|
|
|
|
allow(Ability).to receive(:allowed?).with(user, :create_group).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'does not create new group'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is parent group' do
|
|
|
|
let(:parent) { create(:group) }
|
2021-12-11 22:18:48 +05:30
|
|
|
let(:data) { { 'parent_id' => parent.id, 'path' => 'test' } }
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Ability).to receive(:allowed?).with(user, :create_subgroup, parent).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'does not create new group'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|