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

#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’



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

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



88
89
90
# File 'lib/gds_api/test_helpers/worldwide.rb', line 88

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

#worldwide_api_does_not_have_location(location_slug) ⇒ Object



70
71
72
# File 'lib/gds_api/test_helpers/worldwide.rb', line 70

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

#worldwide_api_has_location(location_slug, details = nil) ⇒ Object



64
65
66
67
68
# File 'lib/gds_api/test_helpers/worldwide.rb', line 64

def 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

#worldwide_api_has_locations(location_slugs) ⇒ Object

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 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
50
51
# File 'lib/gds_api/test_helpers/worldwide.rb', line 16

def worldwide_api_has_locations(location_slugs)
  location_slugs.each {|s| 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

#worldwide_api_has_no_organisations_for_location(location_slug) ⇒ Object



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

def 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

#worldwide_api_has_organisations_for_location(location_slug, json_or_hash) ⇒ Object



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

def 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

#worldwide_api_has_selection_of_locationsObject



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

def worldwide_api_has_selection_of_locations
  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