govuk-content-schema-test-helpers 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 011b735b77af91d6916c2bc5a2a44738c7b35fa3
4
- data.tar.gz: a2604a0fc50abdc8a4354b77e5700d4ef7097cfd
3
+ metadata.gz: 9f9fcae915c287cae77f19e2f189b9bd6858f23f
4
+ data.tar.gz: e50af50fca493575e933c982ce0a36749ab83b13
5
5
  SHA512:
6
- metadata.gz: 18187cd7620e0191b5f4b77a885c923352463bdfc80c859d51827a6e3befe722296a7063bb50a62ef57aafda482f6a52cc6432962e50968411bd084a40f692f6
7
- data.tar.gz: aa600d91f64117aed5a63f2f31e082bdd8a8adcfec1d5ca8114b38bd859aee3cbeb221799b53b64692998ad6ab0bcc69063d2a6df1313dcb43bdfc95bc8ec85f
6
+ metadata.gz: 2428aebd180cbd3a47844240422e4286397010ce29b9ff2f019162db02882af93488ba873239bd1ed3feaa5b0f7413ed6d77aaa9864022e8f47e91b31ab75e0e
7
+ data.tar.gz: 087633067fe7a5f726935981b4e4aca067ac438e72901316ca30d6a389a07290bf7f4f13cdc918ff0a03001c00a843963daa09be1042de98ae90add2773ae92e
data/README.md CHANGED
@@ -12,13 +12,13 @@ This app provides test helpers for working with [alphagov/govuk-content-schemas]
12
12
 
