Skip to content

[DO NOT MERGE] Local authority api with Fake results (leave in place for the moment) #4720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/controllers/api/base_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Api
class BaseController < ApplicationController
end
end
66 changes: 66 additions & 0 deletions app/controllers/api/local_authority_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
module Api
class LocalAuthorityController < Api::BaseController
def index
return fake_index_query if Rails.env.production?

postcode_lookup = PostcodeLookup.new(postcode)

if postcode_lookup.local_custodian_codes.count == 1
local_authority = LocalAuthority.from_local_custodian_code(postcode_lookup.local_custodian_codes.first)
redirect_to "/api/local-authority/#{local_authority.slug}"
else
@address_list_presenter = AddressListPresenter.new(postcode_lookup.addresses)
render json: { addresses: @address_list_presenter.addresses_with_authority_data }
end
rescue LocationError => e
status = e.postcode_error == "invalidPostcodeFormat" ? :bad_request : :not_found
render json: { errors: { postcode: [I18n.t(e.message)] } }, status:
end

def show
@local_authority = Rails.env.production? ? fake_local_authority : LocalAuthority.from_slug(params[:authority_slug])

render json: { local_authority: @local_authority.to_h }
rescue GdsApi::HTTPNotFound
render json: { errors: { local_authority_slug: ["Not found"] } }, status: :not_found
end

private

FAKE_ADDRESS_LIST = [
{ address: "PEARDOWN HOUSE, FENAPPLE CLOSE, BREAM ROAD, WEST THYME, APPLETON, SW1A 2BB", slug: "dorset", name: "Dorset Council" },
{ address: "STRAWBERRY COTTAGE, FENAPPLE CLOSE, BREAM ROAD, WEST THYME, APPLETON, SW1A 2BB", slug: "westminster", name: "City of Westminster" },
].freeze

def fake_index_query
raise LocationError, "invalidPostcodeFormat" if postcode.blank?

case postcode
when "BAD"
raise LocationError, "invalidPostcodeFormat"
when "SW1A 2AA"
redirect_to "/api/local-authority/westminster"
when "SW1A 2BB"
render json: { addresses: FAKE_ADDRESS_LIST }
else
raise LocationError, "noLaMatch"
end
end

def fake_local_authority
case params[:authority_slug]
when "missing"
raise GdsApi::HTTPNotFound, 404
when "derbyshire-dales"
parent = LocalAuthority.new({ "slug" => "derbyshire", "name" => "Derbyshire County Council", "tier" => "county", "homepage_url" => "https://www.gov.uk" })
LocalAuthority.new({ "slug" => "derbyshire-dales", "name" => "Derbyshire Dales District Council", "tier" => "district", "homepage_url" => "https://www.gov.uk" }, parent:)
else
LocalAuthority.new({ "slug" => params[:authority_slug], "name" => params[:authority_slug].gsub("-", " ").titleize, "tier" => "unitary", "homepage_url" => "https://www.gov.uk" })
end
end

def postcode
@postcode ||= PostcodeSanitizer.sanitize(params[:postcode])
end
end
end
10 changes: 10 additions & 0 deletions app/models/local_authority.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ def initialize(map, parent: nil)
@parent = parent
end

def to_h
{
name:,
homepage_url:,
tier:,
slug:,
parent: parent.to_h,
}.compact_blank
end

private_class_method def self.make_from_api_response(response)
if response["local_authorities"].count == 2
parent = LocalAuthority.new(response["local_authorities"].reject { |la| la["tier"] == "district" }.first)
Expand Down
19 changes: 16 additions & 3 deletions app/presenters/address_list_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
class AddressListPresenter
attr_reader :options

def initialize(addresses)
@options = addresses.map do |address|
@addresses = addresses
end

def options
@options ||= @addresses.map do |address|
{
text: address.address,
value: LocalAuthority.from_local_custodian_code(address.local_custodian_code).slug,
}
end
end

def addresses_with_authority_data
@addresses_with_authority_data ||= @addresses.map do |address|
local_authority = LocalAuthority.from_local_custodian_code(address.local_custodian_code)
{
address: address.address,
slug: local_authority.slug,
name: local_authority.name,
}
end
end
end
7 changes: 7 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
get "/development", to: "development#index"
end

