Skip to content

Commit

Permalink
Merge pull request #1275 from alphagov/add-schema-publishing-api-endp…
Browse files Browse the repository at this point in the history
…oints

Add `/schemas` publishing api endpoints
  • Loading branch information
Harriethw authored Jun 18, 2024
2 parents 6f3bfbe + 99b77e0 commit a834848
Show file tree
Hide file tree
Showing 5 changed files with 280 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
53 changes: 53 additions & 0 deletions lib/gds_api/test_helpers/publishing_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
62 changes: 62 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,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
46 changes: 46 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,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
72 changes: 72 additions & 0 deletions test/test_helpers/publishing_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) }
Expand Down

0 comments on commit a834848

Please sign in to comment.