13
13
  - [alphagov/govuk-content-schemas](https://github.com/alphagov/govuk-content-schemas) - contains the examples and schemas returned by this gem
14
14
 
15
- ### Usage
15
+ ### Usage (Publishers and frontend applications)
16
16
 
17
17
  #### Configuration and setup
18
18
 
19
19
  Firstly, you will need a copy of [govuk-content-schemas](http://github.com/alphagov/govuk-content-schemas) on your file system. By default these should be in a sibling directory to your project. Alternatively, you can specify their location with the `GOVUK_CONTENT_SCHEMAS_PATH` environment variable. You should probably duplicate this paragraph (excluding this sentence) in your own `README`.
20
20
 
21
- You will need to configure which type of schemas your app uses. A good place to do this is in `test_helper.rb` or `spec_helper.rb`:
21
+ You will need to configure which type of schemas your app uses. A good place to do this is in `test_helper.rb` or `spec/support/govuk_content_schemas.rb`:
22
22
 
23
23
  ```ruby
24
24
  require 'govuk-content-schema-test-helpers'
@@ -34,22 +34,58 @@ If you are not using Rails, you'll need to set `project_root` differently. Assum
34
34
  config.project_root = File.absolute_path(File.join(File.basename(__FILE__), '..'))
35
35
  ```
36
36
 
37
- #### Loading an example document
37
+ #### Loading example documents
38
38
 
39
39
  ```ruby
40
40
  GovukContentSchemaTestHelpers::Examples.new.get('finder', 'contacts')
41
41
  # => '{ "some": "json" }'
42
42
  ```
43
43
 
44
- #### Loading all examples for a given format
45
-
46
-
47
44
  ```ruby
48
45
  GovukContentSchemaTestHelpers::Examples.new.get_all_for_format('case_study')
49
46
  # => ['{ "first": "example"}', '{ "second": "example" }']
50
47
  ```
51
48
 
52
- This would be useful for checking your app can handle all examples and any that come into existence later:
49
+ #### Frontend contract tests
50
+
51
+ Check that your app can handle requests for all relevant examples,
52
+ including any added later.
53
+
54
+ ##### RSpec
55
+
56
+ ```ruby
57
+ require "spec_helper"
58
+ require "gds_api/test_helpers/content_store"
59
+
60
+ RSpec.describe "Schema compatibility", type: :request do
61
+ include GdsApi::TestHelpers::ContentStore
62
+
63
+ before do
64
+ GovukContentSchemaTestHelpers::Examples.new.get_all_for_format("finder").each do |finder_format|
65
+ content_item = JSON.parse(finder_format)
66
+ content_store_has_item(content_item['base_path'], content_item)
67
+ end
68
+ end
69
+
70
+ all_examples_for_supported_formats = GovukContentSchemaTestHelpers::Examples.new.get_all_for_formats(%w{
71
+ specialist_document
72
+ })
73
+
74
+ all_examples_for_supported_formats.each do |example|
75
+ content_item = JSON.parse(example)
76
+
77
+ it "can handle a request for #{content_item["base_path"]}" do
78
+ content_store_has_item(content_item['base_path'], content_item)
79
+
80
+ get content_item['base_path']
81
+ expect(response.status).to eq(200)
82
+ assert_select 'title', Regexp.new(Regexp.escape(content_item['title']))
83
+ end
84
+ end
85
+ end
86
+ ```
87
+
88
+ ##### Test::Unit/Minitest
53
89
 
54
90
  ```ruby
55
91
  def supported_formats
@@ -76,14 +112,17 @@ This would be useful for checking your app can handle all examples and any that
76
112
  end
77
113
  ```
78
114
 
115
+ #### Publisher contract tests
116
+
117
+ Check that your presenter produces JSON which adheres to the appropriate schema.
79
118
 
80
- #### RSpec matcher
119
+ ##### RSpec
81
120
 
82
- To use the built-in RSpec matcher, add this to spec_helper.rb:
121
+ To use the built-in RSpec matcher, add this to `spec/support/govuk_content_schemas.rb`:
83
122
 
84
123
  ```ruby
85
124
  require 'govuk-content-schema-test-helpers/rspec_matchers'
86
- include GovukContentSchemaTestHelpers::RSpecMatchers
125
+ RSpec.configuration.include GovukContentSchemaTestHelpers::RSpecMatchers
87
126
  ```
88
127
 
89
128
  Then:
@@ -98,7 +137,7 @@ or as an argument matcher:
98
137
  .with("/first-finder", be_valid_against_schema('finder'))
99
138
  ```
100
139
 
101
- #### Test::Unit/Minitest
140
+ ##### Test::Unit/Minitest
102
141
 
103
142
  Setup:
104
143
 
@@ -112,10 +151,10 @@ Then in a test:
112
151
  assert_valid_against_schema(presented_hash, 'case_study')
113
152
  ```
114
153
 
115
- #### Validating against the schema manually
154
+ ##### Validating against the schema manually
116
155
 
117
156
  ```ruby
118
- validator = GovukContentSchemaTestHelpers::Validator.new('finder', '{ "some": "json" }')
157
+ validator = GovukContentSchemaTestHelpers::Validator.new('finder', 'schema', '{ "some": "json" }')
119
158
  validator.valid?
120
159
  # => false
121
160
  validator.errors
@@ -2,24 +2,76 @@ module GovukContentSchemaTestHelpers
2
2
  module RSpecMatchers
3
3
  RSpec::Matchers.define :be_valid_against_schema do |schema_name|
4
4
  match do |actual|
5
- validator = Validator.new(schema_name, actual)
5
+ validator = Validator.new(schema_name, "schema", actual)
6
6
  validator.valid?
7
7
  end
8
8
 
9
- # Required for a helpful message with RSpec 3
10
- description do |actual|
11
- validator = Validator.new(schema_name, actual)
12
- "to be valid against '#{schema_name}' schema. Errors: #{validator.errors}"
9
+ # `failure_message` is RSpec 3.0, `failure_message_for_should` also works
10
+ # in 2.X, but deprecated in rspec 3.0.
11
+ if respond_to?(:failure_message)
12
+ failure_message do |actual|
13
+ ValidationErrorMessage.new(schema_name, "schema", actual).message
14
+ end
15
+ else
16
+ failure_message_for_should do |actual|
17
+ ValidationErrorMessage.new(schema_name, "schema", actual).message
18
+ end
19
+ end
20
+ end
21
+
22
+ RSpec::Matchers.define :be_valid_against_links_schema do |schema_name|
23
+ match do |actual|
24
+ validator = Validator.new(schema_name, "links", actual)
25
+ validator.valid?
13
26
  end
14
27
 
15
- if Gem.loaded_specs['rspec-expectations'].version < Gem::Version.new('3.0.0')
16
- # Required for a helpful message with RSpec 2
17
- # Generates a deprecation warning on 3.2.0
28
+ # `failure_message` is RSpec 3.0, `failure_message_for_should` also works
29
+ # in 2.X, but deprecated in rspec 3.0.
30
+ if respond_to?(:failure_message)
31
+ failure_message do |actual|
32
+ ValidationErrorMessage.new(schema_name, "links", actual).message
33
+ end
34
+ else
18
35
  failure_message_for_should do |actual|
19
- validator = Validator.new(schema_name, actual)
20
- "to be valid against '#{schema_name}' schema. Errors: #{validator.errors}"
36
+ ValidationErrorMessage.new(schema_name, "links", actual).message
21
37
  end
22
38
  end
23
39
  end
24
40
  end
41
+
42
+ class ValidationErrorMessage
43
+ attr_reader :schema_name, :type, :payload
44
+
45
+ def initialize(schema_name, type, payload)
46
+ @schema_name = schema_name
47
+ @type = type
48
+ @payload = payload
49
+ end
50
+
51
+ def message
52
+ <<-doc
53
+ expected the payload to be valid against the '#{schema_name}' schema:
54
+
55
+ #{formatted_payload}
56
+
57
+ Validation errors:
58
+ #{errors}
59
+ doc
60
+ end
61
+
62
+ private
63
+ def errors
64
+ validator = Validator.new(schema_name, type, payload)
65
+ validator.errors.map { |message| "- " + humanized_error(message) }.join("\n")
66
+ end
67
+
68
+ def formatted_payload
69
+ return payload if payload.is_a?(String)
70
+ JSON.pretty_generate(payload)
71
+ end
72
+
73
+ def humanized_error(message)
74
+ message.gsub("The property '#/'", "The item")
75
+ end
76
+ end
25
77
  end
@@ -1,7 +1,12 @@
1
1
  module GovukContentSchemaTestHelpers
2
2
  module TestUnit
3
3
  def assert_valid_against_schema(content_item_hash, format)
4
- validator = GovukContentSchemaTestHelpers::Validator.new(format, content_item_hash.to_json)
4
+ validator = GovukContentSchemaTestHelpers::Validator.new(format, "schema", content_item_hash.to_json)
5
+ assert validator.valid?, "JSON not valid against #{format} schema: #{validator.errors.to_s}"
6
+ end
7
+
8
+ def assert_valid_against_links_schema(content_item_hash, format)
9
+ validator = GovukContentSchemaTestHelpers::Validator.new(format, "links", content_item_hash.to_json)
5
10
  assert validator.valid?, "JSON not valid against #{format} schema: #{validator.errors.to_s}"
6
11
  end
7
12
  end
@@ -2,18 +2,12 @@ require 'json-schema'
2
2
 
3
3
  module GovukContentSchemaTestHelpers
4
4
  class Validator
5
- # Return the first schema path that exists on the filesystem
6
- def self.schema_path(schema_name)
7
- paths = candidate_schema_paths(schema_name)
8
- paths.detect { |path| File.exists?(path) } ||
9
- raise(ImproperlyConfiguredError, "Schema file not found at any of: #{paths.join(', ')}.")
10
- end
11
-
12
- # schema_name should be a string, such as 'finder'
13
- # document should be a JSON string of the document to validate
14
- def initialize(schema_name, document)
5
+ # @param schema - the format (like `topic`). Use `format.links` for the
6
+ def initialize(schema_name, variant, document)
15
7
  Util.check_govuk_content_schemas_path!
16
- @schema_path = GovukContentSchemaTestHelpers::Validator.schema_path(schema_name)
8
+
9
+ @schema_name = schema_name
10
+ @variant = variant
17
11
  @document = document
18
12
  end
19
13
 
@@ -22,22 +16,23 @@ module GovukContentSchemaTestHelpers
22
16
  end
23
17
 
24
18
  def errors
25
- @errors ||= JSON::Validator.fully_validate(@schema_path, @document)
19
+ unless File.exists?(schema_path)
20
+ raise ImproperlyConfiguredError, "Schema file not found at: #{schema_path}"
21
+ end
22
+
23
+ @errors ||= JSON::Validator.fully_validate(schema_path, @document)
26
24
  end
27
25
 
28
26
  private
29
- def self.construct_schema_path(prefix, schema_name)
27
+ def schema_path
30
28
  File.join(
31
29
  Util.govuk_content_schemas_path,
32
- prefix,
33
- schema_name,
30
+ "dist",
31
+ "formats",
32
+ @schema_name,
34
33
  GovukContentSchemaTestHelpers.configuration.schema_type,
35
- "schema.json"
34
+ "#{@variant}.json"
36
35
  )
37
36
  end
38
-
39
- def self.candidate_schema_paths(schema_name)
40
- ["dist/formats", "formats"].map { |prefix| construct_schema_path(prefix, schema_name) }
41
- end
42
37
  end
43
38
  end
@@ -1,3 +1,3 @@
1
1
  module GovukContentSchemaTestHelpers
2
- VERSION = '1.3.0'
2
+ VERSION = '1.4.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk-content-schema-test-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamie Cobbett
@@ -67,6 +67,20 @@ dependencies:
67
67
  - - '='
68
68
  - !ruby/object:Gem::Version
69
69
  version: 3.2.0
70
+ - !ruby/object:Gem::Dependency
71
+ name: pry-byebug
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
70
84
  description: This app provides test helpers for working with [alphagov/govuk-content-schemas](https://github.com/alphagov/govuk-content-schemas)
71
85
  email:
72
86
  - jamie.cobbett@digital.cabinet-office.gov.uk
@@ -106,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
120
  version: '0'
107
121
  requirements: []
108
122
  rubyforge_project:
109
- rubygems_version: 2.4.5
123
+ rubygems_version: 2.4.5.1
110
124
  signing_key:
111
125
  specification_version: 4
112
126
  summary: Test helpers for working with GOV.UK content schemas