From aca872a799c86737e79acb2ddba2632bc68ee833 Mon Sep 17 00:00:00 2001 From: Harriet H-W Date: Wed, 12 Jun 2024 15:24:37 +0100 Subject: [PATCH 1/2] add support for /schemas end points in Publishing API `/schemas` and `/schemas/{schema_name}` were added to the Publishing API in this PR [1] This adds methods to the publishing_api adapter [1] https://github.com/alphagov/publishing-api/pull/2767 Co-authored-by: Tom Hipkin Co-authored-by: Harriet H-W --- lib/gds_api/publishing_api.rb | 47 ++++++++++++++ .../get_schema_by_name_pact_test.rb | 62 +++++++++++++++++++ .../publishing_api/get_schemas_pact_test.rb | 46 ++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 test/pacts/publishing_api/get_schema_by_name_pact_test.rb create mode 100644 test/pacts/publishing_api/get_schemas_pact_test.rb diff --git a/lib/gds_api/publishing_api.rb b/lib/gds_api/publishing_api.rb index 5eabb574..a62d133b 100644 --- a/lib/gds_api/publishing_api.rb +++ b/lib/gds_api/publishing_api.rb @@ -487,6 +487,53 @@ def destroy_intent(base_path) e end + # Get all schemas + # + # @return [GdsApi::Response] A map of schema names with JSON schemas. + # + # @example + # + # publishing_api.get_schemas() + # # => { + # "email_address" => { + # "type": "email_address", + # "required": ["email"], + # "properties": { + # "email": { "type" => "string" }, + # }, + # } + # } + # + # @see https://github.com/alphagov/publishing-api/blob/main/docs/api.md#get-v2schemas + def get_schemas + get_json("#{endpoint}/v2/schemas").to_hash + end + + # Get a content schema by name + # + # @param schema_name [String] + # + # @return [GdsApi::Response] A response mapping schema name with JSON schema. + # + # @example + # + # publishing_api.get_schema("email_address") + # # => { + # "email_address" => { + # "type": "email_address", + # "required": ["email"], + # "properties": { + # "email": { "type" => "string" }, + # }, + # } + # } + # + # @raise [HTTPNotFound] when the schema is not found + # @see https://github.com/alphagov/publishing-api/blob/main/docs/api.md#get-v2schemasschema_name + def get_schema(schema_name) + get_json("#{endpoint}/v2/schemas/#{schema_name}").to_hash + end + private def content_url(content_id, params = {}) diff --git a/test/pacts/publishing_api/get_schema_by_name_pact_test.rb b/test/pacts/publishing_api/get_schema_by_name_pact_test.rb new file mode 100644 index 00000000..3e6eafba --- /dev/null +++ b/test/pacts/publishing_api/get_schema_by_name_pact_test.rb @@ -0,0 +1,62 @@ +require "test_helper" +require "gds_api/publishing_api" + +describe "GdsApi::PublishingApi##get_schemas_by_name pact tests" do + include PactTest + + let(:api_client) { GdsApi::PublishingApi.new(publishing_api_host) } + + let(:schema) do + { + "/govuk/publishing-api/content_schemas/dist/formats/email_address/publisher_v2/schema.json": { + type: "object", + required: %w[a], + properties: { + email_address: { "some" => "schema" }, + }, + }, + } + end + + describe "when a schema is found" do + before do + publishing_api + .given("there is a schema for an email_address") + .upon_receiving("a get schema by name request") + .with( + method: :get, + path: "/v2/schemas/email_address", + ) + .will_respond_with( + status: 200, + body: schema, + ) + end + + it "returns the named schema" do + response = api_client.get_schema("email_address") + assert_equal(schema.to_json, response.to_json) + end + end + + describe "when a schema is not found" do + before do + publishing_api + .given("there is not a schema for an email_address") + .upon_receiving("a get schema by name request") + .with( + method: :get, + path: "/v2/schemas/email_address", + ) + .will_respond_with( + status: 404, + ) + end + + it "returns a 404 error" do + assert_raises(GdsApi::HTTPNotFound) do + api_client.get_schema("email_address") + end + end + end +end diff --git a/test/pacts/publishing_api/get_schemas_pact_test.rb b/test/pacts/publishing_api/get_schemas_pact_test.rb new file mode 100644 index 00000000..ba823018 --- /dev/null +++ b/test/pacts/publishing_api/get_schemas_pact_test.rb @@ -0,0 +1,46 @@ +require "test_helper" +require "gds_api/publishing_api" + +describe "GdsApi::PublishingApi#get_schemas pact tests" do + include PactTest + + let(:api_client) { GdsApi::PublishingApi.new(publishing_api_host) } + + let(:schemas) do + { + "email_address": { + type: "object", + required: %w[a], + properties: { + email_address: { "some" => "schema" }, + }, + }, + "tax_license": { + type: "object", + required: %w[a], + properties: { + tax_license: { "another" => "schema" }, + }, + }, + } + end + + before do + publishing_api + .given("there are publisher schemas") + .upon_receiving("a get schemas request") + .with( + method: :get, + path: "/v2/schemas", + ) + .will_respond_with( + status: 200, + body: schemas, + ) + end + + it "returns all the schemas" do + response = api_client.get_schemas + assert_equal(schemas.to_json, response.to_json) + end +end From 99b77e09f746731877f816acff1246cbffd7695c Mon Sep 17 00:00:00 2001 From: Harriet H-W Date: Wed, 12 Jun 2024 15:25:41 +0100 Subject: [PATCH 2/2] add Publishing API test helpers for `/schemas` `/schemas` and `/schemas/{schema_name}` were added to the Publishing API in this PR [1]. This adds helper methods to stub responses to these end points. [1] https://github.com/alphagov/publishing-api/pull/2767 --- lib/gds_api/test_helpers/publishing_api.rb | 53 ++++++++++++++++ test/test_helpers/publishing_api_test.rb | 72 ++++++++++++++++++++++ 2 files changed, 125 insertions(+) diff --git a/lib/gds_api/test_helpers/publishing_api.rb b/lib/gds_api/test_helpers/publishing_api.rb index 62cf93a4..8d469747 100644 --- a/lib/gds_api/test_helpers/publishing_api.rb +++ b/lib/gds_api/test_helpers/publishing_api.rb @@ -802,6 +802,59 @@ def stub_publishing_api_returns_path_reservation_validation_error_for(base_path, body: { error: }.to_json) end + # Stub a request to get a schema by schema name + # + # @param [String] schema name + # + # @example + # stub_publishing_api_has_schemas_for_schema_name( + # "email_address", + # { "email_address" => { + # "type": "email_address", + # "required": ["email"], + # "properties": { + # "email": { "type" => "string" }, + # }, + # }, + # } + # ) + def stub_publishing_api_has_schemas_for_schema_name(schema_name, schema) + url = "#{PUBLISHING_API_V2_ENDPOINT}/schemas/#{schema_name}" + stub_request(:get, url).to_return(status: 200, body: schema.to_json, headers: {}) + end + + def stub_publishing_api_schema_name_path_to_return_not_found(schema_name) + url = "#{PUBLISHING_API_V2_ENDPOINT}/schemas/#{schema_name}" + stub_request(:get, url).to_return(status: 404, headers: { "Content-Type" => "application/json; charset=utf-8" }) + end + + # Stub a request to get all schemas + # + # + # @example + # stub_publishing_api_has_schemas( + # { + # "email_address" => { + # "type": "email_address", + # "required": ["email"], + # "properties": { + # "email": { "type" => "string" }, + # }, + # }, + # "tax_bracket" => { + # "type": "tax_bracket", + # "required": ["code"], + # "properties": { + # "code": { "type" => "string" }, + # }, + # } + # } + # ) + def stub_publishing_api_has_schemas(schemas) + url = "#{PUBLISHING_API_V2_ENDPOINT}/schemas" + stub_request(:get, url).to_return(status: 200, body: schemas.to_json, headers: {}) + end + private def stub_publishing_api_put(*args) diff --git a/test/test_helpers/publishing_api_test.rb b/test/test_helpers/publishing_api_test.rb index 808c3d26..834a8bf6 100644 --- a/test/test_helpers/publishing_api_test.rb +++ b/test/test_helpers/publishing_api_test.rb @@ -539,6 +539,78 @@ end end + describe "#stub_publishing_api_has_schemas" do + it "returns the given schemas" do + schemas = { + "email_address" => { + "type": "email_address", + "required": %w[email], + "properties": { + "email": { "type" => "string" }, + }, + }, + "tax_bracket" => { + "type": "tax_bracket", + "required": %w[code], + "properties": { + "code": { "type" => "string" }, + }, + }, + } + + stub_publishing_api_has_schemas( + schemas, + ) + + api_response = publishing_api.get_schemas + + assert_equal( + schemas.to_json, + api_response.to_json, + ) + end + end + + describe "#stub_publishing_api_has_schemas_for_schema_name" do + it "returns the given schema" do + schema_name = "email_address" + + schema = { + "email_address" => { + "type": "email_address", + "required": %w[email], + "properties": { + "email": { "type" => "string" }, + }, + }, + } + + stub_publishing_api_has_schemas_for_schema_name( + schema_name, + schema, + ) + + api_response = publishing_api.get_schema(schema_name) + + assert_equal( + schema.to_json, + api_response.to_json, + ) + end + end + + describe "#stub_publishing_api_schema_name_path_to_return_not_found" do + it "returns a GdsApi::HTTPNotFound for a call to get a schema by name" do + schema_name = "missing_schema" + + stub_publishing_api_schema_name_path_to_return_not_found(schema_name) + + assert_raises GdsApi::HTTPNotFound do + publishing_api.get_schema(schema_name) + end + end + end + describe "#request_json_matching predicate" do describe "nested required attribute" do let(:matcher) { request_json_matching("a" => { "b" => 1 }) }