summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/config/node/validator_spec.rb
blob: ad875d553840bf6b0b0797e8d2c645fd8ae35edd (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
require 'spec_helper'

describe Gitlab::Ci::Config::Node::Validator do
  let(:validator) { Class.new(described_class) }
  let(:validator_instance) { validator.new(node) }
  let(:node) { spy('node') }

  shared_examples 'delegated validator' do
    context 'when node is valid' do
      before do
        allow(node).to receive(:test_attribute).and_return('valid value')
      end

      it 'validates attribute in node' do
        expect(node).to receive(:test_attribute)
        expect(validator_instance).to be_valid
      end

      it 'returns no errors' do
        validator_instance.validate

        expect(validator_instance.full_errors).to be_empty
      end
    end

    context 'when node is invalid' do
      before do
        allow(node).to receive(:test_attribute).and_return(nil)
      end

      it 'validates attribute in node' do
        expect(node).to receive(:test_attribute)
        expect(validator_instance).to be_invalid
      end

      it 'returns errors' do
        validator_instance.validate

        expect(validator_instance.full_errors).not_to be_empty
      end
    end
  end

  describe 'attributes validations' do
    before do
      validator.class_eval do
        validates :test_attribute, presence: true
      end
    end

    it_behaves_like 'delegated validator'
  end

  describe 'interface validations' do
    before do
      validator.class_eval do
        validate do
          unless @node.test_attribute == 'valid value'
            errors.add(:test_attribute, 'invalid value')
          end
        end
      end
    end

    it_behaves_like 'delegated validator'
  end
end