gds-api-adapters 38.1.0 → 39.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/lib/gds_api/test_helpers/need_api.rb +0 -15
  3. data/lib/gds_api/test_helpers/organisations.rb +5 -0
  4. data/lib/gds_api/version.rb +1 -1
  5. metadata +3 -103
  6. data/test/asset_manager_test.rb +0 -94
  7. data/test/business_support_api_test.rb +0 -45
  8. data/test/content_api_test.rb +0 -712
  9. data/test/content_store_test.rb +0 -55
  10. data/test/email_alert_api_test.rb +0 -189
  11. data/test/fixtures/finder_api/cma-case-schema.json +0 -103
  12. data/test/fixtures/hello.txt +0 -1
  13. data/test/fixtures/new_policies_for_dwp.json +0 -298
  14. data/test/fixtures/no_services_and_info_data_found_fixture.json +0 -14
  15. data/test/fixtures/old_policies_for_dwp.json +0 -413
  16. data/test/fixtures/services_and_info_fixture.json +0 -73
  17. data/test/fixtures/sub_sector_organisations.json +0 -57
  18. data/test/fixtures/world_organisations_australia.json +0 -490
  19. data/test/gds_api_base_test.rb +0 -110
  20. data/test/gov_uk_delivery_test.rb +0 -67
  21. data/test/govuk_headers_test.rb +0 -30
  22. data/test/helpers_test.rb +0 -23
  23. data/test/imminence_api_test.rb +0 -196
  24. data/test/json_client_test.rb +0 -879
  25. data/test/licence_application_api_test.rb +0 -196
  26. data/test/list_response_test.rb +0 -189
  27. data/test/local_links_manager_api_test.rb +0 -236
  28. data/test/mapit_test.rb +0 -117
  29. data/test/maslow_test.rb +0 -12
  30. data/test/middleware/govuk_header_sniffer_test.rb +0 -18
  31. data/test/need_api_test.rb +0 -345
  32. data/test/organisations_api_test.rb +0 -58
  33. data/test/panopticon_registerer_test.rb +0 -118
  34. data/test/panopticon_test.rb +0 -190
  35. data/test/pp_data_in_test.rb +0 -52
  36. data/test/pp_data_out_test.rb +0 -24
  37. data/test/publishing_api/special_route_publisher_test.rb +0 -104
  38. data/test/publishing_api_test.rb +0 -186
  39. data/test/publishing_api_v2/get_expanded_links_test.rb +0 -85
  40. data/test/publishing_api_v2/get_links_test.rb +0 -89
  41. data/test/publishing_api_v2/lookup_test.rb +0 -70
  42. data/test/publishing_api_v2_test.rb +0 -1649
  43. data/test/response_test.rb +0 -245
  44. data/test/router_test.rb +0 -456
  45. data/test/rummager_helpers_test.rb +0 -20
  46. data/test/rummager_test.rb +0 -150
  47. data/test/support_api_test.rb +0 -189
  48. data/test/support_test.rb +0 -95
  49. data/test/test_helper.rb +0 -55
  50. data/test/test_helpers/email_alert_api_test.rb +0 -24
  51. data/test/test_helpers/pact_helper.rb +0 -13
  52. data/test/test_helpers/panopticon_test.rb +0 -44
  53. data/test/test_helpers/publishing_api_test.rb +0 -129
  54. data/test/test_helpers/publishing_api_v2_test.rb +0 -170
  55. data/test/worldwide_api_test.rb +0 -82
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3cb1959cc394a5171ec4c09c0b3f5b1a39dde285
4
- data.tar.gz: d025956a6c6d34a8e0d68e92c6d0bb75ec5cdcc3
3
+ metadata.gz: e13c9f713f5a9a3e1f1e79f6b26c865a67a4bffb
4
+ data.tar.gz: 1379836ecce2fd12728c9a5f657aaaa641c2f5d3
5
5
  SHA512:
6
- metadata.gz: e24a262a3fceb9f36fb7be05f6cef07d05d8ae64a2c77d70e9ca1dbec1aa6c89df9d1bc5035816c3ec7565328de683bab04cf19e555f9cfabe546c36e00f43a2
7
- data.tar.gz: efcd72026df90795fb2b8b1edb1d8bdd55a7fa5d5ddf0ce3c69d545edec36ff78f26ef78681acefd9e4c249b881d69961b42b47a7dcb40126aef64e9f62f98d4
6
+ metadata.gz: 07437dca1957fa78c58dffb855941cc9a7e32579f3fca9033dbac50e56fa2d3c29196b2825cd2ee19bcfb0ad1ea7c3bb4ba5860c961cbdc1d0aefb3c69fd45de
7
+ data.tar.gz: 8f45e3733c100dc9ba9711bc91e6a5c734ce4e5c80fad8b1c497740bcebea038acad0cc403121e805f30d71204158a30fde1cc4a71217dc39e8a5970fe1cfc6c
@@ -8,21 +8,6 @@ module GdsApi
8
8
 
9
9
  NEED_API_ENDPOINT = Plek.current.find('need-api')
10
10
 
11
- def need_api_has_organisations(organisations)
12
- url = NEED_API_ENDPOINT + "/organisations"
13
-
14
- body = response_base.merge(
15
- "organisations" => organisations.map {|id, attrs|
16
- if attrs.is_a? String
17
- { "id" => id }.merge("name" => attrs)
18
- else
19
- { "id" => id }.merge(attrs)
20
- end
21
- }
22
- )
23
- stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
24
- end
25
-
26
11
  def need_api_has_needs_for_organisation(organisation, needs)
27
12
  url = NEED_API_ENDPOINT + "/needs?organisation_id=#{organisation}"
28
13
 
