gds-api-adapters 4.0.0 → 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  require 'gds_api/test_helpers/json_client_helper'
2
2
  require 'cgi'
3
+ require_relative 'content_api/artefact_stub'
3
4
 
4
5
  module GdsApi
5
6
  module TestHelpers
@@ -78,18 +79,21 @@ module GdsApi
78
79
  end
79
80
 
80
81
  def content_api_has_an_artefact(slug, body = artefact_for_slug(slug))
81
- url = "#{CONTENT_API_ENDPOINT}/#{slug}.json"
82
- stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
82
+ ArtefactStub.new(slug).with_response_body(body).stub
83
83
  end
84
84
 
85
85
  def content_api_has_unpublished_artefact(slug, edition, body = artefact_for_slug(slug))
86
- url = "#{CONTENT_API_ENDPOINT}/#{slug}.json?edition=#{edition}"
87
- stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
86
+ ArtefactStub.new(slug)
87
+ .with_response_body(body)
88
+ .with_query_parameters(edition: edition)
89
+ .stub
88
90
  end
89
91
 
90
92
  def content_api_has_an_artefact_with_snac_code(slug, snac, body = artefact_for_slug(slug))
91
- url = "#{CONTENT_API_ENDPOINT}/#{slug}.json?snac=#{snac}"
92
- stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
93
+ ArtefactStub.new(slug)
94
+ .with_response_body(body)
95
+ .with_query_parameters(snac: snac)
96
+ .stub
93
97
  end
94
98
 
95
99
  def content_api_does_not_have_an_artefact(slug)
@@ -98,8 +102,10 @@ module GdsApi
98
102
  "status" => "not found"
99
103
  }
100
104
  }
101
- url = "#{CONTENT_API_ENDPOINT}/#{slug}.json"
102
- stub_request(:get, url).to_return(status: 404, body: body.to_json, headers: {})
105
+ ArtefactStub.new(slug)
106
+ .with_response_body(body)
107
+ .with_response_status(404)
108
+ .stub
103
109
  end
104
110
 
105
111
  def content_api_has_an_archived_artefact(slug)
@@ -109,10 +115,13 @@ module GdsApi
109
115
  "status_message" => "This item is no longer available"
110
116
  }
111
117
  }
112
- url = "#{CONTENT_API_ENDPOINT}/#{slug}.json"
113
- stub_request(:get, url).to_return(status: 410, body: body.to_json, headers: {})
118
+ ArtefactStub.new(slug)
119
+ .with_response_body(body)
120
+ .with_response_status(410)
121
+ .stub
114
122
  end
115
123
 
124
+ # Stub requests, and then dynamically generate a response based on the slug in the request
116
125
  def stub_content_api_default_artefact
