2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-17 08:06:55 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe Members::DestroyService do
|
2017-10-11 10:47:08 -04:00
|
|
|
let(:current_user) { create(:user) }
|
2016-07-28 13:31:17 -04:00
|
|
|
let(:member_user) { create(:user) }
|
|
|
|
let(:group) { create(:group, :public) }
|
2018-02-16 09:10:22 -05:00
|
|
|
let(:group_project) { create(:project, :public, group: group) }
|
|
|
|
let(:opts) { {} }
|
2016-06-17 08:06:55 -04:00
|
|
|
|
2016-07-28 13:31:17 -04:00
|
|
|
shared_examples 'a service raising ActiveRecord::RecordNotFound' do
|
|
|
|
it 'raises ActiveRecord::RecordNotFound' do
|
2018-02-16 09:10:22 -05:00
|
|
|
expect { described_class.new(current_user).execute(member) }.to raise_error(ActiveRecord::RecordNotFound)
|
2016-06-17 12:59:33 -04:00
|
|
|
end
|
2016-07-28 13:31:17 -04:00
|
|
|
end
|
2016-06-17 12:59:33 -04:00
|
|
|
|
2016-07-28 13:31:17 -04:00
|
|
|
shared_examples 'a service raising Gitlab::Access::AccessDeniedError' do
|
|
|
|
it 'raises Gitlab::Access::AccessDeniedError' do
|
2018-02-16 09:10:22 -05:00
|
|
|
expect { described_class.new(current_user).execute(member) }.to raise_error(Gitlab::Access::AccessDeniedError)
|
2016-06-17 12:59:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-28 13:31:17 -04:00
|
|
|
shared_examples 'a service destroying a member' do
|
2018-07-16 14:30:17 -04:00
|
|
|
before do
|
|
|
|
type = member.is_a?(GroupMember) ? 'Group' : 'Project'
|
2018-12-11 13:15:10 -05:00
|
|
|
expect(TodosDestroyer::EntityLeaveWorker).to receive(:perform_in).with(Todo::WAIT_FOR_DELETE, member.user_id, member.source_id, type)
|
2020-07-13 08:09:18 -04:00
|
|
|
expect(MembersDestroyer::UnassignIssuablesWorker).to receive(:perform_async).with(member.user_id, member.source_id, type) if opts[:unassign_issuables]
|
2018-07-16 14:30:17 -04:00
|
|
|
end
|
|
|
|
|
2016-07-28 13:31:17 -04:00
|
|
|
it 'destroys the member' do
|
2020-09-29 11:10:08 -04:00
|
|
|
expect { described_class.new(current_user).execute(member, **opts) }.to change { member.source.members_and_requesters.count }.by(-1)
|
2018-02-16 09:10:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'destroys member notification_settings' do
|
|
|
|
if member_user.notification_settings.any?
|
2020-09-29 11:10:08 -04:00
|
|
|
expect { described_class.new(current_user).execute(member, **opts) }
|
2018-02-16 09:10:22 -05:00
|
|
|
.to change { member_user.notification_settings.count }.by(-1)
|
|
|
|
else
|
2020-09-29 11:10:08 -04:00
|
|
|
expect { described_class.new(current_user).execute(member, **opts) }
|
2018-02-16 09:10:22 -05:00
|
|
|
.not_to change { member_user.notification_settings.count }
|
|
|
|
end
|
2016-06-17 08:06:55 -04:00
|
|
|
end
|
2017-10-11 10:47:08 -04:00
|
|
|
end
|
2016-06-17 08:06:55 -04:00
|
|
|
|
2018-03-07 14:54:28 -05:00
|
|
|
shared_examples 'a service destroying a member with access' do
|
|
|
|
it_behaves_like 'a service destroying a member'
|
|
|
|
|
2019-10-23 05:06:03 -04:00
|
|
|
it 'invalidates cached counts for assigned issues and merge requests', :aggregate_failures, :sidekiq_might_not_need_inline do
|
2018-03-07 14:54:28 -05:00
|
|
|
create(:issue, project: group_project, assignees: [member_user])
|
2019-04-07 14:35:16 -04:00
|
|
|
create(:merge_request, source_project: group_project, assignees: [member_user])
|
2018-03-07 14:54:28 -05:00
|
|
|
create(:todo, :pending, project: group_project, user: member_user)
|
|
|
|
create(:todo, :done, project: group_project, user: member_user)
|
|
|
|
|
|
|
|
expect(member_user.assigned_open_merge_requests_count).to be(1)
|
|
|
|
expect(member_user.assigned_open_issues_count).to be(1)
|
|
|
|
expect(member_user.todos_pending_count).to be(1)
|
|
|
|
expect(member_user.todos_done_count).to be(1)
|
|
|
|
|
2020-06-23 11:08:41 -04:00
|
|
|
service = described_class.new(current_user)
|
|
|
|
|
|
|
|
if opts[:unassign_issuables]
|
|
|
|
expect(service).to receive(:enqueue_unassign_issuables).with(member)
|
|
|
|
end
|
|
|
|
|
2020-09-29 11:10:08 -04:00
|
|
|
service.execute(member, **opts)
|
2018-03-07 14:54:28 -05:00
|
|
|
|
|
|
|
expect(member_user.assigned_open_merge_requests_count).to be(0)
|
|
|
|
expect(member_user.assigned_open_issues_count).to be(0)
|
|
|
|
expect(member_user.todos_pending_count).to be(0)
|
|
|
|
expect(member_user.todos_done_count).to be(0)
|
2020-06-23 11:08:41 -04:00
|
|
|
|
|
|
|
unless opts[:unassign_issuables]
|
|
|
|
expect(member_user.assigned_merge_requests.opened.count).to be(1)
|
|
|
|
expect(member_user.assigned_issues.opened.count).to be(1)
|
|
|
|
end
|
2018-03-07 14:54:28 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-11 10:47:08 -04:00
|
|
|
shared_examples 'a service destroying an access requester' do
|
2018-02-16 09:10:22 -05:00
|
|
|
it_behaves_like 'a service destroying a member'
|
2016-06-17 08:06:55 -04:00
|
|
|
|
2017-10-11 10:47:08 -04:00
|
|
|
it 'calls Member#after_decline_request' do
|
2018-02-16 09:10:22 -05:00
|
|
|
expect_any_instance_of(NotificationService).to receive(:decline_access_request).with(member)
|
2016-07-28 13:31:17 -04:00
|
|
|
|
2020-09-29 11:10:08 -04:00
|
|
|
described_class.new(current_user).execute(member, **opts)
|
2016-06-17 08:06:55 -04:00
|
|
|
end
|
|
|
|
|
2017-10-11 10:47:08 -04:00
|
|
|
context 'when current user is the member' do
|
|
|
|
it 'does not call Member#after_decline_request' do
|
2018-02-16 09:10:22 -05:00
|
|
|
expect_any_instance_of(NotificationService).not_to receive(:decline_access_request).with(member)
|
2017-10-11 10:47:08 -04:00
|
|
|
|
2020-09-29 11:10:08 -04:00
|
|
|
described_class.new(member_user).execute(member, **opts)
|
2017-10-11 10:47:08 -04:00
|
|
|
end
|
2016-07-28 13:31:17 -04:00
|
|
|
end
|
|
|
|
end
|
2016-06-17 12:59:33 -04:00
|
|
|
|
2018-03-07 14:54:28 -05:00
|
|
|
context 'with a member with access' do
|
2016-07-28 13:31:17 -04:00
|
|
|
before do
|
2018-03-07 14:54:28 -05:00
|
|
|
group_project.update_attribute(:visibility_level, Gitlab::VisibilityLevel::PRIVATE)
|
|
|
|
group.update_attribute(:visibility_level, Gitlab::VisibilityLevel::PRIVATE)
|
2016-06-17 08:06:55 -04:00
|
|
|
end
|
|
|
|
|
2016-07-28 13:31:17 -04:00
|
|
|
context 'when current user cannot destroy the given member' do
|
2018-03-07 14:54:28 -05:00
|
|
|
context 'with a project member' do
|
2018-02-16 09:10:22 -05:00
|
|
|
let(:member) { group_project.members.find_by(user_id: member_user.id) }
|
|
|
|
|
2018-03-07 14:54:28 -05:00
|
|
|
before do
|
|
|
|
group_project.add_developer(member_user)
|
|
|
|
end
|
2016-06-17 08:06:55 -04:00
|
|
|
|
2018-03-07 14:54:28 -05:00
|
|
|
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError'
|
|
|
|
|
|
|
|
it_behaves_like 'a service destroying a member with access' do
|
2020-06-23 11:08:41 -04:00
|
|
|
let(:opts) { { skip_authorization: true, unassign_issuables: true } }
|
2018-03-07 14:54:28 -05:00
|
|
|
end
|
2018-02-16 09:10:22 -05:00
|
|
|
end
|
|
|
|
|
2018-03-07 14:54:28 -05:00
|
|
|
context 'with a group member' do
|
2018-02-16 09:10:22 -05:00
|
|
|
let(:member) { group.members.find_by(user_id: member_user.id) }
|
2018-03-07 14:54:28 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
group.add_developer(member_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError'
|
|
|
|
|
|
|
|
it_behaves_like 'a service destroying a member with access' do
|
2020-06-23 11:08:41 -04:00
|
|
|
let(:opts) { { skip_authorization: true, unassign_issuables: true } }
|
2018-03-07 14:54:28 -05:00
|
|
|
end
|
2016-06-17 08:06:55 -04:00
|
|
|
end
|
2016-07-28 13:31:17 -04:00
|
|
|
end
|
2016-06-17 10:33:37 -04:00
|
|
|
|
2016-07-28 13:31:17 -04:00
|
|
|
context 'when current user can destroy the given member' do
|
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
group_project.add_maintainer(current_user)
|
2017-10-11 10:47:08 -04:00
|
|
|
group.add_owner(current_user)
|
2016-06-17 10:33:37 -04:00
|
|
|
end
|
2016-06-17 12:59:33 -04:00
|
|
|
|
2018-03-07 14:54:28 -05:00
|
|
|
context 'with a project member' do
|
2018-02-16 09:10:22 -05:00
|
|
|
let(:member) { group_project.members.find_by(user_id: member_user.id) }
|
2018-03-07 14:54:28 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
group_project.add_developer(member_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a service destroying a member with access'
|
2020-06-23 11:08:41 -04:00
|
|
|
|
|
|
|
context 'unassign issuables' do
|
|
|
|
it_behaves_like 'a service destroying a member with access' do
|
|
|
|
let(:opts) { { unassign_issuables: true } }
|
|
|
|
end
|
|
|
|
end
|
2016-07-28 13:31:17 -04:00
|
|
|
end
|
2016-06-17 12:59:33 -04:00
|
|
|
|
2020-07-14 20:09:23 -04:00
|
|
|
context 'with a project bot member' do
|
|
|
|
let(:member) { group_project.members.find_by(user_id: member_user.id) }
|
|
|
|
let(:member_user) { create(:user, :project_bot) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
group_project.add_maintainer(member_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the destroy_bot flag is true' do
|
|
|
|
it_behaves_like 'a service destroying a member with access' do
|
|
|
|
let(:opts) { { destroy_bot: true } }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the destroy_bot flag is not specified' do
|
|
|
|
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-07 14:54:28 -05:00
|
|
|
context 'with a group member' do
|
2018-02-16 09:10:22 -05:00
|
|
|
let(:member) { group.members.find_by(user_id: member_user.id) }
|
2018-03-07 14:54:28 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
group.add_developer(member_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a service destroying a member with access'
|
2020-06-23 11:08:41 -04:00
|
|
|
|
|
|
|
context 'unassign issuables' do
|
|
|
|
it_behaves_like 'a service destroying a member with access' do
|
|
|
|
let(:opts) { { unassign_issuables: true } }
|
|
|
|
end
|
|
|
|
end
|
2016-06-17 12:59:33 -04:00
|
|
|
end
|
2017-10-11 10:47:08 -04:00
|
|
|
end
|
|
|
|
end
|
2016-09-09 12:51:31 -04:00
|
|
|
|
2017-10-11 10:47:08 -04:00
|
|
|
context 'with an access requester' do
|
|
|
|
before do
|
2020-09-07 11:09:04 -04:00
|
|
|
group_project.update!(request_access_enabled: true)
|
|
|
|
group.update!(request_access_enabled: true)
|
2018-02-16 09:10:22 -05:00
|
|
|
group_project.request_access(member_user)
|
2017-10-11 10:47:08 -04:00
|
|
|
group.request_access(member_user)
|
|
|
|
end
|
2016-09-09 12:51:31 -04:00
|
|
|
|
2017-10-11 10:47:08 -04:00
|
|
|
context 'when current user cannot destroy the given access requester' do
|
|
|
|
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
|
2018-02-16 09:10:22 -05:00
|
|
|
let(:member) { group_project.requesters.find_by(user_id: member_user.id) }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a service destroying a member' do
|
2018-11-16 10:09:32 -05:00
|
|
|
let(:opts) { { skip_authorization: true, skip_subresources: true } }
|
2018-02-16 09:10:22 -05:00
|
|
|
let(:member) { group_project.requesters.find_by(user_id: member_user.id) }
|
2017-10-11 10:47:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
|
2018-02-16 09:10:22 -05:00
|
|
|
let(:member) { group.requesters.find_by(user_id: member_user.id) }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a service destroying a member' do
|
2019-01-31 15:31:25 -05:00
|
|
|
let(:opts) { { skip_authorization: true, skip_subresources: true } }
|
2018-02-16 09:10:22 -05:00
|
|
|
let(:member) { group.requesters.find_by(user_id: member_user.id) }
|
2017-10-11 10:47:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when current user can destroy the given access requester' do
|
2018-11-16 10:09:32 -05:00
|
|
|
let(:opts) { { skip_subresources: true } }
|
|
|
|
|
2017-10-11 10:47:08 -04:00
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
group_project.add_maintainer(current_user)
|
2017-10-11 10:47:08 -04:00
|
|
|
group.add_owner(current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a service destroying an access requester' do
|
2018-02-16 09:10:22 -05:00
|
|
|
let(:member) { group_project.requesters.find_by(user_id: member_user.id) }
|
2017-10-11 10:47:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a service destroying an access requester' do
|
2018-02-16 09:10:22 -05:00
|
|
|
let(:member) { group.requesters.find_by(user_id: member_user.id) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an invited user' do
|
|
|
|
let(:project_invited_member) { create(:project_member, :invited, project: group_project) }
|
|
|
|
let(:group_invited_member) { create(:group_member, :invited, group: group) }
|
|
|
|
|
|
|
|
context 'when current user cannot destroy the given invited user' do
|
|
|
|
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
|
|
|
|
let(:member) { project_invited_member }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a service destroying a member' do
|
|
|
|
let(:opts) { { skip_authorization: true } }
|
|
|
|
let(:member) { project_invited_member }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
|
|
|
|
let(:member) { group_invited_member }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a service destroying a member' do
|
|
|
|
let(:opts) { { skip_authorization: true } }
|
|
|
|
let(:member) { group_invited_member }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when current user can destroy the given invited user' do
|
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
group_project.add_maintainer(current_user)
|
2018-02-16 09:10:22 -05:00
|
|
|
group.add_owner(current_user)
|
|
|
|
end
|
|
|
|
|
2019-09-18 10:02:45 -04:00
|
|
|
# Regression spec for issue: https://gitlab.com/gitlab-org/gitlab-foss/issues/32504
|
2018-02-16 09:10:22 -05:00
|
|
|
it_behaves_like 'a service destroying a member' do
|
|
|
|
let(:member) { project_invited_member }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a service destroying a member' do
|
|
|
|
let(:member) { group_invited_member }
|
2016-09-09 12:51:31 -04:00
|
|
|
end
|
2016-06-17 08:06:55 -04:00
|
|
|
end
|
|
|
|
end
|
2018-11-16 10:09:32 -05:00
|
|
|
|
|
|
|
context 'subresources' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:member_user) { create(:user) }
|
|
|
|
|
|
|
|
let(:group) { create(:group, :public) }
|
|
|
|
let(:subgroup) { create(:group, parent: group) }
|
|
|
|
let(:subsubgroup) { create(:group, parent: subgroup) }
|
|
|
|
let(:subsubproject) { create(:project, group: subsubgroup) }
|
|
|
|
|
|
|
|
let(:group_project) { create(:project, :public, group: group) }
|
|
|
|
let(:control_project) { create(:project, group: subsubgroup) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:group_member, :developer, group: subsubgroup, user: member_user)
|
2020-09-02 11:10:54 -04:00
|
|
|
create(:project_member, :invited, project: group_project, created_by: member_user)
|
|
|
|
create(:group_member, :invited, group: group, created_by: member_user)
|
|
|
|
create(:project_member, :invited, project: subsubproject, created_by: member_user)
|
|
|
|
create(:group_member, :invited, group: subgroup, created_by: member_user)
|
2018-11-16 10:09:32 -05:00
|
|
|
|
|
|
|
subsubproject.add_developer(member_user)
|
|
|
|
control_project.add_maintainer(user)
|
|
|
|
group.add_owner(user)
|
|
|
|
|
|
|
|
group_member = create(:group_member, :developer, group: group, user: member_user)
|
|
|
|
|
2020-09-29 11:10:08 -04:00
|
|
|
described_class.new(user).execute(group_member)
|
2018-11-16 10:09:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes the project membership' do
|
|
|
|
expect(group_project.members.map(&:user)).not_to include(member_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes the group membership' do
|
|
|
|
expect(group.members.map(&:user)).not_to include(member_user)
|
|
|
|
end
|
|
|
|
|
2019-07-24 05:20:54 -04:00
|
|
|
it 'removes the subgroup membership' do
|
2018-11-16 10:09:32 -05:00
|
|
|
expect(subgroup.members.map(&:user)).not_to include(member_user)
|
|
|
|
end
|
|
|
|
|
2019-07-24 05:20:54 -04:00
|
|
|
it 'removes the subsubgroup membership' do
|
2018-11-16 10:09:32 -05:00
|
|
|
expect(subsubgroup.members.map(&:user)).not_to include(member_user)
|
|
|
|
end
|
|
|
|
|
2019-07-24 05:20:54 -04:00
|
|
|
it 'removes the subsubproject membership' do
|
2018-11-16 10:09:32 -05:00
|
|
|
expect(subsubproject.members.map(&:user)).not_to include(member_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not remove the user from the control project' do
|
|
|
|
expect(control_project.members.map(&:user)).to include(user)
|
|
|
|
end
|
2020-09-02 11:10:54 -04:00
|
|
|
|
|
|
|
it 'removes group members invited by deleted user' do
|
|
|
|
expect(group.members.not_accepted_invitations_by_user(member_user)).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes project members invited by deleted user' do
|
|
|
|
expect(group_project.members.not_accepted_invitations_by_user(member_user)).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes subgroup members invited by deleted user' do
|
|
|
|
expect(subgroup.members.not_accepted_invitations_by_user(member_user)).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes subproject members invited by deleted user' do
|
|
|
|
expect(subsubproject.members.not_accepted_invitations_by_user(member_user)).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'deletion of invitations created by deleted project member' do
|
|
|
|
let(:user) { project.owner }
|
|
|
|
let(:member_user) { create(:user) }
|
|
|
|
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:project_member, :invited, project: project, created_by: member_user)
|
|
|
|
|
|
|
|
project_member = create(:project_member, :maintainer, user: member_user, project: project)
|
|
|
|
|
2020-09-29 11:10:08 -04:00
|
|
|
described_class.new(user).execute(project_member)
|
2020-09-02 11:10:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes project members invited by deleted user' do
|
|
|
|
expect(project.members.not_accepted_invitations_by_user(member_user)).to be_empty
|
|
|
|
end
|
2018-11-16 10:09:32 -05:00
|
|
|
end
|
2016-06-17 08:06:55 -04:00
|
|
|
end
|