Module: GdsApi::TestHelpers::Organisations

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

Constant Summary collapse

ORGANISATIONS_API_ENDPOINT =
Plek.current.find('whitehall-admin')
PUBLIC_HOST =
Plek.current.find('www')

Instance Method Summary collapse

Methods included from CommonResponses

#acronymize_slug, #plural_response_base, #response_base, #titleize_slug

Instance Method Details

#organisation_details_for_slug(slug, content_id = SecureRandom.uuid) ⇒ Object

Constructs a sample organisation

if the slug contains 'ministry' the format will be set to 'Ministerial department' otherwise it will be set to 'Executive agency'


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/gds_api/test_helpers/organisations.rb', line 87

def organisation_details_for_slug(slug, content_id = SecureRandom.uuid)
  {
    "id" => "#{ORGANISATIONS_API_ENDPOINT}/api/organisations/#{slug}",
    "title" => titleize_slug(slug, title_case: true),
    "format" => (slug =~ /ministry/ ? "Ministerial department" : "Executive agency"),
    "updated_at" => "2013-03-25T13:06:42+00:00",
    "web_url" => "#{PUBLIC_HOST}/government/organisations/#{slug}",
    "details" => {
      "slug" => slug,
      "abbreviation" => acronymize_slug(slug),
      "logo_formatted_name" => titleize_slug(slug, title_case: true),
      "organisation_brand_colour_class_name" => slug,
      "organisation_logo_type_class_name" => (slug =~ /ministry/ ? "single-identity" : "eo"),
      "closed_at" => nil,
      "govuk_status" => (slug =~ /ministry/ ? "live" : "joining"),
      "content_id" => content_id,
    },
    "parent_organisations" => [
      {
        "id" => "#{ORGANISATIONS_API_ENDPOINT}/api/organisations/#{slug}-parent-1",
        "web_url" => "#{PUBLIC_HOST}/government/organisations/#{slug}-parent-1"
      },
    ],
    "child_organisations" => [
      {
        "id" => "#{ORGANISATIONS_API_ENDPOINT}/api/organisations/#{slug}-child-1",
        "web_url" => "#{PUBLIC_HOST}/government/organisations/#{slug}-child-1"
      },
    ],
  }
end

#organisation_for_slug(slug) ⇒ Object


79
80
81
# File 'lib/gds_api/test_helpers/organisations.rb', line 79

def organisation_for_slug(slug)
  singular_response_base.merge(organisation_details_for_slug(slug))
end

#organisations_api_does_not_have_organisation(organisation_slug) ⇒ Object


75
76
77
# File 'lib/gds_api/test_helpers/organisations.rb', line 75

def organisations_api_does_not_have_organisation(organisation_slug)
  stub_request(:get, "#{ORGANISATIONS_API_ENDPOINT}/api/organisations/#{organisation_slug}").to_return(status: 404)
end

#organisations_api_has_organisation(organisation_slug, details = nil) ⇒ Object


69
70
71
72
73
# File 'lib/gds_api/test_helpers/organisations.rb', line 69

def organisations_api_has_organisation(organisation_slug, details = nil)
  details ||= organisation_for_slug(organisation_slug)
  stub_request(:get, "#{ORGANISATIONS_API_ENDPOINT}/api/organisations/#{organisation_slug}").
    to_return(status: 200, body: details.to_json)
end

#organisations_api_has_organisations(organisation_slugs) ⇒ Object


14
15
16
17
# File 'lib/gds_api/test_helpers/organisations.rb', line 14

def organisations_api_has_organisations(organisation_slugs)
  bodies = organisation_slugs.map { |slug| organisation_for_slug(slug) }
  organisations_api_has_organisations_with_bodies(bodies)
end

#organisations_api_has_organisations_with_bodies(organisation_bodies) ⇒ Object

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

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


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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gds_api/test_helpers/organisations.rb', line 24

def organisations_api_has_organisations_with_bodies(organisation_bodies)
  # Stub API call to the endpoint for an individual organisation
  organisation_bodies.each do |body|
    slug = body["details"]["slug"]
    organisations_api_has_organisation(slug, body)
  end

  pages = []
  organisation_bodies.each_slice(20) do |bodies|
    pages << bodies
  end

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

    links = { self: "#{ORGANISATIONS_API_ENDPOINT}/api/organisations?page=#{i + 1}" }
    links[:next] = "#{ORGANISATIONS_API_ENDPOINT}/api/organisations?page=#{i + 2}" if pages[i + 1]
    links[:previous] = "#{ORGANISATIONS_API_ENDPOINT}/api/organisations?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

  if pages.empty?    # If there are no pages - and so no organisations specified - then stub /api/organisations.

    stub_request(:get, "#{ORGANISATIONS_API_ENDPOINT}/api/organisations").to_return(status: 200, body: plural_response_base.to_json, headers: {})
  end
end