Skip to content

Commit

Permalink
Add helper to patch links when there is a conflict
Browse files Browse the repository at this point in the history
This improves the documentation and testing for the existing patch_links
test helper, while adding another to simulate the response when a
version conflict occurs.
  • Loading branch information
Ben Thorner committed Nov 5, 2018
1 parent e79e9d4 commit f0f8555
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
37 changes: 37 additions & 0 deletions lib/gds_api/test_helpers/publishing_api_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,39 @@ def stub_publishing_api_put_content(content_id, body, response_hash = {})

# Stub a PATCH /v2/links/:content_id request
#
# @example
# stub_publishing_api_patch_links(
# my_content_id,
# "links" => {
# "taxons" => %w(level_one_topic level_two_topic),
# },
# "previous_version" => "3",
# )
#
# @param content_id [UUID]
# @param body [String]
def stub_publishing_api_patch_links(content_id, body)
stub_publishing_api_patch(content_id, body, '/links')
end

# Stub a PATCH /v2/links/:content_id request to return a 409 response
#
# @example
# stub_publishing_api_patch_links_conflict(
# my_content_id,
# "links" => {
# "taxons" => %w(level_one_topic level_two_topic),
# },
# "previous_version" => "3",
# )
#
# @param content_id [UUID]
# @param body [String]
def stub_publishing_api_patch_links_conflict(content_id, body)
override_response_hash = { status: 409, body: version_conflict(body[:previous_version]) }
stub_publishing_api_patch(content_id, body, '/links', override_response_hash)
end

# Stub a POST /v2/content/:content_id/publish request
#
# @param content_id [UUID]
Expand Down Expand Up @@ -620,6 +647,16 @@ def resource_not_found(content_id, type)
}
}
end

def version_conflict(expected_version, actual_version = expected_version + 1)
{
error: {
code: 409,
message: "A lock-version conflict occurred. The `previous_version` you've sent (#{expected_version}) is not the same as the current lock version of the edition (#{actual_version}).",
fields: { previous_version: ["does not match"] },
}
}
end
end
end
end
52 changes: 49 additions & 3 deletions test/test_helpers/publishing_api_v2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,23 +234,69 @@
end
end

describe "stub_any_publishing_api_publish" do
describe '#stub_publishing_api_patch_links' do
it "stubs a request to patch links" do
content_id = SecureRandom.uuid
body = {
links: {
my_linkset: %w(link_1),
},
previous_version: 4
}

assert_raises WebMock::NetConnectNotAllowedError do
publishing_api.patch_links(content_id, body)
end

stub_publishing_api_patch_links(content_id, body)
response = publishing_api.patch_links(content_id, body)
assert_equal(response.code, 200)
end
end

describe '#stub_publishing_api_patch_links_conflict' do
it "stubs a request to patch links with a 409 conflict response" do
content_id = SecureRandom.uuid
body = {
links: {
my_linkset: %w(link_1),
},
previous_version: 4
}

stub_publishing_api_patch_links_conflict(content_id, body)

error = assert_raises GdsApi::HTTPConflict do
publishing_api.patch_links(content_id, body)
end

assert error.message.include?({
error: {
code: 409,
message: "A lock-version conflict occurred. The `previous_version` you've sent (4) is not the same as the current lock version of the edition (5).",
fields: { previous_version: ["does not match"] },
}
}.to_json)
end
end

describe '#stub_any_publishing_api_publish' do
it "stubs any publish request to the publishing api" do
stub_any_publishing_api_publish
publishing_api.publish("some-content-id", "major")
assert_publishing_api_publish("some-content-id")
end
end

describe "stub_any_publishing_api_unpublish" do
describe '#stub_any_publishing_api_unpublish' do
it "stubs any unpublish request to the publishing api" do
stub_any_publishing_api_unpublish
publishing_api.unpublish("some-content-id", type: :gone)
assert_publishing_api_unpublish("some-content-id")
end
end

describe "stub_any_publishing_api_discard_draft" do
describe '#stub_any_publishing_api_discard_draft' do
it "stubs any discard draft request to the publishing api" do
stub_any_publishing_api_discard_draft
publishing_api.discard_draft("some-content-id")
Expand Down

0 comments on commit f0f8555

Please sign in to comment.