Module: GdsApi::TestHelpers::ContentApi
- Included in:
- ArtefactStub
- Defined in:
- lib/gds_api/test_helpers/content_api.rb,
lib/gds_api/test_helpers/content_api/artefact_stub.rb
Defined Under Namespace
Classes: ArtefactStub
Constant Summary collapse
- CONTENT_API_ENDPOINT =
Generally true. If you are initializing the client differently, you could redefine/override the constant or stub directly.
Plek.current.find('contentapi')
Instance Method Summary collapse
- #artefact_for_slug(slug) ⇒ Object
- #artefact_for_slug_in_a_section(slug, section_slug) ⇒ Object
- #artefact_for_slug_in_a_subsection(slug, subsection_slug) ⇒ Object
- #artefact_for_slug_with_related_artefacts(slug, related_artefact_slugs) ⇒ Object
- #content_api_does_not_have_an_artefact(slug) ⇒ Object
- #content_api_has_an_archived_artefact(slug) ⇒ Object
- #content_api_has_an_artefact(slug, body = artefact_for_slug(slug)) ⇒ Object
- #content_api_has_an_artefact_with_snac_code(slug, snac, body = artefact_for_slug(slug)) ⇒ Object
- #content_api_has_artefacts_in_a_section(slug, artefact_slugs = []) ⇒ Object
- #content_api_has_business_support_scheme(scheme) ⇒ Object
- #content_api_has_licence(details) ⇒ Object
-
#content_api_has_root_sections(slugs_or_sections) ⇒ Object
Takes an array of slugs, or hashes with section details (including a slug).
- #content_api_has_section(slug, parent_slug = nil) ⇒ Object
- #content_api_has_subsections(parent_slug, subsection_slugs) ⇒ Object
- #content_api_has_unpublished_artefact(slug, edition, body = artefact_for_slug(slug)) ⇒ Object
- #content_api_licence_hash(licence_identifier, options = {}) ⇒ Object
- #setup_content_api_business_support_schemes_stubs ⇒ Object
- #setup_content_api_licences_stubs ⇒ Object
-
#stub_content_api_default_artefact ⇒ Object
Stub requests, and then dynamically generate a response based on the slug in the request.
- #tag_for_slug(slug, tag_type, parent_slug = nil) ⇒ Object
Instance Method Details
#artefact_for_slug(slug) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/gds_api/test_helpers/content_api.rb', line 134 def artefact_for_slug(slug) singular_response_base.merge( "title" => titleize_slug(slug), "format" => "guide", "id" => "#{CONTENT_API_ENDPOINT}/#{CGI.escape(slug)}.json", "web_url" => "http://frontend.test.gov.uk/#{slug}", "details" => { "need_id" => "1234", "business_proposition" => false, # To be removed and replaced with proposition tags "format" => "Guide", "alternative_title" => "", "overview" => "This is an overview", "video_summary" => "", "video_url" => "", "parts" => [ { "id" => "overview", "order" => 1, "title" => "Overview", "body" => "<p>Some content</p>" }, { "id" => "#{slug}-part-2", "order" => 2, "title" => "How to make a nomination", "body" => "<p>Some more content</p>" } ] }, "tags" => [], "related" => [] ) end |
#artefact_for_slug_in_a_section(slug, section_slug) ⇒ Object
168 169 170 171 172 |
# File 'lib/gds_api/test_helpers/content_api.rb', line 168 def artefact_for_slug_in_a_section(slug, section_slug) artefact = artefact_for_slug(slug) artefact["tags"] << tag_for_slug(section_slug, "section") artefact end |
#artefact_for_slug_in_a_subsection(slug, subsection_slug) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/gds_api/test_helpers/content_api.rb', line 174 def artefact_for_slug_in_a_subsection(slug, subsection_slug) artefact = artefact_for_slug(slug) # for each "part" of the path, we want to reduce across the # list and build up a tree of nested tags. # This will turn "thing1/thing2" into: # Tag{ thing2, parent: Tag{ thing1 } } tag_tree = nil subsection_slug.split('/').inject(nil) do |last_section, subsection| subsection = [last_section, subsection].join('/') if last_section section = tag_for_slug(subsection, "section") if tag_tree # Because tags are nested within one another, this makes # the current part the top, and the rest we've seen the # ancestors tag_tree = section.merge("parent" => tag_tree) else tag_tree = section end subsection end artefact["tags"] << tag_tree artefact end |
#artefact_for_slug_with_related_artefacts(slug, related_artefact_slugs) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/gds_api/test_helpers/content_api.rb', line 200 def (slug, ) artefact = artefact_for_slug(slug) artefact["related"] = .map do || { "title" => titleize_slug(), "id" => "#{CONTENT_API_ENDPOINT}/#{CGI.escape()}.json", "web_url" => "https://www.test.gov.uk/#{}", "details" => {} } end artefact end |
#content_api_does_not_have_an_artefact(slug) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/gds_api/test_helpers/content_api.rb', line 101 def content_api_does_not_have_an_artefact(slug) body = { "_response_info" => { "status" => "not found" } } ArtefactStub.new(slug) .with_response_body(body) .with_response_status(404) .stub end |
#content_api_has_an_archived_artefact(slug) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/gds_api/test_helpers/content_api.rb', line 113 def content_api_has_an_archived_artefact(slug) body = { "_response_info" => { "status" => "gone", "status_message" => "This item is no longer available" } } ArtefactStub.new(slug) .with_response_body(body) .with_response_status(410) .stub end |
#content_api_has_an_artefact(slug, body = artefact_for_slug(slug)) ⇒ Object
83 84 85 |
# File 'lib/gds_api/test_helpers/content_api.rb', line 83 def content_api_has_an_artefact(slug, body = artefact_for_slug(slug)) ArtefactStub.new(slug).with_response_body(body).stub end |
#content_api_has_an_artefact_with_snac_code(slug, snac, body = artefact_for_slug(slug)) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/gds_api/test_helpers/content_api.rb', line 94 def content_api_has_an_artefact_with_snac_code(slug, snac, body = artefact_for_slug(slug)) ArtefactStub.new(slug) .with_response_body(body) .with_query_parameters(snac: snac) .stub end |
#content_api_has_artefacts_in_a_section(slug, artefact_slugs = []) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/gds_api/test_helpers/content_api.rb', line 46 def content_api_has_artefacts_in_a_section(slug, artefact_slugs=[]) body = plural_response_base.merge( "results" => artefact_slugs.map do |artefact_slug| artefact_for_slug(artefact_slug) end ) sort_orders = ["alphabetical", "curated"] sort_orders.each do |order| url = "#{CONTENT_API_ENDPOINT}/with_tag.json?sort=#{order}&tag=#{CGI.escape(slug)}" stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {}) end end |
#content_api_has_business_support_scheme(scheme) ⇒ Object
244 245 246 247 |
# File 'lib/gds_api/test_helpers/content_api.rb', line 244 def content_api_has_business_support_scheme(scheme) raise "Need a licence identifier" if scheme["details"]["business_support_identifier"].nil? @stubbed_content_api_business_support_schemes << scheme end |
#content_api_has_licence(details) ⇒ Object
298 299 300 301 |
# File 'lib/gds_api/test_helpers/content_api.rb', line 298 def content_api_has_licence(details) raise "Need a licence identifier" if details[:licence_identifier].nil? @stubbed_content_api_licences << details end |
#content_api_has_root_sections(slugs_or_sections) ⇒ Object
Takes an array of slugs, or hashes with section details (including a slug). Will stub out content_api calls for tags of type section to return these sections
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/gds_api/test_helpers/content_api.rb', line 14 def content_api_has_root_sections(slugs_or_sections) sections = slugs_or_sections.map {|s| s.is_a?(Hash) ? s : {:slug => s} } body = plural_response_base.merge( "results" => sections.map do |section| { "id" => "#{CONTENT_API_ENDPOINT}/tags/#{CGI.escape(section[:slug])}.json", "web_url" => nil, "title" => section[:title] || titleize_slug(section[:slug]), "details" => { "type" => "section", "description" => section[:description] || "#{section[:slug]} description", "short_description" => section[:short_description] || "#{section[:slug]} short description", }, "parent" => nil, "content_with_tag" => { "id" => "#{CONTENT_API_ENDPOINT}/with_tag.json?tag=#{CGI.escape(section[:slug])}", "web_url" => "http://www.test.gov.uk/browse/#{section[:slug]}" } } end ) ["#{CONTENT_API_ENDPOINT}/tags.json?type=section", "#{CONTENT_API_ENDPOINT}/tags.json?root_sections=true&type=section"].each do |url| stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {}) end end |
#content_api_has_section(slug, parent_slug = nil) ⇒ Object
40 41 42 43 44 |
# File 'lib/gds_api/test_helpers/content_api.rb', line 40 def content_api_has_section(slug, parent_slug=nil) body = tag_for_slug(slug, "section", parent_slug) url = "#{CONTENT_API_ENDPOINT}/tags/#{CGI.escape(slug)}.json" stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {}) end |
#content_api_has_subsections(parent_slug, subsection_slugs) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/gds_api/test_helpers/content_api.rb', line 59 def content_api_has_subsections(parent_slug, subsection_slugs) parent_section = tag_for_slug(parent_slug, "section") body = plural_response_base.merge( "results" => subsection_slugs.map do |slug| { "id" => "#{CONTENT_API_ENDPOINT}/tags/#{CGI.escape(slug)}.json", "web_url" => nil, "title" => titleize_slug(slug), "details" => { "type" => "section", "description" => "#{slug} description" }, "parent" => parent_section, "content_with_tag" => { "id" => "#{CONTENT_API_ENDPOINT}/with_tag.json?tag=#{CGI.escape(slug)}", "web_url" => "http://www.test.gov.uk/browse/#{slug}" } } end ) url = "#{CONTENT_API_ENDPOINT}/tags.json?type=section&parent_id=#{CGI.escape(parent_slug)}" stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {}) end |
#content_api_has_unpublished_artefact(slug, edition, body = artefact_for_slug(slug)) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/gds_api/test_helpers/content_api.rb', line 87 def content_api_has_unpublished_artefact(slug, edition, body = artefact_for_slug(slug)) ArtefactStub.new(slug) .with_response_body(body) .with_query_parameters(edition: edition) .stub end |
#content_api_licence_hash(licence_identifier, options = {}) ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/gds_api/test_helpers/content_api.rb', line 249 def content_api_licence_hash(licence_identifier, = {}) details = { title: "Publisher title", slug: 'licence-slug', licence_short_description: "Short description of licence" } details.merge!() { "title" => details[:title], "id" => "http://example.org/#{details[:slug]}.json", "web_url" => "http://www.test.gov.uk/#{details[:slug]}", "format" => "licence", "details" => { "need_id" => nil, "business_proposition" => false, "alternative_title" => nil, "overview" => nil, "will_continue_on" => nil, "continuation_link" => nil, "licence_identifier" => licence_identifier, "licence_short_description" => details[:licence_short_description], "licence_overview" => nil, "updated_at" => "2012-10-06T12:00:05+01:00" }, "tags" => [], "related" => [] } end |
#setup_content_api_business_support_schemes_stubs ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/gds_api/test_helpers/content_api.rb', line 231 def setup_content_api_business_support_schemes_stubs @stubbed_content_api_business_support_schemes = [] stub_request(:get, %r{\A#{CONTENT_API_ENDPOINT}/business_support_schemes\.json}).to_return do |request| if request.uri.query_values and request.uri.query_values["identifiers"] ids = request.uri.query_values["identifiers"].split(',') results = @stubbed_content_api_business_support_schemes.select {|bs| ids.include? bs["details"]["business_support_identifier"] } else results = [] end {:body => plural_response_base.merge("results" => results, "total" => results.size).to_json} end end |
#setup_content_api_licences_stubs ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/gds_api/test_helpers/content_api.rb', line 279 def setup_content_api_licences_stubs @stubbed_content_api_licences = [] stub_request(:get, %r{\A#{CONTENT_API_ENDPOINT}/licences}).to_return do |request| if request.uri.query_values and request.uri.query_values["ids"] ids = request.uri.query_values["ids"].split(',') valid_licences = @stubbed_content_api_licences.select { |l| ids.include? l[:licence_identifier] } { :body => { 'results' => valid_licences.map { |licence| content_api_licence_hash(licence[:licence_identifier], licence) } }.to_json } else {:body => {'results' => []}.to_json} end end end |
#stub_content_api_default_artefact ⇒ Object
Stub requests, and then dynamically generate a response based on the slug in the request
127 128 129 130 131 132 |
# File 'lib/gds_api/test_helpers/content_api.rb', line 127 def stub_content_api_default_artefact stub_request(:get, %r{\A#{CONTENT_API_ENDPOINT}/[a-z0-9-]+\.json}).to_return { |request| slug = request.uri.path.split('/').last.chomp('.json') {:body => artefact_for_slug(slug).to_json} } end |
#tag_for_slug(slug, tag_type, parent_slug = nil) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/gds_api/test_helpers/content_api.rb', line 213 def tag_for_slug(slug, tag_type, parent_slug=nil) parent = if parent_slug tag_for_slug(parent_slug, tag_type) end { "title" => titleize_slug(slug.split('/').last), "id" => "#{CONTENT_API_ENDPOINT}/tags/#{CGI.escape(slug)}.json", "details" => { "type" => tag_type }, "content_with_tag" => { "id" => "#{CONTENT_API_ENDPOINT}/with_tag.json?tag=#{CGI.escape(slug)}", "web_url" => "https://www.test.gov.uk/browse/#{slug}", }, "parent" => parent } end |