debian-mirror-gitlab/spec/services/projects/transfer_service_spec.rb

366 lines
11 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe Projects::TransferService do
2018-12-05 23:21:45 +05:30
include GitHelpers
2014-09-02 18:07:02 +05:30
let(:user) { create(:user) }
let(:group) { create(:group) }
2018-03-27 19:54:05 +05:30
let(:project) { create(:project, :repository, :legacy_storage, namespace: user.namespace) }
2014-09-02 18:07:02 +05:30
context 'namespace -> namespace' do
before do
2017-09-10 17:25:29 +05:30
allow_any_instance_of(Gitlab::UploadsTransfer)
.to receive(:move_project).and_return(true)
allow_any_instance_of(Gitlab::PagesTransfer)
.to receive(:move_project).and_return(true)
2014-09-02 18:07:02 +05:30
group.add_owner(user)
2015-09-11 14:41:01 +05:30
@result = transfer_project(project, user, group)
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
it { expect(@result).to be_truthy }
it { expect(project.namespace).to eq(group) }
2014-09-02 18:07:02 +05:30
end
2017-09-10 17:25:29 +05:30
context 'when transfer succeeds' do
before do
group.add_owner(user)
end
it 'sends notifications' do
expect_any_instance_of(NotificationService).to receive(:project_was_moved)
transfer_project(project, user, group)
end
2018-05-09 12:01:36 +05:30
it 'invalidates the user\'s personal_project_count cache' do
expect(user).to receive(:invalidate_personal_projects_count)
transfer_project(project, user, group)
end
2017-09-10 17:25:29 +05:30
it 'executes system hooks' do
transfer_project(project, user, group) do |service|
expect(service).to receive(:execute_system_hooks)
end
end
2018-03-17 18:26:18 +05:30
2020-03-09 13:42:32 +05:30
it 'moves the disk path', :aggregate_failures do
2018-03-17 18:26:18 +05:30
old_path = project.repository.disk_path
old_full_path = project.repository.full_path
transfer_project(project, user, group)
2020-03-09 13:42:32 +05:30
project.reload_repository!
2018-03-17 18:26:18 +05:30
expect(project.repository.disk_path).not_to eq(old_path)
expect(project.repository.full_path).not_to eq(old_full_path)
expect(project.disk_path).not_to eq(old_path)
expect(project.disk_path).to start_with(group.path)
end
it 'updates project full path in .git/config' do
transfer_project(project, user, group)
2018-11-08 19:23:39 +05:30
expect(rugged_config['gitlab.fullpath']).to eq "#{group.full_path}/#{project.path}"
2018-03-17 18:26:18 +05:30
end
2019-02-15 15:39:39 +05:30
it 'updates storage location' do
transfer_project(project, user, group)
expect(project.project_repository).to have_attributes(
disk_path: "#{group.full_path}/#{project.path}",
shard_name: project.repository_storage
)
end
2017-09-10 17:25:29 +05:30
end
context 'when transfer fails' do
let!(:original_path) { project_path(project) }
def attempt_project_transfer(&block)
expect do
transfer_project(project, user, group, &block)
end.to raise_error(ActiveRecord::ActiveRecordError)
end
before do
group.add_owner(user)
expect_any_instance_of(Labels::TransferService).to receive(:execute).and_raise(ActiveRecord::StatementInvalid, "PG ERROR")
end
def project_path(project)
2018-11-08 19:23:39 +05:30
Gitlab::GitalyClient::StorageSettings.allow_disk_access do
project.repository.path_to_repo
end
2017-09-10 17:25:29 +05:30
end
def current_path
project_path(project)
end
it 'rolls back repo location' do
attempt_project_transfer
2020-04-08 14:13:33 +05:30
expect(project.repository.raw.exists?).to be(true)
2017-09-10 17:25:29 +05:30
expect(original_path).to eq current_path
end
2018-03-17 18:26:18 +05:30
it 'rolls back project full path in .git/config' do
attempt_project_transfer
2018-11-08 19:23:39 +05:30
expect(rugged_config['gitlab.fullpath']).to eq project.full_path
2018-03-17 18:26:18 +05:30
end
2017-09-10 17:25:29 +05:30
it "doesn't send move notifications" do
expect_any_instance_of(NotificationService).not_to receive(:project_was_moved)
attempt_project_transfer
end
it "doesn't run system hooks" do
attempt_project_transfer do |service|
expect(service).not_to receive(:execute_system_hooks)
end
end
2019-02-15 15:39:39 +05:30
it 'does not update storage location' do
create(:project_repository, project: project)
attempt_project_transfer
expect(project.project_repository).to have_attributes(
disk_path: project.disk_path,
shard_name: project.repository_storage
)
end
2017-09-10 17:25:29 +05:30
end
2014-09-02 18:07:02 +05:30
context 'namespace -> no namespace' do
before do
2015-09-11 14:41:01 +05:30
@result = transfer_project(project, user, nil)
2014-09-02 18:07:02 +05:30
end
2015-09-11 14:41:01 +05:30
it { expect(@result).to eq false }
2015-04-26 12:48:37 +05:30
it { expect(project.namespace).to eq(user.namespace) }
2017-09-10 17:25:29 +05:30
it { expect(project.errors.messages[:new_namespace].first).to eq 'Please select a new namespace for your project.' }
2014-09-02 18:07:02 +05:30
end
2018-12-13 13:39:08 +05:30
context 'disallow transferring of project with tags' do
2017-08-17 22:00:37 +05:30
let(:container_repository) { create(:container_repository) }
2016-06-02 11:05:42 +05:30
before do
stub_container_registry_config(enabled: true)
2017-08-17 22:00:37 +05:30
stub_container_registry_tags(repository: :any, tags: ['tag'])
project.container_repositories << container_repository
2016-06-02 11:05:42 +05:30
end
subject { transfer_project(project, user, group) }
it { is_expected.to be_falsey }
end
2014-09-02 18:07:02 +05:30
context 'namespace -> not allowed namespace' do
before do
2015-09-11 14:41:01 +05:30
@result = transfer_project(project, user, group)
2014-09-02 18:07:02 +05:30
end
2015-09-11 14:41:01 +05:30
it { expect(@result).to eq false }
2015-04-26 12:48:37 +05:30
it { expect(project.namespace).to eq(user.namespace) }
end
2017-09-10 17:25:29 +05:30
context 'namespace which contains orphan repository with same projects path name' do
2020-04-08 14:13:33 +05:30
let(:fake_repo_path) { File.join(TestEnv.repos_path, group.full_path, "#{project.path}.git") }
2017-09-10 17:25:29 +05:30
before do
group.add_owner(user)
2018-03-17 18:26:18 +05:30
2020-04-08 14:13:33 +05:30
TestEnv.create_bare_repository(fake_repo_path)
2017-09-10 17:25:29 +05:30
@result = transfer_project(project, user, group)
end
after do
2020-04-08 14:13:33 +05:30
FileUtils.rm_rf(fake_repo_path)
2017-09-10 17:25:29 +05:30
end
it { expect(@result).to eq false }
it { expect(project.namespace).to eq(user.namespace) }
it { expect(project.errors[:new_namespace]).to include('Cannot move project') }
end
2018-11-20 20:47:30 +05:30
context 'target namespace containing the same project name' do
before do
group.add_owner(user)
project.update(name: 'new_name')
create(:project, name: 'new_name', group: group, path: 'other')
@result = transfer_project(project, user, group)
end
it { expect(@result).to eq false }
it { expect(project.namespace).to eq(user.namespace) }
it { expect(project.errors[:new_namespace]).to include('Project with same name or path in target namespace already exists') }
end
context 'target namespace containing the same project path' do
before do
group.add_owner(user)
create(:project, name: 'other-name', path: project.path, group: group)
@result = transfer_project(project, user, group)
end
it { expect(@result).to eq false }
it { expect(project.namespace).to eq(user.namespace) }
it { expect(project.errors[:new_namespace]).to include('Project with same name or path in target namespace already exists') }
end
2019-10-31 01:37:42 +05:30
context 'target namespace allows developers to create projects' do
let(:group) { create(:group, project_creation_level: ::Gitlab::Access::DEVELOPER_MAINTAINER_PROJECT_ACCESS) }
context 'the user is a member of the target namespace with developer permissions' do
subject(:transfer_project_result) { transfer_project(project, user, group) }
before do
group.add_developer(user)
end
it 'does not allow project transfer to the target namespace' do
expect(transfer_project_result).to eq false
expect(project.namespace).to eq(user.namespace)
expect(project.errors[:new_namespace]).to include('Transfer failed, please contact an admin.')
end
end
end
2015-09-11 14:41:01 +05:30
def transfer_project(project, user, new_namespace)
2017-09-10 17:25:29 +05:30
service = Projects::TransferService.new(project, user)
yield(service) if block_given?
service.execute(new_namespace)
2014-09-02 18:07:02 +05:30
end
2016-06-02 11:05:42 +05:30
context 'visibility level' do
let(:internal_group) { create(:group, :internal) }
2017-09-10 17:25:29 +05:30
before do
internal_group.add_owner(user)
end
2016-06-02 11:05:42 +05:30
context 'when namespace visibility level < project visibility level' do
2017-08-17 22:00:37 +05:30
let(:public_project) { create(:project, :public, :repository, namespace: user.namespace) }
2016-06-02 11:05:42 +05:30
2017-09-10 17:25:29 +05:30
before do
transfer_project(public_project, user, internal_group)
end
2016-06-02 11:05:42 +05:30
it { expect(public_project.visibility_level).to eq(internal_group.visibility_level) }
end
context 'when namespace visibility level > project visibility level' do
2017-08-17 22:00:37 +05:30
let(:private_project) { create(:project, :private, :repository, namespace: user.namespace) }
2016-06-02 11:05:42 +05:30
2017-09-10 17:25:29 +05:30
before do
transfer_project(private_project, user, internal_group)
end
2016-06-02 11:05:42 +05:30
it { expect(private_project.visibility_level).to eq(Gitlab::VisibilityLevel::PRIVATE) }
end
end
2016-11-03 12:29:30 +05:30
context 'missing group labels applied to issues or merge requests' do
2019-12-04 20:38:33 +05:30
it 'delegates transfer to Labels::TransferService' do
2016-11-03 12:29:30 +05:30
group.add_owner(user)
expect_any_instance_of(Labels::TransferService).to receive(:execute).once.and_call_original
transfer_project(project, user, group)
end
end
2017-08-17 22:00:37 +05:30
2019-12-04 20:38:33 +05:30
context 'missing group milestones applied to issues or merge requests' do
it 'delegates transfer to Milestones::TransferService' do
group.add_owner(user)
expect(Milestones::TransferService).to receive(:new).with(user, project.group, project).and_call_original
expect_any_instance_of(Milestones::TransferService).to receive(:execute).once
transfer_project(project, user, group)
end
end
2018-03-17 18:26:18 +05:30
context 'when hashed storage in use' do
2020-03-09 13:42:32 +05:30
let!(:hashed_project) { create(:project, :repository, namespace: user.namespace) }
let!(:old_disk_path) { hashed_project.repository.disk_path }
2018-03-17 18:26:18 +05:30
before do
group.add_owner(user)
end
2020-03-09 13:42:32 +05:30
it 'does not move the disk path', :aggregate_failures do
new_full_path = "#{group.full_path}/#{hashed_project.path}"
2018-03-17 18:26:18 +05:30
transfer_project(hashed_project, user, group)
2020-03-09 13:42:32 +05:30
hashed_project.reload_repository!
2018-03-17 18:26:18 +05:30
2020-03-09 13:42:32 +05:30
expect(hashed_project.repository).to have_attributes(
disk_path: old_disk_path,
full_path: new_full_path
)
expect(hashed_project.disk_path).to eq(old_disk_path)
end
it 'does not move the disk path when the transfer fails', :aggregate_failures do
old_full_path = hashed_project.full_path
expect_next_instance_of(described_class) do |service|
allow(service).to receive(:execute_system_hooks).and_raise('foo')
end
expect { transfer_project(hashed_project, user, group) }.to raise_error('foo')
hashed_project.reload_repository!
expect(hashed_project.repository).to have_attributes(
disk_path: old_disk_path,
full_path: old_full_path
)
expect(hashed_project.disk_path).to eq(old_disk_path)
2018-03-17 18:26:18 +05:30
end
end
2017-08-17 22:00:37 +05:30
describe 'refreshing project authorizations' do
let(:group) { create(:group) }
let(:owner) { project.namespace.owner }
let(:group_member) { create(:user) }
before do
2018-11-18 11:00:15 +05:30
group.add_user(owner, GroupMember::MAINTAINER)
2017-08-17 22:00:37 +05:30
group.add_user(group_member, GroupMember::DEVELOPER)
end
it 'refreshes the permissions of the old and new namespace' do
transfer_project(project, owner, group)
expect(group_member.authorized_projects).to include(project)
expect(owner.authorized_projects).to include(project)
end
it 'only schedules a single job for every user' do
2017-09-10 17:25:29 +05:30
expect(UserProjectAccessChangedService).to receive(:new)
.with([owner.id, group_member.id])
.and_call_original
2017-08-17 22:00:37 +05:30
transfer_project(project, owner, group)
end
end
2018-11-08 19:23:39 +05:30
def rugged_config
2018-12-05 23:21:45 +05:30
rugged_repo(project.repository).config
2018-11-08 19:23:39 +05:30
end
2014-09-02 18:07:02 +05:30
end