Skip to content

Commit

Permalink
Merge pull request #1291 from alphagov/content-modelling/678-add-sort…
Browse files Browse the repository at this point in the history
…-and-pagination-arguments

Add new args to `get_content_by_embedded_document`
  • Loading branch information
pezholio authored Nov 12, 2024
2 parents fc240db + 86fe03d commit c0055cf
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
15 changes: 9 additions & 6 deletions lib/gds_api/publishing_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,18 +339,21 @@ def get_content_items(params)

# Get content items which embed a reusable content_id
#
# No params are currently permitted.
#
# @example
# @param content_id [UUID]
# @param params [Hash]
#
# publishing_api.get_content_by_embedded_document("4b148ebc-b2bb-40db-8e48-dd8cff363ff7")
# publishing_api.get_content_by_embedded_document(
# "4b148ebc-b2bb-40db-8e48-dd8cff363ff7",
# { page: 1, order: '-last_edited_at' }
# )
#
# @return [GdsApi::Response] A response containing a summarised list of the content items which embed the target.
# The content items returned will be in either the draft of published state.
#
# @see https://github.com/alphagov/publishing-api/blob/main/docs/api.md#get-v2contentcontent_idembedded
def get_content_by_embedded_document(content_id)
get_json("#{endpoint}/v2/content/#{content_id}/embedded")
def get_content_by_embedded_document(content_id, params = {})
query = query_string(params)
get_json("#{endpoint}/v2/content/#{content_id}/embedded#{query}")
end

# Returns an Enumerator of content items for the provided
Expand Down
6 changes: 4 additions & 2 deletions lib/gds_api/test_helpers/publishing_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,9 @@ def stub_publishing_api_has_content(items, params = {})
# )
# @param content_id [UUID, Mocha::ParameterMatchers::Anything]
# @param total Integer
# @param params [Hash]
def stub_publishing_api_has_embedded_content(content_id:, total: 0, results: [])
# @param total_pages Integer
# @param results [Hash]
def stub_publishing_api_has_embedded_content(content_id:, total: 0, total_pages: 0, results: [])
url = if content_id.is_a?(Mocha::ParameterMatchers::Anything)
%r{\A#{PUBLISHING_API_V2_ENDPOINT}/content/[0-9a-fA-F-]{36}/embedded}
else
Expand All @@ -386,6 +387,7 @@ def stub_publishing_api_has_embedded_content(content_id:, total: 0, results: [])
.to_return(body: {
"content_id" => content_id,
"total" => total,
"total_pages" => total_pages,
"results" => results,
}.to_json)
end
Expand Down
38 changes: 38 additions & 0 deletions test/pacts/publishing_api/get_embedded_content_pact_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,44 @@
assert_equal(expected_body, response.parsed_content)
end

it "supports pagination" do
publishing_api
.given("a content item exists (content_id: #{content_id}) that embeds the reusable content (content_id: #{reusable_content_id})")
.upon_receiving("a get_content_by_embedded_document request with pagination")
.with(
method: :get,
path: "/v2/content/#{reusable_content_id}/embedded",
query: "page=2",
)
.will_respond_with(
status: 200,
body: expected_body,
)

response = api_client.get_content_by_embedded_document(reusable_content_id, { page: 2 })

assert_equal(expected_body, response.parsed_content)
end

it "supports sorting" do
publishing_api
.given("a content item exists (content_id: #{content_id}) that embeds the reusable content (content_id: #{reusable_content_id})")
.upon_receiving("a get_content_by_embedded_document request with sorting")
.with(
method: :get,
path: "/v2/content/#{reusable_content_id}/embedded",
query: "order=-last_edited_at",
)
.will_respond_with(
status: 200,
body: expected_body,
)

response = api_client.get_content_by_embedded_document(reusable_content_id, { order: "-last_edited_at" })

assert_equal(expected_body, response.parsed_content)
end

it "responds with 404 if the content item does not exist" do
missing_content_id = "missing-content-id"
publishing_api
Expand Down

0 comments on commit c0055cf

Please sign in to comment.