Skip to content

Commit

Permalink
Created AddressValidationController (#14572)
Browse files Browse the repository at this point in the history
created endpoint to validate user's address and
make suggestions.

Issue #444

Co-authored-by: khoa-v-nguyen <[email protected]>
  • Loading branch information
Khoa-V-Nguyen and Khoa-V-Nguyen authored Nov 17, 2023
1 parent da8ae2c commit dc6750e
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

require 'va_profile/models/validation_address'
require 'va_profile/address_validation/service'

module AskVAApi
module V0
class AddressValidationController < ApplicationController
service_tag 'profile'

def create
address = VAProfile::Models::ValidationAddress.new(address_params)
raise Common::Exceptions::ValidationErrors, address unless address.valid?

Rails.logger.warn('AddressValidationController#create request completed', sso_logging_info)

render(json: service.address_suggestions(address))
end

private

def address_params
params.require(:address).permit(
:address_line1,
:address_line2,
:address_line3,
:address_pou,
:address_type,
:city,
:country_code_iso3,
:international_postal_code,
:province,
:state_code,
:zip_code,
:zip_code_suffix
)
end

def service
@service ||= VAProfile::AddressValidation::Service.new
end
end
end
end
3 changes: 3 additions & 0 deletions modules/ask_va_api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
get '/zipcodes', to: 'static_data#zipcodes'
get '/states', to: 'static_data#states'
get '/provinces', to: 'static_data#provinces'

# address_validation
post '/address_validation', to: 'address_validation#create'
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe AskVAApi::V0::AddressValidationController, type: :request do
let(:user) { FactoryBot.build(:user) }
let(:address) { build(:va_profile_address) }
let(:multiple_match_addr) do
build(:va_profile_address, :multiple_matches)
end

before do
sign_in_as(user)
end

describe '#create' do
context 'with an invalid address' do
it 'returns an error' do
post '/ask_va_api/v0/address_validation', params: { address: build(:va_profile_validation_address).to_h }
expect(response).to have_http_status(:unprocessable_entity)
expect(JSON.parse(response.body)).to eq(
'errors' => [
{
'title' => "Address line1 can't be blank",
'detail' => "address-line1 - can't be blank",
'code' => '100', 'source' =>
{ 'pointer' => 'data/attributes/address-line1' },
'status' => '422'
},
{
'title' => "City can't be blank",
'detail' => "city - can't be blank",
'code' => '100',
'source' => {
'pointer' => 'data/attributes/city'
},
'status' => '422'
},
{
'title' => "State code can't be blank",
'detail' => "state-code - can't be blank",
'code' => '100',
'source' => {
'pointer' => 'data/attributes/state-code'
},
'status' => '422'
},
{
'title' =>
"Zip code can't be blank",
'detail' => "zip-code - can't be blank",
'code' => '100',
'source' => {
'pointer' => 'data/attributes/zip-code'
},
'status' => '422'
}
]
)
end
end

context 'with a found address' do
it 'returns suggested addresses for a given address' do
VCR.use_cassette(
'va_profile/address_validation/candidate_multiple_matches',
VCR::MATCH_EVERYTHING
) do
post '/ask_va_api/v0/address_validation', params: { address: multiple_match_addr.to_h }
expect(JSON.parse(response.body)).to eq(
'addresses' => [
{
'address' => {
'address_line1' => '37 N 1st St',
'address_type' => 'DOMESTIC',
'city' => 'Brooklyn',
'country_name' => 'United States',
'country_code_iso3' => 'USA',
'county_code' => '36047',
'county_name' => 'Kings',
'state_code' => 'NY',
'zip_code' => '11249',
'zip_code_suffix' => '3939'
},
'address_meta_data' => {
'confidence_score' => 100.0,
'address_type' => 'Domestic',
'delivery_point_validation' => 'UNDELIVERABLE'
}
},
{
'address' => {
'address_line1' => '37 S 1st St',
'address_type' => 'DOMESTIC',
'city' => 'Brooklyn',
'country_name' => 'United States',
'country_code_iso3' => 'USA',
'county_code' => '36047',
'county_name' => 'Kings',
'state_code' => 'NY',
'zip_code' => '11249',
'zip_code_suffix' => '4101'
},
'address_meta_data' => {
'confidence_score' => 100.0,
'address_type' => 'Domestic',
'delivery_point_validation' => 'CONFIRMED',
'residential_delivery_indicator' => 'MIXED'
}
}
],
'validation_key' => -646_932_106
)
expect(response).to have_http_status(:ok)
end
end
end
end
end

0 comments on commit dc6750e

Please sign in to comment.