Module: GdsApi::TestHelpers::PublishingApi
- Includes:
- ContentItemHelpers
- Defined in:
- lib/gds_api/test_helpers/publishing_api.rb
Constant Summary collapse
- PUBLISHING_API_V2_ENDPOINT =
"#{Plek.find('publishing-api')}/v2".freeze
- PUBLISHING_API_ENDPOINT =
Plek.find("publishing-api")
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(url, attributes_or_matcher = {}, times = 1) ⇒ Object
-
#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_put_intent(base_path, attributes_or_matcher = {}, times = 1) ⇒ Object
-
#assert_publishing_api_unpublish(content_id, attributes_or_matcher = nil, times = 1) ⇒ Object
Assert that content was unpublished (POST /v2/content/:content_id/unpublish).
-
#request_json_includes(required_attributes) ⇒ Object
Get a request matcher that checks if a JSON request includes a set of attributes.
- #request_json_including(required_attributes) ⇒ Object
-
#request_json_matches(required_attributes) ⇒ Object
Get a request matcher that checks if a JSON request matches a hash.
- #request_json_matching(required_attributes) ⇒ Object
-
#stub_any_publishing_api_call ⇒ Object
Stub any request to the publishing API.
-
#stub_any_publishing_api_call_to_return_not_found ⇒ Object
Stub any 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_path_reservation ⇒ Object
Stub all PUT /paths/:base_path requests.
-
#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_put_intent ⇒ Object
-
#stub_any_publishing_api_republish ⇒ Object
Stub any POST /v2/content/*/publish request.
-
#stub_any_publishing_api_unpublish ⇒ Object
Stub any POST /v2/content/*/unpublish request.
- #stub_any_publishing_api_unreserve_path ⇒ Object
- #stub_publishing_api_destroy_intent(base_path) ⇒ Object
-
#stub_publishing_api_discard_draft(content_id) ⇒ Object
Stub a POST /v2/content/:content_id/discard-draft request.
-
#stub_publishing_api_does_not_have_item(content_id, params = {}) ⇒ Object
Stub GET /v2/content/:content_id to return a 404 response.
-
#stub_publishing_api_does_not_have_links(content_id) ⇒ Object
Stub GET /v2/links/:content_id to return a 404 response.
-
#stub_publishing_api_get_editions(editions, params = {}) ⇒ Object
Stub GET /v2/editions to return a set of editions.
-
#stub_publishing_api_graphql_content_item(query, response_hash = {}) ⇒ Object
Stub a POST /graphql content item request.
-
#stub_publishing_api_graphql_query(query, response_hash = {}) ⇒ Object
Stub a POST /graphql request.
-
#stub_publishing_api_has_content(items, params = {}) ⇒ Object
Stub GET /v2/content/ to return a set of content items.
-
#stub_publishing_api_has_embedded_content(content_id:, total: 0, total_pages: 0, results: [], page_number: nil, order: nil, rollup: {}) ⇒ Object
Stub GET /v2/content/:content_id/embedded to return a list of content items that embed the target content_id.
- #stub_publishing_api_has_embedded_content_for_any_content_id(total: 0, total_pages: 0, results: [], page_number: nil, order: nil, rollup: {}) ⇒ Object
-
#stub_publishing_api_has_expanded_links(links, with_drafts: true, generate: false) ⇒ Object
Stub a request to the expanded links endpoint.
-
#stub_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.
-
#stub_publishing_api_has_item(item, params = {}) ⇒ Object
Stub GET /v2/content/:content_id to return a specific content item hash.
-
#stub_publishing_api_has_item_in_sequence(content_id, items) ⇒ Object
Stub GET /v2/content/:content_id to progress through a series of responses.
-
#stub_publishing_api_has_linkables(linkables, document_type:) ⇒ Object
Stub GET /v2/linkables to return a set of content items with a specific document type.
-
#stub_publishing_api_has_linked_items(items, params = {}) ⇒ Object
Stub calls to the get linked items endpoint.
-
#stub_publishing_api_has_links(links) ⇒ Object
Stub a request to links endpoint.
-
#stub_publishing_api_has_links_for_content_ids(links) ⇒ Object
Stub a request to get links for content ids.
-
#stub_publishing_api_has_lookups(lookup_hash) ⇒ Object
Stub calls to the lookups endpoint.
-
#stub_publishing_api_has_path_reservation_for(path, publishing_app) ⇒ Object
Stub a PUT /paths/:base_path request for a particular publishing application.
-
#stub_publishing_api_has_schemas(schemas) ⇒ Object
Stub a request to get all schemas.
-
#stub_publishing_api_has_schemas_for_schema_name(schema_name, schema) ⇒ Object
Stub a request to get a schema by schema name.
-
#stub_publishing_api_isnt_available ⇒ Object
Stub any request to the publishing API to return a 503 response.
-
#stub_publishing_api_patch_links(content_id, body) ⇒ Object
Stub a PATCH /v2/links/:content_id request.
-
#stub_publishing_api_patch_links_conflict(content_id, body) ⇒ Object
Stub a PATCH /v2/links/:content_id request to return a 409 response.
-
#stub_publishing_api_path_reservation(base_path, params = {}) ⇒ Object
Stub a PUT /paths/:base_path request with the given base_path and request body.
-
#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_put_intent(base_path, params = {}) ⇒ Object
Stub a PUT /publish-intent/:base_path request with the given base_path and request body.
-
#stub_publishing_api_republish(content_id, body = {}, response_hash = {}) ⇒ Object
Stub a POST /v2/content/:content_id/republish request.
-
#stub_publishing_api_returns_path_reservation_validation_error_for(base_path, error_fields = {}) ⇒ Object
Stub a PUT /paths/:base_path request for a particular publishing application.
- #stub_publishing_api_schema_name_path_to_return_not_found(schema_name) ⇒ Object
-
#stub_publishing_api_unpublish(content_id, params, response_hash = {}) ⇒ Object
Stub a POST /v2/content/:content_id/unpublish request.
- #stub_publishing_api_unreserve_path(base_path, publishing_app = /.*/) ⇒ Object
- #stub_publishing_api_unreserve_path_invalid(base_path, publishing_app = /.*/) ⇒ Object
- #stub_publishing_api_unreserve_path_not_found(base_path, publishing_app = /.*/) ⇒ Object
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
292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 292 def assert_publishing_api(verb, url, attributes_or_matcher = nil, times = 1) matcher = if attributes_or_matcher.is_a?(Hash) request_json_matches(attributes_or_matcher) else attributes_or_matcher end if matcher assert_requested(verb, url, times:, &matcher) else assert_requested(verb, url, 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)
281 282 283 284 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 281 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)
271 272 273 274 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 271 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)
251 252 253 254 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 251 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(url, attributes_or_matcher = {}, times = 1) ⇒ Object
795 796 797 798 799 800 801 802 803 804 805 806 807 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 795 def assert_publishing_api_put(url, attributes_or_matcher = {}, times = 1) matcher = if attributes_or_matcher.is_a?(Hash) attributes_or_matcher.empty? ? nil : request_json_matching(attributes_or_matcher) else attributes_or_matcher end if matcher assert_requested(:put, url, times:, &matcher) else assert_requested(:put, url, times:) end 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)
241 242 243 244 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 241 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
225 226 227 228 229 230 231 232 233 234 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 225 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_put_intent(base_path, attributes_or_matcher = {}, times = 1) ⇒ Object
790 791 792 793 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 790 def assert_publishing_api_put_intent(base_path, attributes_or_matcher = {}, times = 1) url = "#{PUBLISHING_API_ENDPOINT}/publish-intent#{base_path}" assert_publishing_api_put(url, attributes_or_matcher, times) 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)
261 262 263 264 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 261 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 |
#request_json_includes(required_attributes) ⇒ Object
Get a request matcher that checks if a JSON request includes a set of attributes
307 308 309 310 311 312 313 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 307 def request_json_includes(required_attributes) lambda do |request| data = JSON.parse(request.body) deep_stringify_keys(required_attributes) .to_a.all? { |key, value| data[key] == value } end end |
#request_json_including(required_attributes) ⇒ Object
816 817 818 819 820 821 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 816 def request_json_including(required_attributes) lambda do |request| data = JSON.parse(request.body) values_match_recursively(required_attributes, data) end end |
#request_json_matches(required_attributes) ⇒ Object
Get a request matcher that checks if a JSON request matches a hash
316 317 318 319 320 321 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 316 def request_json_matches(required_attributes) lambda do |request| data = JSON.parse(request.body) deep_stringify_keys(required_attributes) == data end end |
#request_json_matching(required_attributes) ⇒ Object
809 810 811 812 813 814 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 809 def request_json_matching(required_attributes) lambda do |request| data = JSON.parse(request.body) required_attributes.to_a.all? { |key, value| data[key.to_s] == value } end end |
#stub_any_publishing_api_call ⇒ Object
Stub any request to the publishing API
176 177 178 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 176 def stub_any_publishing_api_call stub_request(:any, %r{\A#{PUBLISHING_API_ENDPOINT}}) end |
#stub_any_publishing_api_call_to_return_not_found ⇒ Object
Stub any request to the publishing API to return a 404 response
181 182 183 184 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 181 def stub_any_publishing_api_call_to_return_not_found stub_request(:any, %r{\A#{PUBLISHING_API_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
171 172 173 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 171 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
151 152 153 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 151 def stub_any_publishing_api_patch_links stub_request(:patch, %r{\A#{PUBLISHING_API_V2_ENDPOINT}/links/}) end |
#stub_any_publishing_api_path_reservation ⇒ Object
Stub all PUT /paths/:base_path requests
850 851 852 853 854 855 856 857 858 859 860 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 850 def stub_any_publishing_api_path_reservation stub_request(:put, %r{\A#{PUBLISHING_API_ENDPOINT}/paths/}).to_return do |request| base_path = request.uri.path.sub(%r{\A/paths}, "") body = JSON.parse(request.body).merge(base_path:) { status: 200, headers: { content_type: "application/json" }, body: body.to_json, } end end |
#stub_any_publishing_api_publish ⇒ Object
Stub any POST /v2/content/*/publish request
156 157 158 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 156 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
146 147 148 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 146 def stub_any_publishing_api_put_content stub_request(:put, %r{\A#{PUBLISHING_API_V2_ENDPOINT}/content/}) end |
#stub_any_publishing_api_put_intent ⇒ Object
786 787 788 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 786 def stub_any_publishing_api_put_intent stub_request(:put, %r{\A#{PUBLISHING_API_ENDPOINT}/publish-intent}) end |
#stub_any_publishing_api_republish ⇒ Object
Stub any POST /v2/content/*/publish request
161 162 163 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 161 def stub_any_publishing_api_republish stub_request(:post, %r{\A#{PUBLISHING_API_V2_ENDPOINT}/content/.*/republish}) end |
#stub_any_publishing_api_unpublish ⇒ Object
Stub any POST /v2/content/*/unpublish request
166 167 168 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 166 def stub_any_publishing_api_unpublish stub_request(:post, %r{\A#{PUBLISHING_API_V2_ENDPOINT}/content/.*/unpublish}) end |
#stub_any_publishing_api_unreserve_path ⇒ Object
752 753 754 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 752 def stub_any_publishing_api_unreserve_path stub_request(:delete, %r{\A#{PUBLISHING_API_ENDPOINT}/paths/}) end |
#stub_publishing_api_destroy_intent(base_path) ⇒ Object
781 782 783 784 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 781 def stub_publishing_api_destroy_intent(base_path) url = "#{PUBLISHING_API_ENDPOINT}/publish-intent#{base_path}" stub_request(:delete, url).to_return(status: 200, body: "{}", headers: { "Content-Type" => "application/json; charset=utf-8" }) end |
#stub_publishing_api_discard_draft(content_id) ⇒ Object
Stub a POST /v2/content/:content_id/discard-draft request
119 120 121 122 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 119 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_does_not_have_item(content_id, params = {}) ⇒ Object
Stub GET /v2/content/:content_id to return a 404 response
506 507 508 509 510 511 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 506 def stub_publishing_api_does_not_have_item(content_id, params = {}) url = "#{PUBLISHING_API_V2_ENDPOINT}/content/#{content_id}" stub_request(:get, url) .with(query: hash_including(params)) .to_return(status: 404, body: resource_not_found(content_id, "content item").to_json, headers: {}) end |
#stub_publishing_api_does_not_have_links(content_id) ⇒ Object
Stub GET /v2/links/:content_id to return a 404 response
647 648 649 650 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 647 def stub_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 |
#stub_publishing_api_get_editions(editions, params = {}) ⇒ Object
Stub GET /v2/editions to return a set of editions
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 716 def stub_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:, 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 |
#stub_publishing_api_graphql_content_item(query, response_hash = {}) ⇒ Object
Stub a POST /graphql content item request
207 208 209 210 211 212 213 214 215 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 207 def stub_publishing_api_graphql_content_item(query, response_hash = {}) url = "#{PUBLISHING_API_ENDPOINT}/graphql" response = { status: 200, body: response_hash.to_json, headers: { "Content-Type" => "application/json; charset=utf-8" }, } stub_request(:post, url).with(body: { query: }).to_return(response) end |
#stub_publishing_api_graphql_query(query, response_hash = {}) ⇒ Object
Stub a POST /graphql request
194 195 196 197 198 199 200 201 202 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 194 def stub_publishing_api_graphql_query(query, response_hash = {}) url = "#{PUBLISHING_API_ENDPOINT}/graphql" response = { status: 200, body: response_hash.to_json, headers: { "Content-Type" => "application/json; charset=utf-8" }, } stub_request(:post, url).with(body: { query: }).to_return(response) end |
#stub_publishing_api_has_content(items, params = {}) ⇒ Object
Stub GET /v2/content/ to return a set of content items
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 336 def stub_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 |
#stub_publishing_api_has_embedded_content(content_id:, total: 0, total_pages: 0, results: [], page_number: nil, order: nil, rollup: {}) ⇒ Object
Stub GET /v2/content/:content_id/embedded to return a list of content items that embed the target content_id
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 399 def ( content_id:, total: 0, total_pages: 0, results: [], page_number: nil, order: nil, rollup: {} ) url = "#{PUBLISHING_API_V2_ENDPOINT}/content/#{content_id}/host-content" query = { "page" => page_number, "order" => order, }.compact stub_request(:get, url) .with(query:) .to_return(body: { "content_id" => content_id, "total" => total, "total_pages" => total_pages, "rollup" => rollup, "results" => results, }.to_json) end |
#stub_publishing_api_has_embedded_content_for_any_content_id(total: 0, total_pages: 0, results: [], page_number: nil, order: nil, rollup: {}) ⇒ Object
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 426 def ( total: 0, total_pages: 0, results: [], page_number: nil, order: nil, rollup: {} ) url = %r{\A#{PUBLISHING_API_V2_ENDPOINT}/content/[0-9a-fA-F-]{36}/host-content} query = { "page" => page_number, "order" => order, }.compact stub_request(:get, url) .with { |request| WebMock::Util::QueryMapper.query_to_values(request.uri.query) == query } .to_return(body: { "content_id" => SecureRandom.uuid, "total" => total, "total_pages" => total_pages, "rollup" => rollup, "results" => results, }.to_json) end |
#stub_publishing_api_has_expanded_links(links, with_drafts: true, generate: false) ⇒ Object
Stub a request to the expanded links endpoint
603 604 605 606 607 608 609 610 611 612 613 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 603 def (links, with_drafts: true, generate: false) links = deep_transform_keys(links, &:to_sym) request_params = {} request_params["with_drafts"] = false unless with_drafts request_params["generate"] = true if generate url = "#{PUBLISHING_API_V2_ENDPOINT}/expanded-links/#{links[:content_id]}" stub_request(:get, url) .with(query: request_params) .to_return(status: 200, body: links.to_json, headers: {}) end |
#stub_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
454 455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 454 def stub_publishing_api_has_fields_for_document(document_type, items, fields) body = Array(items).map do |item| deep_stringify_keys(item).slice(*fields) end query_params = fields.map do |f| "&fields%5B%5D=#{f}" end 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 |
#stub_publishing_api_has_item(item, params = {}) ⇒ Object
Stub GET /v2/content/:content_id to return a specific content item hash
479 480 481 482 483 484 485 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 479 def stub_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 |
#stub_publishing_api_has_item_in_sequence(content_id, items) ⇒ Object
Stub GET /v2/content/:content_id to progress through a series of responses.
490 491 492 493 494 495 496 497 498 499 500 501 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 490 def stub_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 |
#stub_publishing_api_has_linkables(linkables, document_type:) ⇒ Object
Stub GET /v2/linkables to return a set of content items with a specific document type
471 472 473 474 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 471 def stub_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 |
#stub_publishing_api_has_linked_items(items, params = {}) ⇒ Object
Stub calls to the get linked items endpoint
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 685 def stub_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 = PUBLISHING_API_V2_ENDPOINT + "/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 |
#stub_publishing_api_has_links(links) ⇒ Object
Stub a request to links endpoint
543 544 545 546 547 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 543 def stub_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 |
#stub_publishing_api_has_links_for_content_ids(links) ⇒ Object
Stub a request to get links for content ids
639 640 641 642 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 639 def stub_publishing_api_has_links_for_content_ids(links) url = "#{PUBLISHING_API_V2_ENDPOINT}/links/by-content-id" stub_request(:post, url).with(body: { content_ids: links.keys }).to_return(status: 200, body: links.to_json, headers: {}) end |
#stub_publishing_api_has_lookups(lookup_hash) ⇒ Object
Stub calls to the lookups endpoint
663 664 665 666 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 663 def stub_publishing_api_has_lookups(lookup_hash) url = "#{PUBLISHING_API_ENDPOINT}/lookup-by-base-path" stub_request(:post, url).to_return(body: lookup_hash.to_json) end |
#stub_publishing_api_has_path_reservation_for(path, publishing_app) ⇒ Object
Stub a PUT /paths/:base_path request for a particular publishing application. Calling for a different publishing application will return a 422 response.
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 871 def stub_publishing_api_has_path_reservation_for(path, publishing_app) = "#{path} is already reserved by #{publishing_app}" error = { code: 422, message: "Base path #{}", fields: { base_path: [] } } stub_request(:put, "#{PUBLISHING_API_ENDPOINT}/paths#{path}") .to_return(status: 422, headers: { content_type: "application/json" }, body: { error: }.to_json) stub_request(:put, "#{PUBLISHING_API_ENDPOINT}/paths#{path}") .with(body: { "publishing_app" => publishing_app }) .to_return(status: 200, headers: { content_type: "application/json" }, body: { publishing_app:, base_path: path }.to_json) end |
#stub_publishing_api_has_schemas(schemas) ⇒ Object
Stub a request to get all schemas
962 963 964 965 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 962 def stub_publishing_api_has_schemas(schemas) url = "#{PUBLISHING_API_V2_ENDPOINT}/schemas" stub_request(:get, url).to_return(status: 200, body: schemas.to_json, headers: {}) end |
#stub_publishing_api_has_schemas_for_schema_name(schema_name, schema) ⇒ Object
Stub a request to get a schema by schema name
930 931 932 933 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 930 def stub_publishing_api_has_schemas_for_schema_name(schema_name, schema) url = "#{PUBLISHING_API_V2_ENDPOINT}/schemas/#{schema_name}" stub_request(:get, url).to_return(status: 200, body: schema.to_json, headers: {}) end |
#stub_publishing_api_isnt_available ⇒ Object
Stub any request to the publishing API to return a 503 response
187 188 189 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 187 def stub_publishing_api_isnt_available stub_request(:any, /#{PUBLISHING_API_ENDPOINT}\/.*/).to_return(status: 503) end |
#stub_publishing_api_patch_links(content_id, body) ⇒ Object
Stub a PATCH /v2/links/:content_id request
48 49 50 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 48 def stub_publishing_api_patch_links(content_id, body) stub_publishing_api_patch(content_id, body, "/links") end |
#stub_publishing_api_patch_links_conflict(content_id, body) ⇒ Object
Stub a PATCH /v2/links/:content_id request to return a 409 response
65 66 67 68 69 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 65 def stub_publishing_api_patch_links_conflict(content_id, body) previous_version = JSON.parse(body.to_json)["previous_version"] override_response_hash = { status: 409, body: version_conflict(previous_version) } stub_publishing_api_patch(content_id, body, "/links", override_response_hash) end |
#stub_publishing_api_path_reservation(base_path, params = {}) ⇒ Object
Stub a PUT /paths/:base_path request with the given base_path and request body.
835 836 837 838 839 840 841 842 843 844 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 835 def stub_publishing_api_path_reservation(base_path, params = {}) url = PUBLISHING_API_ENDPOINT + "/paths#{base_path}" response = { status: 200, headers: { content_type: "application/json" }, body: params.merge(base_path:).to_json, } stub_request(:put, url).with(body: params).to_return(response) end |
#stub_publishing_api_publish(content_id, body, response_hash = {}) ⇒ Object
Stub a POST /v2/content/:content_id/publish request
76 77 78 79 80 81 82 83 84 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 76 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:).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.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
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 132 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_put_intent(base_path, params = {}) ⇒ Object
Stub a PUT /publish-intent/:base_path request with the given base_path and request body.
769 770 771 772 773 774 775 776 777 778 779 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 769 def stub_publishing_api_put_intent(base_path, params = {}) url = PUBLISHING_API_ENDPOINT + "/publish-intent#{base_path}" body = params.is_a?(String) ? params : params.to_json response = { status: 200, headers: { content_type: "application/json" }, body:, } stub_request(:put, url).with(body: params).to_return(response) end |
#stub_publishing_api_republish(content_id, body = {}, response_hash = {}) ⇒ Object
Stub a POST /v2/content/:content_id/republish request
91 92 93 94 95 96 97 98 99 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 91 def stub_publishing_api_republish(content_id, body = {}, response_hash = {}) url = PUBLISHING_API_V2_ENDPOINT + "/content/#{content_id}/republish" response = { status: 200, body: "{}", headers: { "Content-Type" => "application/json; charset=utf-8" }, }.merge(response_hash) stub_request(:post, url).with(body:).to_return(response) end |
#stub_publishing_api_returns_path_reservation_validation_error_for(base_path, error_fields = {}) ⇒ Object
Stub a PUT /paths/:base_path request for a particular publishing application. Calling for a different publishing application will return a 422 response.
901 902 903 904 905 906 907 908 909 910 911 912 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 901 def stub_publishing_api_returns_path_reservation_validation_error_for(base_path, error_fields = {}) error_fields = { "base_path" => ["Computer says no"] } if error_fields.empty? = "#{error_fields.keys.first.to_s.capitalize.gsub(/_/, ' ')} #{error_fields.values.flatten.first}" error = { code: 422, message:, fields: error_fields } stub_request(:put, "#{PUBLISHING_API_ENDPOINT}/paths#{base_path}") .to_return(status: 422, headers: { content_type: "application/json" }, body: { error: }.to_json) end |
#stub_publishing_api_schema_name_path_to_return_not_found(schema_name) ⇒ Object
935 936 937 938 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 935 def stub_publishing_api_schema_name_path_to_return_not_found(schema_name) url = "#{PUBLISHING_API_V2_ENDPOINT}/schemas/#{schema_name}" stub_request(:get, url).to_return(status: 404, headers: { "Content-Type" => "application/json; charset=utf-8" }) end |
#stub_publishing_api_unpublish(content_id, params, response_hash = {}) ⇒ Object
Stub a POST /v2/content/:content_id/unpublish request
106 107 108 109 110 111 112 113 114 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 106 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 |
#stub_publishing_api_unreserve_path(base_path, publishing_app = /.*/) ⇒ Object
740 741 742 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 740 def stub_publishing_api_unreserve_path(base_path, publishing_app = /.*/) stub_publishing_api_unreserve_path_with_code(base_path, publishing_app, 200) end |
#stub_publishing_api_unreserve_path_invalid(base_path, publishing_app = /.*/) ⇒ Object
748 749 750 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 748 def stub_publishing_api_unreserve_path_invalid(base_path, publishing_app = /.*/) stub_publishing_api_unreserve_path_with_code(base_path, publishing_app, 422) end |
#stub_publishing_api_unreserve_path_not_found(base_path, publishing_app = /.*/) ⇒ Object
744 745 746 |
# File 'lib/gds_api/test_helpers/publishing_api.rb', line 744 def stub_publishing_api_unreserve_path_not_found(base_path, publishing_app = /.*/) stub_publishing_api_unreserve_path_with_code(base_path, publishing_app, 404) end |