Module: GdsApi::TestHelpers::PublishingApiV2
Constant Summary
collapse
- PUBLISHING_API_V2_ENDPOINT =
Plek.current.find('publishing-api') + '/v2'
Instance Method Summary
collapse
-
#assert_publishing_api(verb, url, attributes_or_matcher = nil, times = 1) ⇒ Object
-
#assert_publishing_api_discard_draft(content_id, attributes_or_matcher = nil, times = 1) ⇒ Object
-
#assert_publishing_api_patch_links(content_id, attributes_or_matcher = nil, times = 1) ⇒ Object
-
#assert_publishing_api_publish(content_id, attributes_or_matcher = nil, times = 1) ⇒ Object
-
#assert_publishing_api_put_content(content_id, attributes_or_matcher = nil, times = 1) ⇒ Object
-
#assert_publishing_api_put_content_links_and_publish(body, content_id = nil, publish_body = nil) ⇒ Object
-
#publishing_api_does_not_have_item(content_id) ⇒ Object
-
#publishing_api_does_not_have_links(content_id) ⇒ Object
-
#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 ).
-
#publishing_api_has_expanded_links(links) ⇒ Object
-
#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.
-
#publishing_api_has_item(item) ⇒ Object
-
#publishing_api_has_linkables(linkables, document_type:) ⇒ Object
-
#publishing_api_has_links(links) ⇒ Object
-
#publishing_api_has_lookups(lookup_hash) ⇒ Object
Stub calls to the lookups endpoint.
-
#publishing_api_isnt_available ⇒ Object
-
#request_json_includes(required_attributes) ⇒ Object
-
#request_json_matches(required_attributes) ⇒ Object
-
#stub_any_publishing_api_call ⇒ Object
-
#stub_any_publishing_api_call_to_return_not_found ⇒ Object
-
#stub_any_publishing_api_patch_links ⇒ Object
-
#stub_any_publishing_api_put_content ⇒ Object
-
#stub_publishing_api_discard_draft(content_id) ⇒ Object
-
#stub_publishing_api_patch_links(content_id, body) ⇒ Object
-
#stub_publishing_api_publish(content_id, body, response_hash = {}) ⇒ Object
-
#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.
-
#stub_publishing_api_put_content_links_and_publish(body, content_id = nil, publish_body = nil) ⇒ Object
-
#stub_publishing_api_unpublish(content_id, params, response_hash = {}) ⇒ Object
#content_item_for_base_path, #titleize_base_path
Instance Method Details
#assert_publishing_api(verb, url, attributes_or_matcher = nil, times = 1) ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 123
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
118
119
120
121
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 118
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
113
114
115
116
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 113
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
108
109
110
111
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 108
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
103
104
105
106
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 103
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
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 92
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
193
194
195
196
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 193
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
|
#publishing_api_does_not_have_links(content_id) ⇒ Object
209
210
211
212
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 209
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,
document_type: described_class.publishing_api_document_type,
fields: fields,
page: 1,
per_page: 50
)
161
162
163
164
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 161
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_expanded_links(links) ⇒ Object
204
205
206
207
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 204
def publishing_api_has_expanded_links(links)
url = PUBLISHING_API_V2_ENDPOINT + "/expanded-links/" + links[:content_id]
stub_request(:get, url).to_return(status: 200, body: links.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
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 168
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
187
188
189
190
191
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 187
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
182
183
184
185
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 182
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
|
#publishing_api_has_links(links) ⇒ Object
198
199
200
201
202
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 198
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
225
226
227
228
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 225
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_available ⇒ Object
88
89
90
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 88
def publishing_api_isnt_available
stub_request(:any, /#{PUBLISHING_API_V2_ENDPOINT}\/.*/).to_return(status: 503)
end
|
#request_json_includes(required_attributes) ⇒ Object
137
138
139
140
141
142
143
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 137
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
145
146
147
148
149
150
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 145
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_call ⇒ Object
79
80
81
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 79
def stub_any_publishing_api_call
stub_request(:any, %r{\A#{PUBLISHING_API_V2_ENDPOINT}})
end
|
#stub_any_publishing_api_call_to_return_not_found ⇒ Object
83
84
85
86
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 83
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_patch_links ⇒ Object
75
76
77
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 75
def stub_any_publishing_api_patch_links
stub_request(:patch, %r{\A#{PUBLISHING_API_V2_ENDPOINT}/links/})
end
|
#stub_any_publishing_api_put_content ⇒ Object
71
72
73
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 71
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
53
54
55
56
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 53
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_patch_links(content_id, body) ⇒ Object
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
|
#stub_publishing_api_put_content_links_and_publish(body, content_id = nil, publish_body = nil) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 58
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_unpublish(content_id, params, response_hash = {}) ⇒ Object
43
44
45
46
47
48
49
50
51
|
# File 'lib/gds_api/test_helpers/publishing_api_v2.rb', line 43
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
|