Welcome to mirror list, hosted at ThFree Co, Russian Federation.

validators_spec.rb « entry « config « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6fa9f9d076716b9040766df8e25b82f9797f30b4 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Config::Entry::Validators, feature_category: :pipeline_composition do
  let(:klass) do
    Class.new do
      include ActiveModel::Validations
      include Gitlab::Config::Entry::Validators
    end
  end

  let(:instance) { klass.new }

  describe described_class::MutuallyExclusiveKeysValidator do
    using RSpec::Parameterized::TableSyntax

    before do
      klass.instance_eval do
        validates :config, mutually_exclusive_keys: [:foo, :bar]
      end

      allow(instance).to receive(:config).and_return(config)
    end

    where(:context, :config, :valid_result) do
      'with mutually exclusive keys' | { foo: 1, bar: 2 } | false
      'without mutually exclusive keys' | { foo: 1 } | true
      'without mutually exclusive keys' | { bar: 1 } | true
      'with other keys' | { foo: 1, baz: 2 } | true
    end

    with_them do
      it 'validates the instance' do
        expect(instance.valid?).to be(valid_result)

        unless valid_result
          expect(instance.errors.messages_for(:config)).to include /please use only one of the following keys: foo, bar/
        end
      end
    end
  end

  describe described_class::DisallowedKeysValidator do
    using RSpec::Parameterized::TableSyntax

    where(:config, :disallowed_keys, :ignore_nil, :valid_result) do
      { foo: '1' }                     | 'foo'      | false | false
      { foo: '1', bar: '2', baz: '3' } | 'foo, bar' | false | false
      { baz: '1', qux: '2' }           | ''         | false | true
      { foo: nil }                     | 'foo'      | false | false
      { foo: nil, bar: '2', baz: '3' } | 'foo, bar' | false | false
      { foo: nil, bar: nil, baz: '3' } | 'foo, bar' | false | false
      { baz: nil, qux: nil }           | ''         | false | true
      { foo: '1' }                     | 'foo'      | true  | false
      { foo: '1', bar: '2', baz: '3' } | 'foo, bar' | true  | false
      { baz: '1', qux: '2' }           | ''         | true  | true
      { foo: nil }                     | ''         | true  | true
      { foo: nil, bar: '2', baz: '3' } | 'bar'      | true  | false
      { foo: nil, bar: nil, baz: '3' } | ''         | true  | true
      { baz: nil, qux: nil }           | ''         | true  | true
    end

    with_them do
      before do
        klass.instance_variable_set(:@ignore_nil, ignore_nil)

        klass.instance_eval do
          validates :config, disallowed_keys: {
            in: %i[foo bar],
            ignore_nil: @ignore_nil # rubocop:disable RSpec/InstanceVariable
          }
        end

        allow(instance).to receive(:config).and_return(config)
      end

      it 'validates the instance' do
        expect(instance.valid?).to be(valid_result)

        unless valid_result
          expect(instance.errors.messages_for(:config)).to include "contains disallowed keys: #{disallowed_keys}"
        end
      end
    end

    context 'when custom message is provided' do
      before do
        klass.instance_eval do
          validates :config, disallowed_keys: {
            in: %i[foo bar],
            message: 'custom message'
          }
        end

        allow(instance).to receive(:config).and_return({ foo: '1' })
      end

      it 'returns the custom message when invalid' do
        expect(instance).not_to be_valid
        expect(instance.errors.messages_for(:config)).to include "custom message: foo"
      end
    end
  end

  describe described_class::OnlyOneOfKeysValidator do
    using RSpec::Parameterized::TableSyntax

    where(:config, :valid_result) do
      { foo: '1' }                     | true
      { foo: '1', bar: '2', baz: '3' } | false
      { bar: '2' }                     | true
      { foo: '1' }                     | true
      {}                               | false
      { baz: '3' }                     | false
    end

    with_them do
      before do
        klass.instance_eval do
          validates :config, only_one_of_keys: %i[foo bar]
        end

        allow(instance).to receive(:config).and_return(config)
      end

      it 'validates the instance' do
        expect(instance.valid?).to be(valid_result)

        unless valid_result
          expect(instance.errors.messages_for(:config)).to(
            include "must use exactly one of these keys: foo, bar"
          )
        end
      end
    end
  end
end