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

233 lines
8.1 KiB
Ruby
Raw Normal View History

2016-08-24 12:49:21 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe Members::DestroyService do
2018-03-27 19:54:05 +05:30
let(:current_user) { create(:user) }
2016-11-03 12:29:30 +05:30
let(:member_user) { create(:user) }
let(:group) { create(:group, :public) }
2018-03-27 19:54:05 +05:30
let(:group_project) { create(:project, :public, group: group) }
let(:opts) { {} }
2016-08-24 12:49:21 +05:30
2016-11-03 12:29:30 +05:30
shared_examples 'a service raising ActiveRecord::RecordNotFound' do
it 'raises ActiveRecord::RecordNotFound' do
2018-03-27 19:54:05 +05:30
expect { described_class.new(current_user).execute(member) }.to raise_error(ActiveRecord::RecordNotFound)
2016-08-24 12:49:21 +05:30
end
2016-11-03 12:29:30 +05:30
end
2016-08-24 12:49:21 +05:30
2016-11-03 12:29:30 +05:30
shared_examples 'a service raising Gitlab::Access::AccessDeniedError' do
it 'raises Gitlab::Access::AccessDeniedError' do
2018-03-27 19:54:05 +05:30
expect { described_class.new(current_user).execute(member) }.to raise_error(Gitlab::Access::AccessDeniedError)
2016-08-24 12:49:21 +05:30
end
end
2016-11-03 12:29:30 +05:30
shared_examples 'a service destroying a member' do
2018-11-18 11:00:15 +05:30
before do
type = member.is_a?(GroupMember) ? 'Group' : 'Project'
expect(TodosDestroyer::EntityLeaveWorker).to receive(:perform_in).with(1.hour, member.user_id, member.source_id, type)
end
2016-11-03 12:29:30 +05:30
it 'destroys the member' do
2018-03-27 19:54:05 +05:30
expect { described_class.new(current_user).execute(member, opts) }.to change { member.source.members_and_requesters.count }.by(-1)
2016-11-03 12:29:30 +05:30
end
2018-03-27 19:54:05 +05:30
it 'destroys member notification_settings' do
if member_user.notification_settings.any?
expect { described_class.new(current_user).execute(member, opts) }
.to change { member_user.notification_settings.count }.by(-1)
else
expect { described_class.new(current_user).execute(member, opts) }
.not_to change { member_user.notification_settings.count }
2016-11-03 12:29:30 +05:30
end
2018-03-27 19:54:05 +05:30
end
end
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
shared_examples 'a service destroying a member with access' do
it_behaves_like 'a service destroying a member'
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
it 'invalidates cached counts for todos and assigned issues and merge requests', :aggregate_failures do
create(:issue, project: group_project, assignees: [member_user])
create(:merge_request, source_project: group_project, assignee: member_user)
create(:todo, :pending, project: group_project, user: member_user)
create(:todo, :done, project: group_project, user: member_user)
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
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)
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
described_class.new(current_user).execute(member, opts)
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
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)
end
end
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
shared_examples 'a service destroying an access requester' do
it_behaves_like 'a service destroying a member'
it 'calls Member#after_decline_request' do
expect_any_instance_of(NotificationService).to receive(:decline_access_request).with(member)
described_class.new(current_user).execute(member)
end
context 'when current user is the member' do
it 'does not call Member#after_decline_request' do
expect_any_instance_of(NotificationService).not_to receive(:decline_access_request).with(member)
described_class.new(member_user).execute(member)
2016-11-03 12:29:30 +05:30
end
2016-08-24 12:49:21 +05:30
end
2016-11-03 12:29:30 +05:30
end
2018-03-27 19:54:05 +05:30
context 'with a member with access' do
before do
group_project.update_attribute(:visibility_level, Gitlab::VisibilityLevel::PRIVATE)
group.update_attribute(:visibility_level, Gitlab::VisibilityLevel::PRIVATE)
end
context 'when current user cannot destroy the given member' do
context 'with a project member' do
let(:member) { group_project.members.find_by(user_id: member_user.id) }
before do
group_project.add_developer(member_user)
end
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError'
2016-08-24 12:49:21 +05:30
2018-03-27 19:54:05 +05:30
it_behaves_like 'a service destroying a member with access' do
let(:opts) { { skip_authorization: true } }
end
end
context 'with a group member' do
let(:member) { group.members.find_by(user_id: member_user.id) }
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
let(:opts) { { skip_authorization: true } }
end
end
2016-11-03 12:29:30 +05:30
end
2018-03-27 19:54:05 +05:30
context 'when current user can destroy the given member' do
before do
2018-11-18 11:00:15 +05:30
group_project.add_maintainer(current_user)
2018-03-27 19:54:05 +05:30
group.add_owner(current_user)
end
context 'with a project member' do
let(:member) { group_project.members.find_by(user_id: member_user.id) }
before do
group_project.add_developer(member_user)
end
it_behaves_like 'a service destroying a member with access'
end
context 'with a group member' do
let(:member) { group.members.find_by(user_id: member_user.id) }
before do
group.add_developer(member_user)
end
it_behaves_like 'a service destroying a member with access'
end
2016-08-24 12:49:21 +05:30
end
end
2018-03-27 19:54:05 +05:30
context 'with an access requester' do
2016-08-24 12:49:21 +05:30
before do
2018-11-18 11:00:15 +05:30
group_project.update(request_access_enabled: true)
group.update(request_access_enabled: true)
2018-03-27 19:54:05 +05:30
group_project.request_access(member_user)
group.request_access(member_user)
2016-08-24 12:49:21 +05:30
end
2018-03-27 19:54:05 +05:30
context 'when current user cannot destroy the given access requester' do
2016-11-03 12:29:30 +05:30
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
2018-03-27 19:54:05 +05:30
let(:member) { group_project.requesters.find_by(user_id: member_user.id) }
end
it_behaves_like 'a service destroying a member' do
let(:opts) { { skip_authorization: true } }
let(:member) { group_project.requesters.find_by(user_id: member_user.id) }
2016-11-03 12:29:30 +05:30
end
2016-08-24 12:49:21 +05:30
2016-11-03 12:29:30 +05:30
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
2018-03-27 19:54:05 +05:30
let(:member) { group.requesters.find_by(user_id: member_user.id) }
end
it_behaves_like 'a service destroying a member' do
let(:opts) { { skip_authorization: true } }
let(:member) { group.requesters.find_by(user_id: member_user.id) }
2016-11-03 12:29:30 +05:30
end
2016-08-24 12:49:21 +05:30
end
2018-03-27 19:54:05 +05:30
context 'when current user can destroy the given access requester' do
2016-08-24 12:49:21 +05:30
before do
2018-11-18 11:00:15 +05:30
group_project.add_maintainer(current_user)
2018-03-27 19:54:05 +05:30
group.add_owner(current_user)
end
it_behaves_like 'a service destroying an access requester' do
let(:member) { group_project.requesters.find_by(user_id: member_user.id) }
end
it_behaves_like 'a service destroying an access requester' do
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 }
2016-08-24 12:49:21 +05:30
end
2016-11-03 12:29:30 +05:30
it_behaves_like 'a service destroying a member' do
2018-03-27 19:54:05 +05:30
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 }
2016-08-24 12:49:21 +05:30
end
2016-11-03 12:29:30 +05:30
it_behaves_like 'a service destroying a member' do
2018-03-27 19:54:05 +05:30
let(:opts) { { skip_authorization: true } }
let(:member) { group_invited_member }
2016-08-24 12:49:21 +05:30
end
2018-03-27 19:54:05 +05:30
end
2016-08-24 12:49:21 +05:30
2018-03-27 19:54:05 +05:30
context 'when current user can destroy the given invited user' do
before do
2018-11-18 11:00:15 +05:30
group_project.add_maintainer(current_user)
2018-03-27 19:54:05 +05:30
group.add_owner(current_user)
end
2016-08-24 12:49:21 +05:30
2018-03-27 19:54:05 +05:30
# Regression spec for issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/32504
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-08-24 12:49:21 +05:30
end
end
end
end