summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/config/entry/policy_spec.rb
blob: 69095144daaa503debc450b95ea8abee6d409cf4 (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
require 'spec_helper'

describe Gitlab::Ci::Config::Entry::Policy do
  let(:entry) { described_class.new(config) }

  context 'when using simplified policy' do
    describe 'validations' do
      context 'when entry config value is valid' do
        context 'when config is a branch or tag name' do
          let(:config) { %w[master feature/branch] }

          describe '#valid?' do
            it 'is valid' do
              expect(entry).to be_valid
            end
          end

          describe '#value' do
            it 'returns key value' do
              expect(entry.value).to eq(refs: config)
            end
          end
        end

        context 'when config is a regexp' do
          let(:config) { ['/^issue-.*$/'] }

          describe '#valid?' do
            it 'is valid' do
              expect(entry).to be_valid
            end
          end
        end

        context 'when config is a special keyword' do
          let(:config) { %w[tags triggers branches] }

          describe '#valid?' do
            it 'is valid' do
              expect(entry).to be_valid
            end
          end
        end
      end

      context 'when entry value is not valid' do
        let(:config) { [1] }

        describe '#errors' do
          it 'saves errors' do
            expect(entry.errors)
              .to include /policy config should be an array of strings or regexps/
          end
        end
      end
    end
  end

  context 'when using complex policy' do
    context 'when it is an empty hash' do
      let(:config) { { } }

      it 'reports an error about configuration not being present' do
        expect(entry.errors).to include /can't be blank/
      end
    end

    context 'when it contains unknown keys' do
      let(:config) { { refs: ['something'], invalid: 'master' } }

      it 'is not valid entry' do
        expect(entry).not_to be_valid
        expect(entry.errors)
          .to include /policy config contains unknown keys: invalid/
      end
    end
  end

  context 'when policy strategy does not match' do
    let(:config) { 'string strategy' }

    it 'returns information about errors' do
      expect(entry.errors)
        .to include 'policy has to be either an array of conditions or a hash'
    end
  end
end