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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

167 lines
5.3 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2016-11-03 12:29:30 +05:30
require 'spec_helper'
2023-05-27 22:25:52 +05:30
RSpec.describe Members::ApproveAccessRequestService, feature_category: :subgroups do
2019-12-21 20:55:43 +05:30
let(:project) { create(:project, :public) }
let(:group) { create(:group, :public) }
2018-03-27 19:54:05 +05:30
let(:current_user) { create(:user) }
let(:access_requester_user) { create(:user) }
let(:access_requester) { source.requesters.find_by!(user_id: access_requester_user.id) }
2016-11-03 12:29:30 +05:30
let(:opts) { {} }
2022-07-23 23:45:48 +05:30
let(:params) { {} }
let(:custom_access_level) { Gitlab::Access::MAINTAINER }
2016-11-03 12:29:30 +05:30
shared_examples 'a service raising Gitlab::Access::AccessDeniedError' do
it 'raises Gitlab::Access::AccessDeniedError' do
2023-07-09 08:55:56 +05:30
expect do
described_class.new(current_user, params).execute(access_requester, **opts)
end.to raise_error(Gitlab::Access::AccessDeniedError)
2016-11-03 12:29:30 +05:30
end
end
shared_examples 'a service approving an access request' do
it 'succeeds' do
2023-07-09 08:55:56 +05:30
expect do
described_class.new(current_user, params).execute(access_requester, **opts)
end.to change { source.requesters.count }.by(-1)
2016-11-03 12:29:30 +05:30
end
it 'returns a <Source>Member' do
2022-07-23 23:45:48 +05:30
member = described_class.new(current_user, params).execute(access_requester, **opts)
2016-11-03 12:29:30 +05:30
expect(member).to be_a "#{source.class}Member".constantize
expect(member.requested_at).to be_nil
end
2023-04-23 21:23:45 +05:30
it 'calls the method to resolve access request for the approver' do
expect_next_instance_of(described_class) do |instance|
2023-07-09 08:55:56 +05:30
expect(instance).to receive(:resolve_access_request_todos).with(access_requester)
end
described_class.new(current_user, params).execute(access_requester, **opts)
end
it 'resolves the todos for the access requests' do
expect_next_instance_of(TodoService) do |instance|
expect(instance).to receive(:resolve_access_request_todos).with(access_requester)
2023-04-23 21:23:45 +05:30
end
described_class.new(current_user, params).execute(access_requester, **opts)
end
2016-11-03 12:29:30 +05:30
context 'with a custom access level' do
2022-07-23 23:45:48 +05:30
let(:params) { { access_level: custom_access_level } }
2016-11-03 12:29:30 +05:30
it 'returns a ProjectMember with the custom access level' do
2022-07-23 23:45:48 +05:30
member = described_class.new(current_user, params).execute(access_requester, **opts)
2016-11-03 12:29:30 +05:30
2022-07-23 23:45:48 +05:30
expect(member.access_level).to eq(custom_access_level)
2016-11-03 12:29:30 +05:30
end
end
end
context 'when an access requester is found' do
before do
2018-03-27 19:54:05 +05:30
project.request_access(access_requester_user)
group.request_access(access_requester_user)
2016-11-03 12:29:30 +05:30
end
context 'when current user is nil' do
let(:user) { nil }
2018-03-27 19:54:05 +05:30
context 'and :ldap option is not given' do
2016-11-03 12:29:30 +05:30
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
2018-03-27 19:54:05 +05:30
context 'and :skip_authorization option is false' do
let(:opts) { { skip_authorization: false } }
2016-11-03 12:29:30 +05:30
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
2018-03-27 19:54:05 +05:30
context 'and :skip_authorization option is true' do
let(:opts) { { skip_authorization: true } }
2016-11-03 12:29:30 +05:30
it_behaves_like 'a service approving an access request' do
let(:source) { project }
end
it_behaves_like 'a service approving an access request' do
let(:source) { group }
end
end
end
context 'when current user cannot approve access request to the project' 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 approve access request to the project' 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)
2016-11-03 12:29:30 +05:30
end
it_behaves_like 'a service approving an access request' do
let(:source) { project }
end
it_behaves_like 'a service approving an access request' 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, :public, group: create(:group, :public)) }
let(:source) { group_project }
let(:custom_access_level) { Gitlab::Access::OWNER }
let(:params) { { access_level: custom_access_level } }
before do
group_project.request_access(access_requester_user)
end
context 'maintainers' do
before do
group_project.add_maintainer(current_user)
end
context 'cannot approve the access request of a requester to give them OWNER permissions' do
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError'
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 approve the access request of a requester to give them OWNER permissions' do
it_behaves_like 'a service approving an access request'
end
end
end
2016-11-03 12:29:30 +05:30
end
end