Module: PublishingPlatformApi::TestHelpers::PublishingApi

Includes:
ContentItemHelpers
Defined in:
lib/publishing_platform_api/test_helpers/publishing_api.rb

Constant Summary collapse

PUBLISHING_API_ENDPOINT =
PublishingPlatformLocation.find("publishing-api")

Instance Method Summary collapse

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

Parameters:

  • verb (String)
  • url (String)
  • attributes_or_matcher (Object) (defaults to: nil)
  • times (Integer) (defaults to: 1)


264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 264

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 /content/:content_id/discard-draft)

Parameters:

  • content_id (UUID)
  • attributes_or_matcher (Object) (defaults to: nil)
  • times (Integer) (defaults to: 1)


253
254
255
256
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 253

def assert_publishing_api_discard_draft(content_id, attributes_or_matcher = nil, times = 1)
  url = PUBLISHING_API_ENDPOINT + "/content/#{content_id}/discard-draft"
  assert_publishing_api(:post, url, attributes_or_matcher, times)
end

Assert that links were updated (PATCH /links/:content_id)

Parameters:

  • content_id (UUID)
  • attributes_or_matcher (Object) (defaults to: nil)
  • times (Integer) (defaults to: 1)


243
244
245
246
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 243

def assert_publishing_api_patch_links(content_id, attributes_or_matcher = nil, times = 1)
  url = "#{PUBLISHING_API_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 /content/:content_id/publish)

Parameters:

  • content_id (UUID)
  • attributes_or_matcher (Object) (defaults to: nil)
  • times (Integer) (defaults to: 1)


223
224
225
226
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 223

def assert_publishing_api_publish(content_id, attributes_or_matcher = nil, times = 1)
  url = PUBLISHING_API_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



557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 557

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 /content/:content_id)

Parameters:

  • content_id (UUID)
  • attributes_or_matcher (Object) (defaults to: nil)
  • times (Integer) (defaults to: 1)


213
214
215
216
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 213

def assert_publishing_api_put_content(content_id, attributes_or_matcher = nil, times = 1)
  url = "#{PUBLISHING_API_ENDPOINT}/content/#{content_id}"
  assert_publishing_api(:put, url, attributes_or_matcher, times)
end

Assert that a draft was saved and published, and links were updated.

  • PUT /content/:content_id

  • POST /content/:content_id/publish

  • PATCH /links/:content_id

Parameters:

  • body (String)
  • content_id (UUID) (defaults to: nil)
  • publish_body (Hash) (defaults to: nil)


197
198
199
200
201
202
203
204
205
206
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 197

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 /content/:content_id/unpublish)

Parameters:

  • content_id (UUID)
  • attributes_or_matcher (Object) (defaults to: nil)
  • times (Integer) (defaults to: 1)


233
234
235
236
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 233

def assert_publishing_api_unpublish(content_id, attributes_or_matcher = nil, times = 1)
  url = PUBLISHING_API_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



279
280
281
282
283
284
285
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 279

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



578
579
580
581
582
583
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 578

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



288
289
290
291
292
293
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 288

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



571
572
573
574
575
576
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 571

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_callObject

Stub any request to the publishing API



174
175
176
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 174

def stub_any_publishing_api_call
  stub_request(:any, %r{\A#{PUBLISHING_API_ENDPOINT}})
end

#stub_any_publishing_api_call_to_return_not_foundObject

Stub any request to the publishing API to return a 404 response



179
180
181
182
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 179

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_draftObject

Stub any POST /content/*/discard-draft request



169
170
171
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 169

def stub_any_publishing_api_discard_draft
  stub_request(:post, %r{\A#{PUBLISHING_API_ENDPOINT}/content/.*/discard-draft})
end

Stub any PATCH /links/* request



149
150
151
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 149

def stub_any_publishing_api_patch_links
  stub_request(:patch, %r{\A#{PUBLISHING_API_ENDPOINT}/links/})
end

#stub_any_publishing_api_path_reservationObject

Stub all PUT /paths/:base_path requests

Examples:

stub_any_publishing_api_path_reservation


612
613
614
615
616
617
618
619
620
621
622
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 612

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_publishObject

Stub any POST /content/*/publish request



