Welcome to mirror list, hosted at ThFree Co, Russian Federation.

destroy_service_spec.rb « protected_branches « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 421d4aae5bb630ae9cce65e8c0994be1d693d9e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ProtectedBranches::DestroyService do
  shared_examples 'execute with entity' do
    subject(:service) { described_class.new(entity, user) }

    describe '#execute' do
      it 'destroys a protected branch' do
        service.execute(protected_branch)

        expect(protected_branch).to be_destroyed
      end

      it 'refreshes the cache' do
        expect_next_instance_of(ProtectedBranches::CacheService) do |cache_service|
          expect(cache_service).to receive(:refresh)
        end

        service.execute(protected_branch)
      end

      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
    end
  end

  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 }

    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)
    end

    it_behaves_like 'execute with entity'
  end

  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
end