2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-04-06 11:23:49 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe Projects::MoveAccessService do
|
2018-04-06 11:23:49 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:group) { create(:group) }
|
|
|
|
let(:project_with_access) { create(:project, namespace: user.namespace) }
|
2018-07-11 10:36:08 -04:00
|
|
|
let(:maintainer_user) { create(:user) }
|
2018-04-06 11:23:49 -04:00
|
|
|
let(:reporter_user) { create(:user) }
|
|
|
|
let(:developer_user) { create(:user) }
|
2018-07-11 10:36:08 -04:00
|
|
|
let(:maintainer_group) { create(:group) }
|
2018-04-06 11:23:49 -04:00
|
|
|
let(:reporter_group) { create(:group) }
|
|
|
|
let(:developer_group) { create(:group) }
|
|
|
|
|
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project_with_access.add_maintainer(maintainer_user)
|
2018-04-06 11:23:49 -04:00
|
|
|
project_with_access.add_developer(developer_user)
|
|
|
|
project_with_access.add_reporter(reporter_user)
|
2020-10-14 05:08:46 -04:00
|
|
|
project_with_access.project_group_links.create!(group: maintainer_group, group_access: Gitlab::Access::MAINTAINER)
|
|
|
|
project_with_access.project_group_links.create!(group: developer_group, group_access: Gitlab::Access::DEVELOPER)
|
|
|
|
project_with_access.project_group_links.create!(group: reporter_group, group_access: Gitlab::Access::REPORTER)
|
2018-04-06 11:23:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.new(target_project, user) }
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
shared_examples 'move the accesses' do
|
2021-09-28 11:11:30 -04:00
|
|
|
it 'moves the accesses', :sidekiq_inline do
|
2018-04-06 11:23:49 -04:00
|
|
|
expect(project_with_access.project_members.count).to eq 4
|
|
|
|
expect(project_with_access.project_group_links.count).to eq 3
|
|
|
|
expect(project_with_access.authorized_users.count).to eq 4
|
|
|
|
|
|
|
|
subject.execute(project_with_access)
|
|
|
|
|
|
|
|
expect(project_with_access.project_members.count).to eq 0
|
|
|
|
expect(project_with_access.project_group_links.count).to eq 0
|
|
|
|
expect(project_with_access.authorized_users.count).to eq 1
|
|
|
|
expect(target_project.project_members.count).to eq 4
|
|
|
|
expect(target_project.project_group_links.count).to eq 3
|
|
|
|
expect(target_project.authorized_users.count).to eq 4
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'rollbacks if an exception is raised' do
|
|
|
|
allow(subject).to receive(:success).and_raise(StandardError)
|
|
|
|
|
|
|
|
expect { subject.execute(project_with_groups) }.to raise_error(StandardError)
|
|
|
|
|
|
|
|
expect(project_with_access.project_members.count).to eq 4
|
|
|
|
expect(project_with_access.project_group_links.count).to eq 3
|
|
|
|
expect(project_with_access.authorized_users.count).to eq 4
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when both projects are in the same namespace' do
|
|
|
|
let(:target_project) { create(:project, namespace: user.namespace) }
|
|
|
|
|
|
|
|
it 'does not refresh project owner authorized projects' do
|
|
|
|
allow(project_with_access).to receive(:namespace).and_return(user.namespace)
|
|
|
|
expect(project_with_access.namespace).not_to receive(:refresh_project_authorizations)
|
|
|
|
expect(target_project.namespace).not_to receive(:refresh_project_authorizations)
|
|
|
|
|
|
|
|
subject.execute(project_with_access)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'move the accesses'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when projects are in different namespaces' do
|
|
|
|
let(:target_project) { create(:project, namespace: group) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.add_owner(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'refreshes both project owner authorized projects' do
|
|
|
|
allow(project_with_access).to receive(:namespace).and_return(user.namespace)
|
|
|
|
expect(user.namespace).to receive(:refresh_project_authorizations).once
|
|
|
|
expect(group).to receive(:refresh_project_authorizations).once
|
|
|
|
|
|
|
|
subject.execute(project_with_access)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'move the accesses'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when remove_remaining_elements is false' do
|
|
|
|
let(:target_project) { create(:project, namespace: user.namespace) }
|
|
|
|
let(:options) { { remove_remaining_elements: false } }
|
|
|
|
|
|
|
|
it 'does not remove remaining memberships' do
|
2018-07-11 10:36:08 -04:00
|
|
|
target_project.add_maintainer(maintainer_user)
|
2018-04-06 11:23:49 -04:00
|
|
|
|
2020-11-24 04:09:32 -05:00
|
|
|
subject.execute(project_with_access, **options)
|
2018-04-06 11:23:49 -04:00
|
|
|
|
|
|
|
expect(project_with_access.project_members.count).not_to eq 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not remove remaining group links' do
|
2020-10-14 05:08:46 -04:00
|
|
|
target_project.project_group_links.create!(group: maintainer_group, group_access: Gitlab::Access::MAINTAINER)
|
2018-04-06 11:23:49 -04:00
|
|
|
|
2020-11-24 04:09:32 -05:00
|
|
|
subject.execute(project_with_access, **options)
|
2018-04-06 11:23:49 -04:00
|
|
|
|
|
|
|
expect(project_with_access.project_group_links.count).not_to eq 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not remove remaining authorizations' do
|
|
|
|
target_project.add_developer(developer_user)
|
|
|
|
|
2020-11-24 04:09:32 -05:00
|
|
|
subject.execute(project_with_access, **options)
|
2018-04-06 11:23:49 -04:00
|
|
|
|
|
|
|
expect(project_with_access.project_authorizations.count).not_to eq 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|