117
126
  stub_request(:get, %r{\A#{CONTENT_API_ENDPOINT}/[a-z0-9-]+\.json}).to_return { |request|
118
127
  slug = request.uri.path.split('/').last.chomp('.json')
@@ -0,0 +1,55 @@
1
+ module GdsApi
2
+ module TestHelpers
3
+ module ContentApi
4
+ class ArtefactStub
5
+ include WebMock::API
6
+ # This is ugly, but the nicest way we found to get access to artefact_for_slug
7
+ include GdsApi::TestHelpers::ContentApi
8
+
9
+ attr_accessor :slug, :query_parameters, :response_body, :response_status
10
+
11
+ def initialize(slug)
12
+ @slug = slug
13
+ @query_parameters = {}
14
+ @response_body = artefact_for_slug(slug)
15
+ @response_status = 200
16
+ end
17
+
18
+ def with_query_parameters(hash)
19
+ @query_parameters = hash
20
+ self
21
+ end
22
+
23
+ def with_response_body(response_body)
24
+ @response_body = response_body
25
+ self
26
+ end
27
+
28
+ def with_response_status(response_status)
29
+ @response_status = response_status
30
+ self
31
+ end
32
+
33
+ # Nothing is stubbed until this is called
34
+ def stub
35
+ stub_request(:get, url_without_query)
36
+ .with(query: hash_including(comparable_query_params))
37
+ .to_return(status: @response_status, body: @response_body.to_json)
38
+ end
39
+
40
+ private
41
+ def url_without_query
42
+ "#{CONTENT_API_ENDPOINT}/#{slug}.json"
43
+ end
44
+
45
+ # Ensure that all keys and values are strings
46
+ # because Webmock doesn't handle symbols
47
+ def comparable_query_params
48
+ @query_parameters.each_with_object({}) do |(k,v),hash|
49
+ hash[k.to_s] = v.nil? ? v : v.to_s
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '4.0.0'
2
+ VERSION = '4.1.0'
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: gds-api-adapters
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 4.0.0
5
+ version: 4.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - James Stewart
@@ -166,40 +166,41 @@ extra_rdoc_files: []
166
166
 
167
167
  files:
168
168
  - lib/gds_api/version.rb
169
- - lib/gds_api/typhoeus_client.rb
170
- - lib/gds_api/base.rb
171
- - lib/gds_api/licence_application.rb
169
+ - lib/gds_api/publisher.rb
172
170
  - lib/gds_api/panopticon/registerer.rb
173
- - lib/gds_api/needotron.rb
174
- - lib/gds_api/imminence.rb
175
- - lib/gds_api/rummager.rb
171
+ - lib/gds_api/typhoeus_client.rb
176
172
  - lib/gds_api/content_api.rb
177
- - lib/gds_api/exceptions.rb
178
- - lib/gds_api/json_client.rb
179
- - lib/gds_api/core-ext/openstruct.rb
180
- - lib/gds_api/response.rb
181
- - lib/gds_api/publisher.rb
182
- - lib/gds_api/test_helpers/licence_application.rb
183
- - lib/gds_api/test_helpers/imminence.rb
184
- - lib/gds_api/test_helpers/content_api.rb
173
+ - lib/gds_api/imminence.rb
185
174
  - lib/gds_api/test_helpers/publisher.rb
175
+ - lib/gds_api/test_helpers/content_api.rb
176
+ - lib/gds_api/test_helpers/imminence.rb
177
+ - lib/gds_api/test_helpers/licence_application.rb
178
+ - lib/gds_api/test_helpers/content_api/artefact_stub.rb
186
179
  - lib/gds_api/test_helpers/panopticon.rb
187
180
  - lib/gds_api/test_helpers/json_client_helper.rb
188
- - lib/gds_api/part_methods.rb
181
+ - lib/gds_api/licence_application.rb
182
+ - lib/gds_api/base.rb
183
+ - lib/gds_api/json_client.rb
184
+ - lib/gds_api/response.rb
185
+ - lib/gds_api/rummager.rb
189
186
  - lib/gds_api/panopticon.rb
187
+ - lib/gds_api/core-ext/openstruct.rb
188
+ - lib/gds_api/part_methods.rb
189
+ - lib/gds_api/needotron.rb
190
+ - lib/gds_api/exceptions.rb
190
191
  - lib/gds_api/helpers.rb
191
192
  - README.md
192
193
  - Rakefile
193
- - test/rummager_test.rb
194
- - test/publisher_api_test.rb
195
- - test/licence_application_api_test.rb
196
194
  - test/panopticon_api_test.rb
195
+ - test/publisher_api_test.rb
196
+ - test/rummager_test.rb
197
197
  - test/imminence_api_test.rb
198
+ - test/panopticon_registerer_test.rb
199
+ - test/content_api_test.rb
198
200
  - test/json_client_test.rb
199
- - test/test_helper.rb
200
201
  - test/gds_api_base_test.rb
201
- - test/content_api_test.rb
202
- - test/panopticon_registerer_test.rb
202
+ - test/licence_application_api_test.rb
203
+ - test/test_helper.rb
203
204
  homepage: http://github.com/alphagov/gds-api-adapters
204
205
  licenses: []
205
206
 
@@ -213,7 +214,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
213
214
  requirements:
214
215
  - - ">="
215
216
  - !ruby/object:Gem::Version
216
- hash: -3063781964090340583
217
+ hash: -316804673231244671
217
218
  segments:
218
219
  - 0
219
220
  version: "0"
@@ -222,7 +223,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
222
223
  requirements:
223
224
  - - ">="
224
225
  - !ruby/object:Gem::Version
225
- hash: -3063781964090340583
226
+ hash: -316804673231244671
226
227
  segments:
227
228
  - 0
228
229
  version: "0"
@@ -234,13 +235,13 @@ signing_key:
234
235
  specification_version: 3
235
236
  summary: Adapters to work with GDS APIs
236
237
  test_files:
237
- - test/rummager_test.rb
238
- - test/publisher_api_test.rb
239
- - test/licence_application_api_test.rb
240
238
  - test/panopticon_api_test.rb
239
+ - test/publisher_api_test.rb
240
+ - test/rummager_test.rb
241
241
  - test/imminence_api_test.rb
242
+ - test/panopticon_registerer_test.rb
243
+ - test/content_api_test.rb
242
244
  - test/json_client_test.rb
243
- - test/test_helper.rb
244
245
  - test/gds_api_base_test.rb
245
- - test/content_api_test.rb
246
- - test/panopticon_registerer_test.rb
246
+ - test/licence_application_api_test.rb
247
+ - test/test_helper.rb