Skip to content

Commit

Permalink
Correct invalid address_pou params when initializing VAProfile V3 Add…
Browse files Browse the repository at this point in the history
…ress objects (#20325)

* Correct the address_pou when initializing VAProfile V3 Address objects

* fixing spec

* undo commit
  • Loading branch information
RachalCassity authored Jan 17, 2025
1 parent f2c698f commit 7189935
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 13 deletions.
2 changes: 0 additions & 2 deletions app/controllers/v0/profile/address_validation_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ def create
VAProfile::Models::ValidationAddress.new(address_params)
end

Rails.logger.info("Staging Address params: #{address_params}") if Settings.vsp_environment == 'staging'
Rails.logger.info("Staging validation Address: #{address}") if Settings.vsp_environment == 'staging'
Rails.logger.info("Staging Address valid: #{address.valid?}") if Settings.vsp_environment == 'staging'

raise Common::Exceptions::ValidationErrors, address unless address.valid?
Expand Down
5 changes: 1 addition & 4 deletions lib/va_profile/models/v3/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,11 @@ def in_json_v2
# @return [VAProfile::Models::V3::Address] the model built from the response body
# rubocop:disable Metrics/MethodLength
def self.build_from(body)
# Ensure address_pou is RESIDENCE OR CORRESPONDENCE. RESIDENCE/CHOICE is no longer valid.
address_pou = body['address_pou'].include?('RESIDENCE') ? 'RESIDENCE' : 'CORRESPONDENCE'

VAProfile::Models::V3::Address.new(
address_line1: body['address_line1'],
address_line2: body['address_line2'],
address_line3: body['address_line3'],
address_pou: address_pou,
address_pou: body['address_pou'],
address_type: body['address_type'].upcase,
bad_address: body['bad_address'],
city: body['city_name'],
Expand Down
7 changes: 7 additions & 0 deletions lib/va_profile/models/v3/base_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ class BaseAddress < Base
validates :county_code, absence: true
end

def initialize(attributes = {})
# Address_pou RESIDENCE/CHOICE was changed to RESIDENCE for Contact Information API v2
# This will update the address_pou when address_pou is RESIDENCE/CHOICE
super
self.address_pou = 'RESIDENCE' if address_pou == 'RESIDENCE/CHOICE'
end

def ascii_only
address = [
address_line1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class AddressValidationController < ApplicationController
service_tag 'profile'

def create
validation_model = if Flipper.enabled?(:va_v3_contact_information_service, @current_user)
VAProfile::Models::V3::ValidationAddress
else
VAProfile::Models::ValidationAddress
end
address = validation_model.new(address_params)
address = if Flipper.enabled?(:va_v3_contact_information_service)
VAProfile::Models::V3::ValidationAddress.new(address_params)
else
VAProfile::Models::ValidationAddress.new(address_params)
end

raise Common::Exceptions::ValidationErrors, address unless address.valid?

Rails.logger.warn('AddressValidationController#create request completed', sso_logging_info)
Expand Down Expand Up @@ -47,7 +47,7 @@ def address_params
end

def service
@service ||= if Flipper.enabled?(:va_v3_contact_information_service, @current_user)
@service ||= if Flipper.enabled?(:va_v3_contact_information_service)
VAProfile::V3::AddressValidation::Service.new
else
VAProfile::AddressValidation::Service.new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,165 @@
end
end
end

describe 'contact information v2' do
let(:user) { build(:user) }
let(:multiple_match_addr) { build(:va_profile_v3_address, :multiple_matches) }
let(:invalid_address) { build(:va_profile_v3_validation_address).to_h }
let(:incorrect_address_pou) { build(:va_profile_v3_address, :incorrect_address_pou) }

before do
allow(Flipper).to receive(:enabled?).with(:va_v3_contact_information_service).and_return(true)
end

shared_examples 'invalid address' do
it 'returns an error' do
post '/ask_va_api/v0/address_validation', params: { address: invalid_address }
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

shared_examples 'found address' do
it 'returns suggested addresses for a given address' do
VCR.use_cassette('va_profile/v3/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'
}
}
],
'validation_key' => -646_932_106
)
expect(response).to have_http_status(:ok)
end
end
end

context 'request contains invalid address_pou params' do
it 'returns a valid address' do
VCR.use_cassette('va_profile/v3/address_validation/candidate_multiple_matches', VCR::MATCH_EVERYTHING) do
post '/ask_va_api/v0/address_validation', params: { address: incorrect_address_pou.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'
}
}
],
'validation_key' => '-646932106'
)
expect(response).to have_http_status(:ok)
end
end
end
end
end
53 changes: 53 additions & 0 deletions spec/controllers/v0/profile/address_validation_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
let(:user) { build(:user) }
let(:multiple_match_addr) { build(:va_profile_v3_address, :multiple_matches) }
let(:invalid_address) { build(:va_profile_v3_validation_address).to_h }
let(:incorrect_address_pou) { build(:va_profile_v3_address, :incorrect_address_pou) }

before do
allow(Flipper).to receive(:enabled?).with(:va_v3_contact_information_service).and_return(true)
Expand Down Expand Up @@ -233,6 +234,58 @@
end
end

context 'request contains invalid address_pou params' do
it 'returns a valid address' do
VCR.use_cassette('va_profile/v3/address_validation/candidate_multiple_matches', VCR::MATCH_EVERYTHING) do
post(:create, params: { address: incorrect_address_pou.to_h })
expect(response.status).to eq(200)
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'
}
}
],
'validation_key' => '-646932106'
)
end
end
end

context 'with user signed in' do
before { sign_in_as(user) }

Expand Down
8 changes: 8 additions & 0 deletions spec/factories/va_profile/v3/addresses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@
zip_code { '11249' }
end

trait :incorrect_address_pou do
address_line1 { '37 1st st' }
city { 'Brooklyn' }
state_code { 'NY' }
zip_code { '11249' }
address_pou { 'RESIDENCE/CHOICE' }
end

trait :override do
address_pou { VAProfile::Models::V3::Address::CORRESPONDENCE }
address_line1 { '1494 Martin Luther King Rd' }
Expand Down
10 changes: 10 additions & 0 deletions spec/lib/va_profile/v2/contact_information/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@
end
end

context 'when the address_pou is invalid' do
it 'returns a status of 200' do
VCR.use_cassette('va_profile/v2/contact_information/post_address_success_invalid_pou', VCR::MATCH_EVERYTHING) do
address = build(:va_profile_v3_address, :incorrect_address_pou)
response = subject.post_address(address)
expect(response).to be_ok
end
end
end

context 'when an ID is included' do
it 'raises an exception' do
VCR.use_cassette('va_profile/v2/contact_information/post_address_w_id_error', VCR::MATCH_EVERYTHING) do
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7189935

Please sign in to comment.