Skip to content
Merged
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
31 changes: 31 additions & 0 deletions app/controllers/idv/in_person/address_search_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Idv
module InPerson
class AddressSearchController < ApplicationController
include RenderConditionConcern

check_or_render_not_found -> { IdentityConfig.store.arcgis_search_enabled }

def index
render json: addresses
end

protected

def addresses
suggestion = geocoder.suggest(permitted_params[:address]).first
return [] unless suggestion
geocoder.find_address_candidates(suggestion.magic_key).slice(0, 1)
rescue Faraday::ConnectionFailed
[]
end

def geocoder
@geocoder ||= ArcgisApi::Geocoder.new
end

def permitted_params
params.permit(:address)
end
end
end
end
1 change: 1 addition & 0 deletions config/application.yml.default
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ address_identity_proofing_supported_country_codes: '["AS", "GU", "MP", "PR", "US
arcgis_api_root_url: 'https://gis.gsa.gov'
arcgis_api_username: ''
arcgis_api_password: ''
arcgis_search_enabled: false
asset_host: ''
async_wait_timeout_seconds: 60
async_stale_job_timeout_seconds: 300
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@
as: :in_person_ready_to_verify
get '/in_person/usps_locations' => 'in_person/usps_locations#index'
put '/in_person/usps_locations' => 'in_person/usps_locations#update'
post '/in_person/addresses' => 'in_person/address_search#index'
get '/in_person/:step' => 'in_person#show', as: :in_person_step
put '/in_person/:step' => 'in_person#update'

Expand Down
1 change: 1 addition & 0 deletions lib/identity_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def self.build_store(config_map)
config.add(:arcgis_api_root_url, type: :string)
config.add(:arcgis_api_username, type: :string)
config.add(:arcgis_api_password, type: :string)
config.add(:arcgis_search_enabled, type: :boolean)
config.add(:aws_http_retry_limit, type: :integer)
config.add(:aws_http_retry_max_delay, type: :integer)
config.add(:aws_http_timeout, type: :integer)
Expand Down
86 changes: 86 additions & 0 deletions spec/controllers/idv/in_person/address_search_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
require 'rails_helper'

describe Idv::InPerson::AddressSearchController do
include IdvHelper

let(:user) { create(:user) }
let(:sp) { nil }
let(:arcgis_search_enabled) { true }

before do
stub_analytics
stub_sign_in(user) if user
allow(IdentityConfig.store).to receive(:arcgis_search_enabled).
and_return(arcgis_search_enabled)
allow(controller).to receive(:current_sp).and_return(sp)
end

describe '#index' do
let(:geocoder) { double('Geocoder') }
let(:suggestions) do
[
OpenStruct.new({ magic_key: 'a' }),
]
end

let(:addresses) do
[
{ name: 'Address 1' },
{ name: 'Address 2' },
{ name: 'Address 3' },
{ name: 'Address 4' },
]
end
subject(:response) { get :index }

before do
allow(controller).to receive(:geocoder).and_return(geocoder)
allow(geocoder).to receive(:find_address_candidates).and_return(addresses)
allow(geocoder).to receive(:suggest).and_return(suggestions)
end

context 'with successful fetch' do
it 'gets successful response' do
response = get :index
json = response.body
addresses = JSON.parse(json)
expect(addresses.length).to eq 1
end

context 'with no suggestions' do
let(:suggestions) do
[]
end

it 'returns empty array' do
response = get :index
json = response.body
addresses = JSON.parse(json)
expect(addresses.length).to eq 0
end
end
end

context 'with unsuccessful fetch' do
before do
exception = Faraday::ConnectionFailed.new('error')
allow(geocoder).to receive(:suggest).and_raise(exception)
end

it 'gets an empty pilot response' do
response = get :index
json = response.body
addresses = JSON.parse(json)
expect(addresses.length).to eq 0
end
end

context 'with feature disabled' do
let(:arcgis_search_enabled) { false }

it 'renders 404' do
expect(response.status).to eq(404)
end
end
end
end