2019-07-25 01:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-04 09:45:14 -04:00
|
|
|
module SchemaPath
|
2019-03-12 13:10:24 -04:00
|
|
|
def self.expand(schema, dir = nil)
|
2021-01-06 13:10:52 -05:00
|
|
|
return schema unless schema.is_a?(String)
|
|
|
|
|
2019-03-12 13:10:24 -04:00
|
|
|
if Gitlab.ee? && dir.nil?
|
|
|
|
ee_path = expand(schema, 'ee')
|
|
|
|
|
|
|
|
return ee_path if File.exist?(ee_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
Rails.root.join(dir.to_s, 'spec', "fixtures/api/schemas/#{schema}.json").to_s
|
2017-08-04 09:45:14 -04:00
|
|
|
end
|
2017-06-06 13:35:28 -04:00
|
|
|
end
|
|
|
|
|
2019-03-12 13:10:24 -04:00
|
|
|
RSpec::Matchers.define :match_response_schema do |schema, dir: nil, **options|
|
2016-08-01 14:41:30 -04:00
|
|
|
match do |response|
|
2017-08-04 09:45:14 -04:00
|
|
|
@errors = JSON::Validator.fully_validate(
|
|
|
|
SchemaPath.expand(schema, dir), response.body, options)
|
2017-07-26 08:33:09 -04:00
|
|
|
|
|
|
|
@errors.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
failure_message do |response|
|
2017-08-04 09:45:14 -04:00
|
|
|
"didn't match the schema defined by #{SchemaPath.expand(schema, dir)}" \
|
2017-07-26 08:33:09 -04:00
|
|
|
" The validation errors were:\n#{@errors.join("\n")}"
|
2017-06-06 13:35:28 -04:00
|
|
|
end
|
|
|
|
end
|
2016-08-01 14:41:30 -04:00
|
|
|
|
2019-03-12 13:10:24 -04:00
|
|
|
RSpec::Matchers.define :match_schema do |schema, dir: nil, **options|
|
2017-06-06 13:35:28 -04:00
|
|
|
match do |data|
|
2019-03-12 13:10:24 -04:00
|
|
|
@errors = JSON::Validator.fully_validate(
|
|
|
|
SchemaPath.expand(schema, dir), data, options)
|
|
|
|
|
|
|
|
@errors.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
failure_message do |response|
|
2021-01-13 07:10:27 -05:00
|
|
|
"didn't match the schema defined by #{schema_name(schema, dir)}" \
|
2019-03-12 13:10:24 -04:00
|
|
|
" The validation errors were:\n#{@errors.join("\n")}"
|
2016-08-01 14:41:30 -04:00
|
|
|
end
|
2021-01-13 07:10:27 -05:00
|
|
|
|
|
|
|
def schema_name(schema, dir)
|
|
|
|
return 'provided schema' unless schema.is_a?(String)
|
|
|
|
|
|
|
|
SchemaPath.expand(schema, dir)
|
|
|
|
end
|
2016-08-01 14:41:30 -04:00
|
|
|
end
|