From c7be09afd85145b34d9efa4432041c35499dc2b0 Mon Sep 17 00:00:00 2001 From: Ben Thorner Date: Fri, 15 Mar 2019 17:10:26 +0000 Subject: [PATCH] Add test helpers to complement new adapter method --- lib/gds_api/test_helpers/publishing_api.rb | 18 ++++++++++++++ test/publishing_api_test.rb | 12 +++++----- test/test_helpers/publishing_api_test.rb | 28 ++++++++++++++++++++++ 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/lib/gds_api/test_helpers/publishing_api.rb b/lib/gds_api/test_helpers/publishing_api.rb index 0da22ebc..96350408 100644 --- a/lib/gds_api/test_helpers/publishing_api.rb +++ b/lib/gds_api/test_helpers/publishing_api.rb @@ -11,6 +11,18 @@ module PublishingApi PUBLISHING_API_ENDPOINT = Plek.current.find('publishing-api') + def stub_publishing_api_unreserve_path(base_path, publishing_app) + stub_publishing_api_unreserve_path_with_code(base_path, publishing_app, 200) + end + + def stub_publishing_api_unreserve_path_not_found(base_path, publishing_app) + stub_publishing_api_unreserve_path_with_code(base_path, publishing_app, 404) + end + + def stub_publishing_api_unreserve_path_invalid(base_path, publishing_app) + stub_publishing_api_unreserve_path_with_code(base_path, publishing_app, 422) + end + def stub_publishing_api_put_intent(base_path, body = intent_for_publishing_api(base_path)) url = PUBLISHING_API_ENDPOINT + "/publish-intent" + base_path body = body.to_json unless body.is_a?(String) @@ -101,6 +113,12 @@ def stub_publishing_api_returns_path_reservation_validation_error_for(path, erro private + def stub_publishing_api_unreserve_path_with_code(base_path, publishing_app, code) + url = PUBLISHING_API_ENDPOINT + "/paths" + base_path + body = { publishing_app: publishing_app } + stub_request(:delete, url).with(body: body).to_return(status: code, body: '{}', headers: { "Content-Type" => "application/json; charset=utf-8" }) + end + def values_match_recursively(expected_value, actual_value) case expected_value when Hash diff --git a/test/publishing_api_test.rb b/test/publishing_api_test.rb index 04d0e31f..6b40a38f 100644 --- a/test/publishing_api_test.rb +++ b/test/publishing_api_test.rb @@ -62,9 +62,9 @@ }, ) - assert_raises GdsApi::HTTPNotFound do - @api_client.unreserve_path(base_path, publishing_app) - end + assert_raises GdsApi::HTTPNotFound do + @api_client.unreserve_path(base_path, publishing_app) + end end it "raises an error if the reservation is with another app" do @@ -90,9 +90,9 @@ }, ) - assert_raises GdsApi::HTTPUnprocessableEntity do - @api_client.unreserve_path(base_path, publishing_app) - end + assert_raises GdsApi::HTTPUnprocessableEntity do + @api_client.unreserve_path(base_path, publishing_app) + end end end diff --git a/test/test_helpers/publishing_api_test.rb b/test/test_helpers/publishing_api_test.rb index af56c993..83987d93 100644 --- a/test/test_helpers/publishing_api_test.rb +++ b/test/test_helpers/publishing_api_test.rb @@ -7,6 +7,34 @@ let(:base_api_url) { Plek.current.find("publishing-api") } let(:publishing_api) { GdsApi::PublishingApi.new(base_api_url) } + describe "#stub_publishing_api_unreserve_path" do + it "stubs the unreserve path API call" do + stub_publishing_api_unreserve_path("/foo", "myapp") + api_response = publishing_api.unreserve_path("/foo", "myapp") + assert_equal(api_response.code, 200) + end + end + + describe "#stub_publishing_api_unreserve_path_not_found" do + it "stubs the unreserve path API call" do + stub_publishing_api_unreserve_path_not_found("/foo", "myapp") + + assert_raises GdsApi::HTTPNotFound do + publishing_api.unreserve_path("/foo", "myapp") + end + end + end + + describe "#stub_publishing_api_unreserve_path_invalid" do + it "stubs the unreserve path API call" do + stub_publishing_api_unreserve_path_invalid("/foo", "myapp") + + assert_raises GdsApi::HTTPUnprocessableEntity do + publishing_api.unreserve_path("/foo", "myapp") + end + end + end + describe '#request_json_matching predicate' do describe "nested required attribute" do let(:matcher) { request_json_matching("a" => { "b" => 1 }) }