2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe ProtectedBranches::CreateService do
|
2023-03-04 22:38:38 +05:30
|
|
|
shared_examples 'execute with entity' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
name: name,
|
|
|
|
merge_access_levels_attributes: [{ access_level: Gitlab::Access::MAINTAINER }],
|
|
|
|
push_access_levels_attributes: [{ access_level: Gitlab::Access::MAINTAINER }]
|
|
|
|
}
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
subject(:service) { described_class.new(entity, user, params) }
|
2022-08-27 11:52:29 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
describe '#execute' do
|
|
|
|
let(:name) { 'master' }
|
2021-12-07 22:27:20 +05:30
|
|
|
|
2022-02-05 19:09:49 +05:30
|
|
|
it 'creates a new protected branch' do
|
|
|
|
expect { service.execute }.to change(ProtectedBranch, :count).by(1)
|
2023-03-04 22:38:38 +05:30
|
|
|
expect(entity.protected_branches.last.push_access_levels.map(&:access_level)).to match_array([Gitlab::Access::MAINTAINER])
|
|
|
|
expect(entity.protected_branches.last.merge_access_levels.map(&:access_level)).to match_array([Gitlab::Access::MAINTAINER])
|
2021-12-07 22:27:20 +05:30
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
it 'refreshes the cache' do
|
|
|
|
expect_next_instance_of(ProtectedBranches::CacheService) do |cache_service|
|
|
|
|
expect(cache_service).to receive(:refresh)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
service.execute
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
context 'when protecting a branch with a name that contains HTML tags' do
|
|
|
|
let(:name) { 'foo<b>bar<\b>' }
|
|
|
|
|
|
|
|
it 'creates a new protected branch' do
|
|
|
|
expect { service.execute }.to change(ProtectedBranch, :count).by(1)
|
|
|
|
expect(entity.protected_branches.last.name).to eq(name)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
context 'when a policy restricts rule creation' do
|
|
|
|
it "prevents creation of the protected branch rule" do
|
|
|
|
disallow(:create_protected_branch, an_instance_of(ProtectedBranch))
|
|
|
|
|
|
|
|
expect do
|
|
|
|
service.execute
|
|
|
|
end.to raise_error(Gitlab::Access::AccessDeniedError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new protected branch if we skip authorization step' do
|
|
|
|
expect { service.execute(skip_authorization: true) }.to change(ProtectedBranch, :count).by(1)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
2023-03-04 22:38:38 +05:30
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
context 'with entity project' do
|
|
|
|
let_it_be_with_reload(:entity) { create(:project) }
|
|
|
|
let(:user) { entity.first_owner }
|
2022-08-27 11:52:29 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
it_behaves_like 'execute with entity'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with entity group' do
|
|
|
|
let_it_be_with_reload(:entity) { create(:group) }
|
|
|
|
let_it_be_with_reload(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Ability).to receive(:allowed?).with(user, :create_protected_branch, instance_of(ProtectedBranch)).and_return(true)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
it_behaves_like 'execute with entity'
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
2022-08-27 11:52:29 +05:30
|
|
|
|
|
|
|
def disallow(ability, protected_branch)
|
|
|
|
allow(Ability).to receive(:allowed?).and_call_original
|
|
|
|
allow(Ability).to receive(:allowed?).with(user, ability, protected_branch).and_return(false)
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|