debian-mirror-gitlab/spec/support/matchers/schema_matcher.rb

50 lines
1.2 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
module SchemaPath
2019-07-07 11:18:12 +05:30
def self.expand(schema, dir = nil)
2021-03-08 18:12:59 +05:30
return schema unless schema.is_a?(String)
2019-07-07 11:18:12 +05:30
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-09-10 17:25:29 +05:30
end
end
2019-07-07 11:18:12 +05:30
RSpec::Matchers.define :match_response_schema do |schema, dir: nil, **options|
2016-09-13 17:45:13 +05:30
match do |response|
2017-09-10 17:25:29 +05:30
@errors = JSON::Validator.fully_validate(
SchemaPath.expand(schema, dir), response.body, options)
@errors.empty?
end
failure_message do |response|
"didn't match the schema defined by #{SchemaPath.expand(schema, dir)}" \
" The validation errors were:\n#{@errors.join("\n")}"
end
end
2016-09-13 17:45:13 +05:30
2019-07-07 11:18:12 +05:30
RSpec::Matchers.define :match_schema do |schema, dir: nil, **options|
2017-09-10 17:25:29 +05:30
match do |data|
2019-07-07 11:18:12 +05:30
@errors = JSON::Validator.fully_validate(
SchemaPath.expand(schema, dir), data, options)
@errors.empty?
end
failure_message do |response|
2021-03-08 18:12:59 +05:30
"didn't match the schema defined by #{schema_name(schema, dir)}" \
2019-07-07 11:18:12 +05:30
" The validation errors were:\n#{@errors.join("\n")}"
2016-09-13 17:45:13 +05:30
end
2021-03-08 18:12:59 +05:30
def schema_name(schema, dir)
return 'provided schema' unless schema.is_a?(String)
SchemaPath.expand(schema, dir)
end
2016-09-13 17:45:13 +05:30
end