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 }) }