2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe ProtectedBranches::DestroyService do
|
2022-08-27 11:52:29 +05:30
|
|
|
let_it_be_with_reload(:project) { create(:project) }
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
let!(:protected_branch) { create(:protected_branch, project: project) }
|
2022-03-02 08:16:31 +05:30
|
|
|
let(:user) { project.first_owner }
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
subject(:service) { described_class.new(project, user) }
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
describe '#execute' do
|
2018-05-09 12:01:36 +05:30
|
|
|
it 'destroys a protected branch' do
|
|
|
|
service.execute(protected_branch)
|
|
|
|
|
|
|
|
expect(protected_branch).to be_destroyed
|
|
|
|
end
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
it 'refreshes the cache' do
|
|
|
|
expect_next_instance_of(ProtectedBranches::CacheService) do |cache_service|
|
|
|
|
expect(cache_service).to receive(:refresh)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
service.execute(protected_branch)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a policy restricts rule deletion' do
|
2018-05-09 12:01:36 +05:30
|
|
|
it "prevents deletion of the protected branch rule" do
|
2022-08-27 11:52:29 +05:30
|
|
|
disallow(:destroy_protected_branch, protected_branch)
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
expect do
|
|
|
|
service.execute(protected_branch)
|
|
|
|
end.to raise_error(Gitlab::Access::AccessDeniedError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|