Module: GdsApi::TestHelpers::PublishingApiV2
- Includes:
- ContentItemHelpers
- Defined in:
- lib/gds_api/test_helpers/publishing_api_v2.rb
Constant Summary collapse
- PUBLISHING_API_V2_ENDPOINT =
Plek.current.find('publishing-api') + '/v2'
Instance Method Summary collapse
-
#assert_publishing_api(verb, url, attributes_or_matcher = nil, times = 1) ⇒ Object
Assert that a request was made to the publishing API.
-
#assert_publishing_api_discard_draft(content_id, attributes_or_matcher = nil, times = 1) ⇒ Object
Assert that a draft was discarded (POST /v2/content/:content_id/discard-draft).
-
#assert_publishing_api_patch_links(content_id, attributes_or_matcher = nil, times = 1) ⇒ Object
Assert that links were updated (PATCH /v2/links/:content_id).
-
#assert_publishing_api_publish(content_id, attributes_or_matcher = nil, times = 1) ⇒ Object
Assert that content was published (POST /v2/content/:content_id/publish).
-
#assert_publishing_api_put_content(content_id, attributes_or_matcher = nil, times = 1) ⇒ Object
Assert that content was saved (PUT /v2/content/:content_id).
-
#assert_publishing_api_put_content_links_and_publish(body, content_id = nil, publish_body = nil) ⇒ Object
Assert that a draft was saved and published, and links were updated.
-
#assert_publishing_api_unpublish(content_id, attributes_or_matcher = nil, times = 1) ⇒ Object
Assert that content was unpublished (POST /v2/content/:content_id/unpublish).
-
#publishing_api_does_not_have_item(content_id) ⇒ Object
Stub GET /v2/content/:content_id to return a 404 response.
-
#publishing_api_does_not_have_links(content_id) ⇒ Object
Stub GET /v2/links/:content_id to return a 404 response.
-
#publishing_api_get_editions(editions, params = {}) ⇒ Object
Stub GET /v2/editions to return a set of editions.
-
#publishing_api_has_content(items, params = {}) ⇒ Object
Stub GET /v2/content/ to return a set of content items.
-
#publishing_api_has_expanded_links(links, with_drafts: true) ⇒ Object
Stub a request to the expanded links endpoint.
-
#publishing_api_has_fields_for_document(document_type, items, fields) ⇒ Object
This method has been refactored into publishing_api_has_content (above) publishing_api_has_content allows for flexible passing in of arguments, please use instead.
-
#publishing_api_has_item(item, params = {}) ⇒ Object
Stub GET /v2/content/:content_id to return a specific content item hash.
-
#publishing_api_has_item_in_sequence(content_id, items) ⇒ Object
Stub GET /v2/content/:content_id to progress through a series of responses.
-
#publishing_api_has_linkables(linkables, document_type:) ⇒ Object
Stub GET /v2/linkables to return a set of content items with a specific document type.
-
#publishing_api_has_linked_items(items, params = {}) ⇒ Object
Stub calls to the get linked items endpoint.
-
#publishing_api_has_links(links) ⇒ Object
Stub a request to links endpoint.
-
#publishing_api_has_lookups(lookup_hash) ⇒ Object
Stub calls to the lookups endpoint.
-
#publishing_api_isnt_available ⇒ Object
Stub any version 2 request to the publishing API to return a 503 response.
-
#request_json_includes(required_attributes) ⇒ Object
Get a request matcher that checks if a JSON request includes a set of attributes.
-
#request_json_matches(required_attributes) ⇒ Object
Get a request matcher that checks if a JSON request matches a hash.
-
#stub_any_publishing_api_call ⇒ Object
Stub any version 2 request to the publishing API.
-
#stub_any_publishing_api_call_to_return_not_found ⇒ Object
Stub any version 2 request to the publishing API to return a 404 response.
-
#stub_any_publishing_api_discard_draft ⇒ Object
Stub any POST /v2/content/*/discard-draft request.
-
#stub_any_publishing_api_patch_links ⇒ Object
Stub any PATCH /v2/links/* request.
-
#stub_any_publishing_api_publish ⇒ Object
Stub any POST /v2/content/*/publish request.
-
#stub_any_publishing_api_put_content ⇒ Object
Stub any PUT /v2/content/* request.
-
#stub_any_publishing_api_unpublish ⇒ Object
Stub any POST /v2/content/*/unpublish request.
-
#stub_publishing_api_discard_draft(content_id) ⇒ Object
Stub a POST /v2/content/:content_id/discard-draft request.
-
#stub_publishing_api_patch_links(content_id, body) ⇒ Object
Stub a PATCH /v2/links/:content_id request.
-
#stub_publishing_api_publish(content_id, body, response_hash = {}) ⇒ Object
Stub a POST /v2/content/:content_id/publish request.
-
#stub_publishing_api_put_content(content_id, body, response_hash = {}) ⇒ Object
Stub a PUT /v2/content/:content_id request with the given content id and request body.
-
#stub_publishing_api_put_content_links_and_publish(body, content_id = nil, publish_body = nil) ⇒ Object
Stub requests issued when publishing a new draft.
-
#stub_publishing_api_unpublish(content_id, params, response_hash = {}) ⇒ Object
Stub a POST /v2/content/:content_id/unpublish request.
Methods included from ContentItemHelpers
#content_item_for_base_path, #gone_content_item_for_base_path, #titleize_base_path
Instance Method Details
#assert_publishing_api(verb, url, attributes_or_matcher = nil, times = 1) ⇒ Object
Assert that a request was made to the publishing API
218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 218 def assert_publishing_api(verb, url, attributes_or_matcher = nil, times = 1) if attributes_or_matcher.is_a?(Hash) matcher = request_json_matches(attributes_or_matcher) else matcher = attributes_or_matcher end if matcher assert_requested(verb, url, times: times, &matcher) else assert_requested(verb, url, times: times) end end |
#assert_publishing_api_discard_draft(content_id, attributes_or_matcher = nil, times = 1) ⇒ Object
Assert that a draft was discarded (POST /v2/content/:content_id/discard-draft)
207 208 209 210 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 207 def assert_publishing_api_discard_draft(content_id, attributes_or_matcher = nil, times = 1) url = PUBLISHING_API_V2_ENDPOINT + "/content/#{content_id}/discard-draft" assert_publishing_api(:post, url, attributes_or_matcher, times) end |
#assert_publishing_api_patch_links(content_id, attributes_or_matcher = nil, times = 1) ⇒ Object
Assert that links were updated (PATCH /v2/links/:content_id)
197 198 199 200 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 197 def assert_publishing_api_patch_links(content_id, attributes_or_matcher = nil, times = 1) url = PUBLISHING_API_V2_ENDPOINT + "/links/" + content_id assert_publishing_api(:patch, url, attributes_or_matcher, times) end |
#assert_publishing_api_publish(content_id, attributes_or_matcher = nil, times = 1) ⇒ Object
Assert that content was published (POST /v2/content/:content_id/publish)
177 178 179 180 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 177 def assert_publishing_api_publish(content_id, attributes_or_matcher = nil, times = 1) url = PUBLISHING_API_V2_ENDPOINT + "/content/#{content_id}/publish" assert_publishing_api(:post, url, attributes_or_matcher, times) end |
#assert_publishing_api_put_content(content_id, attributes_or_matcher = nil, times = 1) ⇒ Object
Assert that content was saved (PUT /v2/content/:content_id)
167 168 169 170 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 167 def assert_publishing_api_put_content(content_id, attributes_or_matcher = nil, times = 1) url = PUBLISHING_API_V2_ENDPOINT + "/content/" + content_id assert_publishing_api(:put, url, attributes_or_matcher, times) end |
#assert_publishing_api_put_content_links_and_publish(body, content_id = nil, publish_body = nil) ⇒ Object
Assert that a draft was saved and published, and links were updated.
-
PUT /v2/content/:content_id
-
POST /v2/content/:content_id/publish
-
PATCH /v2/links/:content_id
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 151 def assert_publishing_api_put_content_links_and_publish(body, content_id = nil, publish_body = nil) content_id ||= body[:content_id] if publish_body.nil? publish_body = { update_type: body.fetch(:update_type) } publish_body[:locale] = body[:locale] if body[:locale] end assert_publishing_api_put_content(content_id, body.except(:links)) assert_publishing_api_patch_links(content_id, body.slice(:links)) unless body.slice(:links).empty? assert_publishing_api_publish(content_id, publish_body) end |
#assert_publishing_api_unpublish(content_id, attributes_or_matcher = nil, times = 1) ⇒ Object
Assert that content was unpublished (POST /v2/content/:content_id/unpublish)
187 188 189 190 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 187 def assert_publishing_api_unpublish(content_id, attributes_or_matcher = nil, times = 1) url = PUBLISHING_API_V2_ENDPOINT + "/content/#{content_id}/unpublish" assert_publishing_api(:post, url, attributes_or_matcher, times) end |
#publishing_api_does_not_have_item(content_id) ⇒ Object
Stub GET /v2/content/:content_id to return a 404 response
349 350 351 352 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 349 def publishing_api_does_not_have_item(content_id) url = PUBLISHING_API_V2_ENDPOINT + "/content/" + content_id stub_request(:get, url).to_return(status: 404, body: resource_not_found(content_id, "content item").to_json, headers: {}) end |
#publishing_api_does_not_have_links(content_id) ⇒ Object
Stub GET /v2/links/:content_id to return a 404 response
454 455 456 457 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 454 def publishing_api_does_not_have_links(content_id) url = PUBLISHING_API_V2_ENDPOINT + "/links/" + content_id stub_request(:get, url).to_return(status: 404, body: resource_not_found(content_id, "link set").to_json, headers: {}) end |
#publishing_api_get_editions(editions, params = {}) ⇒ Object
Stub GET /v2/editions to return a set of editions
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 523 def publishing_api_get_editions(editions, params = {}) url = PUBLISHING_API_V2_ENDPOINT + "/editions" results = editions.map do |edition| next edition unless params[:fields] edition.select { |k| params[:fields].include?(k) } end per_page = (params[:per_page] || 100).to_i results = results.take(per_page) body = { results: results, links: [ { rel: "self", href: "#{PUBLISHING_API_V2_ENDPOINT}/editions" }, ], } stub_request(:get, url) .with(query: params) .to_return(status: 200, body: body.to_json, headers: {}) end |
#publishing_api_has_content(items, params = {}) ⇒ Object
Stub GET /v2/content/ to return a set of content items
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 262 def publishing_api_has_content(items, params = {}) url = PUBLISHING_API_V2_ENDPOINT + "/content" if params.respond_to? :fetch per_page = params.fetch(:per_page, 50) page = params.fetch(:page, 1) else per_page = 50 page = 1 end start_position = (page - 1) * per_page page_items = items.slice(start_position, per_page) || [] number_of_pages = if items.count < per_page 1 else (items.count / per_page.to_f).ceil end body = { results: page_items, total: items.count, pages: number_of_pages, current_page: page } stub_request(:get, url) .with(query: params) .to_return(status: 200, body: body.to_json, headers: {}) end |
#publishing_api_has_expanded_links(links, with_drafts: true) ⇒ Object
Stub a request to the expanded links endpoint
444 445 446 447 448 449 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 444 def (links, with_drafts: true) links = deep_transform_keys(links, &:to_sym) query = with_drafts ? "" : "?with_drafts=false" url = PUBLISHING_API_V2_ENDPOINT + "/expanded-links/" + links[:content_id] + query stub_request(:get, url).to_return(status: 200, body: links.to_json, headers: {}) end |
#publishing_api_has_fields_for_document(document_type, items, fields) ⇒ Object
This method has been refactored into publishing_api_has_content (above) publishing_api_has_content allows for flexible passing in of arguments, please use instead
297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 297 def publishing_api_has_fields_for_document(document_type, items, fields) body = Array(items).map { |item| deep_stringify_keys(item).slice(*fields) } query_params = fields.map { |f| "&fields%5B%5D=#{f}" } url = PUBLISHING_API_V2_ENDPOINT + "/content?document_type=#{document_type}#{query_params.join('')}" stub_request(:get, url).to_return(status: 200, body: { results: body }.to_json, headers: {}) end |
#publishing_api_has_item(item, params = {}) ⇒ Object
Stub GET /v2/content/:content_id to return a specific content item hash
322 323 324 325 326 327 328 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 322 def publishing_api_has_item(item, params = {}) item = deep_transform_keys(item, &:to_sym) url = PUBLISHING_API_V2_ENDPOINT + "/content/" + item[:content_id] stub_request(:get, url) .with(query: hash_including(params)) .to_return(status: 200, body: item.to_json, headers: {}) end |
#publishing_api_has_item_in_sequence(content_id, items) ⇒ Object
Stub GET /v2/content/:content_id to progress through a series of responses.
333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 333 def publishing_api_has_item_in_sequence(content_id, items) items = items.each { |item| deep_transform_keys(item, &:to_sym) } url = PUBLISHING_API_V2_ENDPOINT + "/content/" + content_id calls = -1 stub_request(:get, url).to_return do |_request| calls += 1 item = items[calls] || items.last { status: 200, body: item.to_json, headers: {} } end end |
#publishing_api_has_linkables(linkables, document_type:) ⇒ Object
Stub GET /v2/linkables to return a set of content items with a specific document type
314 315 316 317 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 314 def publishing_api_has_linkables(linkables, document_type:) url = PUBLISHING_API_V2_ENDPOINT + "/linkables?document_type=#{document_type}" stub_request(:get, url).to_return(status: 200, body: linkables.to_json, headers: {}) end |
#publishing_api_has_linked_items(items, params = {}) ⇒ Object
Stub calls to the get linked items endpoint
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 492 def publishing_api_has_linked_items(items, params = {}) content_id = params.fetch(:content_id) link_type = params.fetch(:link_type) fields = params.fetch(:fields, %w(base_path content_id document_type title)) url = Plek.current.find('publishing-api') + "/v2/linked/#{content_id}" request_parmeters = { "fields" => fields, "link_type" => link_type, } stub_request(:get, url) .with(query: request_parmeters) .and_return( body: items.to_json, status: 200 ) end |
#publishing_api_has_links(links) ⇒ Object
Stub a request to links endpoint
384 385 386 387 388 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 384 def publishing_api_has_links(links) links = deep_transform_keys(links, &:to_sym) url = PUBLISHING_API_V2_ENDPOINT + "/links/" + links[:content_id] stub_request(:get, url).to_return(status: 200, body: links.to_json, headers: {}) end |
#publishing_api_has_lookups(lookup_hash) ⇒ Object
Stub calls to the lookups endpoint
470 471 472 473 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 470 def publishing_api_has_lookups(lookup_hash) url = Plek.current.find('publishing-api') + '/lookup-by-base-path' stub_request(:post, url).to_return(body: lookup_hash.to_json) end |
#publishing_api_isnt_available ⇒ Object
Stub any version 2 request to the publishing API to return a 503 response
139 140 141 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 139 def publishing_api_isnt_available stub_request(:any, /#{PUBLISHING_API_V2_ENDPOINT}\/.*/).to_return(status: 503) end |
#request_json_includes(required_attributes) ⇒ Object
Get a request matcher that checks if a JSON request includes a set of attributes
233 234 235 236 237 238 239 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 233 def request_json_includes(required_attributes) ->(request) do data = JSON.parse(request.body) deep_stringify_keys(required_attributes). to_a.all? { |key, value| data[key] == value } end end |
#request_json_matches(required_attributes) ⇒ Object
Get a request matcher that checks if a JSON request matches a hash
242 243 244 245 246 247 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 242 def request_json_matches(required_attributes) ->(request) do data = JSON.parse(request.body) deep_stringify_keys(required_attributes) == data end end |
#stub_any_publishing_api_call ⇒ Object
Stub any version 2 request to the publishing API
128 129 130 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 128 def stub_any_publishing_api_call stub_request(:any, %r{\A#{PUBLISHING_API_V2_ENDPOINT}}) end |
#stub_any_publishing_api_call_to_return_not_found ⇒ Object
Stub any version 2 request to the publishing API to return a 404 response
133 134 135 136 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 133 def stub_any_publishing_api_call_to_return_not_found stub_request(:any, %r{\A#{PUBLISHING_API_V2_ENDPOINT}}) .to_return(status: 404, headers: { "Content-Type" => "application/json; charset=utf-8" }) end |
#stub_any_publishing_api_discard_draft ⇒ Object
Stub any POST /v2/content/*/discard-draft request
123 124 125 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 123 def stub_any_publishing_api_discard_draft stub_request(:post, %r{\A#{PUBLISHING_API_V2_ENDPOINT}/content/.*/discard-draft}) end |
#stub_any_publishing_api_patch_links ⇒ Object
Stub any PATCH /v2/links/* request
108 109 110 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 108 def stub_any_publishing_api_patch_links stub_request(:patch, %r{\A#{PUBLISHING_API_V2_ENDPOINT}/links/}) end |
#stub_any_publishing_api_publish ⇒ Object
Stub any POST /v2/content/*/publish request
113 114 115 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 113 def stub_any_publishing_api_publish stub_request(:post, %r{\A#{PUBLISHING_API_V2_ENDPOINT}/content/.*/publish}) end |
#stub_any_publishing_api_put_content ⇒ Object
Stub any PUT /v2/content/* request
103 104 105 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 103 def stub_any_publishing_api_put_content stub_request(:put, %r{\A#{PUBLISHING_API_V2_ENDPOINT}/content/}) end |
#stub_any_publishing_api_unpublish ⇒ Object
Stub any POST /v2/content/*/unpublish request
118 119 120 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 118 def stub_any_publishing_api_unpublish stub_request(:post, %r{\A#{PUBLISHING_API_V2_ENDPOINT}/content/.*/unpublish}) end |
#stub_publishing_api_discard_draft(content_id) ⇒ Object
Stub a POST /v2/content/:content_id/discard-draft request
76 77 78 79 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 76 def stub_publishing_api_discard_draft(content_id) url = PUBLISHING_API_V2_ENDPOINT + "/content/#{content_id}/discard-draft" stub_request(:post, url).to_return(status: 200, headers: { "Content-Type" => "application/json; charset=utf-8" }) end |
#stub_publishing_api_patch_links(content_id, body) ⇒ Object
Stub a PATCH /v2/links/:content_id request
39 40 41 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 39 def stub_publishing_api_patch_links(content_id, body) stub_publishing_api_patch(content_id, body, '/links') end |
#stub_publishing_api_publish(content_id, body, response_hash = {}) ⇒ Object
Stub a POST /v2/content/:content_id/publish request
48 49 50 51 52 53 54 55 56 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 48 def stub_publishing_api_publish(content_id, body, response_hash = {}) url = PUBLISHING_API_V2_ENDPOINT + "/content/#{content_id}/publish" response = { status: 200, body: '{}', headers: { "Content-Type" => "application/json; charset=utf-8" } }.merge(response_hash) stub_request(:post, url).with(body: body).to_return(response) end |
#stub_publishing_api_put_content(content_id, body, response_hash = {}) ⇒ Object
Stub a PUT /v2/content/:content_id request with the given content id and request body. if no response_hash is given, a default response as follows is created: 200, body: ‘{’, headers: => “application/json; charset=utf-8”}
if a response is given, then it will be merged with the default response. if the given parameter for the response body is a Hash, it will be converted to JSON.
The following two examples are equivalent:
31 32 33 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 31 def stub_publishing_api_put_content(content_id, body, response_hash = {}) stub_publishing_api_put(content_id, body, '/content', response_hash) end |
#stub_publishing_api_put_content_links_and_publish(body, content_id = nil, publish_body = nil) ⇒ Object
Stub requests issued when publishing a new draft.
-
PUT /v2/content/:content_id
-
POST /v2/content/:content_id/publish
-
PATCH /v2/links/:content_id
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 89 def stub_publishing_api_put_content_links_and_publish(body, content_id = nil, publish_body = nil) content_id ||= body[:content_id] if publish_body.nil? publish_body = { update_type: body.fetch(:update_type) } publish_body[:locale] = body[:locale] if body[:locale] end stubs = [] stubs << stub_publishing_api_put_content(content_id, body.except(:links)) stubs << stub_publishing_api_patch_links(content_id, body.slice(:links)) unless body.slice(:links).empty? stubs << stub_publishing_api_publish(content_id, publish_body) stubs end |
#stub_publishing_api_unpublish(content_id, params, response_hash = {}) ⇒ Object
Stub a POST /v2/content/:content_id/unpublish request
63 64 65 66 67 68 69 70 71 |
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 63 def stub_publishing_api_unpublish(content_id, params, response_hash = {}) url = PUBLISHING_API_V2_ENDPOINT + "/content/#{content_id}/unpublish" response = { status: 200, body: '{}', headers: { "Content-Type" => "application/json; charset=utf-8" } }.merge(response_hash) stub_request(:post, url).with(params).to_return(response) end |