diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-11-28 18:06:57 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-11-28 18:06:57 +0300 |
commit | 7cdd70dcec27402e89e65451b4b1feb75b5eb267 (patch) | |
tree | 1691c8e1afd469fa426ecf5bc127de8df16d4855 /spec/services/projects | |
parent | 79348faced5e7e62103ad27f6a6594dfdca463e2 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/services/projects')
-rw-r--r-- | spec/services/projects/destroy_service_spec.rb | 5 | ||||
-rw-r--r-- | spec/services/projects/unlink_fork_service_spec.rb | 169 | ||||
-rw-r--r-- | spec/services/projects/update_service_spec.rb | 105 |
3 files changed, 259 insertions, 20 deletions
diff --git a/spec/services/projects/destroy_service_spec.rb b/spec/services/projects/destroy_service_spec.rb index 642986bb176..d8ba042af35 100644 --- a/spec/services/projects/destroy_service_spec.rb +++ b/spec/services/projects/destroy_service_spec.rb @@ -296,9 +296,12 @@ describe Projects::DestroyService do end context 'as the root of a fork network' do - let!(:fork_network) { create(:fork_network, root_project: project) } + let!(:fork_1) { fork_project(project, user) } + let!(:fork_2) { fork_project(project, user) } it 'updates the fork network with the project name' do + fork_network = project.fork_network + destroy_project(project, user) fork_network.reload diff --git a/spec/services/projects/unlink_fork_service_spec.rb b/spec/services/projects/unlink_fork_service_spec.rb index a1175bf7123..a6bdc69cdca 100644 --- a/spec/services/projects/unlink_fork_service_spec.rb +++ b/spec/services/projects/unlink_fork_service_spec.rb @@ -2,13 +2,13 @@ require 'spec_helper' -describe Projects::UnlinkForkService do +describe Projects::UnlinkForkService, :use_clean_rails_memory_store_caching do include ProjectForksHelper subject { described_class.new(forked_project, user) } let(:project) { create(:project, :public) } - let(:forked_project) { fork_project(project, user) } + let!(:forked_project) { fork_project(project, user) } let(:user) { create(:user) } context 'with opened merge request on the source project' do @@ -86,4 +86,169 @@ describe Projects::UnlinkForkService do expect { subject.execute }.not_to raise_error end end + + context 'when given project is a source of forks' do + let!(:forked_project_2) { fork_project(project, user) } + let!(:fork_of_fork) { fork_project(forked_project, user) } + + subject { described_class.new(project, user) } + + context 'with opened merge requests from fork back to root project' do + let!(:merge_request) { create(:merge_request, source_project: project, target_project: forked_project) } + let!(:merge_request2) { create(:merge_request, source_project: project, target_project: fork_project(project)) } + let!(:merge_request_in_fork) { create(:merge_request, source_project: forked_project, target_project: forked_project) } + + let(:mr_close_service) { MergeRequests::CloseService.new(project, user) } + + before do + allow(MergeRequests::CloseService).to receive(:new) + .with(project, user) + .and_return(mr_close_service) + end + + it 'closes all pending merge requests' do + expect(mr_close_service).to receive(:execute).with(merge_request) + expect(mr_close_service).to receive(:execute).with(merge_request2) + + subject.execute + end + + it 'does not close merge requests that do not come from the project being unlinked' do + expect(mr_close_service).not_to receive(:execute).with(merge_request_in_fork) + + subject.execute + end + end + + it 'removes its link to the fork network and updates direct network members' do + expect(project.fork_network_member).to be_present + expect(project.fork_network).to be_present + expect(project.forked_to_members.count).to eq(2) + expect(forked_project.forked_to_members.count).to eq(1) + expect(fork_of_fork.forked_to_members.count).to eq(0) + + subject.execute + + project.reload + forked_project.reload + fork_of_fork.reload + + expect(project.fork_network_member).to be_nil + expect(project.fork_network).to be_nil + expect(forked_project.fork_network).to have_attributes(root_project_id: nil, + deleted_root_project_name: project.full_name) + expect(project.forked_to_members.count).to eq(0) + expect(forked_project.forked_to_members.count).to eq(1) + expect(fork_of_fork.forked_to_members.count).to eq(0) + end + + it 'refreshes the forks count cache of the given project' do + expect(project.forks_count).to eq(2) + + subject.execute + + expect(project.forks_count).to be_zero + end + + context 'when given project is a fork of an unlinked parent' do + let!(:fork_of_fork) { fork_project(forked_project, user) } + let(:lfs_object) { create(:lfs_object) } + + before do + lfs_object.projects << project + end + + it 'saves lfs objects to the root project' do + # Remove parent from network + described_class.new(forked_project, user).execute + + described_class.new(fork_of_fork, user).execute + + expect(lfs_object.projects).to include(fork_of_fork) + end + end + + context 'and is node with a parent' do + subject { described_class.new(forked_project, user) } + + context 'with opened merge requests from and to given project' do + let!(:mr_from_parent) { create(:merge_request, source_project: project, target_project: forked_project) } + let!(:mr_to_parent) { create(:merge_request, source_project: forked_project, target_project: project) } + let!(:mr_to_child) { create(:merge_request, source_project: forked_project, target_project: fork_of_fork) } + let!(:mr_from_child) { create(:merge_request, source_project: fork_of_fork, target_project: forked_project) } + let!(:merge_request_in_fork) { create(:merge_request, source_project: forked_project, target_project: forked_project) } + + let(:mr_close_service) { MergeRequests::CloseService.new(forked_project, user) } + + before do + allow(MergeRequests::CloseService).to receive(:new) + .with(forked_project, user) + .and_return(mr_close_service) + end + + it 'close all pending merge requests' do + merge_requests = [mr_from_parent, mr_to_parent, mr_from_child, mr_to_child] + + merge_requests.each do |mr| + expect(mr_close_service).to receive(:execute).with(mr).and_call_original + end + + subject.execute + + merge_requests = MergeRequest.where(id: merge_requests) + + expect(merge_requests).to all(have_attributes(state: 'closed')) + end + + it 'does not close merge requests which do not come from the project being unlinked' do + expect(mr_close_service).not_to receive(:execute).with(merge_request_in_fork) + + subject.execute + end + end + + it 'refreshes the forks count cache of the parent and the given project' do + expect(project.forks_count).to eq(2) + expect(forked_project.forks_count).to eq(1) + + subject.execute + + expect(project.forks_count).to eq(1) + expect(forked_project.forks_count).to eq(0) + end + + it 'removes its link to the fork network and updates direct network members' do + expect(project.fork_network).to be_present + expect(forked_project.fork_network).to be_present + expect(fork_of_fork.fork_network).to be_present + + expect(project.forked_to_members.count).to eq(2) + expect(forked_project.forked_to_members.count).to eq(1) + expect(fork_of_fork.forked_to_members.count).to eq(0) + + subject.execute + project.reload + forked_project.reload + fork_of_fork.reload + + expect(project.fork_network).to be_present + expect(forked_project.fork_network).to be_nil + expect(fork_of_fork.fork_network).to be_present + + expect(project.forked_to_members.count).to eq(1) # 1 child is gone + expect(forked_project.forked_to_members.count).to eq(0) + expect(fork_of_fork.forked_to_members.count).to eq(0) + end + end + end + + context 'when given project is not part of a fork network' do + let!(:project_without_forks) { create(:project, :public) } + + subject { described_class.new(project_without_forks, user) } + + it 'does not raise errors' do + expect { subject.execute }.not_to raise_error + end + end end diff --git a/spec/services/projects/update_service_spec.rb b/spec/services/projects/update_service_spec.rb index c848a5397e1..3092fb7116a 100644 --- a/spec/services/projects/update_service_spec.rb +++ b/spec/services/projects/update_service_spec.rb @@ -16,7 +16,14 @@ describe Projects::UpdateService do let(:admin) { create(:admin) } context 'when changing visibility level' do - context 'when visibility_level is INTERNAL' do + def expect_to_call_unlink_fork_service + service = Projects::UnlinkForkService.new(project, user) + + expect(Projects::UnlinkForkService).to receive(:new).with(project, user).and_return(service) + expect(service).to receive(:execute).and_call_original + end + + context 'when visibility_level changes to INTERNAL' do it 'updates the project to internal' do expect(TodosDestroyer::ProjectPrivateWorker).not_to receive(:perform_in) @@ -25,9 +32,21 @@ describe Projects::UpdateService do expect(result).to eq({ status: :success }) expect(project).to be_internal end + + context 'and project is PUBLIC' do + before do + project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC) + end + + it 'unlinks project from fork network' do + expect_to_call_unlink_fork_service + + update_project(project, user, visibility_level: Gitlab::VisibilityLevel::INTERNAL) + end + end end - context 'when visibility_level is PUBLIC' do + context 'when visibility_level changes to PUBLIC' do it 'updates the project to public' do expect(TodosDestroyer::ProjectPrivateWorker).not_to receive(:perform_in) @@ -36,9 +55,17 @@ describe Projects::UpdateService do expect(result).to eq({ status: :success }) expect(project).to be_public end + + context 'and project is PRIVATE' do + it 'does not unlink project from fork network' do + expect(Projects::UnlinkForkService).not_to receive(:new) + + update_project(project, user, visibility_level: Gitlab::VisibilityLevel::PUBLIC) + end + end end - context 'when visibility_level is PRIVATE' do + context 'when visibility_level changes to PRIVATE' do before do project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC) end @@ -52,6 +79,30 @@ describe Projects::UpdateService do expect(result).to eq({ status: :success }) expect(project).to be_private end + + context 'and project is PUBLIC' do + before do + project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC) + end + + it 'unlinks project from fork network' do + expect_to_call_unlink_fork_service + + update_project(project, user, visibility_level: Gitlab::VisibilityLevel::PRIVATE) + end + end + + context 'and project is INTERNAL' do + before do + project.update!(visibility_level: Gitlab::VisibilityLevel::INTERNAL) + end + + it 'unlinks project from fork network' do + expect_to_call_unlink_fork_service + + update_project(project, user, visibility_level: Gitlab::VisibilityLevel::PRIVATE) + end + end end context 'when visibility levels are restricted to PUBLIC only' do @@ -107,28 +158,48 @@ describe Projects::UpdateService do let(:project) { create(:project, :internal) } let(:forked_project) { fork_project(project) } - it 'updates forks visibility level when parent set to more restrictive' do - opts = { visibility_level: Gitlab::VisibilityLevel::PRIVATE } + context 'and unlink forks feature flag is off' do + before do + stub_feature_flags(unlink_fork_network_upon_visibility_decrease: false) + end + + it 'updates forks visibility level when parent set to more restrictive' do + opts = { visibility_level: Gitlab::VisibilityLevel::PRIVATE } + + expect(project).to be_internal + expect(forked_project).to be_internal + + expect(update_project(project, admin, opts)).to eq({ status: :success }) + + expect(project).to be_private + expect(forked_project.reload).to be_private + end + + it 'does not update forks visibility level when parent set to less restrictive' do + opts = { visibility_level: Gitlab::VisibilityLevel::PUBLIC } - expect(project).to be_internal - expect(forked_project).to be_internal + expect(project).to be_internal + expect(forked_project).to be_internal - expect(update_project(project, admin, opts)).to eq({ status: :success }) + expect(update_project(project, admin, opts)).to eq({ status: :success }) - expect(project).to be_private - expect(forked_project.reload).to be_private + expect(project).to be_public + expect(forked_project.reload).to be_internal + end end - it 'does not update forks visibility level when parent set to less restrictive' do - opts = { visibility_level: Gitlab::VisibilityLevel::PUBLIC } + context 'and unlink forks feature flag is on' do + it 'does not change visibility of forks' do + opts = { visibility_level: Gitlab::VisibilityLevel::PRIVATE } - expect(project).to be_internal - expect(forked_project).to be_internal + expect(project).to be_internal + expect(forked_project).to be_internal - expect(update_project(project, admin, opts)).to eq({ status: :success }) + expect(update_project(project, admin, opts)).to eq({ status: :success }) - expect(project).to be_public - expect(forked_project.reload).to be_internal + expect(project).to be_private + expect(forked_project.reload).to be_internal + end end end |