debian-mirror-gitlab/spec/services/users/destroy_service_spec.rb

233 lines
7.3 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe Users::DestroyService do
2017-08-17 22:00:37 +05:30
describe "Deletes a user and all their personal projects" do
let!(:user) { create(:user) }
let!(:admin) { create(:admin) }
2018-03-17 18:26:18 +05:30
let!(:namespace) { user.namespace }
2017-09-10 17:25:29 +05:30
let!(:project) { create(:project, namespace: namespace) }
2017-08-17 22:00:37 +05:30
let(:service) { described_class.new(admin) }
2018-03-17 18:26:18 +05:30
let(:gitlab_shell) { Gitlab::Shell.new }
2017-08-17 22:00:37 +05:30
context 'no options are given' do
it 'deletes the user' do
user_data = service.execute(user)
expect { user_data['email'].to eq(user.email) }
expect { User.find(user.id) }.to raise_error(ActiveRecord::RecordNotFound)
2018-03-17 18:26:18 +05:30
expect { Namespace.find(namespace.id) }.to raise_error(ActiveRecord::RecordNotFound)
2017-08-17 22:00:37 +05:30
end
it 'will delete the project' do
2018-11-08 19:23:39 +05:30
expect_next_instance_of(Projects::DestroyService) do |destroy_service|
2018-11-20 20:47:30 +05:30
expect(destroy_service).to receive(:execute).once.and_return(true)
2018-11-08 19:23:39 +05:30
end
2017-08-17 22:00:37 +05:30
service.execute(user)
end
end
context 'projects in pending_delete' do
before do
project.pending_delete = true
project.save
end
it 'destroys a project in pending_delete' do
2018-11-08 19:23:39 +05:30
expect_next_instance_of(Projects::DestroyService) do |destroy_service|
2018-11-20 20:47:30 +05:30
expect(destroy_service).to receive(:execute).once.and_return(true)
2018-11-08 19:23:39 +05:30
end
2017-08-17 22:00:37 +05:30
service.execute(user)
expect { Project.find(project.id) }.to raise_error(ActiveRecord::RecordNotFound)
end
end
context "a deleted user's issues" do
let(:project) { create(:project) }
before do
project.add_developer(user)
end
context "for an issue the user was assigned to" do
let!(:issue) { create(:issue, project: project, assignees: [user]) }
before do
service.execute(user)
end
it 'does not delete issues the user is assigned to' do
expect(Issue.find_by_id(issue.id)).to be_present
end
it 'migrates the issue so that it is "Unassigned"' do
migrated_issue = Issue.find_by_id(issue.id)
expect(migrated_issue.assignees).to be_empty
end
end
end
context "a deleted user's merge_requests" do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :repository) }
2017-08-17 22:00:37 +05:30
before do
project.add_developer(user)
end
context "for an merge request the user was assigned to" do
2019-07-31 22:56:46 +05:30
let!(:merge_request) { create(:merge_request, source_project: project, assignees: [user]) }
2017-08-17 22:00:37 +05:30
before do
service.execute(user)
end
it 'does not delete merge requests the user is assigned to' do
expect(MergeRequest.find_by_id(merge_request.id)).to be_present
end
it 'migrates the merge request so that it is "Unassigned"' do
migrated_merge_request = MergeRequest.find_by_id(merge_request.id)
2019-07-31 22:56:46 +05:30
expect(migrated_merge_request.assignees).to be_empty
2017-08-17 22:00:37 +05:30
end
end
end
context "solo owned groups present" do
let(:solo_owned) { create(:group) }
let(:member) { create(:group_member) }
let(:user) { member.user }
before do
solo_owned.group_members = [member]
service.execute(user)
end
it 'does not delete the user' do
expect(User.find(user.id)).to eq user
end
end
context "deletions with solo owned groups" do
let(:solo_owned) { create(:group) }
let(:member) { create(:group_member) }
let(:user) { member.user }
before do
solo_owned.group_members = [member]
service.execute(user, delete_solo_owned_groups: true)
end
it 'deletes solo owned groups' do
expect { Project.find(solo_owned.id) }.to raise_error(ActiveRecord::RecordNotFound)
end
it 'deletes the user' do
expect { User.find(user.id) }.to raise_error(ActiveRecord::RecordNotFound)
end
end
context "deletion permission checks" do
it 'does not delete the user when user is not an admin' do
other_user = create(:user)
expect { described_class.new(other_user).execute(user) }.to raise_error(Gitlab::Access::AccessDeniedError)
expect(User.exists?(user.id)).to be(true)
end
it 'allows admins to delete anyone' do
described_class.new(admin).execute(user)
expect(User.exists?(user.id)).to be(false)
end
it 'allows users to delete their own account' do
described_class.new(user).execute(user)
expect(User.exists?(user.id)).to be(false)
end
end
context "migrating associated records" do
2017-09-10 17:25:29 +05:30
let!(:issue) { create(:issue, author: user) }
2017-08-17 22:00:37 +05:30
it 'delegates to the `MigrateToGhostUser` service to move associated records to the ghost user' do
2017-09-10 17:25:29 +05:30
expect_any_instance_of(Users::MigrateToGhostUserService).to receive(:execute).once.and_call_original
2017-08-17 22:00:37 +05:30
service.execute(user)
2017-09-10 17:25:29 +05:30
expect(issue.reload.author).to be_ghost
2017-08-17 22:00:37 +05:30
end
it 'does not run `MigrateToGhostUser` if hard_delete option is given' do
expect_any_instance_of(Users::MigrateToGhostUserService).not_to receive(:execute)
service.execute(user, hard_delete: true)
2017-09-10 17:25:29 +05:30
expect(Issue.exists?(issue.id)).to be_falsy
2017-08-17 22:00:37 +05:30
end
end
2018-03-17 18:26:18 +05:30
describe "user personal's repository removal" do
2018-11-20 20:47:30 +05:30
context 'storages' do
before do
perform_enqueued_jobs { service.execute(user) }
end
context 'legacy storage' do
let!(:project) { create(:project, :empty_repo, :legacy_storage, namespace: user.namespace) }
it 'removes repository' do
2019-12-21 20:55:43 +05:30
expect(gitlab_shell.repository_exists?(project.repository_storage, "#{project.disk_path}.git")).to be_falsey
2018-11-20 20:47:30 +05:30
end
end
2018-03-17 18:26:18 +05:30
2018-11-20 20:47:30 +05:30
context 'hashed storage' do
let!(:project) { create(:project, :empty_repo, namespace: user.namespace) }
2018-03-17 18:26:18 +05:30
2018-11-20 20:47:30 +05:30
it 'removes repository' do
2019-12-21 20:55:43 +05:30
expect(gitlab_shell.repository_exists?(project.repository_storage, "#{project.disk_path}.git")).to be_falsey
2018-11-20 20:47:30 +05:30
end
2018-03-17 18:26:18 +05:30
end
end
2018-11-20 20:47:30 +05:30
context 'repository removal status is taken into account' do
it 'raises exception' do
expect_next_instance_of(::Projects::DestroyService) do |destroy_service|
expect(destroy_service).to receive(:execute).and_return(false)
end
2018-03-17 18:26:18 +05:30
2018-11-20 20:47:30 +05:30
expect { service.execute(user) }
.to raise_error(Users::DestroyService::DestroyError, "Project #{project.id} can't be deleted" )
2018-03-17 18:26:18 +05:30
end
end
end
describe "calls the before/after callbacks" do
it 'of project_members' do
2019-07-31 22:56:46 +05:30
expect_any_instance_of(ProjectMember).to receive(:run_callbacks).with(:find).once
expect_any_instance_of(ProjectMember).to receive(:run_callbacks).with(:initialize).once
2018-03-17 18:26:18 +05:30
expect_any_instance_of(ProjectMember).to receive(:run_callbacks).with(:destroy).once
service.execute(user)
end
it 'of group_members' do
group_member = create(:group_member)
group_member.group.group_members.create(user: user, access_level: 40)
2019-07-31 22:56:46 +05:30
expect_any_instance_of(GroupMember).to receive(:run_callbacks).with(:find).once
expect_any_instance_of(GroupMember).to receive(:run_callbacks).with(:initialize).once
2018-03-17 18:26:18 +05:30
expect_any_instance_of(GroupMember).to receive(:run_callbacks).with(:destroy).once
service.execute(user)
end
end
2017-08-17 22:00:37 +05:30
end
end