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

187 lines
5.4 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2018-03-27 19:54:05 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Members::UpdateService do
2018-03-27 19:54:05 +05:30
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) }
2022-07-23 23:45:48 +05:30
let(:access_level) { Gitlab::Access::MAINTAINER }
2018-03-27 19:54:05 +05:30
let(:params) do
2022-07-23 23:45:48 +05:30
{ access_level: access_level }
2018-03-27 19:54:05 +05:30
end
2021-03-11 19:13:27 +05:30
subject { described_class.new(current_user, params).execute(member, permission: permission) }
2018-03-27 19:54:05 +05:30
shared_examples 'a service raising Gitlab::Access::AccessDeniedError' do
it 'raises Gitlab::Access::AccessDeniedError' do
2021-03-11 19:13:27 +05:30
expect { subject }
2018-03-27 19:54:05 +05:30
.to raise_error(Gitlab::Access::AccessDeniedError)
end
end
shared_examples 'a service updating a member' do
it 'updates the member' do
2019-01-03 12:48:30 +05:30
expect(TodosDestroyer::EntityLeaveWorker).not_to receive(:perform_in).with(Todo::WAIT_FOR_DELETE, member.user_id, member.source_id, source.class.name)
2021-03-11 19:13:27 +05:30
updated_member = subject.fetch(:member)
2018-03-27 19:54:05 +05:30
expect(updated_member).to be_valid
2022-07-23 23:45:48 +05:30
expect(updated_member.access_level).to eq(access_level)
2018-03-27 19:54:05 +05:30
end
2019-01-03 12:48:30 +05:30
2021-03-11 19:13:27 +05:30
it 'returns success status' do
result = subject.fetch(:status)
expect(result).to eq(:success)
end
2019-01-03 12:48:30 +05:30
context 'when member is downgraded to guest' do
2020-10-04 03:57:07 +05:30
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-03-11 19:13:27 +05:30
updated_member = subject.fetch(:member)
2020-10-04 03:57:07 +05:30
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'
2019-01-03 12:48:30 +05:30
end
2020-10-04 03:57:07 +05:30
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
2019-01-03 12:48:30 +05:30
2020-10-04 03:57:07 +05:30
context 'when access_level is invalid' do
let(:params) { { access_level: 'invalid' } }
2019-01-03 12:48:30 +05:30
2020-10-04 03:57:07 +05:30
it 'raises an error' do
expect { described_class.new(current_user, params) }.to raise_error(ArgumentError, 'invalid value for Integer(): "invalid"')
2019-01-03 12:48:30 +05:30
end
end
2021-03-11 19:13:27 +05:30
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
2018-03-27 19:54:05 +05:30
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-11-18 11:00:15 +05:30
project.add_maintainer(current_user)
2018-03-27 19:54:05 +05:30
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
2022-07-23 23:45:48 +05:30
context 'in a project' do
let_it_be(:group_project) { create(:project, group: create(:group)) }
let(:source) { group_project }
context 'a project maintainer' do
before do
group_project.add_maintainer(current_user)
end
context 'cannot update a member to OWNER' do
before do
group_project.add_developer(member_user)
end
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
let(:access_level) { Gitlab::Access::OWNER }
end
end
context 'cannot update themselves to OWNER' do
let(:member) { source.members_and_requesters.find_by!(user_id: current_user.id) }
before do
group_project.add_developer(member_user)
end
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
let(:access_level) { Gitlab::Access::OWNER }
end
end
context 'cannot downgrade a member from OWNER' do
before do
group_project.add_owner(member_user)
end
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
let(:access_level) { Gitlab::Access::MAINTAINER }
end
end
end
context 'owners' do
before do
# so that `current_user` is considered an `OWNER` in the project via inheritance.
group_project.group.add_owner(current_user)
end
context 'can update a member to OWNER' do
before do
group_project.add_developer(member_user)
end
it_behaves_like 'a service updating a member' do
let(:access_level) { Gitlab::Access::OWNER }
end
end
context 'can downgrade a member from OWNER' do
before do
group_project.add_owner(member_user)
end
it_behaves_like 'a service updating a member' do
let(:access_level) { Gitlab::Access::MAINTAINER }
end
end
end
end
2018-03-27 19:54:05 +05:30
end