diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-05-19 15:44:42 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-05-19 15:44:42 +0000 |
commit | 4555e1b21c365ed8303ffb7a3325d773c9b8bf31 (patch) | |
tree | 5423a1c7516cffe36384133ade12572cf709398d /spec/support/matchers | |
parent | e570267f2f6b326480d284e0164a6464ba4081bc (diff) | |
download | gitlab-ce-4555e1b21c365ed8303ffb7a3325d773c9b8bf31.tar.gz |
Add latest changes from gitlab-org/gitlab@13-12-stable-eev13.12.0-rc42
Diffstat (limited to 'spec/support/matchers')
-rw-r--r-- | spec/support/matchers/access_matchers_generic.rb | 2 | ||||
-rw-r--r-- | spec/support/matchers/markdown_matchers.rb | 2 | ||||
-rw-r--r-- | spec/support/matchers/schema_matcher.rb | 58 |
3 files changed, 41 insertions, 21 deletions
diff --git a/spec/support/matchers/access_matchers_generic.rb b/spec/support/matchers/access_matchers_generic.rb index 13955750f4f..a38a83a2547 100644 --- a/spec/support/matchers/access_matchers_generic.rb +++ b/spec/support/matchers/access_matchers_generic.rb @@ -35,7 +35,7 @@ module AccessMatchersGeneric run_matcher(action, role, @membership, @owned_objects) do |action| action.call - rescue => e + rescue StandardError => e @error = e raise unless e.is_a?(ERROR_CLASS) end diff --git a/spec/support/matchers/markdown_matchers.rb b/spec/support/matchers/markdown_matchers.rb index 47cffad8c41..dfdb5bc01ae 100644 --- a/spec/support/matchers/markdown_matchers.rb +++ b/spec/support/matchers/markdown_matchers.rb @@ -291,4 +291,4 @@ module RSpec::Matchers::DSL::Macros end end -MarkdownMatchers.prepend_if_ee('EE::MarkdownMatchers') +MarkdownMatchers.prepend_mod_with('MarkdownMatchers') diff --git a/spec/support/matchers/schema_matcher.rb b/spec/support/matchers/schema_matcher.rb index f0e7a52c51e..94e4359b1dd 100644 --- a/spec/support/matchers/schema_matcher.rb +++ b/spec/support/matchers/schema_matcher.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true module SchemaPath + @schema_cache = {} + def self.expand(schema, dir = nil) return schema unless schema.is_a?(String) @@ -12,38 +14,56 @@ module SchemaPath Rails.root.join(dir.to_s, 'spec', "fixtures/api/schemas/#{schema}.json").to_s end + + 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 + + @schema_cache[schema_path] + end + + 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 + end end RSpec::Matchers.define :match_response_schema do |schema, dir: nil, **options| match do |response| - @errors = JSON::Validator.fully_validate( - SchemaPath.expand(schema, dir), response.body, options) + schema_path = Pathname.new(SchemaPath.expand(schema, dir)) + validator = SchemaPath.validator(schema_path) - @errors.empty? - end + data = Gitlab::Json.parse(response.body) - failure_message do |response| - "didn't match the schema defined by #{SchemaPath.expand(schema, dir)}" \ - " The validation errors were:\n#{@errors.join("\n")}" + validator.valid?(data) end end -RSpec::Matchers.define :match_schema do |schema, dir: nil, **options| +RSpec::Matchers.define :match_snowplow_schema do |schema, dir: nil, **options| match do |data| - @errors = JSON::Validator.fully_validate( - SchemaPath.expand(schema, dir), data, options) - - @errors.empty? - end + schema_path = Pathname.new(Rails.root.join(dir.to_s, 'spec', "fixtures/product_intelligence/#{schema}.json").to_s) + validator = SchemaPath.validator(schema_path) - failure_message do |response| - "didn't match the schema defined by #{schema_name(schema, dir)}" \ - " The validation errors were:\n#{@errors.join("\n")}" + validator.valid?(data.stringify_keys) end +end - def schema_name(schema, dir) - return 'provided schema' unless schema.is_a?(String) +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) - SchemaPath.expand(schema, dir) + if data.is_a?(String) + validator.valid?(Gitlab::Json.parse(data)) + else + validator.valid?(data) + end end end |