summaryrefslogtreecommitdiff
path: root/spec/services/protected_branches/api_service_spec.rb
blob: c98e253267b40900a356556d314aa4116662a681 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ProtectedBranches::ApiService do
  shared_examples 'execute with entity' do
    it 'creates a protected branch with prefilled defaults' do
      expect(::ProtectedBranches::CreateService).to receive(:new).with(
        entity, user, hash_including(
                        push_access_levels_attributes: [{ access_level: Gitlab::Access::MAINTAINER }],
                        merge_access_levels_attributes: [{ access_level: Gitlab::Access::MAINTAINER }]
                      )
      ).and_call_original

      expect(described_class.new(entity, user, { name: 'new name' }).create).to be_valid
    end

    it 'updates a protected branch without prefilled defaults' do
      expect(::ProtectedBranches::UpdateService).to receive(:new).with(
        entity, user, hash_including(
                        push_access_levels_attributes: [],
                        merge_access_levels_attributes: []
                      )
      ).and_call_original

      expect do
        expect(described_class.new(entity, user, { name: 'new name' }).update(protected_branch)).to be_valid
      end.not_to change { protected_branch.reload.allow_force_push }
    end
  end

  context 'with entity project' do
    let_it_be_with_reload(:entity) { create(:project) }
    let_it_be_with_reload(:protected_branch) { create(:protected_branch, project: entity, allow_force_push: true) }
    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_it_be_with_reload(:protected_branch) do
      create(:protected_branch, group: entity, project: nil, allow_force_push: true)
    end

    before do
      allow(Ability).to receive(:allowed?).with(user, :update_protected_branch, protected_branch).and_return(true)
      allow(Ability)
        .to receive(:allowed?)
        .with(user, :create_protected_branch, instance_of(ProtectedBranch))
        .and_return(true)
    end

    it_behaves_like 'execute with entity'
  end
end