2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-28 13:30:34 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
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) { {} }
|
2016-07-28 13:30:34 -04:00
|
|
|
|
|
|
|
shared_examples 'a service raising ActiveRecord::RecordNotFound' do
|
|
|
|
it 'raises ActiveRecord::RecordNotFound' do
|
2018-02-16 09:10:22 -05:00
|
|
|
expect { described_class.new(current_user).execute(access_requester, opts) }.to raise_error(ActiveRecord::RecordNotFound)
|
2016-07-28 13:30:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'a service raising Gitlab::Access::AccessDeniedError' do
|
|
|
|
it 'raises Gitlab::Access::AccessDeniedError' do
|
2018-02-16 09:10:22 -05:00
|
|
|
expect { described_class.new(current_user).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
|
2018-02-16 09:10:22 -05:00
|
|
|
expect { described_class.new(current_user).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
|
2018-02-16 09:10:22 -05:00
|
|
|
member = described_class.new(current_user).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
|
|
|
|
it 'returns a ProjectMember with the custom access level' do
|
2018-07-11 10:36:08 -04:00
|
|
|
member = described_class.new(current_user, access_level: Gitlab::Access::MAINTAINER).execute(access_requester, opts)
|
2016-07-28 13:30:34 -04:00
|
|
|
|
2018-07-11 10:36:08 -04:00
|
|
|
expect(member.access_level).to eq(Gitlab::Access::MAINTAINER)
|
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
|
|
|
|
end
|
|
|
|
end
|