Skip to content

Commit

Permalink
Merge pull request #841 from alphagov/move-paths-endpoint
Browse files Browse the repository at this point in the history
Add `put_path` method to PublishingApiV2
  • Loading branch information
tijmenb authored Jul 31, 2018
2 parents 8b177ce + 6ac90dc commit c77013c
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/gds_api/publishing_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def destroy_intent(base_path)
e
end

# @deprecated Use GdsApi::PublishingApiV2
def put_path(base_path, payload)
put_json(paths_url(base_path), payload)
end
Expand Down
13 changes: 13 additions & 0 deletions lib/gds_api/publishing_api_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,19 @@ def get_links_for_content_ids(content_ids)
post_json("#{endpoint}/v2/links/by-content-id", content_ids: content_ids).to_hash
end

# Reserves a path for a publishing application
#
# Returns success or failure only.
#
# @param payload [Hash]
# @option payload [Hash] publishing_app The publishing application, like `content-tagger`
#
# @see https://docs.publishing.service.gov.uk/apis/publishing-api/api.html#put-pathsbase_path
def put_path(base_path, payload)
url = "#{endpoint}/paths#{base_path}"
put_json(url, payload)
end

private

def content_url(content_id, params = {})
Expand Down
97 changes: 97 additions & 0 deletions test/publishing_api_v2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1990,4 +1990,101 @@ def content_item_for_content_id(content_id, attrs = {})
proc { @api_client.patch_links(nil, links: {}) }.must_raise ArgumentError
end
end

describe "#put_path" do
it "returns 200 if the path was successfully reserved" do
base_path = "/test-intent"
payload = {
publishing_app: "publisher"
}

publishing_api
.given("no content exists")
.upon_receiving("a request to put a path")
.with(
method: :put,
path: "/paths#{base_path}",
body: payload,
headers: {
"Content-Type" => "application/json"
},
)
.will_respond_with(
status: 200,
body: {},
headers: {
"Content-Type" => "application/json; charset=utf-8"
}
)

response = @api_client.put_path(base_path, payload)
assert_equal 200, response.code
end

it "returns 422 if the request is invalid" do
base_path = "/test-item"
payload = {
publishing_app: "whitehall"
}

publishing_api
.given("/test-item has been reserved by the Publisher application")
.upon_receiving("a request to change publishing app")
.with(
method: :put,
path: "/paths#{base_path}",
body: payload,
headers: {
"Content-Type" => "application/json"
}
)
.will_respond_with(
status: 422,
body: {
"error" => {
"code" => 422,
"message" => Pact.term(generate: "Unprocessable", matcher: /\S+/),
"fields" => {
"base_path" => Pact.each_like("has been reserved", min: 1),
},
},
},
)

error = assert_raises GdsApi::HTTPUnprocessableEntity do
@api_client.put_path(base_path, payload)
end
assert_equal "Unprocessable", error.error_details["error"]["message"]
end

it "returns 200 if an existing path was overridden" do
base_path = "/test-item"
payload = {
publishing_app: "whitehall",
override_existing: "true",
}

publishing_api
.given("/test-item has been reserved by the Publisher application")
.upon_receiving("a request to change publishing app with override_existing set")
.with(
method: :put,
path: "/paths#{base_path}",
body: payload,
headers: {
"Content-Type" => "application/json"
},
)
.will_respond_with(
status: 200,
body: {},
headers: {
"Content-Type" => "application/json; charset=utf-8"
}
)

response = @api_client.put_path(base_path, payload)
assert_equal 200, response.code
end
end
end

0 comments on commit c77013c

Please sign in to comment.