2016-07-28 13:30:34 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Members::ApproveAccessRequestService, services: true do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:access_requester) { create(:user) }
|
2016-11-11 07:51:50 -05:00
|
|
|
let(:project) { create(:empty_project, :public, :access_requestable) }
|
|
|
|
let(:group) { create(:group, :public, :access_requestable) }
|
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
|
2016-10-28 05:03:08 -04:00
|
|
|
expect { described_class.new(source, user, params).execute(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
|
2016-10-28 05:03:08 -04:00
|
|
|
expect { described_class.new(source, user, params).execute(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
|
2016-10-28 05:03:08 -04:00
|
|
|
expect { described_class.new(source, user, params).execute(opts) }.to change { source.requesters.count }.by(-1)
|
2016-07-28 13:30:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a <Source>Member' do
|
2016-10-28 05:03:08 -04:00
|
|
|
member = described_class.new(source, user, params).execute(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
|
2016-10-28 05:03:08 -04:00
|
|
|
let(:params2) { params.merge(user_id: access_requester.id, access_level: Gitlab::Access::MASTER) }
|
2016-07-28 13:30:34 -04:00
|
|
|
|
|
|
|
it 'returns a ProjectMember with the custom access level' do
|
2016-10-28 05:03:08 -04:00
|
|
|
member = described_class.new(source, user, params2).execute(opts)
|
2016-07-28 13:30:34 -04:00
|
|
|
|
|
|
|
expect(member.access_level).to eq Gitlab::Access::MASTER
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no access requester are found' do
|
|
|
|
let(:params) { { user_id: 42 } }
|
|
|
|
|
|
|
|
it_behaves_like 'a service raising ActiveRecord::RecordNotFound' do
|
|
|
|
let(:source) { project }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a service raising ActiveRecord::RecordNotFound' do
|
|
|
|
let(:source) { group }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when an access requester is found' do
|
|
|
|
before do
|
|
|
|
project.request_access(access_requester)
|
|
|
|
group.request_access(access_requester)
|
|
|
|
end
|
|
|
|
let(:params) { { user_id: access_requester.id } }
|
|
|
|
|
2016-10-28 05:03:08 -04:00
|
|
|
context 'when current user is nil' do
|
|
|
|
let(:user) { nil }
|
|
|
|
|
|
|
|
context 'and :force option is not given' 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 'and :force option is false' do
|
|
|
|
let(:opts) { { force: false } }
|
|
|
|
|
|
|
|
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 'and :force option is true' do
|
|
|
|
let(:opts) { { force: true } }
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
context 'and :force param is true' do
|
|
|
|
let(:params) { { user_id: access_requester.id, force: true } }
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
project.team << [user, :master]
|
|
|
|
group.add_owner(user)
|
|
|
|
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
|
2016-09-09 12:06:36 -04:00
|
|
|
|
|
|
|
context 'when given a :id' do
|
|
|
|
let(:params) { { id: project.requesters.find_by!(user_id: access_requester.id).id } }
|
|
|
|
|
|
|
|
it_behaves_like 'a service approving an access request' do
|
|
|
|
let(:source) { project }
|
|
|
|
end
|
|
|
|
end
|
2016-07-28 13:30:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|