2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe ProtectedBranches::UpdateService do
|
2023-03-04 22:38:38 +05:30
|
|
|
shared_examples 'execute with entity' do
|
|
|
|
let(:params) { { name: new_name } }
|
2022-08-27 11:52:29 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
subject(:service) { described_class.new(entity, user, params) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
describe '#execute' do
|
|
|
|
let(:new_name) { 'new protected branch name' }
|
|
|
|
let(:result) { service.execute(protected_branch) }
|
2022-08-27 11:52:29 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
it 'updates a protected branch' do
|
|
|
|
expect(result.reload.name).to eq(params[:name])
|
|
|
|
end
|
2021-12-07 22:27:20 +05:30
|
|
|
|
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
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
result
|
2022-08-27 11:52:29 +05:30
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
context 'when updating name of a protected branch to one that contains HTML tags' do
|
|
|
|
let(:new_name) { 'foo<b>bar<\b>' }
|
|
|
|
let(:result) { service.execute(protected_branch) }
|
2021-12-07 22:27:20 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
it 'updates a protected branch' do
|
|
|
|
expect(result.reload.name).to eq(new_name)
|
|
|
|
end
|
2021-12-07 22:27:20 +05:30
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
context 'when a policy restricts rule update' do
|
|
|
|
it "prevents update of the protected branch rule" do
|
|
|
|
disallow(:update_protected_branch, protected_branch)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
expect { service.execute(protected_branch) }.to raise_error(Gitlab::Access::AccessDeniedError)
|
|
|
|
end
|
2017-08-17 22:00:37 +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!(:protected_branch) { create(:protected_branch, project: entity) }
|
|
|
|
let(:user) { entity.first_owner }
|
2018-05-09 12:01:36 +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) }
|
|
|
|
let!(:protected_branch) { create(:protected_branch, group: entity, project: nil) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Ability).to receive(:allowed?).with(user, :update_protected_branch, protected_branch).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'
|
2017-08-17 22:00:37 +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
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|