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



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 108

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 = {}, times = 1) ⇒ Object



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

def assert_publishing_api_discard_draft(content_id, attributes_or_matcher = {}, 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_publish(content_id, attributes_or_matcher = {}, times = 1) ⇒ Object



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

def assert_publishing_api_publish(content_id, attributes_or_matcher = {}, 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 = {}, times = 1) ⇒ Object



88
89
90
91
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 88

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


77
78
79
80
81
82
83
84
85
86
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 77

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_put_links(content_id, body.slice(:links)) unless body.slice(:links).empty?
  assert_publishing_api_publish(content_id, publish_body)
end


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

def assert_publishing_api_put_links(content_id, attributes_or_matcher = {}, times = 1)
  url = PUBLISHING_API_V2_ENDPOINT + "/links/" + content_id
  assert_publishing_api(:put, url, attributes_or_matcher, times)
end

#publishing_api_has_fields_for_format(format, items, fields) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 137

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

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

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

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

#publishing_api_has_item(item) ⇒ Object



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

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_isnt_availableObject



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

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

#request_json_includes(required_attributes) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 122

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



130
131
132
133
134
135
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 130

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



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

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



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

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

#stub_any_publishing_api_put_contentObject



56
57
58
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 56

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


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

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

#stub_publishing_api_discard_draft(content_id) ⇒ Object



38
39
40
41
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 38

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_publish(content_id, body) ⇒ Object



33
34
35
36
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 33

def stub_publishing_api_publish(content_id, body)
  url = PUBLISHING_API_V2_ENDPOINT + "/content/#{content_id}/publish"
  stub_request(:post, url).with(body: body).to_return(status: 200, body: '{}', headers: {"Content-Type" => "application/json; charset=utf-8"})
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


43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 43

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_put_links(content_id, body.slice(:links)) unless body.slice(:links).empty?
  stubs << stub_publishing_api_publish(content_id, publish_body)
  stubs
end


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

def stub_publishing_api_put_links(content_id, body)
  stub_publishing_api_put(content_id, body, '/links')
end