diff --git a/lib/gds_api/json_client.rb b/lib/gds_api/json_client.rb index 6e7e98e6..7f9368cf 100644 --- a/lib/gds_api/json_client.rb +++ b/lib/gds_api/json_client.rb @@ -64,8 +64,8 @@ def patch_json(url, params, additional_headers = {}) do_json_request(:patch, url, params, additional_headers) end - def delete_json(url, additional_headers = {}) - do_json_request(:delete, url, nil, additional_headers) + def delete_json(url, params = {}, additional_headers = {}) + do_json_request(:delete, url, params, additional_headers) end def delete_json_with_params!(url, params, additional_headers = {}) diff --git a/lib/gds_api/publishing_api.rb b/lib/gds_api/publishing_api.rb index 6bd41915..32faff22 100644 --- a/lib/gds_api/publishing_api.rb +++ b/lib/gds_api/publishing_api.rb @@ -12,8 +12,17 @@ def destroy_intent(base_path) e end + def unreserve_path(base_path, publishing_app) + payload = { publishing_app: publishing_app } + delete_json(unreserve_url(base_path), payload) + end + private + def unreserve_url(base_path) + "#{endpoint}/paths#{base_path}" + end + def intent_url(base_path) "#{endpoint}/publish-intent#{base_path}" end diff --git a/lib/gds_api/rummager.rb b/lib/gds_api/rummager.rb index 479eab86..81c61b1a 100644 --- a/lib/gds_api/rummager.rb +++ b/lib/gds_api/rummager.rb @@ -19,6 +19,7 @@ def add_document(type, id, document) def delete_document(type, id) delete_json( "#{documents_url}/#{id}", + nil, _type: type, ) end diff --git a/test/json_client_test.rb b/test/json_client_test.rb index 62313f14..62ee81d0 100644 --- a/test/json_client_test.rb +++ b/test/json_client_test.rb @@ -425,7 +425,8 @@ def test_client_can_set_custom_headers_on_deletes stub_request(:delete, "http://some.other.endpoint/some.json").to_return(status: 200) GdsApi::JsonClient.new.delete_json("http://some.other.endpoint/some.json", - "HEADER-A" => "B", "HEADER-C" => "D") + {}, + "HEADER-A" => "B", "HEADER-C" => "D") assert_requested(:delete, %r{/some.json}) do |request| headers_with_uppercase_names = Hash[request.headers.collect { |key, value| [key.upcase, value] }] diff --git a/test/publishing_api_test.rb b/test/publishing_api_test.rb index b5a31eac..04d0e31f 100644 --- a/test/publishing_api_test.rb +++ b/test/publishing_api_test.rb @@ -11,6 +11,91 @@ @api_client = GdsApi::PublishingApi.new(publishing_api_host) end + describe "#unreserve_path" do + it "responds with 200 OK if reservation is owned by the app" do + publishing_app = "publisher" + base_path = "/test-item" + + publishing_api + .given("/test-item has been reserved by the Publisher application") + .upon_receiving("a request to unreserve a path") + .with( + method: :delete, + path: "/paths#{base_path}", + body: { publishing_app: publishing_app }, + headers: { + "Content-Type" => "application/json" + }, + ) + .will_respond_with( + status: 200, + body: {}, + headers: { + "Content-Type" => "application/json; charset=utf-8" + }, + ) + + response = @api_client.unreserve_path(base_path, publishing_app) + assert_equal 200, response.code + end + + it "raises an error if the reservation does not exist" do + publishing_app = "publisher" + base_path = "/test-item" + + publishing_api + .given("no content exists") + .upon_receiving("a request to unreserve a non-existant path") + .with( + method: :delete, + path: "/paths#{base_path}", + body: { publishing_app: publishing_app }, + headers: { + "Content-Type" => "application/json" + }, + ) + .will_respond_with( + status: 404, + body: {}, + headers: { + "Content-Type" => "application/json; charset=utf-8" + }, + ) + + 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 + publishing_app = "whitehall" + base_path = "/test-item" + + publishing_api + .given("/test-item has been reserved by the Publisher application") + .upon_receiving("a request to unreserve a path owned by another app") + .with( + method: :delete, + path: "/paths#{base_path}", + body: { publishing_app: "whitehall" }, + headers: { + "Content-Type" => "application/json" + }, + ) + .will_respond_with( + status: 422, + body: {}, + headers: { + "Content-Type" => "application/json; charset=utf-8" + }, + ) + + assert_raises GdsApi::HTTPUnprocessableEntity do + @api_client.unreserve_path(base_path, publishing_app) + end + end + end + describe "#put_intent" do it "responds with 200 OK if publish intent is valid" do base_path = "/test-intent"