summaryrefslogtreecommitdiff
path: root/spec/models/hooks/active_hook_filter_spec.rb
blob: 47c0fbdb1062ad925f540dd726b103c0dd78ea10 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ActiveHookFilter do
  subject(:filter) { described_class.new(hook) }

  describe '#matches?' do
    using RSpec::Parameterized::TableSyntax

    context 'for various types of branch_filter' do
      let(:hook) do
        build(:project_hook, push_events: true, issues_events: true)
      end

      where(:branch_filter_strategy, :branch_filter, :ref, :expected_matches?) do
        'all_branches'  | 'master'        | 'refs/heads/master'                       | true
        'all_branches'  | ''              | 'refs/heads/master'                       | true
        'all_branches'  | nil             | 'refs/heads/master'                       | true
        'all_branches'  | '.*'            | 'refs/heads/master'                       | true
        'wildcard'      | 'master'        | 'refs/heads/master'                       | true
        'wildcard'      | 'master'        | 'refs/heads/my_branch'                    | false
        'wildcard'      | 'features/*'    | 'refs/heads/features/my-branch'           | true
        'wildcard'      | 'features/*'    | 'refs/heads/features/my-branch/something' | true
        'wildcard'      | 'features/*'    | 'refs/heads/master'                       | false
        'wildcard'      | nil             | 'refs/heads/master'                       | true
        'wildcard'      | ''              | 'refs/heads/master'                       | true
        'regex'         | 'master'        | 'refs/heads/master'                       | true
        'regex'         | 'master'        | 'refs/heads/my_branch'                    | false
        'regex'         | 'features/*'    | 'refs/heads/xxxx/features/my-branch'      | true
        'regex'         | 'features/*'    | 'refs/heads/features/'                    | true
        'regex'         | 'features/*'    | 'refs/heads/features'                     | true
        'regex'         | 'features/.*'   | 'refs/heads/features/my-branch'           | true
        'regex'         | 'features/.*'   | 'refs/heads/features/my-branch/something' | true
        'regex'         | 'features/.*'   | 'refs/heads/master'                       | false
        'regex'         | '(feature|dev)' | 'refs/heads/feature'                      | true
        'regex'         | '(feature|dev)' | 'refs/heads/dev'                          | true
        'regex'         | '(feature|dev)' | 'refs/heads/master'                       | false
        'regex'         | nil             | 'refs/heads/master'                       | true
        'regex'         | ''              | 'refs/heads/master'                       | true
      end

      with_them do
        before do
          hook.assign_attributes(
            push_events_branch_filter: branch_filter,
            branch_filter_strategy: branch_filter_strategy
          )
        end

        it { expect(filter.matches?(:push_hooks, { ref: ref })).to be expected_matches? }
        it { expect(filter.matches?(:issues_events, { ref: ref })).to be true }
      end

      context 'when the branch filter is a invalid regex' do
        let(:hook) do
          build(
            :project_hook,
            push_events: true,
            push_events_branch_filter: 'master',
            branch_filter_strategy: 'regex'
          )
        end

        before do
          allow(hook).to receive(:push_events_branch_filter).and_return("invalid-regex[")
        end

        it { expect(filter.matches?(:push_hooks, { ref: 'refs/heads/master' })).to be false }
      end

      context 'when the branch filter is not properly set to nil' do
        let(:hook) do
          build(
            :project_hook,
            push_events: true,
            branch_filter_strategy: 'all_branches'
          )
        end

        before do
          allow(hook).to receive(:push_events_branch_filter).and_return("master")
        end

        it { expect(filter.matches?(:push_hooks, { ref: 'refs/heads/feature1' })).to be true }
      end
    end

    context 'when feature flag is disabled' do
      before do
        stub_feature_flags(enhanced_webhook_support_regex: false)
      end

      let(:hook) do
        build(
          :project_hook,
          push_events: true,
          push_events_branch_filter: '(master)',
          branch_filter_strategy: 'regex'
        )
      end

      it { expect(filter.matches?(:push_hooks, { ref: 'refs/heads/master' })).to be false }
      it { expect(filter.matches?(:push_hooks, { ref: 'refs/heads/(master)' })).to be true }
    end
  end
end