2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-11 10:47:08 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe Members::UpdateService do
|
2017-10-11 10:47:08 -04:00
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
let(:group) { create(:group, :public) }
|
|
|
|
let(:current_user) { create(:user) }
|
|
|
|
let(:member_user) { create(:user) }
|
|
|
|
let(:permission) { :update }
|
|
|
|
let(:member) { source.members_and_requesters.find_by!(user_id: member_user.id) }
|
|
|
|
let(:params) do
|
2018-07-11 10:36:08 -04:00
|
|
|
{ access_level: Gitlab::Access::MAINTAINER }
|
2017-10-11 10:47:08 -04:00
|
|
|
end
|
|
|
|
|
2021-01-28 16:09:04 -05:00
|
|
|
subject { described_class.new(current_user, params).execute(member, permission: permission) }
|
|
|
|
|
2017-10-11 10:47:08 -04:00
|
|
|
shared_examples 'a service raising Gitlab::Access::AccessDeniedError' do
|
|
|
|
it 'raises Gitlab::Access::AccessDeniedError' do
|
2021-01-28 16:09:04 -05:00
|
|
|
expect { subject }
|
2017-10-11 10:47:08 -04:00
|
|
|
.to raise_error(Gitlab::Access::AccessDeniedError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'a service updating a member' do
|
|
|
|
it 'updates the member' do
|
2018-12-11 13:15:10 -05:00
|
|
|
expect(TodosDestroyer::EntityLeaveWorker).not_to receive(:perform_in).with(Todo::WAIT_FOR_DELETE, member.user_id, member.source_id, source.class.name)
|
|
|
|
|
2021-01-28 16:09:04 -05:00
|
|
|
updated_member = subject.fetch(:member)
|
2017-10-11 10:47:08 -04:00
|
|
|
|
|
|
|
expect(updated_member).to be_valid
|
2018-07-11 10:36:08 -04:00
|
|
|
expect(updated_member.access_level).to eq(Gitlab::Access::MAINTAINER)
|
2017-10-11 10:47:08 -04:00
|
|
|
end
|
2018-12-11 13:15:10 -05:00
|
|
|
|
2021-01-28 16:09:04 -05:00
|
|
|
it 'returns success status' do
|
|
|
|
result = subject.fetch(:status)
|
|
|
|
|
|
|
|
expect(result).to eq(:success)
|
|
|
|
end
|
|
|
|
|
2018-12-11 13:15:10 -05:00
|
|
|
context 'when member is downgraded to guest' do
|
2020-10-01 14:10:20 -04:00
|
|
|
shared_examples 'schedules to delete confidential todos' do
|
|
|
|
it do
|
|
|
|
expect(TodosDestroyer::EntityLeaveWorker).to receive(:perform_in).with(Todo::WAIT_FOR_DELETE, member.user_id, member.source_id, source.class.name).once
|
|
|
|
|
2021-01-28 16:09:04 -05:00
|
|
|
updated_member = subject.fetch(:member)
|
2020-10-01 14:10:20 -04:00
|
|
|
|
|
|
|
expect(updated_member).to be_valid
|
|
|
|
expect(updated_member.access_level).to eq(Gitlab::Access::GUEST)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with Gitlab::Access::GUEST level as a string' do
|
|
|
|
let(:params) { { access_level: Gitlab::Access::GUEST.to_s } }
|
|
|
|
|
|
|
|
it_behaves_like 'schedules to delete confidential todos'
|
2018-12-11 13:15:10 -05:00
|
|
|
end
|
|
|
|
|
2020-10-01 14:10:20 -04:00
|
|
|
context 'with Gitlab::Access::GUEST level as an integer' do
|
|
|
|
let(:params) { { access_level: Gitlab::Access::GUEST } }
|
|
|
|
|
|
|
|
it_behaves_like 'schedules to delete confidential todos'
|
|
|
|
end
|
|
|
|
end
|
2018-12-11 13:15:10 -05:00
|
|
|
|
2020-10-01 14:10:20 -04:00
|
|
|
context 'when access_level is invalid' do
|
|
|
|
let(:params) { { access_level: 'invalid' } }
|
2018-12-11 13:15:10 -05:00
|
|
|
|
2020-10-01 14:10:20 -04:00
|
|
|
it 'raises an error' do
|
|
|
|
expect { described_class.new(current_user, params) }.to raise_error(ArgumentError, 'invalid value for Integer(): "invalid"')
|
2018-12-11 13:15:10 -05:00
|
|
|
end
|
|
|
|
end
|
2021-01-28 16:09:04 -05:00
|
|
|
|
|
|
|
context 'when member is not valid' do
|
|
|
|
let(:params) { { expires_at: 2.days.ago } }
|
|
|
|
|
|
|
|
it 'returns error status' do
|
|
|
|
result = subject
|
|
|
|
|
|
|
|
expect(result[:status]).to eq(:error)
|
|
|
|
end
|
|
|
|
end
|
2017-10-11 10:47:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(member_user)
|
|
|
|
group.add_developer(member_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when current user cannot update the given member' do
|
|
|
|
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
|
|
|
|
let(:source) { project }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
|
|
|
|
let(:source) { group }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when current user can update the given member' do
|
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(current_user)
|
2017-10-11 10:47:08 -04:00
|
|
|
group.add_owner(current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a service updating a member' do
|
|
|
|
let(:source) { project }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a service updating a member' do
|
|
|
|
let(:source) { group }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|