2019-07-07 11:18:12 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe ProtectedBranch::PushAccessLevel do
|
2018-11-18 11:00:15 +05:30
|
|
|
it { is_expected.to validate_inclusion_of(:access_level).in_array([Gitlab::Access::MAINTAINER, Gitlab::Access::DEVELOPER, Gitlab::Access::NO_ACCESS]) }
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
describe 'associations' do
|
|
|
|
it { is_expected.to belong_to(:deploy_key) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'validations' do
|
|
|
|
it 'is not valid when a record exists with the same access level' do
|
|
|
|
protected_branch = create(:protected_branch)
|
|
|
|
create(:protected_branch_push_access_level, protected_branch: protected_branch)
|
|
|
|
level = build(:protected_branch_push_access_level, protected_branch: protected_branch)
|
|
|
|
|
|
|
|
expect(level).to be_invalid
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is not valid when a record exists with the same access level' do
|
|
|
|
protected_branch = create(:protected_branch)
|
|
|
|
deploy_key = create(:deploy_key, projects: [protected_branch.project])
|
|
|
|
create(:protected_branch_push_access_level, protected_branch: protected_branch, deploy_key: deploy_key)
|
|
|
|
level = build(:protected_branch_push_access_level, protected_branch: protected_branch, deploy_key: deploy_key)
|
|
|
|
|
|
|
|
expect(level).to be_invalid
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'checks that a deploy key is enabled for the same project as the protected branch\'s' do
|
|
|
|
level = build(:protected_branch_push_access_level, deploy_key: create(:deploy_key))
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
expect { level.save! }.to raise_error(ActiveRecord::RecordInvalid)
|
2021-01-29 00:20:46 +05:30
|
|
|
expect(level.errors.full_messages).to contain_exactly('Deploy key is not enabled for this project')
|
|
|
|
end
|
|
|
|
end
|
2021-02-22 17:27:13 +05:30
|
|
|
|
|
|
|
describe '#check_access' do
|
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:protected_branch) { create(:protected_branch, :no_one_can_push, project: project) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:deploy_key) { create(:deploy_key, user: user) }
|
2021-09-30 23:02:18 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
let!(:deploy_keys_project) { create(:deploy_keys_project, project: project, deploy_key: deploy_key, can_push: can_push) }
|
|
|
|
let(:can_push) { true }
|
|
|
|
|
|
|
|
before_all do
|
2021-07-02 01:05:55 +05:30
|
|
|
project.add_maintainer(user)
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when this push_access_level is tied to a deploy key' do
|
|
|
|
let(:push_access_level) { create(:protected_branch_push_access_level, protected_branch: protected_branch, deploy_key: deploy_key) }
|
|
|
|
|
|
|
|
context 'when the deploy key is among the active keys for this project' do
|
|
|
|
specify do
|
|
|
|
expect(push_access_level.check_access(user)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the deploy key is not among the active keys of this project' do
|
|
|
|
let(:can_push) { false }
|
|
|
|
|
|
|
|
it 'is false' do
|
|
|
|
expect(push_access_level.check_access(user)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#type' do
|
|
|
|
let(:push_level_access) { build(:protected_branch_push_access_level) }
|
|
|
|
|
|
|
|
it 'returns :deploy_key when a deploy key is tied to the protected branch' do
|
|
|
|
push_level_access.deploy_key = create(:deploy_key)
|
|
|
|
|
|
|
|
expect(push_level_access.type).to eq(:deploy_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns :role by default' do
|
|
|
|
expect(push_level_access.type).to eq(:role)
|
|
|
|
end
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|