Skip to content

Commit

Permalink
Add unreserve_path to Pub API adapter
Browse files Browse the repository at this point in the history
This adds a new method to the Pub API adapter to complement the new
endpoint to unreserve a path in the Publishing API.
  • Loading branch information
Ben Thorner committed Mar 15, 2019
1 parent 674c2fa commit 57c317f
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/gds_api/json_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {})
Expand Down
9 changes: 9 additions & 0 deletions lib/gds_api/publishing_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/gds_api/rummager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def add_document(type, id, document)
def delete_document(type, id)
delete_json(
"#{documents_url}/#{id}",
nil,
_type: type,
)
end
Expand Down
3 changes: 2 additions & 1 deletion test/json_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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] }]
Expand Down
85 changes: 85 additions & 0 deletions test/publishing_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 57c317f

Please sign in to comment.