summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/github_import/importer/protected_branch_importer_spec.rb
blob: 6dc6db739f4fc25d2471b4422ee31f4ab4b53b03 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::GithubImport::Importer::ProtectedBranchImporter do
  subject(:importer) { described_class.new(github_protected_branch, project, client) }

  let(:allow_force_pushes_on_github) { true }
  let(:github_protected_branch) do
    Gitlab::GithubImport::Representation::ProtectedBranch.new(
      id: 'protection',
      allow_force_pushes: allow_force_pushes_on_github
    )
  end

  let(:project) { create(:project, :repository) }
  let(:client) { instance_double('Gitlab::GithubImport::Client') }

  describe '#execute' do
    let(:create_service) { instance_double('ProtectedBranches::CreateService') }

    shared_examples 'create branch protection by the strictest ruleset' do
      let(:expected_ruleset) do
        {
          name: 'protection',
          push_access_levels_attributes: [{ access_level: Gitlab::Access::MAINTAINER }],
          merge_access_levels_attributes: [{ access_level: Gitlab::Access::MAINTAINER }],
          allow_force_push: expected_allow_force_push
        }
      end

      it 'calls service with the correct arguments' do
        expect(ProtectedBranches::CreateService).to receive(:new).with(
          project,
          project.creator,
          expected_ruleset
        ).and_return(create_service)

        expect(create_service).to receive(:execute).with(skip_authorization: true)
        importer.execute
      end

      it 'creates protected branch and access levels for given github rule' do
        expect { importer.execute }.to change(ProtectedBranch, :count).by(1)
          .and change(ProtectedBranch::PushAccessLevel, :count).by(1)
          .and change(ProtectedBranch::MergeAccessLevel, :count).by(1)
      end
    end

    context 'when branch is protected on GitLab' do
      before do
        create(
          :protected_branch,
          project: project,
          name: 'protect*',
          allow_force_push: allow_force_pushes_on_gitlab
        )
      end

      context 'when branch protection rule on Gitlab is stricter than on Github' do
        let(:allow_force_pushes_on_github) { true }
        let(:allow_force_pushes_on_gitlab) { false }
        let(:expected_allow_force_push) { false }

        it_behaves_like 'create branch protection by the strictest ruleset'
      end

      context 'when branch protection rule on Github is stricter than on Gitlab' do
        let(:allow_force_pushes_on_github) { false }
        let(:allow_force_pushes_on_gitlab) { true }
        let(:expected_allow_force_push) { false }

        it_behaves_like 'create branch protection by the strictest ruleset'
      end

      context 'when branch protection rules on Github and Gitlab are the same' do
        let(:allow_force_pushes_on_github) { true }
        let(:allow_force_pushes_on_gitlab) { true }
        let(:expected_allow_force_push) { true }

        it_behaves_like 'create branch protection by the strictest ruleset'
      end
    end

    context 'when branch is not protected on GitLab' do
      let(:expected_allow_force_push) { true }

      it_behaves_like 'create branch protection by the strictest ruleset'
    end
  end
end