Skip to content

Commit

Permalink
add Publishing API test helpers for /schemas
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 helper methods to stub responses to these
end points.

[1] alphagov/publishing-api#2767
  • Loading branch information
Harriethw committed Jun 14, 2024
1 parent 4268670 commit 768af01
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
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
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 768af01

Please sign in to comment.