Module: GdsApi::TestHelpers::PublishingApiV2

Includes:
ContentItemHelpers
Defined in:
lib/gds_api/test_helpers/publishing_api_v2.rb

Constant Summary collapse

PUBLISHING_API_V2_ENDPOINT =
Plek.current.find('publishing-api') + '/v2'

Instance Method Summary collapse

Methods included from ContentItemHelpers

#content_item_for_base_path, #titleize_base_path

Instance Method Details

#assert_publishing_api(verb, url, attributes_or_matcher = nil, times = 1) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 113

def assert_publishing_api(verb, url, attributes_or_matcher = nil, times = 1)
  if attributes_or_matcher.is_a?(Hash)
    matcher = request_json_matches(attributes_or_matcher)
  else
    matcher = attributes_or_matcher
  end

  if matcher
    assert_requested(verb, url, times: times, &matcher)
  else
    assert_requested(verb, url, times: times)
  end
end

#assert_publishing_api_discard_draft(content_id, attributes_or_matcher = nil, times = 1) ⇒ Object



108
109
110
111
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 108

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


103
104
105
106
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 103

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



98
99
100
101
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 98

def assert_publishing_api_publish(content_id, attributes_or_matcher = nil, times = 1)
  url = PUBLISHING_API_V2_ENDPOINT + "/content/#{content_id}/publish"
  assert_publishing_api(:post, url, attributes_or_matcher, times)
end

#assert_publishing_api_put_content(content_id, attributes_or_matcher = nil, times = 1) ⇒ Object



93
94
95
96
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 93

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


82
83
84
85
86
87
88
89
90
91
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 82

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

#publishing_api_does_not_have_item(content_id) ⇒ Object



183
184
185
186
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 183

def publishing_api_does_not_have_item(content_id)
  url = PUBLISHING_API_V2_ENDPOINT + "/content/" + content_id
  stub_request(:get, url).to_return(status: 404, body: resource_not_found(content_id, "content item").to_json, headers: {})
end


194
195
196
197
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 194

def publishing_api_does_not_have_links(content_id)
  url = PUBLISHING_API_V2_ENDPOINT + "/links/" + content_id
  stub_request(:get, url).to_return(status: 404, body: resource_not_found(content_id, "link set").to_json, headers: {})
end

#publishing_api_has_content(items, params = {}) ⇒ Object

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

)



151
152
153
154
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 151

def publishing_api_has_content(items, params = {})
  url = PUBLISHING_API_V2_ENDPOINT + "/content"
  stub_request(:get, url).with(:query => params).to_return(status: 200, body: { results: items }.to_json, headers: {})
end

#publishing_api_has_fields_for_document(format, 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



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 158

def publishing_api_has_fields_for_document(format, items, fields)
  body = Array(items).map { |item|
    item.with_indifferent_access.slice(*fields)
  }

  query_params = fields.map { |f|
    "&fields%5B%5D=#{f}"
  }

  url = PUBLISHING_API_V2_ENDPOINT + "/content?document_type=#{format}#{query_params.join('')}"

  stub_request(:get, url).to_return(:status => 200, :body => { results: body }.to_json, :headers => {})
end

#publishing_api_has_item(item) ⇒ Object



177
178
179
180
181
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 177

def publishing_api_has_item(item)
  item = item.with_indifferent_access
  url = PUBLISHING_API_V2_ENDPOINT + "/content/" + item[:content_id]
  stub_request(:get, url).to_return(status: 200, body: item.to_json, headers: {})
end

#publishing_api_has_linkables(linkables, document_type:) ⇒ Object



172
173
174
175
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 172

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


188
189
190
191
192
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 188

def publishing_api_has_links(links)
  links = links.with_indifferent_access
  url = PUBLISHING_API_V2_ENDPOINT + "/links/" + links[:content_id]
  stub_request(:get, url).to_return(status: 200, body: links.to_json, headers: {})
end

#publishing_api_has_lookups(lookup_hash) ⇒ Object

Stub calls to the lookups endpoint

Examples:


publishing_api_has_lookups({
  "/foo" => "51ac4247-fd92-470a-a207-6b852a97f2db",
  "/bar" => "261bd281-f16c-48d5-82d2-9544019ad9ca"
})

Parameters:

  • lookup_hash (Hash)

    Hash with base_path as key, content_id as value.



210
211
212
213
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 210

def publishing_api_has_lookups(lookup_hash)
  url = Plek.current.find('publishing-api') + '/lookup-by-base-path'
  stub_request(:post, url).to_return(body: lookup_hash.to_json)
end

#publishing_api_isnt_availableObject



78
79
80
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 78

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

#request_json_includes(required_attributes) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 127

def request_json_includes(required_attributes)
  ->(request) do
    data = JSON.parse(request.body)
    deep_stringify_keys(required_attributes).
      to_a.all? { |key, value| data[key] == value }
  end
end

#request_json_matches(required_attributes) ⇒ Object



135
136
137
138
139
140
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 135

def request_json_matches(required_attributes)
  ->(request) do
    data = JSON.parse(request.body)
    deep_stringify_keys(required_attributes) == data
  end
end

#stub_any_publishing_api_callObject



69
70
71
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 69

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

#stub_any_publishing_api_call_to_return_not_foundObject



73
74
75
76
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 73

def stub_any_publishing_api_call_to_return_not_found
  stub_request(:any, %r{\A#{PUBLISHING_API_V2_ENDPOINT}})
    .to_return(status: 404, headers: {"Content-Type" => "application/json; charset=utf-8"})
end


65
66
67
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 65

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

#stub_any_publishing_api_put_contentObject



61
62
63
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 61

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

#stub_publishing_api_discard_draft(content_id) ⇒ Object



43
44
45
46
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 43

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


29
30
31
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 29

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

#stub_publishing_api_publish(content_id, body, response_hash = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 33

def stub_publishing_api_publish(content_id, body, response_hash = {})
  url = PUBLISHING_API_V2_ENDPOINT + "/content/#{content_id}/publish"
  response = {
    status: 200,
    body: '{}',
    headers: {"Content-Type" => "application/json; charset=utf-8"}
  }.merge(response_hash)
  stub_request(:post, url).with(body: body).to_return(response)
end

#stub_publishing_api_put_content(content_id, body, response_hash = {}) ⇒ Object

stubs 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.

e.g. The following two examples are equivalent:

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

  • stub_publishing_api_put_content(my_content_id, my_request_body, { status: 201, body: 33 })



25
26
27
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 25

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


48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 48

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