2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-28 13:30:34 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe Members::ApproveAccessRequestService do
|
2019-10-16 08:06:32 -04:00
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
let(:group) { create(:group, :public) }
|
2017-10-11 10:47:08 -04:00
|
|
|
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-10-28 05:03:08 -04:00
|
|
|
let(:opts) { {} }
|
2022-06-02 17:09:25 -04:00
|
|
|
let(:params) { {} }
|
|
|
|
let(:custom_access_level) { Gitlab::Access::MAINTAINER }
|
2016-07-28 13:30:34 -04:00
|
|
|
|
|
|
|
shared_examples 'a service raising Gitlab::Access::AccessDeniedError' do
|
|
|
|
it 'raises Gitlab::Access::AccessDeniedError' do
|
2022-06-02 17:09:25 -04:00
|
|
|
expect { described_class.new(current_user, params).execute(access_requester, **opts) }.to raise_error(Gitlab::Access::AccessDeniedError)
|
2016-07-28 13:30:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'a service approving an access request' do
|
|
|
|
it 'succeeds' do
|
2022-06-02 17:09:25 -04:00
|
|
|
expect { described_class.new(current_user, params).execute(access_requester, **opts) }.to change { source.requesters.count }.by(-1)
|
2016-07-28 13:30:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a <Source>Member' do
|
2022-06-02 17:09:25 -04:00
|
|
|
member = described_class.new(current_user, params).execute(access_requester, **opts)
|
2016-07-28 13:30:34 -04:00
|
|
|
|
2016-10-03 10:09:57 -04:00
|
|
|
expect(member).to be_a "#{source.class}Member".constantize
|
2016-07-28 13:30:34 -04:00
|
|
|
expect(member.requested_at).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a custom access level' do
|
2022-06-02 17:09:25 -04:00
|
|
|
let(:params) { { access_level: custom_access_level } }
|
|
|
|
|
2016-07-28 13:30:34 -04:00
|
|
|
it 'returns a ProjectMember with the custom access level' do
|
2022-06-02 17:09:25 -04:00
|
|
|
member = described_class.new(current_user, params).execute(access_requester, **opts)
|
2016-07-28 13:30:34 -04:00
|
|
|
|
2022-06-02 17:09:25 -04:00
|
|
|
expect(member.access_level).to eq(custom_access_level)
|
2016-07-28 13:30:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when an access requester is found' do
|
|
|
|
before do
|
2017-10-11 10:47:08 -04:00
|
|
|
project.request_access(access_requester_user)
|
|
|
|
group.request_access(access_requester_user)
|
2016-07-28 13:30:34 -04:00
|
|
|
end
|
|
|
|
|
2016-10-28 05:03:08 -04:00
|
|
|
context 'when current user is nil' do
|
|
|
|
let(:user) { nil }
|
|
|
|
|
2017-10-11 10:47:08 -04:00
|
|
|
context 'and :ldap option is not given' do
|
2016-10-28 05:03:08 -04:00
|
|
|
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-02-16 09:10:22 -05:00
|
|
|
context 'and :skip_authorization option is false' do
|
|
|
|
let(:opts) { { skip_authorization: false } }
|
2016-10-28 05:03:08 -04:00
|
|
|
|
|
|
|
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-02-16 09:10:22 -05:00
|
|
|
context 'and :skip_authorization option is true' do
|
|
|
|
let(:opts) { { skip_authorization: true } }
|
2016-10-28 05:03:08 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-07-28 13:30:34 -04:00
|
|
|
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-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)
|
2016-07-28 13:30:34 -04:00
|
|
|
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-06-02 17:09:25 -04:00
|
|
|
|
|
|
|
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-07-28 13:30:34 -04:00
|
|
|
end
|
|
|
|
end
|