File: json_schema_validator_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (230 lines) | stat: -rw-r--r-- 7,157 bytes parent folder | download
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe JsonSchemaValidator, feature_category: :shared do
  describe '#validates_each' do
    let(:build_report_result) { build(:ci_build_report_result, :with_junit_success) }

    subject { validator.validate(build_report_result) }

    context 'when filename is set' do
      let(:validator) { described_class.new(attributes: [:data], filename: "build_report_result_data") }

      context 'when data is valid' do
        it 'returns no errors' do
          subject

          expect(build_report_result.errors).to be_empty
        end
      end

      context 'when data is invalid' do
        context 'when error message is not provided' do
          it 'returns default set error message i.e `must be a valid json schema`' do
            build_report_result.data = { invalid: 'data' }
            validator.validate(build_report_result)

            expect(build_report_result.errors.size).to eq(1)
            expect(build_report_result.errors.full_messages).to eq(["Data must be a valid json schema"])
          end
        end
      end

      context 'when error message is provided' do
        let(:validator) { described_class.new(attributes: [:data], filename: "build_report_result_data", message: "error in build-report-json") }

        it 'returns the provided error message' do
          build_report_result.data = { invalid: 'data' }
          validator.validate(build_report_result)

          expect(build_report_result.errors.size).to eq(1)
          expect(build_report_result.errors.full_messages).to eq(["Data error in build-report-json"])
        end
      end
    end

    context 'when filename is not set' do
      let(:validator) { described_class.new(attributes: [:data]) }

      it 'raises an ArgumentError' do
        expect { subject }.to raise_error(ArgumentError)
      end
    end

    context 'when filename is invalid' do
      let(:validator) { described_class.new(attributes: [:data], filename: "invalid$filename") }

      it 'raises a FilenameError' do
        expect { subject }.to raise_error(described_class::FilenameError)
      end
    end

    describe 'hash_conversion option' do
      context 'when hash_conversion is enabled' do
        let(:validator) { described_class.new(attributes: [:data], filename: "build_report_result_data", hash_conversion: true) }

        it 'returns no errors' do
          subject

          expect(build_report_result.errors).to be_empty
        end
      end
    end

    context 'when detail_errors is true' do
      let(:validator) { described_class.new(attributes: [:data], detail_errors: true, filename: "build_report_result_data") }

      context 'when data is valid' do
        it 'returns no errors' do
          subject

          expect(build_report_result.errors).to be_empty
        end
      end

      context 'when data is invalid' do
        it 'returns json schema is invalid' do
          build_report_result.data = { invalid: 'data' }

          subject

          expect(build_report_result.errors.size).to eq(1)
          expect(build_report_result.errors.full_messages).to match_array(
            ["Data object property at `/invalid` is a disallowed additional property"]
          )
        end
      end
    end

    context 'when validating config with oneOf JSON schema' do
      let(:config) do
        {
          run: [
            {
              name: 'hello_steps',
              step: 'gitlab.com/gitlab-org/ci-cd/runner-tools/echo-step',
              inputs: {
                echo: 'hello steps!'
              }
            }
          ]
        }
      end

      let(:job) { Gitlab::Ci::Config::Entry::Job.new(config, name: :rspec) }
      let(:errors) { ActiveModel::Errors.new(job) }

      let(:validator) do
        described_class.new(
          attributes: [:run],
          base_directory: 'app/validators/json_schemas',
          filename: 'run_steps',
          hash_conversion: true,
          detail_errors: true
        )
      end

      before do
        job.compose!
        allow(job).to receive(:errors).and_return(errors)
      end

      subject { validator.validate(job) }

      context 'when the value is a valid array of hashes' do
        before do
          allow(job).to receive(:read_attribute_for_validation).and_return(config[:run])
        end

        it 'returns no errors' do
          subject

          expect(job.errors).to be_empty
        end
      end

      context 'when a required property is missing' do
        before do
          config[:run] = [{ name: 'hello_steps' }]
          allow(job).to receive(:read_attribute_for_validation).and_return(config[:run])
        end

        it 'returns an error message' do
          subject

          expect(job.errors).not_to be_empty
          expect("#{job.errors.first.attribute} #{job.errors.first.type}").to eq("run object at `/0` is missing required properties: step")
        end
      end

      context 'when oneOf validation fails' do
        before do
          config[:run] = [nil]
          allow(job).to receive(:read_attribute_for_validation).and_return(config[:run])
        end

        it 'returns an error message' do
          subject

          expect(job.errors).not_to be_empty
          expect("#{job.errors.first.attribute} #{job.errors.first.type}").to eq(
            "run value at /0 should use only one of: step, script"
          )
        end
      end

      context 'when there is a general validation error' do
        before do
          config[:run] = 'not an array'
          allow(job).to receive(:read_attribute_for_validation).and_return(config[:run])
        end

        it 'returns an error message' do
          subject

          expect(job.errors).not_to be_empty
          expect("#{job.errors.first.attribute} #{job.errors.first.type}").to eq("run value at root is not an array")
        end
      end

      context 'when a non-array value violates oneOf constraint' do
        let(:schema) do
          {
            "type" => "object",
            "properties" => {
              "run" => {
                "oneOf" => [
                  { required: ["step"], title: "step" },
                  { required: ["script"], title: "script" }
                ]
              }
            }
          }
        end

        let(:validator) do
          described_class.new(
            attributes: [:run],
            filename: 'test_schema',
            detail_errors: true
          )
        end

        before do
          config[:run] = 'C'
          allow(job).to receive(:read_attribute_for_validation).and_return({ run: config[:run] })
          allow(JSONSchemer).to receive(:schema).and_return(JSONSchemer.schema(schema))
          allow(File).to receive(:read).with(anything).and_return(schema.to_json)
        end

        it 'returns an error message for oneOf violation without data pointer' do
          subject

          expect(job.errors).not_to be_empty
          expect("#{job.errors.first.attribute} #{job.errors.first.type}").to eq("run should use only one of: step, script")
        end
      end
    end
  end
end