@@ -59,6 +59,11 @@ module GdsApi
59
59
  to_return(status: 200, body: page_details.to_json, headers: { "Link" => link_headers.join(", ") })
60
60
  end
61
61
  end
62
+
63
+ if pages.empty?
64
+ # If there are no pages - and so no organisations specified - then stub /api/organisations.
65
+ stub_request(:get, "#{ORGANISATIONS_API_ENDPOINT}/api/organisations").to_return(status: 200, body: plural_response_base.to_json, headers: {})
66
+ end
62
67
  end
63
68
 
64
69
  def organisations_api_has_organisation(organisation_slug, details = nil)
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '38.1.0'.freeze
2
+ VERSION = '39.0.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gds-api-adapters
3
3
  version: !ruby/object:Gem::Version
4
- version: 38.1.0
4
+ version: 39.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Stewart
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-15 00:00:00.000000000 Z
11
+ date: 2017-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: plek
@@ -407,56 +407,6 @@ files:
407
407
  - lib/gds_api/test_helpers/worldwide.rb
408
408
  - lib/gds_api/version.rb
409
409
  - lib/gds_api/worldwide.rb
410
- - test/asset_manager_test.rb
411
- - test/business_support_api_test.rb
412
- - test/content_api_test.rb
413
- - test/content_store_test.rb
414
- - test/email_alert_api_test.rb
415
- - test/fixtures/finder_api/cma-case-schema.json
416
- - test/fixtures/hello.txt
417
- - test/fixtures/new_policies_for_dwp.json
418
- - test/fixtures/no_services_and_info_data_found_fixture.json
419
- - test/fixtures/old_policies_for_dwp.json
420
- - test/fixtures/services_and_info_fixture.json
421
- - test/fixtures/sub_sector_organisations.json
422
- - test/fixtures/world_organisations_australia.json
423
- - test/gds_api_base_test.rb
424
- - test/gov_uk_delivery_test.rb
425
- - test/govuk_headers_test.rb
426
- - test/helpers_test.rb
427
- - test/imminence_api_test.rb
428
- - test/json_client_test.rb
429
- - test/licence_application_api_test.rb
430
- - test/list_response_test.rb
431
- - test/local_links_manager_api_test.rb
432
- - test/mapit_test.rb
433
- - test/maslow_test.rb
434
- - test/middleware/govuk_header_sniffer_test.rb
435
- - test/need_api_test.rb
436
- - test/organisations_api_test.rb
437
- - test/panopticon_registerer_test.rb
438
- - test/panopticon_test.rb
439
- - test/pp_data_in_test.rb
440
- - test/pp_data_out_test.rb
441
- - test/publishing_api/special_route_publisher_test.rb
442
- - test/publishing_api_test.rb
443
- - test/publishing_api_v2/get_expanded_links_test.rb
444
- - test/publishing_api_v2/get_links_test.rb
445
- - test/publishing_api_v2/lookup_test.rb
446
- - test/publishing_api_v2_test.rb
447
- - test/response_test.rb
448
- - test/router_test.rb
449
- - test/rummager_helpers_test.rb
450
- - test/rummager_test.rb
451
- - test/support_api_test.rb
452
- - test/support_test.rb
453
- - test/test_helper.rb
454
- - test/test_helpers/email_alert_api_test.rb
455
- - test/test_helpers/pact_helper.rb
456
- - test/test_helpers/panopticon_test.rb
457
- - test/test_helpers/publishing_api_test.rb
458
- - test/test_helpers/publishing_api_v2_test.rb
459
- - test/worldwide_api_test.rb
460
410
  homepage: http://github.com/alphagov/gds-api-adapters
461
411
  licenses: []
462
412
  metadata: {}
@@ -480,55 +430,5 @@ rubygems_version: 2.5.1
480
430
  signing_key:
481
431
  specification_version: 4
482
432
  summary: Adapters to work with GDS APIs
483
- test_files:
484
- - test/content_api_test.rb
485
- - test/json_client_test.rb
486
- - test/content_store_test.rb
487
- - test/publishing_api_test.rb
488
- - test/business_support_api_test.rb
489
- - test/middleware/govuk_header_sniffer_test.rb
490
- - test/local_links_manager_api_test.rb
491
- - test/list_response_test.rb
492
- - test/maslow_test.rb
493
- - test/panopticon_registerer_test.rb
494
- - test/pp_data_out_test.rb
495
- - test/support_test.rb
496
- - test/asset_manager_test.rb
497
- - test/helpers_test.rb
498
- - test/licence_application_api_test.rb
499
- - test/test_helper.rb
500
- - test/govuk_headers_test.rb
501
- - test/mapit_test.rb
502
- - test/rummager_helpers_test.rb
503
- - test/publishing_api_v2_test.rb
504
- - test/publishing_api_v2/lookup_test.rb
505
- - test/publishing_api_v2/get_expanded_links_test.rb
506
- - test/publishing_api_v2/get_links_test.rb
507
- - test/support_api_test.rb
508
- - test/organisations_api_test.rb
509
- - test/gds_api_base_test.rb
510
- - test/router_test.rb
511
- - test/publishing_api/special_route_publisher_test.rb
512
- - test/need_api_test.rb
513
- - test/panopticon_test.rb
514
- - test/pp_data_in_test.rb
515
- - test/fixtures/hello.txt
516
- - test/fixtures/new_policies_for_dwp.json
517
- - test/fixtures/no_services_and_info_data_found_fixture.json
518
- - test/fixtures/services_and_info_fixture.json
519
- - test/fixtures/old_policies_for_dwp.json
520
- - test/fixtures/world_organisations_australia.json
521
- - test/fixtures/sub_sector_organisations.json
522
- - test/fixtures/finder_api/cma-case-schema.json
523
- - test/worldwide_api_test.rb
524
- - test/test_helpers/publishing_api_test.rb
525
- - test/test_helpers/publishing_api_v2_test.rb
526
- - test/test_helpers/pact_helper.rb
527
- - test/test_helpers/panopticon_test.rb
528
- - test/test_helpers/email_alert_api_test.rb
529
- - test/gov_uk_delivery_test.rb
530
- - test/imminence_api_test.rb
531
- - test/email_alert_api_test.rb
532
- - test/response_test.rb
533
- - test/rummager_test.rb
433
+ test_files: []
534
434
  has_rdoc:
