Skip to content

Commit

Permalink
add support for /schemas end points in Publishing API
Browse files Browse the repository at this point in the history
`/schemas` and `/schemas/{schema_name}` were added
to the Publishing API in this PR [1]

This adds methods to the publishing_api adapter

[1] alphagov/publishing-api#2767
  • Loading branch information
Harriethw committed Jun 12, 2024
1 parent 6f3bfbe commit c2fef36
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
47 changes: 47 additions & 0 deletions lib/gds_api/publishing_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {})
Expand Down
56 changes: 56 additions & 0 deletions test/pacts/publishing_api/get_schema_by_name_pact_test.rb
Original file line number Diff line number Diff line change
@@ -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 with given name")
.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_schemas
assert_equal 200, response.code
assert_equal :schema, response.body
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
response = api_client.get_schema("email_address")
assert_equal 404, response.code
end
end
end
35 changes: 35 additions & 0 deletions test/pacts/publishing_api/get_schemas_pact_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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 two 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 200, response.code
assert_equal :schemas, response.body
end
end

0 comments on commit c2fef36

Please sign in to comment.