debian-mirror-gitlab/spec/services/protected_branches/destroy_service_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

61 lines
1.8 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2018-05-09 12:01:36 +05:30
require 'spec_helper'
2023-05-27 22:25:52 +05:30
RSpec.describe ProtectedBranches::DestroyService, feature_category: :compliance_management do
2023-03-04 22:38:38 +05:30
shared_examples 'execute with entity' do
subject(:service) { described_class.new(entity, user) }
2022-08-27 11:52:29 +05:30
2023-03-04 22:38:38 +05:30
describe '#execute' do
it 'destroys a protected branch' do
service.execute(protected_branch)
2018-05-09 12:01:36 +05:30
2023-03-04 22:38:38 +05:30
expect(protected_branch).to be_destroyed
end
2018-05-09 12:01:36 +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
2018-05-09 12:01:36 +05:30
2023-03-04 22:38:38 +05:30
service.execute(protected_branch)
2018-05-09 12:01:36 +05:30
end
2023-03-04 22:38:38 +05:30
context 'when a policy restricts rule deletion' do
it "prevents deletion of the protected branch rule" do
disallow(:destroy_protected_branch, protected_branch)
expect do
service.execute(protected_branch)
end.to raise_error(Gitlab::Access::AccessDeniedError)
end
end
2022-08-27 11:52:29 +05:30
end
2023-03-04 22:38:38 +05:30
end
2022-08-27 11:52:29 +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 }
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) }
let!(:protected_branch) { create(:protected_branch, group: entity, project: nil) }
before do
allow(Ability).to receive(:allowed?).with(user, :destroy_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'
2018-05-09 12:01:36 +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
2018-05-09 12:01:36 +05:30
end