2019-07-25 01:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-04 09:45:14 -04:00
|
|
|
module SchemaPath
|
2021-04-26 08:09:44 -04:00
|
|
|
@schema_cache = {}
|
|
|
|
|
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
|
|
|
|
2021-04-26 08:09:44 -04:00
|
|
|
def self.validator(schema_path)
|
|
|
|
unless @schema_cache.key?(schema_path)
|
|
|
|
@schema_cache[schema_path] = JSONSchemer.schema(schema_path, ref_resolver: SchemaPath.file_ref_resolver)
|
|
|
|
end
|
2017-07-26 08:33:09 -04:00
|
|
|
|
2021-04-26 08:09:44 -04:00
|
|
|
@schema_cache[schema_path]
|
2017-07-26 08:33:09 -04:00
|
|
|
end
|
|
|
|
|
2021-04-26 08:09:44 -04:00
|
|
|
def self.file_ref_resolver
|
|
|
|
proc do |uri|
|
|
|
|
file = Rails.root.join(uri.path)
|
|
|
|
raise StandardError, "Ref file #{uri.path} must be json" unless uri.path.ends_with?('.json')
|
|
|
|
raise StandardError, "File #{file.to_path} doesn't exists" unless file.exist?
|
|
|
|
|
|
|
|
Gitlab::Json.parse(File.read(file))
|
|
|
|
end
|
2017-06-06 13:35:28 -04:00
|
|
|
end
|
|
|
|
end
|
2016-08-01 14:41:30 -04:00
|
|
|
|
2021-04-26 08:09:44 -04:00
|
|
|
RSpec::Matchers.define :match_response_schema do |schema, dir: nil, **options|
|
|
|
|
match do |response|
|
|
|
|
schema_path = Pathname.new(SchemaPath.expand(schema, dir))
|
|
|
|
validator = SchemaPath.validator(schema_path)
|
2019-03-12 13:10:24 -04:00
|
|
|
|
2021-04-26 08:09:44 -04:00
|
|
|
data = Gitlab::Json.parse(response.body)
|
2019-03-12 13:10:24 -04:00
|
|
|
|
2021-04-26 08:09:44 -04:00
|
|
|
validator.valid?(data)
|
2016-08-01 14:41:30 -04:00
|
|
|
end
|
2021-04-26 08:09:44 -04:00
|
|
|
end
|
2021-01-13 07:10:27 -05:00
|
|
|
|
2021-07-30 14:09:08 -04:00
|
|
|
RSpec::Matchers.define :match_metric_definition_schema do |path, dir: nil, **options|
|
|
|
|
match do |data|
|
|
|
|
schema_path = Pathname.new(Rails.root.join(dir.to_s, path).to_s)
|
|
|
|
validator = SchemaPath.validator(schema_path)
|
|
|
|
|
|
|
|
data = data.stringify_keys if data.is_a? Hash
|
|
|
|
|
|
|
|
validator.valid?(data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-11 20:10:27 -04:00
|
|
|
RSpec::Matchers.define :match_snowplow_schema do |schema, dir: nil, **options|
|
|
|
|
match do |data|
|
|
|
|
schema_path = Pathname.new(Rails.root.join(dir.to_s, 'spec', "fixtures/product_intelligence/#{schema}.json").to_s)
|
|
|
|
validator = SchemaPath.validator(schema_path)
|
|
|
|
|
|
|
|
validator.valid?(data.stringify_keys)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-26 08:09:44 -04:00
|
|
|
RSpec::Matchers.define :match_schema do |schema, dir: nil, **options|
|
|
|
|
match do |data|
|
|
|
|
schema = SchemaPath.expand(schema, dir)
|
|
|
|
schema = Pathname.new(schema) if schema.is_a?(String)
|
|
|
|
validator = SchemaPath.validator(schema)
|
|
|
|
|
|
|
|
if data.is_a?(String)
|
|
|
|
validator.valid?(Gitlab::Json.parse(data))
|
|
|
|
else
|
|
|
|
validator.valid?(data)
|
|
|
|
end
|
2021-01-13 07:10:27 -05:00
|
|
|
end
|
2016-08-01 14:41:30 -04:00
|
|
|
end
|