gds-api-adapters 63.0.0 → 63.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,697 +5,11 @@ require "json"
5
5
 
6
6
  module GdsApi
7
7
  module TestHelpers
8
- # @api documented
9
8
  module PublishingApiV2
10
- include ContentItemHelpers
9
+ include PublishingApi
11
10
 
12
- PUBLISHING_API_V2_ENDPOINT = Plek.current.find("publishing-api") + "/v2"
13
- PUBLISHING_API_ENDPOINT = Plek.current.find("publishing-api")
14
-
15
- # Stub a PUT /v2/content/:content_id request with the given content id and request body.
16
- # if no response_hash is given, a default response as follows is created:
17
- # {status: 200, body: '{}', headers: {"Content-Type" => "application/json; charset=utf-8"}}
18
- #
19
- # if a response is given, then it will be merged with the default response.
20
- # if the given parameter for the response body is a Hash, it will be converted to JSON.
21
- #
22
- # The following two examples are equivalent:
23
- # @example
24
- # stub_publishing_api_put_content(my_content_id, my_request_body, { status: 201, body: {version: 33}.to_json })
25
- #
26
- # @example
27
- # stub_publishing_api_put_content(my_content_id, my_request_body, { status: 201, body: {version: 33} })
28
- #
29
- # @param content_id [UUID]
30
- # @param body [String]
31
- # @param response_hash [Hash]
32
- def stub_publishing_api_put_content(content_id, body, response_hash = {})
33
- stub_publishing_api_put(content_id, body, "/content", response_hash)
34
- end
35
-
36
- # Stub a PATCH /v2/links/:content_id request
37
- #
38
- # @example
39
- # stub_publishing_api_patch_links(
40
- # my_content_id,
41
- # "links" => {
42
- # "taxons" => %w(level_one_topic level_two_topic),
43
- # },
44
- # "previous_version" => 3,
45
- # )
46
- #
47
- # @param content_id [UUID]
48
- # @param body [String]
49
- def stub_publishing_api_patch_links(content_id, body)
50
- stub_publishing_api_patch(content_id, body, "/links")
51
- end
52
-
53
- # Stub a PATCH /v2/links/:content_id request to return a 409 response
54
- #
55
- # @example
56
- # stub_publishing_api_patch_links_conflict(
57
- # my_content_id,
58
- # "links" => {
59
- # "taxons" => %w(level_one_topic level_two_topic),
60
- # },
61
- # "previous_version" => 3,
62
- # )
63
- #
64
- # @param content_id [UUID]
65
- # @param body [String]
66
- def stub_publishing_api_patch_links_conflict(content_id, body)
67
- previous_version = JSON.parse(body.to_json)["previous_version"]
68
- override_response_hash = { status: 409, body: version_conflict(previous_version) }
69
- stub_publishing_api_patch(content_id, body, "/links", override_response_hash)
70
- end
71
-
72
- # Stub a POST /v2/content/:content_id/publish request
73
- #
74
- # @param content_id [UUID]
75
- # @param body [String]
76
- # @param response_hash [Hash]
77
- def stub_publishing_api_publish(content_id, body, response_hash = {})
78
- url = PUBLISHING_API_V2_ENDPOINT + "/content/#{content_id}/publish"
79
- response = {
80
- status: 200,
81
- body: "{}",
82
- headers: { "Content-Type" => "application/json; charset=utf-8" },
83
- }.merge(response_hash)
84
- stub_request(:post, url).with(body: body).to_return(response)
85
- end
86
-
87
- # Stub a POST /v2/content/:content_id/republish request
88
- #
89
- # @param content_id [UUID]
90
- # @param body [String]
91
- # @param response_hash [Hash]
92
- def stub_publishing_api_republish(content_id, body = {}, response_hash = {})
93
- url = PUBLISHING_API_V2_ENDPOINT + "/content/#{content_id}/republish"
94
- response = {
95
- status: 200,
96
- body: "{}",
97
- headers: { "Content-Type" => "application/json; charset=utf-8" },
98
- }.merge(response_hash)
99
- stub_request(:post, url).with(body: body).to_return(response)
100
- end
101
-
102
- # Stub a POST /v2/content/:content_id/unpublish request
103
- #
104
- # @param content_id [UUID]
105
- # @param params [Hash]
106
- # @param body [String]
107
- def stub_publishing_api_unpublish(content_id, params, response_hash = {})
108
- url = PUBLISHING_API_V2_ENDPOINT + "/content/#{content_id}/unpublish"
109
- response = {
110
- status: 200,
111
- body: "{}",
112
- headers: { "Content-Type" => "application/json; charset=utf-8" },
113
- }.merge(response_hash)
114
- stub_request(:post, url).with(params).to_return(response)
115
- end
116
-
117
- # Stub a POST /v2/content/:content_id/discard-draft request
118
- #
119
- # @param content_id [UUID]
120
- def stub_publishing_api_discard_draft(content_id)
121
- url = PUBLISHING_API_V2_ENDPOINT + "/content/#{content_id}/discard-draft"
122
- stub_request(:post, url).to_return(status: 200, headers: { "Content-Type" => "application/json; charset=utf-8" })
123
- end
124
-
125
- # Stub requests issued when publishing a new draft.
126
- # - PUT /v2/content/:content_id
127
- # - POST /v2/content/:content_id/publish
128
- # - PATCH /v2/links/:content_id
129
- #
130
- # @param body [String]
131
- # @param content_id [UUID]
132
- # @param publish_body [Hash]
133
- def stub_publishing_api_put_content_links_and_publish(body, content_id = nil, publish_body = nil)
134
- content_id ||= body[:content_id]
135
- if publish_body.nil?
136
- publish_body = { update_type: body.fetch(:update_type) }
137
- publish_body[:locale] = body[:locale] if body[:locale]
138
- end
139
- stubs = []
140
- stubs << stub_publishing_api_put_content(content_id, body.except(:links))
141
- stubs << stub_publishing_api_patch_links(content_id, body.slice(:links)) unless body.slice(:links).empty?
142
- stubs << stub_publishing_api_publish(content_id, publish_body)
143
- stubs
144
- end
145
-
146
- # Stub any PUT /v2/content/* request
147
- def stub_any_publishing_api_put_content
148
- stub_request(:put, %r{\A#{PUBLISHING_API_V2_ENDPOINT}/content/})
149
- end
150
-
151
- # Stub any PATCH /v2/links/* request
152
- def stub_any_publishing_api_patch_links
153
- stub_request(:patch, %r{\A#{PUBLISHING_API_V2_ENDPOINT}/links/})
154
- end
155
-
156
- # Stub any POST /v2/content/*/publish request
157
- def stub_any_publishing_api_publish
158
- stub_request(:post, %r{\A#{PUBLISHING_API_V2_ENDPOINT}/content/.*/publish})
159
- end
160
-
161
- # Stub any POST /v2/content/*/publish request
162
- def stub_any_publishing_api_republish
163
- stub_request(:post, %r{\A#{PUBLISHING_API_V2_ENDPOINT}/content/.*/republish})
164
- end
165
-
166
- # Stub any POST /v2/content/*/unpublish request
167
- def stub_any_publishing_api_unpublish
168
- stub_request(:post, %r{\A#{PUBLISHING_API_V2_ENDPOINT}/content/.*/unpublish})
169
- end
170
-
171
- # Stub any POST /v2/content/*/discard-draft request
172
- def stub_any_publishing_api_discard_draft
173
- stub_request(:post, %r{\A#{PUBLISHING_API_V2_ENDPOINT}/content/.*/discard-draft})
174
- end
175
-
176
- # Stub any version 2 request to the publishing API
177
- def stub_any_publishing_api_call
178
- stub_request(:any, %r{\A#{PUBLISHING_API_V2_ENDPOINT}})
179
- end
180
-
181
- # Stub any version 2 request to the publishing API to return a 404 response
182
- def stub_any_publishing_api_call_to_return_not_found
183
- stub_request(:any, %r{\A#{PUBLISHING_API_V2_ENDPOINT}})
184
- .to_return(status: 404, headers: { "Content-Type" => "application/json; charset=utf-8" })
185
- end
186
-
187
- # Stub any version 2 request to the publishing API to return a 503 response
188
- def stub_publishing_api_isnt_available
189
- stub_request(:any, /#{PUBLISHING_API_V2_ENDPOINT}\/.*/).to_return(status: 503)
190
- stub_request(:any, /#{PUBLISHING_API_ENDPOINT}\/.*/).to_return(status: 503)
191
- end
192
-
193
- # Assert that a draft was saved and published, and links were updated.
194
- # - PUT /v2/content/:content_id
195
- # - POST /v2/content/:content_id/publish
196
- # - PATCH /v2/links/:content_id
197
- #
198
- # @param body [String]
199
- # @param content_id [UUID]
200
- # @param publish_body [Hash]
201
- def assert_publishing_api_put_content_links_and_publish(body, content_id = nil, publish_body = nil)
202
- content_id ||= body[:content_id]
203
- if publish_body.nil?
204
- publish_body = { update_type: body.fetch(:update_type) }
205
- publish_body[:locale] = body[:locale] if body[:locale]
206
- end
207
- assert_publishing_api_put_content(content_id, body.except(:links))
208
- assert_publishing_api_patch_links(content_id, body.slice(:links)) unless body.slice(:links).empty?
209
- assert_publishing_api_publish(content_id, publish_body)
210
- end
211
-
212
- # Assert that content was saved (PUT /v2/content/:content_id)
213
- #
214
- # @param content_id [UUID]
215
- # @param attributes_or_matcher [Object]
216
- # @param times [Integer]
217
- def assert_publishing_api_put_content(content_id, attributes_or_matcher = nil, times = 1)
218
- url = PUBLISHING_API_V2_ENDPOINT + "/content/" + content_id
219
- assert_publishing_api(:put, url, attributes_or_matcher, times)
220
- end
221
-
222
- # Assert that content was published (POST /v2/content/:content_id/publish)
223
- #
224
- # @param content_id [UUID]
225
- # @param attributes_or_matcher [Object]
226
- # @param times [Integer]
227
- def assert_publishing_api_publish(content_id, attributes_or_matcher = nil, times = 1)
228
- url = PUBLISHING_API_V2_ENDPOINT + "/content/#{content_id}/publish"
229
- assert_publishing_api(:post, url, attributes_or_matcher, times)
230
- end
231
-
232
- # Assert that content was unpublished (POST /v2/content/:content_id/unpublish)
233
- #
234
- # @param content_id [UUID]
235
- # @param attributes_or_matcher [Object]
236
- # @param times [Integer]
237
- def assert_publishing_api_unpublish(content_id, attributes_or_matcher = nil, times = 1)
238
- url = PUBLISHING_API_V2_ENDPOINT + "/content/#{content_id}/unpublish"
239
- assert_publishing_api(:post, url, attributes_or_matcher, times)
240
- end
241
-
242
- # Assert that links were updated (PATCH /v2/links/:content_id)
243
- #
244
- # @param content_id [UUID]
245
- # @param attributes_or_matcher [Object]
246
- # @param times [Integer]
247
- def assert_publishing_api_patch_links(content_id, attributes_or_matcher = nil, times = 1)
248
- url = PUBLISHING_API_V2_ENDPOINT + "/links/" + content_id
249
- assert_publishing_api(:patch, url, attributes_or_matcher, times)
250
- end
251
-
252
- # Assert that a draft was discarded (POST /v2/content/:content_id/discard-draft)
253
- #
254
- # @param content_id [UUID]
255
- # @param attributes_or_matcher [Object]
256
- # @param times [Integer]
257
- def assert_publishing_api_discard_draft(content_id, attributes_or_matcher = nil, times = 1)
258
- url = PUBLISHING_API_V2_ENDPOINT + "/content/#{content_id}/discard-draft"
259
- assert_publishing_api(:post, url, attributes_or_matcher, times)
260
- end
261
-
262
- # Assert that a request was made to the publishing API
263
- #
264
- # @param verb [String]
265
- # @param url [String]
266
- # @param attributes_or_matcher [Object]
267
- # @param times [Integer]
268
- def assert_publishing_api(verb, url, attributes_or_matcher = nil, times = 1)
269
- if attributes_or_matcher.is_a?(Hash)
270
- matcher = request_json_matches(attributes_or_matcher)
271
- else
272
- matcher = attributes_or_matcher
273
- end
274
-
275
- if matcher
276
- assert_requested(verb, url, times: times, &matcher)
277
- else
278
- assert_requested(verb, url, times: times)
279
- end
280
- end
281
-
282
- # Get a request matcher that checks if a JSON request includes a set of attributes
283
- def request_json_includes(required_attributes)
284
- ->(request) do
285
- data = JSON.parse(request.body)
286
- deep_stringify_keys(required_attributes).
287
- to_a.all? { |key, value| data[key] == value }
288
- end
289
- end
290
-
291
- # Get a request matcher that checks if a JSON request matches a hash
292
- def request_json_matches(required_attributes)
293
- ->(request) do
294
- data = JSON.parse(request.body)
295
- deep_stringify_keys(required_attributes) == data
296
- end
297
- end
298
-
299
- # Stub GET /v2/content/ to return a set of content items
300
- #
301
- # @example
302
- #
303
- # stub_publishing_api_has_content(
304
- # vehicle_recalls_and_faults, # this is a variable containing an array of content items
305
- # document_type: described_class.publishing_api_document_type, #example of a document_type: "vehicle_recalls_and_faults_alert"
306
- # fields: fields, #example: let(:fields) { %i[base_path content_id public_updated_at title publication_state] }
307
- # page: 1,
308
- # per_page: 50
309
- # )
310
- # @param items [Array]
311
- # @param params [Hash]
312
- def stub_publishing_api_has_content(items, params = {})
313
- url = PUBLISHING_API_V2_ENDPOINT + "/content"
314
-
315
- if params.respond_to? :fetch
316
- per_page = params.fetch(:per_page, 50)
317
- page = params.fetch(:page, 1)
318
- else
319
- per_page = 50
320
- page = 1
321
- end
322
-
323
- start_position = (page - 1) * per_page
324
- page_items = items.slice(start_position, per_page) || []
325
-
326
- number_of_pages =
327
- if items.count < per_page
328
- 1
329
- else
330
- (items.count / per_page.to_f).ceil
331
- end
332
-
333
- body = {
334
- results: page_items,
335
- total: items.count,
336
- pages: number_of_pages,
337
- current_page: page,
338
- }
339
-
340
- stub_request(:get, url)
341
- .with(query: params)
342
- .to_return(status: 200, body: body.to_json, headers: {})
343
- end
344
-
345
- # This method has been refactored into publishing_api_has_content (above)
346
- # publishing_api_has_content allows for flexible passing in of arguments, please use instead
347
- def stub_publishing_api_has_fields_for_document(document_type, items, fields)
348
- body = Array(items).map { |item|
349
- deep_stringify_keys(item).slice(*fields)
350
- }
351
-
352
- query_params = fields.map { |f|
353
- "&fields%5B%5D=#{f}"
354
- }
355
-
356
- url = PUBLISHING_API_V2_ENDPOINT + "/content?document_type=#{document_type}#{query_params.join('')}"
357
-
358
- stub_request(:get, url).to_return(status: 200, body: { results: body }.to_json, headers: {})
359
- end
360
-
361
- # Stub GET /v2/linkables to return a set of content items with a specific document type
362
- #
363
- # @param linkables [Array]
364
- def stub_publishing_api_has_linkables(linkables, document_type:)
365
- url = PUBLISHING_API_V2_ENDPOINT + "/linkables?document_type=#{document_type}"
366
- stub_request(:get, url).to_return(status: 200, body: linkables.to_json, headers: {})
367
- end
368
-
369
- # Stub GET /v2/content/:content_id to return a specific content item hash
370
- #
371
- # @param item [Hash]
372
- def stub_publishing_api_has_item(item, params = {})
373
- item = deep_transform_keys(item, &:to_sym)
374
- url = PUBLISHING_API_V2_ENDPOINT + "/content/" + item[:content_id]
375
- stub_request(:get, url)
376
- .with(query: hash_including(params))
377
- .to_return(status: 200, body: item.to_json, headers: {})
378
- end
379
-
380
- # Stub GET /v2/content/:content_id to progress through a series of responses.
381
- #
382
- # @param items [Array]
383
- def stub_publishing_api_has_item_in_sequence(content_id, items)
384
- items = items.each { |item| deep_transform_keys(item, &:to_sym) }
385
- url = PUBLISHING_API_V2_ENDPOINT + "/content/" + content_id
386
- calls = -1
387
-
388
- stub_request(:get, url).to_return do |_request|
389
- calls += 1
390
- item = items[calls] || items.last
391
-
392
- { status: 200, body: item.to_json, headers: {} }
393
- end
394
- end
395
-
396
- # Stub GET /v2/content/:content_id to return a 404 response
397
- #
398
- # @param content_id [UUID]
399
- def stub_publishing_api_does_not_have_item(content_id)
400
- url = PUBLISHING_API_V2_ENDPOINT + "/content/" + content_id
401
- stub_request(:get, url).to_return(status: 404, body: resource_not_found(content_id, "content item").to_json, headers: {})
402
- end
403
-
404
- # Stub a request to links endpoint
405
- #
406
- # @param [Hash] links the structure of the links hash
407
- #
408
- # @example
409
- #
410
- # stub_publishing_api_has_links(
411
- # {
412
- # "content_id" => "64aadc14-9bca-40d9-abb6-4f21f9792a05",
413
- # "links" => {
414
- # "mainstream_browse_pages" => ["df2e7a3e-2078-45de-a75a-fd37d027427e"],
415
- # "parent" => ["df2e7a3e-2078-45de-a75a-fd37d027427e"],
416
- # "organisations" => ["569a9ee5-c195-4b7f-b9dc-edc17a09113f", "5c54ae52-341b-499e-a6dd-67f04633b8cf"]
417
- # },
418
- # "version" => 6
419
- # }
420
- # )
421
- #
422
- # @example
423
- #
424
- # Services.publishing_api.get_links("64aadc14-9bca-40d9-abb6-4f21f9792a05")
425
- # => {
426
- # "content_id" => "64aadc14-9bca-40d9-abb6-4f21f9792a05",
427
- # "links" => {
428
- # "mainstream_browse_pages" => ["df2e7a3e-2078-45de-a75a-fd37d027427e"],
429
- # "parent" => ["df2e7a3e-2078-45de-a75a-fd37d027427e"],
430
- # "organisations" => ["569a9ee5-c195-4b7f-b9dc-edc17a09113f", "5c54ae52-341b-499e-a6dd-67f04633b8cf"]
431
- # },
432
- # "version" => 6
433
- # }
434
- def stub_publishing_api_has_links(links)
435
- links = deep_transform_keys(links, &:to_sym)
436
- url = PUBLISHING_API_V2_ENDPOINT + "/links/" + links[:content_id]
437
- stub_request(:get, url).to_return(status: 200, body: links.to_json, headers: {})
438
- end
439
-
440
- # Stub a request to the expanded links endpoint
441
- #
442
- # @param [Hash] links the structure of the links hash
443
- #
444
- # @example
445
- # stub_publishing_api_has_expanded_links(
446
- # {
447
- # "content_id" => "64aadc14-9bca-40d9-abb4-4f21f9792a05",
448
- # "expanded_links" => {
449
- # "mainstream_browse_pages" => [
450
- # {
451
- # "content_id" => "df2e7a3e-2078-45de-a76a-fd37d027427a",
452
- # "base_path" => "/a/base/path",
453
- # "document_type" => "mainstream_browse_page",
454
- # "locale" => "en",
455
- # "links" => {},
456
- # # ...
457
- # }
458
- # ],
459
- # "parent" => [
460
- # {
461
- # "content_id" => "df2e7a3e-2028-45de-a75a-fd37d027427e",
462
- # "document_type" => "mainstream_browse_page",
463
- # # ...
464
- # },
465
- # ]
466
- # }
467
- # }
468
- # )
469
- #
470
- # @example
471
- # Services.publishing_api.expanded_links("64aadc14-9bca-40d9-abb4-4f21f9792a05")
472
- # => {
473
- # "content_id" => "64aadc14-9bca-40d9-abb4-4f21f9792a05",
474
- # "expanded_links" => {
475
- # "mainstream_browse_pages" => [
476
- # {
477
- # "content_id" => "df2e7a3e-2078-45de-a76a-fd37d027427a",
478
- # "base_path" => "/a/base/path",
479
- # "document_type" => "mainstream_browse_page",
480
- # "locale" => "en",
481
- # "links" => {},
482
- # ...
483
- # }
484
- # ],
485
- # "parent" => [
486
- # {
487
- # "content_id" => "df2e7a3e-2028-45de-a75a-fd37d027427e",
488
- # "document_type" => "mainstream_browse_page",
489
- # ...
490
- # },
491
- # ]
492
- # }
493
- # }
494
- def stub_publishing_api_has_expanded_links(links, with_drafts: true, generate: false)
495
- links = deep_transform_keys(links, &:to_sym)
496
- request_params = {}
497
- request_params["with_drafts"] = false if !with_drafts
498
- request_params["generate"] = true if generate
499
-
500
- url = PUBLISHING_API_V2_ENDPOINT + "/expanded-links/" + links[:content_id]
501
- stub_request(:get, url)
502
- .with(query: request_params)
503
- .to_return(status: 200, body: links.to_json, headers: {})
504
- end
505
-
506
- # Stub a request to get links for content ids
507
- #
508
- # @param [Hash] links the links for each content id
509
- #
510
- # @example
511
- # stub_publishing_api_has_links_for_content_ids(
512
- # { "2878337b-bed9-4e7f-85b6-10ed2cbcd504" => {
513
- # "links" => { "taxons" => ["eb6965c7-3056-45d0-ae50-2f0a5e2e0854"] }
514
- # },
515
- # "eec13cea-219d-4896-9c97-60114da23559" => {
516
- # "links" => {}
517
- # }
518
- # }
519
- # )
520
- #
521
- # @example
522
- # Services.publishing_api.get_links_for_content_ids(["2878337b-bed9-4e7f-85b6-10ed2cbcd504"])
523
- # => {
524
- # "2878337b-bed9-4e7f-85b6-10ed2cbcd504" => {
525
- # "links" => [
526
- # "eb6965c7-3056-45d0-ae50-2f0a5e2e0854"
527
- # ]
528
- # }
529
- # }
530
- def stub_publishing_api_has_links_for_content_ids(links)
531
- url = PUBLISHING_API_V2_ENDPOINT + "/links/by-content-id"
532
- stub_request(:post, url).with(body: { content_ids: links.keys }).to_return(status: 200, body: links.to_json, headers: {})
533
- end
534
-
535
- # Stub GET /v2/links/:content_id to return a 404 response
536
- #
537
- # @param content_id [UUID]
538
- def stub_publishing_api_does_not_have_links(content_id)
539
- url = PUBLISHING_API_V2_ENDPOINT + "/links/" + content_id
540
- stub_request(:get, url).to_return(status: 404, body: resource_not_found(content_id, "link set").to_json, headers: {})
541
- end
542
-
543
- # Stub calls to the lookups endpoint
544
- #
545
- # @param lookup_hash [Hash] Hash with base_path as key, content_id as value.
546
- #
547
- # @example
548
- #
549
- # stub_publishing_api_has_lookups({
550
- # "/foo" => "51ac4247-fd92-470a-a207-6b852a97f2db",
551
- # "/bar" => "261bd281-f16c-48d5-82d2-9544019ad9ca"
552
- # })
553
- #
554
- def stub_publishing_api_has_lookups(lookup_hash)
555
- url = Plek.current.find("publishing-api") + "/lookup-by-base-path"
556
- stub_request(:post, url).to_return(body: lookup_hash.to_json)
557
- end
558
-
559
- #
560
- # Stub calls to the get linked items endpoint
561
- #
562
- # @param items [Array] The linked items we wish to return
563
- # @param params [Hash] A hash of parameters
564
- #
565
- # @example
566
- #
567
- # stub_publishing_api_has_linked_items(
568
- # [ item_1, item_2 ],
569
- # {
570
- # content_id: "51ac4247-fd92-470a-a207-6b852a97f2db",
571
- # link_type: "taxons",
572
- # fields: ["title", "description", "base_path"]
573
- # }
574
- # )
575
- #
576
- def stub_publishing_api_has_linked_items(items, params = {})
577
- content_id = params.fetch(:content_id)
578
- link_type = params.fetch(:link_type)
579
- fields = params.fetch(:fields, %w(base_path content_id document_type title))
580
-
581
- url = Plek.current.find("publishing-api") + "/v2/linked/#{content_id}"
582
-
583
- request_parmeters = {
584
- "fields" => fields,
585
- "link_type" => link_type,
586
- }
587
-
588
- stub_request(:get, url)
589
- .with(query: request_parmeters)
590
- .and_return(
591
- body: items.to_json,
592
- status: 200,
593
- )
594
- end
595
-
596
- # Stub GET /v2/editions to return a set of editions
597
- #
598
- # @example
599
- #
600
- # stub_publishing_api_get_editions(
601
- # vehicle_recalls_and_faults, # this is a variable containing an array of editions
602
- # fields: fields, #example: let(:fields) { %i[base_path content_id public_updated_at title publication_state] }
603
- # per_page: 50
604
- # )
605
- # @param items [Array]
606
- # @param params [Hash]
607
- def stub_publishing_api_get_editions(editions, params = {})
608
- url = PUBLISHING_API_V2_ENDPOINT + "/editions"
609
-
610
- results = editions.map do |edition|
611
- next edition unless params[:fields]
612
-
613
- edition.select { |k| params[:fields].include?(k) }
614
- end
615
-
616
- per_page = (params[:per_page] || 100).to_i
617
- results = results.take(per_page)
618
-
619
- body = {
620
- results: results,
621
- links: [
622
- { rel: "self", href: "#{PUBLISHING_API_V2_ENDPOINT}/editions" },
623
- ],
624
- }
625
-
626
- stub_request(:get, url)
627
- .with(query: params)
628
- .to_return(status: 200, body: body.to_json, headers: {})
629
- end
630
-
631
- # Aliases for DEPRECATED methods
632
- alias_method :publishing_api_isnt_available, :stub_publishing_api_isnt_available
633
- alias_method :publishing_api_has_content, :stub_publishing_api_has_content
634
- alias_method :publishing_api_has_fields_for_document, :stub_publishing_api_has_fields_for_document
635
- alias_method :publishing_api_has_linkables, :stub_publishing_api_has_linkables
636
- alias_method :publishing_api_has_item, :stub_publishing_api_has_item
637
- alias_method :publishing_api_has_item_in_sequence, :stub_publishing_api_has_item_in_sequence
638
- alias_method :publishing_api_does_not_have_item, :stub_publishing_api_does_not_have_item
639
- alias_method :publishing_api_has_links, :stub_publishing_api_has_links
640
- alias_method :publishing_api_has_expanded_links, :stub_publishing_api_has_expanded_links
641
- alias_method :publishing_api_has_links_for_content_ids, :stub_publishing_api_has_links_for_content_ids
642
- alias_method :publishing_api_does_not_have_links, :stub_publishing_api_does_not_have_links
643
- alias_method :publishing_api_has_lookups, :stub_publishing_api_has_lookups
644
- alias_method :publishing_api_has_linked_items, :stub_publishing_api_has_linked_items
645
- alias_method :publishing_api_get_editions, :stub_publishing_api_get_editions
646
-
647
- private
648
-
649
- def stub_publishing_api_put(*args)
650
- stub_publishing_api_postlike_call(:put, *args)
651
- end
652
-
653
- def stub_publishing_api_patch(*args)
654
- stub_publishing_api_postlike_call(:patch, *args)
655
- end
656
-
657
- def stub_publishing_api_postlike_call(method, content_id, body, resource_path, override_response_hash = {})
658
- response_hash = { status: 200, body: "{}", headers: { "Content-Type" => "application/json; charset=utf-8" } }
659
- response_hash.merge!(override_response_hash)
660
- response_hash[:body] = response_hash[:body].to_json if response_hash[:body].is_a?(Hash)
661
- url = PUBLISHING_API_V2_ENDPOINT + resource_path + "/" + content_id
662
- stub_request(method, url).with(body: body).to_return(response_hash)
663
- end
664
-
665
- def deep_stringify_keys(hash)
666
- deep_transform_keys(hash, &:to_s)
667
- end
668
-
669
- def deep_transform_keys(object, &block)
670
- case object
671
- when Hash
672
- object.each_with_object({}) do |(key, value), result|
673
- result[yield(key)] = deep_transform_keys(value, &block)
674
- end
675
- when Array
676
- object.map { |item| deep_transform_keys(item, &block) }
677
- else
678
- object
679
- end
680
- end
681
-
682
- def resource_not_found(content_id, type)
683
- {
684
- error: {
685
- code: 404,
686
- message: "Could not find #{type} with content_id: #{content_id}",
687
- },
688
- }
689
- end
690
-
691
- def version_conflict(expected_version, actual_version = expected_version + 1)
692
- {
693
- error: {
694
- code: 409,
695
- message: "A lock-version conflict occurred. The `previous_version` you've sent (#{expected_version}) is not the same as the current lock version of the edition (#{actual_version}).",
696
- fields: { previous_version: ["does not match"] },
697
- },
698
- }
11
+ def self.included(_base)
12
+ warn "GdsApi::TestHelpers::PublishingApiV2 is deprecated. Use GdsApi::TestHelpers::PublishingApi instead."
699
13
  end
700
14
  end
701
15
  end