-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created
AddressValidationController
(#14572)
created endpoint to validate user's address and make suggestions. Issue #444 Co-authored-by: khoa-v-nguyen <[email protected]>
- Loading branch information
1 parent
da8ae2c
commit dc6750e
Showing
3 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
modules/ask_va_api/app/controllers/ask_va_api/v0/address_validation_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
modules/ask_va_api/spec/requests/v0/address_validation_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |