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

114 lines
3.7 KiB
Ruby
Raw Normal View History

2016-11-03 12:29:30 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe Members::ApproveAccessRequestService do
let(:project) { create(:project, :public, :access_requestable) }
2017-08-17 22:00:37 +05:30
let(:group) { create(:group, :public, :access_requestable) }
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) { {} }
shared_examples 'a service raising ActiveRecord::RecordNotFound' do
it 'raises ActiveRecord::RecordNotFound' do
2018-03-27 19:54:05 +05:30
expect { described_class.new(current_user).execute(access_requester, opts) }.to raise_error(ActiveRecord::RecordNotFound)
2016-11-03 12:29:30 +05:30
end
end
shared_examples 'a service raising Gitlab::Access::AccessDeniedError' do
it 'raises Gitlab::Access::AccessDeniedError' do
2018-03-27 19:54:05 +05:30
expect { described_class.new(current_user).execute(access_requester, opts) }.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
2018-03-27 19:54:05 +05:30
expect { described_class.new(current_user).execute(access_requester, opts) }.to change { source.requesters.count }.by(-1)
2016-11-03 12:29:30 +05:30
end
it 'returns a <Source>Member' do
2018-03-27 19:54:05 +05:30
member = described_class.new(current_user).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
context 'with a custom access level' do
it 'returns a ProjectMember with the custom access level' do
2018-11-18 11:00:15 +05:30
member = described_class.new(current_user, access_level: Gitlab::Access::MAINTAINER).execute(access_requester, opts)
2016-11-03 12:29:30 +05:30
2018-11-18 11:00:15 +05:30
expect(member.access_level).to eq(Gitlab::Access::MAINTAINER)
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
end
end