2013-03-12 06:37:53 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Projects::TransferService do
|
2018-10-01 23:21:46 -04:00
|
|
|
include GitHelpers
|
|
|
|
|
2017-08-22 19:19:35 -04:00
|
|
|
let(:gitlab_shell) { Gitlab::Shell.new }
|
2014-05-28 12:04:18 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:group) { create(:group) }
|
2017-12-01 08:58:49 -05:00
|
|
|
let(:project) { create(:project, :repository, :legacy_storage, namespace: user.namespace) }
|
2013-03-12 06:37:53 -04:00
|
|
|
|
2014-05-28 12:04:18 -04:00
|
|
|
context 'namespace -> namespace' do
|
2013-03-12 06:37:53 -04:00
|
|
|
before do
|
2017-06-21 09:48:12 -04:00
|
|
|
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-05-28 12:04:18 -04:00
|
|
|
group.add_owner(user)
|
2015-07-02 10:47:15 -04:00
|
|
|
@result = transfer_project(project, user, group)
|
2013-03-12 06:37:53 -04:00
|
|
|
end
|
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
it { expect(@result).to be_truthy }
|
|
|
|
it { expect(project.namespace).to eq(group) }
|
2013-03-12 06:37:53 -04:00
|
|
|
end
|
|
|
|
|
2017-06-02 09:44:59 -04:00
|
|
|
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)
|
|
|
|
|
2017-06-29 18:11:33 -04:00
|
|
|
transfer_project(project, user, group)
|
|
|
|
end
|
|
|
|
|
2018-04-04 11:14:19 -04:00
|
|
|
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-06-02 09:44:59 -04:00
|
|
|
it 'executes system hooks' do
|
2017-07-31 08:30:18 -04:00
|
|
|
transfer_project(project, user, group) do |service|
|
|
|
|
expect(service).to receive(:execute_system_hooks)
|
|
|
|
end
|
2017-06-02 09:44:59 -04:00
|
|
|
end
|
2017-11-17 01:39:29 -05:00
|
|
|
|
|
|
|
it 'disk path has moved' do
|
|
|
|
old_path = project.repository.disk_path
|
|
|
|
old_full_path = project.repository.full_path
|
|
|
|
|
|
|
|
transfer_project(project, user, group)
|
|
|
|
|
|
|
|
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
|
2017-12-19 14:18:26 -05:00
|
|
|
|
|
|
|
it 'updates project full path in .git/config' do
|
|
|
|
transfer_project(project, user, group)
|
|
|
|
|
2018-06-05 11:51:14 -04:00
|
|
|
expect(rugged_config['gitlab.fullpath']).to eq "#{group.full_path}/#{project.path}"
|
2017-12-19 14:18:26 -05:00
|
|
|
end
|
2018-11-15 04:17:41 -05:00
|
|
|
|
|
|
|
context 'new group has a kubernetes cluster' do
|
|
|
|
let(:group_cluster) { create(:cluster, :group, :provided_by_gcp) }
|
|
|
|
let(:group) { group_cluster.group }
|
|
|
|
|
|
|
|
let(:token) { 'aaaa' }
|
|
|
|
let(:service_account_creator) { double(Clusters::Gcp::Kubernetes::CreateServiceAccountService, execute: true) }
|
|
|
|
let(:secrets_fetcher) { double(Clusters::Gcp::Kubernetes::FetchKubernetesTokenService, execute: token) }
|
|
|
|
|
|
|
|
subject { transfer_project(project, user, group) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
expect(Clusters::Gcp::Kubernetes::CreateServiceAccountService).to receive(:namespace_creator).and_return(service_account_creator)
|
|
|
|
expect(Clusters::Gcp::Kubernetes::FetchKubernetesTokenService).to receive(:new).and_return(secrets_fetcher)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates kubernetes namespace for the project' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(project.kubernetes_namespaces.count).to eq(1)
|
|
|
|
|
|
|
|
kubernetes_namespace = group_cluster.kubernetes_namespaces.first
|
|
|
|
expect(kubernetes_namespace).to be_present
|
|
|
|
expect(kubernetes_namespace.project).to eq(project)
|
|
|
|
end
|
|
|
|
end
|
2017-06-02 09:44:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when transfer fails' do
|
|
|
|
let!(:original_path) { project_path(project) }
|
|
|
|
|
2017-07-31 08:30:18 -04:00
|
|
|
def attempt_project_transfer(&block)
|
2017-06-02 09:44:59 -04:00
|
|
|
expect do
|
2017-07-31 08:30:18 -04:00
|
|
|
transfer_project(project, user, group, &block)
|
2017-06-02 09:44:59 -04:00
|
|
|
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-06-05 11:51:14 -04:00
|
|
|
Gitlab::GitalyClient::StorageSettings.allow_disk_access do
|
|
|
|
project.repository.path_to_repo
|
|
|
|
end
|
2017-06-02 09:44:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def current_path
|
|
|
|
project_path(project)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'rolls back repo location' do
|
|
|
|
attempt_project_transfer
|
|
|
|
|
2018-04-13 06:57:19 -04:00
|
|
|
expect(gitlab_shell.exists?(project.repository_storage, "#{project.disk_path}.git")).to be(true)
|
2017-06-02 09:44:59 -04:00
|
|
|
expect(original_path).to eq current_path
|
|
|
|
end
|
|
|
|
|
2017-12-19 14:18:26 -05:00
|
|
|
it 'rolls back project full path in .git/config' do
|
|
|
|
attempt_project_transfer
|
|
|
|
|
2018-06-05 11:51:14 -04:00
|
|
|
expect(rugged_config['gitlab.fullpath']).to eq project.full_path
|
2017-12-19 14:18:26 -05:00
|
|
|
end
|
|
|
|
|
2017-06-02 09:44:59 -04:00
|
|
|
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
|
2017-07-31 08:30:18 -04:00
|
|
|
attempt_project_transfer do |service|
|
|
|
|
expect(service).not_to receive(:execute_system_hooks)
|
|
|
|
end
|
2017-06-02 09:44:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-12 06:37:53 -04:00
|
|
|
context 'namespace -> no namespace' do
|
2014-05-28 12:04:18 -04:00
|
|
|
before do
|
2015-07-02 10:47:15 -04:00
|
|
|
@result = transfer_project(project, user, nil)
|
2014-05-28 12:04:18 -04:00
|
|
|
end
|
2013-03-12 06:37:53 -04:00
|
|
|
|
2015-06-13 18:24:51 -04:00
|
|
|
it { expect(@result).to eq false }
|
2015-02-12 13:17:35 -05:00
|
|
|
it { expect(project.namespace).to eq(user.namespace) }
|
2017-05-18 12:22:02 -04:00
|
|
|
it { expect(project.errors.messages[:new_namespace].first).to eq 'Please select a new namespace for your project.' }
|
2013-03-12 06:37:53 -04:00
|
|
|
end
|
|
|
|
|
2018-10-30 06:53:01 -04:00
|
|
|
context 'disallow transferring of project with tags' do
|
2017-03-23 10:54:59 -04:00
|
|
|
let(:container_repository) { create(:container_repository) }
|
2016-12-15 22:24:05 -05:00
|
|
|
|
2016-05-16 19:03:55 -04:00
|
|
|
before do
|
|
|
|
stub_container_registry_config(enabled: true)
|
2017-04-03 09:52:24 -04:00
|
|
|
stub_container_registry_tags(repository: :any, tags: ['tag'])
|
2017-03-24 07:41:42 -04:00
|
|
|
project.container_repositories << container_repository
|
2016-05-16 19:03:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
subject { transfer_project(project, user, group) }
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
|
2014-05-28 12:04:18 -04:00
|
|
|
context 'namespace -> not allowed namespace' do
|
|
|
|
before do
|
2015-07-02 10:47:15 -04:00
|
|
|
@result = transfer_project(project, user, group)
|
2014-05-28 12:04:18 -04:00
|
|
|
end
|
|
|
|
|
2015-06-13 18:24:51 -04:00
|
|
|
it { expect(@result).to eq false }
|
2015-02-12 13:17:35 -05:00
|
|
|
it { expect(project.namespace).to eq(user.namespace) }
|
2013-03-12 06:37:53 -04:00
|
|
|
end
|
2014-10-04 05:44:20 -04:00
|
|
|
|
2017-08-22 19:19:35 -04:00
|
|
|
context 'namespace which contains orphan repository with same projects path name' do
|
2017-09-28 13:07:22 -04:00
|
|
|
let(:repository_storage) { 'default' }
|
2018-03-14 09:42:49 -04:00
|
|
|
let(:repository_storage_path) { Gitlab.config.repositories.storages[repository_storage].legacy_disk_path }
|
2017-08-22 19:19:35 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
group.add_owner(user)
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2018-03-14 04:56:22 -04:00
|
|
|
unless gitlab_shell.create_repository(repository_storage, "#{group.full_path}/#{project.path}")
|
2017-09-28 13:07:22 -04:00
|
|
|
raise 'failed to add repository'
|
|
|
|
end
|
2017-08-22 19:19:35 -04:00
|
|
|
|
|
|
|
@result = transfer_project(project, user, group)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2018-04-13 06:57:19 -04:00
|
|
|
gitlab_shell.remove_repository(repository_storage, "#{group.full_path}/#{project.path}")
|
2017-08-22 19:19:35 -04:00
|
|
|
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-08-29 04:14:37 -04:00
|
|
|
context 'target namespace containing the same project name' do
|
|
|
|
before do
|
|
|
|
group.add_owner(user)
|
2018-08-30 02:49:24 -04:00
|
|
|
project.update(name: 'new_name')
|
2018-08-29 04:14:37 -04:00
|
|
|
|
2018-08-30 02:49:24 -04:00
|
|
|
create(:project, name: 'new_name', group: group, path: 'other')
|
2018-08-29 04:14:37 -04:00
|
|
|
|
|
|
|
@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)
|
|
|
|
|
2018-08-30 02:49:24 -04:00
|
|
|
create(:project, name: 'other-name', path: project.path, group: group)
|
2018-08-29 04:14:37 -04:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
2015-07-02 10:47:15 -04:00
|
|
|
def transfer_project(project, user, new_namespace)
|
2017-07-31 08:30:18 -04:00
|
|
|
service = Projects::TransferService.new(project, user)
|
|
|
|
|
|
|
|
yield(service) if block_given?
|
|
|
|
|
|
|
|
service.execute(new_namespace)
|
2014-10-04 05:44:20 -04:00
|
|
|
end
|
2016-04-13 14:28:10 -04:00
|
|
|
|
|
|
|
context 'visibility level' do
|
|
|
|
let(:internal_group) { create(:group, :internal) }
|
|
|
|
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
internal_group.add_owner(user)
|
|
|
|
end
|
2016-04-13 14:28:10 -04:00
|
|
|
|
|
|
|
context 'when namespace visibility level < project visibility level' do
|
2017-03-27 17:14:01 -04:00
|
|
|
let(:public_project) { create(:project, :public, :repository, namespace: user.namespace) }
|
2016-04-13 14:28:10 -04:00
|
|
|
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
transfer_project(public_project, user, internal_group)
|
|
|
|
end
|
2016-04-13 14:28:10 -04:00
|
|
|
|
|
|
|
it { expect(public_project.visibility_level).to eq(internal_group.visibility_level) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when namespace visibility level > project visibility level' do
|
2017-03-27 17:14:01 -04:00
|
|
|
let(:private_project) { create(:project, :private, :repository, namespace: user.namespace) }
|
2016-04-13 14:28:10 -04:00
|
|
|
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
transfer_project(private_project, user, internal_group)
|
|
|
|
end
|
2016-04-13 14:28:10 -04:00
|
|
|
|
|
|
|
it { expect(private_project.visibility_level).to eq(Gitlab::VisibilityLevel::PRIVATE) }
|
|
|
|
end
|
|
|
|
end
|
2016-09-26 16:10:54 -04:00
|
|
|
|
|
|
|
context 'missing group labels applied to issues or merge requests' do
|
|
|
|
it 'delegates tranfer to Labels::TransferService' do
|
|
|
|
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-02-07 08:55:42 -05:00
|
|
|
|
2017-11-17 01:39:29 -05:00
|
|
|
context 'when hashed storage in use' do
|
2017-12-01 08:58:49 -05:00
|
|
|
let(:hashed_project) { create(:project, :repository, namespace: user.namespace) }
|
2017-11-17 01:39:29 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
group.add_owner(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not move the directory' do
|
|
|
|
old_path = hashed_project.repository.disk_path
|
|
|
|
old_full_path = hashed_project.repository.full_path
|
|
|
|
|
|
|
|
transfer_project(hashed_project, user, group)
|
|
|
|
project.reload
|
|
|
|
|
|
|
|
expect(hashed_project.repository.disk_path).to eq(old_path)
|
|
|
|
expect(hashed_project.repository.full_path).to eq(old_full_path)
|
|
|
|
expect(hashed_project.disk_path).to eq(old_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-07 08:55:42 -05:00
|
|
|
describe 'refreshing project authorizations' do
|
|
|
|
let(:group) { create(:group) }
|
|
|
|
let(:owner) { project.namespace.owner }
|
|
|
|
let(:group_member) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
group.add_user(owner, GroupMember::MAINTAINER)
|
2017-02-07 08:55:42 -05:00
|
|
|
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-06-21 09:48:12 -04:00
|
|
|
expect(UserProjectAccessChangedService).to receive(:new)
|
|
|
|
.with([owner.id, group_member.id])
|
|
|
|
.and_call_original
|
2017-02-07 08:55:42 -05:00
|
|
|
|
|
|
|
transfer_project(project, owner, group)
|
|
|
|
end
|
|
|
|
end
|
2018-06-05 11:51:14 -04:00
|
|
|
|
|
|
|
def rugged_config
|
2018-10-01 23:21:46 -04:00
|
|
|
rugged_repo(project.repository).config
|
2018-06-05 11:51:14 -04:00
|
|
|
end
|
2013-03-12 06:37:53 -04:00
|
|
|
end
|