154
155
156
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 154

def stub_any_publishing_api_publish
  stub_request(:post, %r{\A#{PUBLISHING_API_ENDPOINT}/content/.*/publish})
end

#stub_any_publishing_api_put_contentObject

Stub any PUT /content/* request



144
145
146
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 144

def stub_any_publishing_api_put_content
  stub_request(:put, %r{\A#{PUBLISHING_API_ENDPOINT}/content/})
end

#stub_any_publishing_api_republishObject

Stub any POST /content/*/publish request



159
160
161
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 159

def stub_any_publishing_api_republish
  stub_request(:post, %r{\A#{PUBLISHING_API_ENDPOINT}/content/.*/republish})
end

#stub_any_publishing_api_unpublishObject

Stub any POST /content/*/unpublish request



164
165
166
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 164

def stub_any_publishing_api_unpublish
  stub_request(:post, %r{\A#{PUBLISHING_API_ENDPOINT}/content/.*/unpublish})
end

#stub_any_publishing_api_unreserve_pathObject



553
554
555
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 553

def stub_any_publishing_api_unreserve_path
  stub_request(:delete, %r{\A#{PUBLISHING_API_ENDPOINT}/paths/})
end

#stub_publishing_api_discard_draft(content_id) ⇒ Object

Stub a POST /content/:content_id/discard-draft request

Parameters:

  • content_id (UUID)


117
118
119
120
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 117

def stub_publishing_api_discard_draft(content_id)
  url = PUBLISHING_API_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 /content/:content_id to return a 404 response

Parameters:

  • content_id (UUID)


395
396
397
398
399
400
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 395

def stub_publishing_api_does_not_have_item(content_id, params = {})
  url = "#{PUBLISHING_API_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 GET /links/:content_id to return a 404 response

Parameters:

  • content_id (UUID)


536
537
538
539
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 536

def stub_publishing_api_does_not_have_links(content_id)
  url = "#{PUBLISHING_API_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_has_content(items, params = {}) ⇒ Object

Stub GET /content/ to return a set of content items

Examples:


stub_publishing_api_has_content(
  vehicle_recalls_and_faults,   # this is a variable containing an array of content items
  document_type: described_class.publishing_api_document_type,   #example of a document_type: "vehicle_recalls_and_faults_alert"
  fields: fields,   #example: let(:fields) { %i[base_path content_id public_updated_at title publication_state] }
  page: 1,
  per_page: 50
)

Parameters:

  • items (Array)
  • params (Hash) (defaults to: {})


308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 308

def stub_publishing_api_has_content(items, params = {})
  url = "#{PUBLISHING_API_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 a request to the expanded links endpoint

Examples:

stub_publishing_api_has_expanded_links(
  {
    "content_id" => "64aadc14-9bca-40d9-abb4-4f21f9792a05",
    "expanded_links" => {
      "mainstream_browse_pages" => [
        {
          "content_id" => "df2e7a3e-2078-45de-a76a-fd37d027427a",
          "base_path" => "/a/base/path",
          "document_type" => "mainstream_browse_page",
          "locale" => "en",
          "links" => {},
          # ...
        }
      ],
      "parent" => [
        {
          "content_id" => "df2e7a3e-2028-45de-a75a-fd37d027427e",
          "document_type" => "mainstream_browse_page",
          # ...
        },
      ]
    }
  }
)
Services.publishing_api.expanded_links("64aadc14-9bca-40d9-abb4-4f21f9792a05")
=>  {
      "content_id" => "64aadc14-9bca-40d9-abb4-4f21f9792a05",
      "expanded_links" => {
        "mainstream_browse_pages" => [
          {
            "content_id" => "df2e7a3e-2078-45de-a76a-fd37d027427a",
            "base_path" => "/a/base/path",
            "document_type" => "mainstream_browse_page",
            "locale" => "en",
            "links" => {},
            ...
          }
        ],
        "parent" => [
          {
            "content_id" => "df2e7a3e-2028-45de-a75a-fd37d027427e",
            "document_type" => "mainstream_browse_page",
            ...
          },
        ]
      }
    }

Parameters:

  • links (Hash)

    the structure of the links hash



492
493
494
495
496
497
498
499
500
501
502
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 492

def stub_publishing_api_has_expanded_links(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_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



343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 343

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_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 /content/:content_id to return a specific content item hash

Parameters:

  • item (Hash)


368
369
370
371
372
373
374
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 368

def stub_publishing_api_has_item(item, params = {})
  item = deep_transform_keys(item, &:to_sym)
  url = "#{PUBLISHING_API_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 /content/:content_id to progress through a series of responses.

Parameters:

  • items (Array)


379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 379

def stub_publishing_api_has_item_in_sequence(content_id, items)
  items = items.each { |item| deep_transform_keys(item, &:to_sym) }
  url = "#{PUBLISHING_API_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 /linkables to return a set of content items with a specific document type

Parameters:

  • linkables (Array)


360
361
362
363
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 360

def stub_publishing_api_has_linkables(linkables, document_type:)
  url = PUBLISHING_API_ENDPOINT + "/linkables?document_type=#{document_type}"
  stub_request(:get, url).to_return(status: 200, body: linkables.to_json, headers: {})
end

Stub a request to links endpoint

Examples:


stub_publishing_api_has_links(
  {
    "content_id" => "64aadc14-9bca-40d9-abb6-4f21f9792a05",
    "links" => {
      "mainstream_browse_pages" => ["df2e7a3e-2078-45de-a75a-fd37d027427e"],
      "parent" => ["df2e7a3e-2078-45de-a75a-fd37d027427e"],
      "organisations" => ["569a9ee5-c195-4b7f-b9dc-edc17a09113f", "5c54ae52-341b-499e-a6dd-67f04633b8cf"]
    },
    "version" => 6
  }
)

Services.publishing_api.get_links("64aadc14-9bca-40d9-abb6-4f21f9792a05")
=> {
     "content_id" => "64aadc14-9bca-40d9-abb6-4f21f9792a05",
     "links" => {
       "mainstream_browse_pages" => ["df2e7a3e-2078-45de-a75a-fd37d027427e"],
       "parent" => ["df2e7a3e-2078-45de-a75a-fd37d027427e"],
       "organisations" => ["569a9ee5-c195-4b7f-b9dc-edc17a09113f", "5c54ae52-341b-499e-a6dd-67f04633b8cf"]
     },
     "version" => 6
   }

Parameters:

  • links (Hash)

    the structure of the links hash



432
433
434
435
436
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 432

def stub_publishing_api_has_links(links)
  links = deep_transform_keys(links, &:to_sym)
  url = "#{PUBLISHING_API_ENDPOINT}/links/#{links[:content_id]}"
  stub_request(:get, url).to_return(status: 200, body: links.to_json, headers: {})
end

Stub a request to get links for content ids

Examples:

stub_publishing_api_has_links_for_content_ids(
  { "2878337b-bed9-4e7f-85b6-10ed2cbcd504" => {
      "links" => { "taxons" => ["eb6965c7-3056-45d0-ae50-2f0a5e2e0854"] }
    },
    "eec13cea-219d-4896-9c97-60114da23559" => {
      "links" => {}
    }
  }
)
Services.publishing_api.get_links_for_content_ids(["2878337b-bed9-4e7f-85b6-10ed2cbcd504"])
=>  {
      "2878337b-bed9-4e7f-85b6-10ed2cbcd504" => {
        "links" => [
          "eb6965c7-3056-45d0-ae50-2f0a5e2e0854"
        ]
      }
    }

Parameters:

  • links (Hash)

    the links for each content id



528
529
530
531
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 528

def stub_publishing_api_has_links_for_content_ids(links)
  url = "#{PUBLISHING_API_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_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.

Examples:

stub_publishing_api_has_path_reservation_for("/foo", "content-publisher")

Parameters:

  • base_path (String)
  • publishing_app (String)


633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 633

def stub_publishing_api_has_path_reservation_for(path, publishing_app)
  message = "#{path} is already reserved by #{publishing_app}"
  error = { code: 422,
            message: "Base path #{message}",
            fields: { base_path: [message] } }

  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_isnt_availableObject

Stub any request to the publishing API to return a 503 response



185
186
187
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 185

def stub_publishing_api_isnt_available
  stub_request(:any, /#{PUBLISHING_API_ENDPOINT}\/.*/).to_return(status: 503)
end

Stub a PATCH /links/:content_id request

Examples:

stub_publishing_api_patch_links(
  my_content_id,
  "links" => {
    "taxons" => %w(level_one_topic level_two_topic),
  },
  "previous_version" => 3,
)

Parameters:

  • content_id (UUID)
  • body (String)


46
47
48
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 46

def stub_publishing_api_patch_links(content_id, body)
  stub_publishing_api_patch(content_id, body, "/links")
end

Stub a PATCH /links/:content_id request to return a 409 response

Examples:

stub_publishing_api_patch_links_conflict(
  my_content_id,
  "links" => {
    "taxons" => %w(level_one_topic level_two_topic),
  },
  "previous_version" => 3,
)

Parameters:

  • content_id (UUID)
  • body (String)


63
64
65
66
67
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 63

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.

Examples:

stub_publishing_api_path_reservation(
  "/path/to",
  publishing_app: "content-publisher",
  override_existing: true,
)

Parameters:

  • base_path (String)
  • params (Hash) (defaults to: {})


597
598
599
600
601
602
603
604
605
606
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 597

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 /content/:content_id/publish request

Parameters:

  • content_id (UUID)
  • body (String)
  • response_hash (Hash) (defaults to: {})


74
75
76
77
78
79
80
81
82
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 74

def stub_publishing_api_publish(content_id, body, response_hash = {})
  url = PUBLISHING_API_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 /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:

Examples:

stub_publishing_api_put_content(my_content_id, my_request_body, { status: 201, body: {version: 33}.to_json })
stub_publishing_api_put_content(my_content_id, my_request_body, { status: 201, body: {version: 33} })

Parameters:

  • content_id (UUID)
  • body (String)
  • response_hash (Hash) (defaults to: {})


29
30
31
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 29

def stub_publishing_api_put_content(content_id, body, response_hash = {})
  stub_publishing_api_put(content_id, body, "/content", response_hash)
end

Stub requests issued when publishing a new draft.

  • PUT /content/:content_id

  • POST /content/:content_id/publish

  • PATCH /links/:content_id

Parameters:

  • body (String)
  • content_id (UUID) (defaults to: nil)
  • publish_body (Hash) (defaults to: nil)


130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 130

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_republish(content_id, body = {}, response_hash = {}) ⇒ Object

Stub a POST /content/:content_id/republish request

Parameters:

  • content_id (UUID)
  • body (String) (defaults to: {})
  • response_hash (Hash) (defaults to: {})


89
90
91
92
93
94
95
96
97
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 89

def stub_publishing_api_republish(content_id, body = {}, response_hash = {})
  url = PUBLISHING_API_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.

Examples:

stub_publishing_api_returns_path_reservation_validation_error_for(
  "/foo",
  "field" => ["error 1", "error 2"]
)

Parameters:

  • base_path (String)
  • error_fields (Hash) (defaults to: {})


663
664
665
666
667
668
669
670
671
672
673
674
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 663

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?

  message = "#{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_unpublish(content_id, params, response_hash = {}) ⇒ Object

Stub a POST /content/:content_id/unpublish request

Parameters:

  • content_id (UUID)
  • params (Hash)
  • body (String)


104
105
106
107
108
109
110
111
112
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 104

def stub_publishing_api_unpublish(content_id, params, response_hash = {})
  url = PUBLISHING_API_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



541
542
543
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 541

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



549
550
551
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 549

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



545
546
547
# File 'lib/publishing_platform_api/test_helpers/publishing_api.rb', line 545

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