Module: GdsApi::TestHelpers::Worldwide

Includes:
CommonResponses
Defined in:
lib/gds_api/test_helpers/worldwide.rb

Constant Summary collapse

WORLDWIDE_API_ENDPOINT =
Plek.current.find('whitehall-admin')

Instance Method Summary collapse

Methods included from CommonResponses

#acronymize_slug, #plural_response_base, #response_base, #titleize_slug

Instance Method Details

#stub_worldwide_api_does_not_have_location(location_slug) ⇒ Object Also known as: worldwide_api_has_does_not_have_location



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

def stub_worldwide_api_does_not_have_location(location_slug)
  stub_request(:get, "#{WORLDWIDE_API_ENDPOINT}/api/world-locations/#{location_slug}").to_return(status: 404)
end

#stub_worldwide_api_has_location(location_slug, details = nil) ⇒ Object Also known as: worldwide_api_has_location



62
63
64
65
66
# File 'lib/gds_api/test_helpers/worldwide.rb', line 62

def stub_worldwide_api_has_location(location_slug, details = nil)
  details ||= world_location_for_slug(location_slug)
  stub_request(:get, "#{WORLDWIDE_API_ENDPOINT}/api/world-locations/#{location_slug}").
    to_return(status: 200, body: details.to_json)
end

#stub_worldwide_api_has_locations(location_slugs) ⇒ Object Also known as: worldwide_api_has_locations

Sets up the index endpoints for the given country slugs The stubs are setup to paginate in chunks of 20

This also sets up the individual endpoints for each slug by calling stub_worldwide_api_has_location below



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gds_api/test_helpers/worldwide.rb', line 16

def stub_worldwide_api_has_locations(location_slugs)
  location_slugs.each { |s| stub_worldwide_api_has_location(s) }
  pages = []
  location_slugs.each_slice(20) do |slugs|
    pages << slugs.map { |s| world_location_details_for_slug(s) }
  end

  pages.each_with_index do |page, i|
    page_details = plural_response_base.merge("results" => page,
      "total" => location_slugs.size,
      "pages" => pages.size,
      "current_page" => i + 1,
      "page_size" => 20,
      "start_index" => i * 20 + 1)

    links = { self: "#{WORLDWIDE_API_ENDPOINT}/api/world-locations?page=#{i + 1}" }
    links[:next] = "#{WORLDWIDE_API_ENDPOINT}/api/world-locations?page=#{i + 2}" if pages[i + 1]
    links[:previous] = "#{WORLDWIDE_API_ENDPOINT}/api/world-locations?page=#{i}" unless i == 0
    page_details["_response_info"]["links"] = []
    link_headers = []
    links.each do |rel, href|
      page_details["_response_info"]["links"] << { "rel" => rel, "href" => href }
      link_headers << "<#{href}>; rel=\"#{rel}\""
    end

    stub_request(:get, links[:self]).
      to_return(status: 200, body: page_details.to_json, headers: { "Link" => link_headers.join(", ") })
    if i == 0
      # First page exists at URL with and without page param
      stub_request(:get, links[:self].sub(/\?page=1/, '')).
        to_return(status: 200, body: page_details.to_json, headers: { "Link" => link_headers.join(", ") })
    end
  end
end

#stub_worldwide_api_has_no_organisations_for_location(location_slug) ⇒ Object Also known as: worldwide_api_has_no_organisations_for_location



79
80
81
82
83
84
# File 'lib/gds_api/test_helpers/worldwide.rb', line 79

def stub_worldwide_api_has_no_organisations_for_location(location_slug)
  details = { "results" => [], "total" => 0, "_response_info" => { "status" => "ok" } }
  url = "#{WORLDWIDE_API_ENDPOINT}/api/world-locations/#{location_slug}/organisations"
  stub_request(:get, url).
    to_return(status: 200, body: details.to_json, headers: { "Link" => "<#{url}; rel\"self\"" })
end

#stub_worldwide_api_has_organisations_for_location(location_slug, json_or_hash) ⇒ Object Also known as: worldwide_api_has_organisations_for_location



72
73
74
75
76
77
# File 'lib/gds_api/test_helpers/worldwide.rb', line 72

def stub_worldwide_api_has_organisations_for_location(location_slug, json_or_hash)
  json = json_or_hash.is_a?(Hash) ? json_or_hash.to_json : json_or_hash
  url = "#{WORLDWIDE_API_ENDPOINT}/api/world-locations/#{location_slug}/organisations"
  stub_request(:get, url).
    to_return(status: 200, body: json, headers: { "Link" => "<#{url}; rel\"self\"" })
end

#stub_worldwide_api_has_selection_of_locationsObject Also known as: worldwide_api_has_selection_of_locations



51
52
53
54
55
56
57
58
59
60
# File 'lib/gds_api/test_helpers/worldwide.rb', line 51

def stub_worldwide_api_has_selection_of_locations
  stub_worldwide_api_has_locations %w(
    afghanistan angola australia bahamas belarus brazil brunei cambodia chad
    croatia denmark eritrea france ghana iceland japan laos luxembourg malta
    micronesia mozambique nicaragua panama portugal sao-tome-and-principe singapore
    south-korea sri-lanka uk-delegation-to-council-of-europe
    uk-delegation-to-organization-for-security-and-co-operation-in-europe
    united-kingdom venezuela vietnam
  )
end

#world_location_details_for_slug(slug) ⇒ Object

Constructs a sample world_location

if the slug contains ‘delegation’ or ‘mission’ the format will be set to ‘International delegation’ othersiwe it will be set to ‘World location’



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/gds_api/test_helpers/worldwide.rb', line 94

def world_location_details_for_slug(slug)
  {
    "id" => "https://www.gov.uk/api/world-locations/#{slug}",
    "title" => titleize_slug(slug, title_case: true),
    "format" => (slug =~ /(delegation|mission)/ ? "International delegation" : "World location"),
    "updated_at" => "2013-03-25T13:06:42+00:00",
    "web_url" => "https://www.gov.uk/government/world/#{slug}",
    "details" => {
      "slug" => slug,
      "iso2" => slug[0..1].upcase,
    },
    "organisations" => {
      "id" => "https://www.gov.uk/api/world-locations/#{slug}/organisations",
      "web_url" => "https://www.gov.uk/government/world/#{slug}#organisations"
    },
  }
end

#world_location_for_slug(slug) ⇒ Object



86
87
88
# File 'lib/gds_api/test_helpers/worldwide.rb', line 86

def world_location_for_slug(slug)
  singular_response_base.merge(world_location_details_for_slug(slug))
end