Skip to content

Commit

Permalink
Merge pull request #57 from alphagov/json-schema-integration
Browse files Browse the repository at this point in the history
Add JSON schema validation to integration tests
  • Loading branch information
csutter authored Oct 25, 2023
2 parents 41c86c6 + 55f117e commit ccc4100
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ group :document_sync_worker do
end

group :test do
gem "json_schemer"
gem "simplecov", require: false
end

Expand Down
11 changes: 11 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,18 @@ GEM
capybara (>= 3.36)
puma
selenium-webdriver (>= 4.0)
hana (1.3.7)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
io-console (0.6.0)
irb (1.8.3)
rdoc
reline (>= 0.3.8)
json (2.6.3)
json_schemer (2.0.0)
hana (~> 1.3)
regexp_parser (~> 2.0)
simpleidn (~> 0.2)
jsonpath (1.1.5)
multi_json
language_server-protocol (3.17.0.3)
Expand Down Expand Up @@ -409,6 +414,8 @@ GEM
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
simpleidn (0.2.1)
unf (~> 0.1.4)
sorted_set (1.0.3)
rbtree
set (~> 1.0)
Expand All @@ -417,6 +424,9 @@ GEM
thor (1.2.2)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unf (0.1.4)
unf_ext
unf_ext (0.0.8.2)
unicode-display_width (2.5.0)
webrick (1.8.1)
websocket (1.2.10)
Expand All @@ -439,6 +449,7 @@ DEPENDENCIES
govuk_app_config
govuk_message_queue_consumer
govuk_test
json_schemer
jsonpath
oj
plek
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/document_sync_worker_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

it "is added to the repository" do
result = repository.get("f75d26a3-25a4-4c31-beea-a77cada4ce12")

expect(result[:metadata]).to match_json_schema(metadata_json_schema)
expect(result[:metadata]).to eq(
content_id: "f75d26a3-25a4-4c31-beea-a77cada4ce12",
title: "Ebola medal for over 3000 heroes",
Expand All @@ -30,6 +32,7 @@
],
locale: "en",
)

expect(result[:content]).to start_with("<div class=\"govspeak\"><p>The government has")
expect(result[:content]).to end_with("response to Ebola</a>.</p>\n</div>\n\n</div>")
expect(result[:content].length).to eq(4_932)
Expand All @@ -42,6 +45,8 @@

it "is added to the repository" do
result = repository.get("b662d0a3-c20d-4167-8056-b9c7d058d860")

expect(result[:metadata]).to match_json_schema(metadata_json_schema)
expect(result[:metadata]).to eq(
content_id: "b662d0a3-c20d-4167-8056-b9c7d058d860",
title: "Austria travel advice",
Expand All @@ -58,6 +63,7 @@
],
locale: "en",
)

expect(result[:content]).to be_empty
end
end
Expand All @@ -68,6 +74,8 @@

it "is added to the repository" do
result = repository.get("5c880596-7631-11e4-a3cb-005056011aef")

expect(result[:metadata]).to match_json_schema(metadata_json_schema)
expect(result[:metadata]).to eq(
content_id: "5c880596-7631-11e4-a3cb-005056011aef",
title: "Travel advice for fans going to Champions League and Europa League matches this week",
Expand All @@ -86,6 +94,7 @@
],
locale: "en",
)

expect(result[:content]).to start_with("<div class=\"govspeak\"><p>In the UEFA Champions")
expect(result[:content]).to end_with("football fans</a>.</p>\n</div>")
expect(result[:content].length).to eq(2_118)
Expand All @@ -98,6 +107,8 @@

it "is added to the repository" do
result = repository.get("526d5caf-221b-4c7b-9e74-b3e0b189fc8d")

expect(result[:metadata]).to match_json_schema(metadata_json_schema)
expect(result[:metadata]).to eq(
content_id: "526d5caf-221b-4c7b-9e74-b3e0b189fc8d",
title: "Brighton & Hove City Council",
Expand All @@ -112,6 +123,7 @@
part_of_taxonomy_tree: [],
locale: "en",
)

expect(result[:content]).to be_blank
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@
end

config.include FixtureHelpers
config.include SchemaHelpers
end
14 changes: 14 additions & 0 deletions spec/support/matchers/match_json_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
RSpec::Matchers.define :match_json_schema do |expected_schema|
match do |json_string|
@errors = expected_schema.validate(json_string).map { _1["error"] }
@errors.none?
end

failure_message do
"Expected JSON to match schema, but validation failed:\n#{@errors.join("\n")}"
end

failure_message_when_negated do
"Expected JSON not to match schema, but it did."
end
end
18 changes: 18 additions & 0 deletions spec/support/schema_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module SchemaHelpers
# TODO: This is the best option out of a bad bunch right now as this schema lives in a different
# repository. Maybe eventually we will vendor the schema, or get it directly from the GCP API
# client?
METADATA_JSON_SCHEMA_URI = URI.parse(
"https://raw.githubusercontent.com/alphagov/search-v2-infrastructure/main/terraform/modules/google_discovery_engine_restapi/files/datastore_schema.json",
)

# Returns a JSONSchemer object representing the JSON schema for document metadata
def metadata_json_schema
@metadata_json_schema ||= begin
remote_schema = Net::HTTP.get(METADATA_JSON_SCHEMA_URI)
schema_contents = JSON.parse(remote_schema)

JSONSchemer.schema(schema_contents)
end
end
end

0 comments on commit ccc4100

Please sign in to comment.