2016-03-14 06:18:03 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Users::DestroyService do
|
2016-03-14 06:18:03 -04:00
|
|
|
describe "Deletes a user and all their personal projects" do
|
2017-02-04 03:14:17 -05:00
|
|
|
let!(:user) { create(:user) }
|
|
|
|
let!(:admin) { create(:admin) }
|
2017-08-21 09:28:07 -04:00
|
|
|
let!(:namespace) { user.namespace }
|
2017-08-02 15:55:11 -04:00
|
|
|
let!(:project) { create(:project, namespace: namespace) }
|
2017-02-04 03:14:17 -05:00
|
|
|
let(:service) { described_class.new(admin) }
|
2017-08-21 09:28:07 -04:00
|
|
|
let(:gitlab_shell) { Gitlab::Shell.new }
|
2016-03-14 06:18:03 -04:00
|
|
|
|
|
|
|
context 'no options are given' do
|
|
|
|
it 'deletes the user' do
|
2016-08-13 08:45:31 -04:00
|
|
|
user_data = service.execute(user)
|
2016-03-14 06:18:03 -04:00
|
|
|
|
2016-05-28 22:54:17 -04:00
|
|
|
expect { user_data['email'].to eq(user.email) }
|
|
|
|
expect { User.find(user.id) }.to raise_error(ActiveRecord::RecordNotFound)
|
2018-01-02 11:21:28 -05:00
|
|
|
expect { Namespace.find(namespace.id) }.to raise_error(ActiveRecord::RecordNotFound)
|
2016-03-14 06:18:03 -04:00
|
|
|
end
|
|
|
|
|
2017-03-31 19:59:46 -04:00
|
|
|
it 'will delete the project' do
|
2018-06-21 10:54:11 -04:00
|
|
|
expect_next_instance_of(Projects::DestroyService) do |destroy_service|
|
2018-08-13 07:54:31 -04:00
|
|
|
expect(destroy_service).to receive(:execute).once.and_return(true)
|
2018-06-21 10:54:11 -04:00
|
|
|
end
|
2016-03-14 06:18:03 -04:00
|
|
|
|
2016-08-13 08:45:31 -04:00
|
|
|
service.execute(user)
|
2016-03-14 06:18:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-31 19:59:46 -04:00
|
|
|
context 'projects in pending_delete' do
|
|
|
|
before do
|
|
|
|
project.pending_delete = true
|
|
|
|
project.save
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'destroys a project in pending_delete' do
|
2018-06-21 10:54:11 -04:00
|
|
|
expect_next_instance_of(Projects::DestroyService) do |destroy_service|
|
2018-08-13 07:54:31 -04:00
|
|
|
expect(destroy_service).to receive(:execute).once.and_return(true)
|
2018-06-21 10:54:11 -04:00
|
|
|
end
|
2017-03-31 19:59:46 -04:00
|
|
|
|
|
|
|
service.execute(user)
|
|
|
|
|
|
|
|
expect { Project.find(project.id) }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-11 01:27:43 -05:00
|
|
|
context "a deleted user's issues" do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project) }
|
2016-11-11 01:27:43 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
2017-04-06 13:03:07 -04:00
|
|
|
context "for an issue the user was assigned to" do
|
2017-05-04 16:55:36 -04:00
|
|
|
let!(:issue) { create(:issue, project: project, assignees: [user]) }
|
2016-11-11 01:27:43 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
service.execute(user)
|
|
|
|
end
|
|
|
|
|
2017-04-06 13:03:07 -04:00
|
|
|
it 'does not delete issues the user is assigned to' do
|
2016-11-11 01:27:43 -05:00
|
|
|
expect(Issue.find_by_id(issue.id)).to be_present
|
|
|
|
end
|
|
|
|
|
2017-04-06 13:03:07 -04:00
|
|
|
it 'migrates the issue so that it is "Unassigned"' do
|
2016-11-11 01:27:43 -05:00
|
|
|
migrated_issue = Issue.find_by_id(issue.id)
|
|
|
|
|
2017-05-05 06:41:35 -04:00
|
|
|
expect(migrated_issue.assignees).to be_empty
|
2016-11-11 01:27:43 -05:00
|
|
|
end
|
2017-04-06 13:03:07 -04:00
|
|
|
end
|
|
|
|
end
|
2017-02-16 02:05:10 -05:00
|
|
|
|
2017-04-06 13:03:07 -04:00
|
|
|
context "a deleted user's merge_requests" do
|
2017-08-01 14:51:52 -04:00
|
|
|
let(:project) { create(:project, :repository) }
|
2017-04-06 13:03:07 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
2016-11-11 01:27:43 -05:00
|
|
|
end
|
|
|
|
|
2017-04-06 13:03:07 -04:00
|
|
|
context "for an merge request the user was assigned to" do
|
|
|
|
let!(:merge_request) { create(:merge_request, source_project: project, assignee: user) }
|
2016-11-11 01:27:43 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
service.execute(user)
|
|
|
|
end
|
|
|
|
|
2017-04-06 13:03:07 -04:00
|
|
|
it 'does not delete merge requests the user is assigned to' do
|
|
|
|
expect(MergeRequest.find_by_id(merge_request.id)).to be_present
|
2016-11-11 01:27:43 -05:00
|
|
|
end
|
|
|
|
|
2017-04-06 13:03:07 -04:00
|
|
|
it 'migrates the merge request so that it is "Unassigned"' do
|
|
|
|
migrated_merge_request = MergeRequest.find_by_id(merge_request.id)
|
2016-11-11 01:27:43 -05:00
|
|
|
|
2017-04-06 13:03:07 -04:00
|
|
|
expect(migrated_merge_request.assignee).to be_nil
|
2016-11-11 01:27:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-14 06:18:03 -04:00
|
|
|
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]
|
2016-08-13 08:45:31 -04:00
|
|
|
service.execute(user)
|
2016-03-14 06:18:03 -04:00
|
|
|
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]
|
2016-08-13 08:45:31 -04:00
|
|
|
service.execute(user, delete_solo_owned_groups: true)
|
2016-03-14 06:18:03 -04:00
|
|
|
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
|
2017-02-04 03:14:17 -05:00
|
|
|
|
|
|
|
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
|
2017-04-05 03:20:53 -04:00
|
|
|
|
2017-04-06 13:03:07 -04:00
|
|
|
context "migrating associated records" do
|
2017-06-01 12:30:01 -04:00
|
|
|
let!(:issue) { create(:issue, author: user) }
|
|
|
|
|
2017-04-06 13:03:07 -04:00
|
|
|
it 'delegates to the `MigrateToGhostUser` service to move associated records to the ghost user' do
|
2017-06-01 12:30:01 -04:00
|
|
|
expect_any_instance_of(Users::MigrateToGhostUserService).to receive(:execute).once.and_call_original
|
2017-04-06 06:06:36 -04:00
|
|
|
|
2017-04-06 13:03:07 -04:00
|
|
|
service.execute(user)
|
2017-06-01 12:30:01 -04:00
|
|
|
|
|
|
|
expect(issue.reload.author).to be_ghost
|
2017-04-05 12:55:52 -04:00
|
|
|
end
|
2017-04-16 11:21:40 -04:00
|
|
|
|
|
|
|
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-06-01 12:30:01 -04:00
|
|
|
|
|
|
|
expect(Issue.exists?(issue.id)).to be_falsy
|
2017-04-16 11:21:40 -04:00
|
|
|
end
|
2017-04-05 03:20:53 -04:00
|
|
|
end
|
2017-08-21 09:28:07 -04:00
|
|
|
|
|
|
|
describe "user personal's repository removal" do
|
2018-08-13 07:54:31 -04:00
|
|
|
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
|
|
|
|
expect(gitlab_shell.exists?(project.repository_storage, "#{project.disk_path}.git")).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
2017-08-21 09:28:07 -04:00
|
|
|
|
2018-08-13 07:54:31 -04:00
|
|
|
context 'hashed storage' do
|
|
|
|
let!(:project) { create(:project, :empty_repo, namespace: user.namespace) }
|
2017-08-21 09:28:07 -04:00
|
|
|
|
2018-08-13 07:54:31 -04:00
|
|
|
it 'removes repository' do
|
|
|
|
expect(gitlab_shell.exists?(project.repository_storage, "#{project.disk_path}.git")).to be_falsey
|
|
|
|
end
|
2017-08-21 09:28:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-13 07:54:31 -04:00
|
|
|
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
|
2017-08-21 09:28:07 -04:00
|
|
|
|
2018-08-13 07:54:31 -04:00
|
|
|
expect { service.execute(user) }
|
|
|
|
.to raise_error(Users::DestroyService::DestroyError, "Project #{project.id} can't be deleted" )
|
2017-08-21 09:28:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-01-02 10:06:44 -05:00
|
|
|
|
|
|
|
describe "calls the before/after callbacks" do
|
|
|
|
it 'of project_members' do
|
|
|
|
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)
|
|
|
|
|
|
|
|
expect_any_instance_of(GroupMember).to receive(:run_callbacks).with(:destroy).once
|
|
|
|
|
|
|
|
service.execute(user)
|
|
|
|
end
|
|
|
|
end
|
2016-03-14 06:18:03 -04:00
|
|
|
end
|
|
|
|
end
|