@@ -1,94 +0,0 @@
1
- require 'test_helper'
2
- require 'gds_api/asset_manager'
3
- require 'gds_api/test_helpers/asset_manager'
4
- require 'json'
5
-
6
- describe GdsApi::AssetManager do
7
- include GdsApi::TestHelpers::AssetManager
8
-
9
- let(:base_api_url) { Plek.current.find('asset-manager') }
10
- let(:api) { GdsApi::AssetManager.new(base_api_url) }
11
-
12
- let(:file_fixture) { load_fixture_file("hello.txt") }
13
-
14
- let(:asset_url) { [base_api_url, "assets", asset_id].join("/") }
15
- let(:asset_id) { "new-asset-id" }
16
-
17
- let(:asset_manager_response) {
18
- {
19
- asset: {
20
- id: asset_url,
21
- }
22
- }
23
- }
24
-
25
- it "creates an asset with a file" do
26
- req = stub_request(:post, "#{base_api_url}/assets").
27
- with(body: %r{Content\-Disposition: form\-data; name="asset\[file\]"; filename="hello\.txt"\r\nContent\-Type: text/plain}).
28
- to_return(body: JSON.dump(asset_manager_response), status: 201)
29
-
30
- response = api.create_asset(file: file_fixture)
31
-
32
- assert_equal asset_url, response['asset']['id']
33
- assert_requested(req)
34
- end
35
-
36
- it "returns not found when an asset does not exist" do
37
- asset_manager_does_not_have_an_asset("not-really-here")
38
-
39
- assert_raises GdsApi::HTTPNotFound do
40
- api.asset("not-really-here")
41
- end
42
- end
43
-
44
- describe "an asset exists" do
45
- before do
46
- asset_manager_has_an_asset(
47
- asset_id,
48
- "name" => "photo.jpg",
49
- "content_type" => "image/jpeg",
50
- "file_url" => "http://fooey.gov.uk/media/photo.jpg",
51
- )
52
- end
53
-
54
- let(:asset_id) { "test-id" }
55
-
56
- it "updates an asset with a file" do
57
- req = stub_request(:put, "#{base_api_url}/assets/test-id").
58
- to_return(body: JSON.dump(asset_manager_response), status: 200)
59
-
60
- response = api.update_asset(asset_id, file: file_fixture)
61
-
62
- assert_equal "#{base_api_url}/assets/#{asset_id}", response['asset']['id']
63
- assert_requested(req)
64
- end
65
-
66
- it "retrieves an asset" do
67
- asset = api.asset(asset_id)
68
-
69
- assert_equal "photo.jpg", asset['name']
70
- assert_equal "image/jpeg", asset['content_type']
71
- assert_equal "http://fooey.gov.uk/media/photo.jpg", asset['file_url']
72
- end
73
- end
74
-
75
- it "deletes an asset for the given id" do
76
- req = stub_request(:delete, "#{base_api_url}/assets/#{asset_id}").
77
- to_return(body: JSON.dump(asset_manager_response), status: 200)
78
-
79
- response = api.delete_asset(asset_id)
80
-
81
- assert_equal "#{base_api_url}/assets/#{asset_id}", response['asset']['id']
82
- assert_requested(req)
83
- end
84
-
85
- it "restores an asset for the given id" do
86
- req = stub_request(:post, "#{base_api_url}/assets/#{asset_id}/restore").
87
- to_return(body: JSON.dump(asset_manager_response), status: 200)
88
-
89
- response = api.restore_asset(asset_id)
90
-
91
- assert_equal "#{base_api_url}/assets/#{asset_id}", response['asset']['id']
92
- assert_requested(req)
93
- end
94
- end
@@ -1,45 +0,0 @@
1
- require 'test_helper'
2
- require 'gds_api/business_support_api'
3
- require 'gds_api/test_helpers/business_support_api'
4
-
5
- describe GdsApi::BusinessSupportApi do
6
- include GdsApi::TestHelpers::BusinessSupportApi
7
-
8
- before do
9
- @base_api_url = Plek.current.find("business-support-api")
10
- @api = GdsApi::BusinessSupportApi.new(@base_api_url)
11
- setup_business_support_api_schemes_stubs
12
- end
13
-
14
- describe "searching for schemes" do
15
- it "should return all schemes when called with no facets" do
16
- business_support_api_has_schemes([:scheme1, :scheme2, :scheme3])
17
- response = @api.schemes
18
- assert_equal [{ "title" => "scheme1" }, { "title" => "scheme2" }, { "title" => "scheme3" }], response['results']
19
- end
20
- it "should return schemes for applicable facets" do
21
- business_support_api_has_scheme(:scottish_manufacturing, locations: 'scotland', sectors: 'manufacturing', support_types: 'grant,loan')
22
- response = @api.schemes(locations: 'scotland', sectors: 'manufacturing', support_types: 'grant,loan')
23
- assert_equal 1, response["results"].size
24
- assert_equal "scottish_manufacturing", response["results"].first["title"]
25
- end
26
- it "should return an empty result when facets are not applicable" do
27
- business_support_api_has_scheme(:super_secret, locations: 'the moon', sectors: 'espionage')
28
- response = @api.schemes(locations: 'earth', sectors: 'espionage')
29
- assert_empty response["results"]
30
- end
31
- end
32
-
33
- describe "finding a scheme by slug" do
34
- it "should give the scheme details" do
35
- business_support_api_has_a_scheme('superbiz-wunderfundz', title: 'Superbiz wunderfundz',
36
- short_description: 'Wunderfundz for your superbiz',
37
- body: 'Do you run or work for a Superbiz? Well we have the Wunderfundz.')
38
- response = @api.scheme('superbiz-wunderfundz')
39
- assert_equal 'business_support', response['format']
40
- assert_equal 'Superbiz wunderfundz', response['title']
41
- assert_equal 'Wunderfundz for your superbiz', response['details']['short_description']
42
- assert_equal 'Do you run or work for a Superbiz? Well we have the Wunderfundz.', response['details']['body']
43
- end
44
- end
45
- end
@@ -1,712 +0,0 @@
1
- require 'test_helper'
2
- require 'gds_api/content_api'
3
- require 'gds_api/test_helpers/content_api'
4
-
5
- describe GdsApi::ContentApi do
6
- include GdsApi::TestHelpers::ContentApi
7
-
8
- before do
9
- @base_api_url = Plek.current.find("contentapi")
10
- @api = GdsApi::ContentApi.new(@base_api_url)
11
- end
12
-
13
- describe "when asked for relative web URLs" do
14
- before do
15
- @api = GdsApi::ContentApi.new(
16
- @base_api_url,
17
- web_urls_relative_to: "http://www.test.gov.uk"
18
- )
19
- end
20
-
21
- it "should use relative URLs for an artefact" do
22
- artefact_response = artefact_for_slug_in_a_section("bank-holidays", "cheese")
23
-
24
- # Rewrite the web_url fields to have a common prefix
25
- # The helper's default is to point the web_url for an artefact at the
26
- # frontend app, and the web_url for a tag's content to www: to test the
27
- # rewriting properly, they need to be the same
28
- artefact_response["web_url"] = "http://www.test.gov.uk/bank-holidays"
29
- section_tag_content = artefact_response["tags"][0]["content_with_tag"]
30
- section_tag_content["web_url"] = "http://www.test.gov.uk/browse/cheese"
31
-
32
- content_api_has_an_artefact("bank-holidays", artefact_response)
33
- artefact = @api.artefact("bank-holidays")
34
-
35
- assert_equal "Bank holidays", artefact['title']
36
- assert_equal "/bank-holidays", artefact['web_url']
37
-
38
- assert_equal(
39
- "/browse/cheese",
40
- artefact['tags'][0]['content_with_tag']['web_url']
41
- )
42
- end
43
-
44
- it "should use relative URLs for tag listings" do
45
- content_api_has_root_sections %w(housing benefits tax)
46
- tags = @api.root_sections
47
-
48
- assert_equal 3, tags.count
49
- tags.each do |tag|
50
- web_url = tag['content_with_tag']['web_url']
51
- assert web_url.start_with?("/browse/"), web_url
52
- end
53
- end
54
-
55
- describe "with caching enabled" do
56
- before do
57
- @original_cache = GdsApi::JsonClient.cache
58
- GdsApi::JsonClient.cache = LRUCache.new(max_size: 10, ttl: 10)
59
- end
60
-
61
- it "should not pollute the cache with relative URLs" do
62
- artefact_response = artefact_for_slug("bank-holidays")
63
- artefact_response["web_url"] = "http://www.test.gov.uk/bank-holidays"
64
- content_api_has_an_artefact("bank-holidays", artefact_response)
65
-
66
- assert_equal "/bank-holidays", @api.artefact("bank-holidays")['web_url']
67
-
68
- clean_api = GdsApi::ContentApi.new(@base_api_url)
69
- clean_artefact = clean_api.artefact("bank-holidays")
70
-
71
- assert_equal(
72
- "http://www.test.gov.uk/bank-holidays",
73
- clean_artefact['web_url']
74
- )
75
- end
76
-
77
- after do
78
- GdsApi::JsonClient.cache = @original_cache
79
- end
80
- end
81
- end
82
-
83
- describe "sections" do
84
- it "should show a list of sections" do
85
- content_api_has_root_sections(["crime"])
86
- response = @api.sections
87
-
88
- # Old-style dictionary access
89
- first_section = response["results"][0]
90
- assert_equal "#{@base_api_url}/tags/sections/crime.json", first_section["id"]
91
-
92
- # Also check attribute access
93
- first_section = response.first
94
- assert_equal(
95
- "#{@base_api_url}/tags/sections/crime.json",
96
- first_section['id']
97
- )
98
- end
99
-
100
- def section_page_url(page_parameter)
101
- if page_parameter
102
- "#{GdsApi::TestHelpers::ContentApi::CONTENT_API_ENDPOINT}/tags.json?type=section&page=#{page_parameter}"
103
- else
104
- "#{GdsApi::TestHelpers::ContentApi::CONTENT_API_ENDPOINT}/tags.json?type=section"
105
- end
106
- end
107
-
108
- def stub_section_page(page_parameter, options)
109
- total_pages = options.fetch :of
110
-
111
- url = section_page_url(page_parameter)
112
-
113
- page_number = page_parameter || 1
114
- # e.g. page 2 -> 11..20
115
- range_start = (page_number - 1) * 10 + 1
116
- range_end = page_number * 10
117
- tags = (range_start..range_end).map { |number|
118
- tag_for_slug("section-#{number}", "section")
119
- }
120
- body = plural_response_base.merge(
121
- "results" => tags
122
- )
123
-
124
- links = []
125
- if page_number > 1
126
- links << "<#{section_page_url(page_number - 1)}>; rel=\"previous\""
127
- end
128
- if page_number < total_pages
129
- links << "<#{section_page_url(page_number + 1)}>; rel=\"next\""
130
- end
131
-
132
- stub_request(:get, url).to_return(
133
- status: 200,
134
- body: body.to_json,
135
- headers: { "Link" => links.join(",") }
136
- )
137
- end
138
-
139
- it "should allow iteration across pages" do
140
- [nil, 2].each do |page_parameter|
141
- stub_section_page(page_parameter, of: 2)
142
- end
143
-
144
- sections = @api.sections
145
- assert_equal 20, sections.with_subsequent_pages.count
146
- assert_equal(
147
- "Section 20",
148
- sections.with_subsequent_pages.to_a.last['title']
149
- )
150
- end
151
-
152
- it "should iterate across three or more pages" do
153
- [nil, 2, 3].each do |page_parameter|
154
- stub_section_page(page_parameter, of: 3)
155
- end
156
-
157
- sections = @api.sections
158
- assert_equal 30, sections.with_subsequent_pages.count
159
- assert_equal(
160
- "Section 30",
161
- sections.with_subsequent_pages.to_a.last['title']
162
- )
163
- end
164
-
165
- it "should not load a page multiple times" do
166
- [nil, 2].each do |page_parameter|
167
- stub_section_page(page_parameter, of: 2)
168
- end
169
-
170
- sections = @api.sections
171
-
172
- 3.times do
173
- # Loop through all the items, just to make sure we load all the pages
174
- sections.with_subsequent_pages.each do end
175
- end
176
-
177
- assert_requested :get, section_page_url(2), times: 1
178
- end
179
-
180
- it "should display a single page of sections" do
181
- stub_section_page(nil, of: 1)
182
- sections = @api.sections
183
- assert_equal 10, sections.with_subsequent_pages.count
184
- end
185
- end
186
-
187
- describe "artefact" do
188
- it "should show the artefact" do
189
- content_api_has_an_artefact("devolution-uk")
190
- response = @api.artefact("devolution-uk")
191
- assert_equal "#{@base_api_url}/devolution-uk.json", response["id"]
192
- end
193
-
194
- it "should be able to fetch unpublished editions when authenticated" do
195
- api = GdsApi::ContentApi.new(@base_api_url, bearer_token: 'MY_BEARER_TOKEN')
196
- content_api_has_unpublished_artefact("devolution-uk", 3)
197
- response = api.artefact("devolution-uk", edition: 3)
198
- assert_equal "#{@base_api_url}/devolution-uk.json", response["id"]
199
- end
200
-
201
- it "should raise an exception if no bearer token is used when fetching unpublished editions" do
202
- content_api_has_unpublished_artefact("devolution-uk", 3)
203
- assert_raises GdsApi::NoBearerToken do
204
- @api.artefact("devolution-uk", edition: 3)
205
- end
206
- end
207
-
208
- it "should raise a 410 if an artefact has been archived" do
209
- content_api_has_an_archived_artefact("atlantis")
210
- assert_raises GdsApi::HTTPGone do
211
- @api.artefact!("atlantis")
212
- end
213
- end
214
-
215
- it "should be able to fetch artefacts with a '/' in the slug" do
216
- content_api_has_an_artefact("foreign-travel-advice/aruba")
217
- response = @api.artefact("foreign-travel-advice/aruba")
218
- assert_requested(:get, "#{@base_api_url}/foreign-travel-advice%2Faruba.json")
219
- assert_equal "#{@base_api_url}/foreign-travel-advice%2Faruba.json", response["id"]
220
- end
221
- end
222
-
223
- describe "artefacts" do
224
- before :each do
225
- @artefacts_endpoint = "#{GdsApi::TestHelpers::ContentApi::CONTENT_API_ENDPOINT}/artefacts.json"
226
- end
227
-
228
- it "should return a listresponse for the artefacts" do
229
- WebMock.stub_request(:get, @artefacts_endpoint).
230
- to_return(body: {
231
- "_response_info" => { "status" => "ok" },
232
- "total" => 4,
233
- "results" => [
234
- { "format" => "answer", "web_url" => "http://www.test.gov.uk/foo" },
235
- { "format" => "local_transaction", "web_url" => "http://www.test.gov.uk/bar/baz" },
236
- { "format" => "place", "web_url" => "http://www.test.gov.uk/somewhere" },
237
- { "format" => "guide", "web_url" => "http://www.test.gov.uk/vat" },
238
- ]
239
- }.to_json)
240
-
241
- response = @api.artefacts
242
- assert_equal 4, response.count
243
- assert_equal(
244
- %w(answer local_transaction place guide),
245
- response.map { |item| item['format'] }
246
- )
247
- end
248
-
249
- it "should work with a paginated response" do
250
- WebMock.stub_request(:get, @artefacts_endpoint).
251
- to_return(
252
- body: {
253
- "_response_info" => { "status" => "ok" },
254
- "total" => 4,
255
- "results" => [
256
- { "format" => "answer", "web_url" => "http://www.test.gov.uk/foo" },
257
- { "format" => "local_transaction", "web_url" => "http://www.test.gov.uk/bar/baz" },
258
- { "format" => "place", "web_url" => "http://www.test.gov.uk/somewhere" },
259
- { "format" => "guide", "web_url" => "http://www.test.gov.uk/vat" },
260
- ]
261
- }.to_json,
262
- headers: { "Link" => "<#{@artefacts_endpoint}?page=2>; rel=\"next\"" }
263
- )
264
- WebMock.stub_request(:get, "#{@artefacts_endpoint}?page=2").
265
- to_return(
266
- body: {
267
- "_response_info" => { "status" => "ok" },
268
- "total" => 3,
269
- "results" => [
270
- { "format" => "answer", "web_url" => "http://www.test.gov.uk/foo2" },
271
- { "format" => "local_transaction", "web_url" => "http://www.test.gov.uk/bar/baz2" },
272
- { "format" => "guide", "web_url" => "http://www.test.gov.uk/vat2" },
273
- ]
274
- }.to_json
275
- )
276
- response = @api.artefacts
277
- assert_equal 7, response.with_subsequent_pages.count
278
- assert_equal(
279
- "http://www.test.gov.uk/vat2",
280
- response.with_subsequent_pages.to_a.last['web_url']
281
- )
282
- end
283
- end
284
-
285
- describe "artefacts for need" do
286
- it "should fetch artefacts with a given need id" do
287
- content_api_has_artefacts_for_need_id(100123, [
288
- { "format" => "answer", "web_url" => "http://www.gov.uk/burrito" },
289
- { "format" => "guide", "web_url" => "http://www.gov.uk/burrito-standard" },
290
- { "format" => "transaction", "web_url" => "http://www.gov.uk/local-burrito-place" }
291
- ])
292
-
293
- response = @api.for_need(100123)
294
-
295
- assert_equal 3, response.count
296
- assert_equal(
297
- [
298
- "http://www.gov.uk/burrito",
299
- "http://www.gov.uk/burrito-standard",
300
- "http://www.gov.uk/local-burrito-place"
301
- ],
302
- response.map { |item| item['web_url'] }
303
- )
304
- assert_equal(
305
- %w(answer guide transaction),
306
- response.map { |item| item['format'] }
307
- )
308
- end
309
- end
310
-
311
- describe "tags" do
312
- it "returns a list of tags of a given type" do
313
- content_api_has_tags("author", ["justin-thyme"])
314
- response = @api.tags("author")
315
-
316
- # Old-style dictionary access
317
- first_section = response["results"][0]
318
- assert_equal "#{@base_api_url}/tags/authors/justin-thyme.json", first_section["id"]
319
-
320
- # Also check attribute access
321
- first_section = response.first
322
- assert_equal(
323
- "#{@base_api_url}/tags/authors/justin-thyme.json",
324
- first_section['id']
325
- )
326
- end
327
-
328
- it "returns a sorted list of tags of a given type" do
329
- content_api_has_sorted_tags("author", "alphabetical", ["justin-thyme"])
330
- response = @api.tags("author", sort: "alphabetical")
331
-
332
- first_section = response.first
333
- assert_equal(
334
- "#{@base_api_url}/tags/authors/justin-thyme.json",
335
- first_section['id']
336
- )
337
- end
338
-
339
- it "returns draft tags if requested" do
340
- content_api_has_draft_and_live_tags(type: "specialist", draft: ["draft-tag-1"], live: ["live-tag-1"])
341
-
342
- all_tags = @api.tags("specialist", draft: true)
343
- assert_equal(
344
- [
345
- ["draft-tag-1", "draft"],
346
- ["live-tag-1", "live"]
347
- ].to_set,
348
- all_tags.map { |t| [t['slug'], t['state']] }.to_set
349
- )
350
-
351
- live_tags = @api.tags("specialist")
352
- assert_equal(
353
- [["live-tag-1", "live"]],
354
- live_tags.map { |t| [t['slug'], t['state']] }
355
- )
356
- end
357
-
358
- it "returns a list of root tags of a given type" do
359
- content_api_has_root_tags("author", ["oliver-sudden", "percy-vere"])
360
- response = @api.root_tags("author")
361
-
362
- # Old-style dictionary access
363
- first_section = response["results"][0]
364
- assert_equal "#{@base_api_url}/tags/authors/oliver-sudden.json", first_section["id"]
365
-
366
- # Also check attribute access
367
- first_section = response.first
368
- assert_equal(
369
- "#{@base_api_url}/tags/authors/oliver-sudden.json",
370
- first_section['id']
371
- )
372
- end
373
-
374
- it "returns a list of child tags of a given type" do
375
- content_api_has_child_tags("genre", "indie", ["indie/indie-rock"])
376
- response = @api.child_tags("genre", "indie")
377
-
378
- # Old-style dictionary access
379
- first_section = response["results"][0]
380
- assert_equal "#{@base_api_url}/tags/genres/indie%2Findie-rock.json", first_section["id"]
381
-
382
- # Also check attribute access
383
- first_section = response.first
384
- assert_equal(
385
- "#{@base_api_url}/tags/genres/indie%2Findie-rock.json",
386
- first_section['id']
387
- )
388
- end
389
-
390
- it "returns a sorted list of child tags of a given type" do
391
- content_api_has_sorted_child_tags("genre", "indie", "alphabetical", ["indie/indie-rock"])
392
- response = @api.child_tags("genre", "indie", sort: "alphabetical")
393
-
394
- first_section = response.first
395
- assert_equal(
396
- "#{@base_api_url}/tags/genres/indie%2Findie-rock.json",
397
- first_section['id']
398
- )
399
- end
400
-
401
- it "returns tag information for a section" do
402
- content_api_has_section("crime-and-justice")
403
- response = @api.tag("crime-and-justice", "section")
404
-
405
- assert_equal "Crime and justice", response["title"]
406
- end
407
-
408
- it "returns tag information for a tag and tag type" do
409
- content_api_has_tag("genre", "reggae")
410
- response = @api.tag("reggae", "genre")
411
-
412
- assert_equal "Reggae", response['title']
413
- end
414
-
415
- it "permits cache busting" do
416
- content_api_has_tags("specialist_sector", ["oil-and-gas/licensing"])
417
-
418
- @api.tags("specialist_sector")
419
- assert_requested :get, "#{@base_api_url}/tags.json?type=specialist_sector"
420
-
421
- @api.tags("specialist_sector", bust_cache: true)
422
- assert_requested :get, Regexp.new("#{@base_api_url}/tags.json\\?cachebust=\\d+&type=specialist_sector")
423
- end
424
- end
425
-
426
- describe "licence" do
427
- it "should return an artefact with licence for a snac code" do
428
- content_api_has_an_artefact_with_snac_code("licence-example", '1234', "title" => "Licence Example",
429
- "slug" => "licence-example",
430
- "details" => {
431
- "licence" => {
432
- "location_specific" => false,
433
- "availability" => %w(England Wales),
434
- "authorities" => []
435
- }
436
- })
437
- response = @api.artefact('licence-example', snac: '1234')
438
-
439
- assert_equal "Licence Example", response["title"]
440
- assert_equal %w(England Wales), response["details"]["licence"]["availability"]
441
- end
442
-
443
- it "should escape snac code when searching for licence" do
444
- stub_request(:get, "#{@base_api_url}/licence-example.json?snac=snacks%21").
445
- to_return(status: 200,
446
- body: { "test" => "ing" }.to_json,
447
- headers: {})
448
-
449
- @api.artefact("licence-example", snac: "snacks!")
450
-
451
- assert_requested :get, "#{@base_api_url}/licence-example.json?snac=snacks%21"
452
- end
453
-
454
- it "should return an unpublished artefact with a snac code" do
455
- body = artefact_for_slug('licence-example')
456
- url = "#{@base_api_url}/licence-example.json?snac=1234&edition=1"
457
- stub_request(:get, url).to_return(status: 200, body: body.to_json)
458
-
459
- api = GdsApi::ContentApi.new(@base_api_url, bearer_token: 'MY_BEARER_TOKEN')
460
- response = api.artefact('licence-example', snac: '1234', edition: '1')
461
-
462
- assert_equal "Licence example", response["title"]
463
- end
464
- end
465
-
466
- describe "local authorities" do
467
- it "should raise if no local authority found" do
468
- stub_request(:get, "#{@base_api_url}/local_authorities/does-not-exist.json").
469
- with(headers: GdsApi::JsonClient.default_request_headers).
470
- to_return(status: 404,
471
- body: { "_response_info" => { "status" => "ok" } }.to_json,
472
- headers: {})
473
-
474
- assert_raises(GdsApi::HTTPNotFound) do
475
- @api.local_authority("does-not-exist")
476
- end
477
- end
478
-
479
- it "should produce a LocalAuthority hash for an existing snac code" do
480
- body_response = {
481
- "name" => "Solihull Metropolitan Borough Council",
482
- "snac_code" => "00CT",
483
- "id" => "#{@base_api_url}/local_authorities/00CT.json",
484
- "_response_info" => { "status" => "ok" }
485
- }
486
-
487
- stub_request(:get, "#{@base_api_url}/local_authorities/00CT.json").
488
- with(headers: GdsApi::JsonClient.default_request_headers).
489
- to_return(status: 200,
490
- body: body_response.to_json,
491
- headers: {})
492
-
493
- response = @api.local_authority("00CT").to_hash
494
-
495
- assert_equal body_response, response
496
- end
497
-
498
- it "should return an empty result set if name not found" do
499
- body_response = {
500
- "_response_info" => { "status" => "ok" },
501
- "description" => "Local Authorities",
502
- "total" => 0,
503
- "results" => []
504
- }.to_json
505
-
506
- stub_request(:get, "#{@base_api_url}/local_authorities.json?name=Swansalona").
507
- with(headers: GdsApi::JsonClient.default_request_headers).
508
- to_return(status: 200,
509
- body: body_response,
510
- headers: {})
511
-
512
- response = @api.local_authorities_by_name("Swansalona")
513
-
514
- assert_equal 0, response["total"]
515
- assert_equal [], response["results"]
516
- end
517
-
518
- it "should return an empty result set if snac code not found" do
519
- body_response = {
520
- "_response_info" => { "status" => "ok" },
521
- "description" => "Local Authorities",
522
- "total" => 0,
523
- "results" => []
524
- }.to_json
525
-
526
- stub_request(:get, "#{@base_api_url}/local_authorities.json?snac_code=SNACKS").
527
- with(headers: GdsApi::JsonClient.default_request_headers).
528
- to_return(status: 200,
529
- body: body_response,
530
- headers: {})
531
-
532
- response = @api.local_authorities_by_snac_code("SNACKS")
533
-
534
- assert_equal 0, response["total"]
535
- assert_equal [], response["results"]
536
- end
537
-
538
- it "should have an array of results for a name search" do
539
- body_response = {
540
- "_response_info" => { "status" => "ok" },
541
- "description" => "Local Authorities",
542
- "total" => 2,
543
- "results" => [{
544
- "name" => "Swansalona Council",
545
- "snac_code" => "00VT",
546
- "id" => "#{@base_api_url}/local_authorities/00VT.json"
547
- },
548
- {
549
- "name" => "Swansea Council",
550
- "snac_code" => "00CT",
551
- "id" => "#{@base_api_url}/local_authorities/00VT.json"
552
- }]
553
- }.to_json
554
-
555
- stub_request(:get, "#{@base_api_url}/local_authorities.json?name=Swans").
556
- with(headers: GdsApi::JsonClient.default_request_headers).
557
- to_return(status: 200,
558
- body: body_response,
559
- headers: {})
560
-
561
- response = @api.local_authorities_by_name("Swans")
562
-
563
- assert_equal 2, response["total"]
564
- assert_equal "Swansalona Council", response["results"][0]["name"]
565
- end
566
-
567
- it "should escape snac code when calling unique a local authority" do
568
- stub_request(:get, "#{@base_api_url}/local_authorities/escape%21.json").
569
- to_return(status: 200,
570
- body: { "test" => "ing" }.to_json,
571
- headers: {})
572
-
573
- @api.local_authority("escape!")
574
-
575
- assert_requested :get, "#{@base_api_url}/local_authorities/escape%21.json"
576
- end
577
-
578
- it "should escape name when searching for local authorities" do
579
- stub_request(:get, "#{@base_api_url}/local_authorities.json?name=name%21").
580
- to_return(status: 200,
581
- body: { "test" => "ing" }.to_json,
582
- headers: {})
583
-
584
- @api.local_authorities_by_name("name!")
585
-
586
- assert_requested :get, "#{@base_api_url}/local_authorities.json?name=name%21"
587
- end
588
-
589
- it "should escape snac code when searching for local authorities" do
590
- stub_request(:get, "#{@base_api_url}/local_authorities.json?snac_code=snacks%21").
591
- to_return(status: 200,
592
- body: { "test" => "ing" }.to_json,
593
- headers: {})
594
-
595
- @api.local_authorities_by_snac_code("snacks!")
596
-
597
- assert_requested :get, "#{@base_api_url}/local_authorities.json?snac_code=snacks%21"
598
- end
599
- end
600
-
601
- describe "business support schemes" do
602
- it "should query content_api for business_support_schemes" do
603
- stub_request(:get, %r{\A#{@base_api_url}/business_support_schemes.json}).
604
- to_return(status: 200, body: { "foo" => "bar" }.to_json)
605
-
606
- response = @api.business_support_schemes(drink: "coffee")
607
-
608
- assert_equal({ "foo" => "bar" }, response.to_hash)
609
- assert_requested :get, "#{@base_api_url}/business_support_schemes.json?drink=coffee", times: 1
610
- end
611
-
612
- it "should raise an error if content_api returns 404" do
613
- stub_request(:get, %r{\A#{@base_api_url}/business_support_schemes.json}).
614
- to_return(status: 404, body: "Not Found")
615
-
616
- assert_raises GdsApi::HTTPNotFound do
617
- @api.business_support_schemes(%w(foo bar))
618
- end
619
- end
620
-
621
- it "should raise an error if content_api returns a 50x error" do
622
- stub_request(:get, %r{\A#{@base_api_url}/business_support_schemes.json}).
623
- to_return(status: 503, body: "Gateway timeout")
624
-
625
- assert_raises GdsApi::HTTPServerError do
626
- @api.business_support_schemes(%w(foo bar))
627
- end
628
- end
629
-
630
- describe "test helpers" do
631
- it "should have representative test helpers" do
632
- setup_content_api_business_support_schemes_stubs
633
- s1 = { "title" => "Scheme 1", "format" => "business_support" }
634
- content_api_has_business_support_scheme(s1, locations: "england", sectors: "farming")
635
- s2 = { "title" => "Scheme 2", "format" => "business_support" }
636
- content_api_has_business_support_scheme(s2, sectors: "farming")
637
- s3 = { "title" => "Scheme 3", "format" => "business_support" }
638
- content_api_has_business_support_scheme(s3, locations: "england", sectors: "farming")
639
-
640
- response = @api.business_support_schemes(locations: "england", sectors: "farming").to_hash
641
-
642
- assert_equal 2, response["total"]
643
- assert_equal s1["title"], response["results"].first["title"]
644
- assert_equal s3["title"], response["results"].last["title"]
645
- end
646
- end
647
- end
648
-
649
- describe "getting licence details" do
650
- it "should get licence details" do
651
- setup_content_api_licences_stubs
652
-
653
- content_api_has_licence licence_identifier: "1234", title: 'Test Licence 1', slug: 'test-licence-1',
654
- licence_short_description: 'A short description'
655
- content_api_has_licence licence_identifier: "1235", title: 'Test Licence 2', slug: 'test-licence-2',
656
- licence_short_description: 'A short description'
657
- content_api_has_licence licence_identifier: "AB1234", title: 'Test Licence 3', slug: 'test-licence-3',
658
- licence_short_description: 'A short description'
659
-
660
- results = @api.licences_for_ids([1234, 'AB1234', 'something'])['results']
661
- assert_equal 2, results.size
662
- assert_equal(
663
- %w(1234 AB1234),
664
- results.map { |r| r['details']['licence_identifier'] }
665
- )
666
- assert_equal(
667
- ['Test Licence 1', 'Test Licence 3'],
668
- results.map { |item| item['title'] }.sort
669
- )
670
- assert_equal(
671
- [
672
- 'http://www.test.gov.uk/test-licence-1',
673
- 'http://www.test.gov.uk/test-licence-3'
674
- ],
675
- results.map { |item| item['web_url'] }.sort
676
- )
677
- assert_equal(
678
- 'A short description',
679
- results[0]['details']['licence_short_description']
680
- )
681
- assert_equal(
682
- 'A short description',
683
- results[1]['details']['licence_short_description']
684
- )
685
- end
686
-
687
- it "should return empty array with no licences" do
688
- setup_content_api_licences_stubs
689
-
690
- assert_equal [], @api.licences_for_ids([123, 124])['results']
691
- end
692
-
693
- it "should raise an error if publisher returns an error" do
694
- stub_request(:get, %r[\A#{@base_api_url}/licences]).
695
- to_return(status: [503, "Service temporarily unabailable"])
696
-
697
- assert_raises GdsApi::HTTPServerError do
698
- @api.licences_for_ids([123, 124])
699
- end
700
- end
701
- end
702
-
703
- def api_response_for_results(results)
704
- {
705
- "_response_info" => {
706
- "status" => "ok",
707
- },
708
- "total" => results.size,
709
- "results" => results,
710
- }
711
- end
712
- end