scope "/api" do
scope "/local-authority" do
get "/" => "api/local_authority#index"
get "/:authority_slug" => "api/local_authority#show"
end
end

get "/find-local-council" => "find_local_council#index"
post "/find-local-council" => "find_local_council#find"
get "/find-local-council/multiple_authorities" => "find_local_council#multiple_authorities"
Expand Down
128 changes: 128 additions & 0 deletions spec/requests/local_authority_api_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
require "gds_api/test_helpers/local_links_manager"

RSpec.describe "Local Authority API" do
include GdsApi::TestHelpers::LocalLinksManager
include LocationHelpers

before { content_store_has_random_item(base_path: "/api/local-authority") }

describe "querying" do
it "returns a 400 if there is no query parameter" do
get "/api/local-authority"

expect(response).to have_http_status(:bad_request)
end

it "returns a 400 if the postcode is empty" do
get "/api/local-authority?postcode="

expect(response).to have_http_status(:bad_request)
end

it "returns a 400 if the postcode is invalid" do
stub_locations_api_does_not_have_a_bad_postcode("ZZZ")
get "/api/local-authority?postcode=ZZZ"

expect(response).to have_http_status(:bad_request)
end

it "returns a 400 if the postcode is not found" do
stub_locations_api_has_no_location("SW12 1ZZ")
get "/api/local-authority?postcode=SW121ZZ"

expect(response).to have_http_status(:not_found)
end

context "when a postcode is matched to a single authority" do
before { configure_locations_api_and_local_authority("SW1A 1AA", %w[westminster], 5990) }

it "redirects to the a 404 if there is no query parameter" do
get "/api/local-authority?postcode=SW1A1AA"

expect(response).to redirect_to("/api/local-authority/westminster")
end
end

context "when a postcode spans multiple authorities" do
before do
stub_locations_api_has_location(
"CH25 9BJ",
[
{ "address" => "House 1", "local_custodian_code" => "1" },
{ "address" => "House 2", "local_custodian_code" => "2" },
{ "address" => "House 3", "local_custodian_code" => "3" },
],
)
stub_local_links_manager_has_a_local_authority("achester", local_custodian_code: 1)
stub_local_links_manager_has_a_local_authority("beechester", local_custodian_code: 2)
stub_local_links_manager_has_a_local_authority("ceechester", local_custodian_code: 3)
end

it "returns a list of addresses to choose from" do
get "/api/local-authority?postcode=CH259BJ"

expect(response).to have_http_status(:ok)
expect(response.parsed_body).to eq({
"addresses" => [
{ "address" => "House 1", "slug" => "achester", "name" => "Achester" },
{ "address" => "House 2", "slug" => "beechester", "name" => "Beechester" },
{ "address" => "House 3", "slug" => "ceechester", "name" => "Ceechester" },
],
})
end
end
end

describe "local authority slugs" do
context "when the slug is not found"
before { stub_local_links_manager_does_not_have_an_authority("foo") }

it "returns a 404 if the slug is not found" do
get "/api/local-authority/foo"

expect(response).to have_http_status(:not_found)
end

context "when the slug points to a unitary authority" do
before { stub_local_links_manager_has_a_local_authority("westminster") }

it "returns the authority with no parent and tier set to unitary" do
get "/api/local-authority/westminster"

expect(response).to have_http_status(:ok)
expect(response.parsed_body).to eq({
"local_authority" => {
"name" => "Westminster",
"homepage_url" => "http://westminster.example.com",
"tier" => "unitary",
"slug" => "westminster",
},
})
end
end

context "when the slug points to a district authority" do
before { stub_local_links_manager_has_a_district_and_county_local_authority("aylesbury", "buckinghamshire") }

it "returns the authority with the parent and tier set to district" do
get "/api/local-authority/aylesbury"

expect(response).to have_http_status(:ok)
expect(response.parsed_body).to eq({
"local_authority" => {
"name" => "Aylesbury",
"homepage_url" => "http://aylesbury.example.com",
"tier" => "district",
"slug" => "aylesbury",
"parent" => {
"name" => "Buckinghamshire",
"homepage_url" => "http://buckinghamshire.example.com",
"tier" => "county",
"slug" => "buckinghamshire",
},
},
})
end
end
end
end