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