2016-06-17 08:06:55 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Members::DestroyService do
|
2016-06-17 08:06:55 -04:00
|
|
|
let(:user) { create(:user) }
|
2016-07-28 13:31:17 -04:00
|
|
|
let(:member_user) { create(:user) }
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project, :public) }
|
2016-07-28 13:31:17 -04:00
|
|
|
let(:group) { create(:group, :public) }
|
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
|
|
|
|
expect { described_class.new(source, user, params).execute }.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
|
|
|
|
expect { described_class.new(source, user, params).execute }.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
|
|
|
|
it 'destroys the member' do
|
|
|
|
expect { described_class.new(source, user, params).execute }.to change { source.members.count }.by(-1)
|
2016-06-17 08:06:55 -04:00
|
|
|
end
|
|
|
|
|
2016-07-28 13:31:17 -04:00
|
|
|
context 'when the given member is an access requester' do
|
|
|
|
before do
|
|
|
|
source.members.find_by(user_id: member_user).destroy
|
2016-11-11 07:51:50 -05:00
|
|
|
source.update_attributes(request_access_enabled: true)
|
2016-07-28 13:31:17 -04:00
|
|
|
source.request_access(member_user)
|
|
|
|
end
|
|
|
|
let(:access_requester) { source.requesters.find_by(user_id: member_user) }
|
|
|
|
|
|
|
|
it_behaves_like 'a service raising ActiveRecord::RecordNotFound'
|
|
|
|
|
|
|
|
%i[requesters all].each do |scope|
|
|
|
|
context "and #{scope} scope is passed" do
|
|
|
|
it 'destroys the access requester' do
|
|
|
|
expect { described_class.new(source, user, params).execute(scope) }.to change { source.requesters.count }.by(-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls Member#after_decline_request' do
|
|
|
|
expect_any_instance_of(NotificationService).to receive(:decline_access_request).with(access_requester)
|
|
|
|
|
|
|
|
described_class.new(source, user, params).execute(scope)
|
|
|
|
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(access_requester)
|
|
|
|
|
|
|
|
described_class.new(source, member_user, params).execute(scope)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-06-17 08:06:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-28 13:31:17 -04:00
|
|
|
context 'when no member are found' do
|
|
|
|
let(:params) { { user_id: 42 } }
|
|
|
|
|
|
|
|
it_behaves_like 'a service raising ActiveRecord::RecordNotFound' do
|
|
|
|
let(:source) { project }
|
2016-06-17 08:06:55 -04:00
|
|
|
end
|
|
|
|
|
2016-07-28 13:31:17 -04:00
|
|
|
it_behaves_like 'a service raising ActiveRecord::RecordNotFound' do
|
|
|
|
let(:source) { group }
|
|
|
|
end
|
|
|
|
end
|
2016-06-17 12:59:33 -04:00
|
|
|
|
2016-07-28 13:31:17 -04:00
|
|
|
context 'when a member is found' do
|
|
|
|
before do
|
|
|
|
project.team << [member_user, :developer]
|
|
|
|
group.add_developer(member_user)
|
2016-06-17 08:06:55 -04:00
|
|
|
end
|
2016-07-28 13:31:17 -04:00
|
|
|
let(:params) { { user_id: member_user.id } }
|
2016-06-17 08:06:55 -04:00
|
|
|
|
2016-07-28 13:31:17 -04:00
|
|
|
context 'when current user cannot destroy the given member' do
|
|
|
|
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
|
|
|
|
let(:source) { project }
|
2016-06-17 08:06:55 -04:00
|
|
|
end
|
|
|
|
|
2016-07-28 13:31:17 -04:00
|
|
|
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
|
|
|
|
let(:source) { group }
|
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
|
|
|
|
project.team << [user, :master]
|
|
|
|
group.add_owner(user)
|
2016-06-17 10:33:37 -04:00
|
|
|
end
|
2016-06-17 12:59:33 -04:00
|
|
|
|
2016-07-28 13:31:17 -04:00
|
|
|
it_behaves_like 'a service destroying a member' do
|
|
|
|
let(:source) { project }
|
|
|
|
end
|
2016-06-17 12:59:33 -04:00
|
|
|
|
2016-07-28 13:31:17 -04:00
|
|
|
it_behaves_like 'a service destroying a member' do
|
|
|
|
let(:source) { group }
|
2016-06-17 12:59:33 -04:00
|
|
|
end
|
2016-09-09 12:51:31 -04:00
|
|
|
|
|
|
|
context 'when given a :id' do
|
|
|
|
let(:params) { { id: project.members.find_by!(user_id: user.id).id } }
|
|
|
|
|
|
|
|
it 'destroys the member' do
|
2017-06-21 09:48:12 -04:00
|
|
|
expect { described_class.new(project, user, params).execute }
|
|
|
|
.to change { project.members.count }.by(-1)
|
2016-09-09 12:51:31 -04:00
|
|
|
end
|
|
|
|
end
|
2016-06-17 08:06:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|