From b534e54dba9c627b4f28702be5291e1733bf6469 Mon Sep 17 00:00:00 2001 From: Harriet H-W Date: Wed, 12 Jun 2024 15:24:37 +0100 Subject: [PATCH] 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 --- lib/gds_api/publishing_api.rb | 47 ++++++++++++++++ .../get_schema_by_name_pact_test.rb | 56 +++++++++++++++++++ .../publishing_api/get_schemas_pact_test.rb | 34 +++++++++++ 3 files changed, 137 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..f46cb494 --- /dev/null +++ b/test/pacts/publishing_api/get_schema_by_name_pact_test.rb @@ -0,0 +1,56 @@ +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(:schema) do + { + "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 a given name") + .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..0c9592e3 --- /dev/null +++ b/test/pacts/publishing_api/get_schemas_pact_test.rb @@ -0,0 +1,34 @@ +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(:schemas) do + { + "email_address": { "some": "schema" }, + "tax_licence": { "another": "schema" }, + } + end + + before do + publishing_api + .given("there are 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