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
35 changes: 29 additions & 6 deletions app/services/usps_in_person_proofing/enrollment_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,36 @@ def send_ready_to_verify_email(user, enrollment)
end
end

# Create and start tracking an in-person enrollment with USPS
#
# @param [InPersonEnrollment] enrollment The new enrollment record for tracking the enrollment
# @param [Pii::Attributes] pii The PII associated with the in-person enrollment
# @return [String] The enrollment code
# @raise [Exception::RequestEnrollException] Raised with a problem creating the enrollment
def create_usps_enrollment(enrollment, pii)
# Use the enrollment's unique_id value if it exists, otherwise use the deprecated
# #usps_unique_id value in order to remain backwards-compatible. LG-7024 will remove this
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not directly related to this PR, but should we add LG-7024 to our scheduled work for upcoming sprints?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eileen-nava I think that's a good idea, would recommend bringing it up in the Slack channel w/ David & Sumi.

unique_id = enrollment.unique_id || enrollment.usps_unique_id
address = [pii['address1'], pii['address2']].select(&:present?).join(' ')
pii = pii.to_h

# If we're using secondary ID capture (aka double address verification),
# then send the state ID address to USPS. Otherwise send the residential address.
if enrollment.capture_secondary_id_enabled? && !pii[:same_address_as_id]
pii = pii.except(*SECONDARY_ID_ADDRESS_MAP.values).
transform_keys(SECONDARY_ID_ADDRESS_MAP)
end

address = [pii[:address1], pii[:address2]].select(&:present?).join(' ')

applicant = UspsInPersonProofing::Applicant.new(
{
unique_id: unique_id,
first_name: transliterate(pii['first_name']),
last_name: transliterate(pii['last_name']),
first_name: transliterate(pii[:first_name]),
last_name: transliterate(pii[:last_name]),
address: transliterate(address),
city: transliterate(pii['city']),
state: pii['state'],
zip_code: pii['zipcode'],
city: transliterate(pii[:city]),
state: pii[:state],
zip_code: pii[:zipcode],
email: 'no-reply@login.gov',
},
)
Expand Down Expand Up @@ -79,6 +94,14 @@ def usps_proofer

private

SECONDARY_ID_ADDRESS_MAP = {
state_id_address1: :address1,
state_id_address2: :address2,
state_id_city: :city,
state_id_jurisdiction: :state,
state_id_zipcode: :zipcode,
}.freeze

def handle_bad_request_error(err, enrollment)
message = err.response.dig(:body, 'responseMessage') || err.message
raise Exception::RequestEnrollException.new(message, err, enrollment.id)
Expand Down
85 changes: 70 additions & 15 deletions spec/services/usps_in_person_proofing/enrollment_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
let(:user) { build(:user) }
let(:current_address_matches_id) { false }
let(:pii) do
Idp::Constants::MOCK_IDV_APPLICANT_WITH_PHONE.
merge(same_address_as_id: current_address_matches_id).
transform_keys(&:to_s)
Pii::Attributes.new_from_hash(
Idp::Constants::MOCK_IDV_APPLICANT_WITH_PHONE.
merge(same_address_as_id: current_address_matches_id).
transform_keys(&:to_s),
)
end
subject(:subject) { described_class }
let(:subject_analytics) { FakeAnalytics.new }
let(:transliterator) { UspsInPersonProofing::Transliterator.new }
let(:service_provider) { nil }
let(:usps_ipp_transliteration_enabled) { true }
let(:in_person_capture_secondary_id_enabled) { false }

before(:each) do
stub_request_token
Expand All @@ -29,6 +32,8 @@
allow(subject).to receive(:analytics).and_return(subject_analytics)
allow(IdentityConfig.store).to receive(:usps_ipp_transliteration_enabled).
and_return(usps_ipp_transliteration_enabled)
allow(IdentityConfig.store).to receive(:in_person_capture_secondary_id_enabled).
and_return(in_person_capture_secondary_id_enabled)
end

describe '#schedule_in_person_enrollment' do
Expand All @@ -53,11 +58,15 @@
end

context 'an establishing enrollment record exists for the user' do
let(:proofer) { UspsInPersonProofing::Mock::Proofer.new }

before do
allow(Rails).to receive(:cache).and_return(
ActiveSupport::Cache::RedisCacheStore.new(url: IdentityConfig.store.redis_throttle_url),
)
allow(subject).to receive(:usps_proofer).and_return(proofer)
end

it 'updates the existing enrollment record' do
expect(user.in_person_enrollments.length).to eq(1)

Expand All @@ -71,11 +80,8 @@
let(:usps_ipp_transliteration_enabled) { false }

it 'creates usps enrollment without using transliteration' do
mock_proofer = double(UspsInPersonProofing::Mock::Proofer)
expect(subject).to receive(:usps_proofer).and_return(mock_proofer)

expect(transliterator).not_to receive(:transliterate)
expect(mock_proofer).to receive(:request_enroll) do |applicant|
expect(proofer).to receive(:request_enroll) do |applicant|
expect(applicant.first_name).to eq(Idp::Constants::MOCK_IDV_APPLICANT[:first_name])
expect(applicant.last_name).to eq(Idp::Constants::MOCK_IDV_APPLICANT[:last_name])
expect(applicant.address).to eq(Idp::Constants::MOCK_IDV_APPLICANT[:address1])
Expand All @@ -90,15 +96,66 @@

subject.schedule_in_person_enrollment(user, pii)
end

describe 'double address verification' do
let(:pii) do
Pii::Attributes.new_from_hash(
Idp::Constants::MOCK_IDV_APPLICANT_WITH_PHONE.
merge(same_address_as_id: current_address_matches_id).
merge(Idp::Constants::MOCK_IDV_APPLICANT_STATE_ID_ADDRESS).
transform_keys(&:to_s),
)
end

context 'feature enabled' do
let(:in_person_capture_secondary_id_enabled) { true }

it 'maps enrollment address fields' do
expect(proofer).to receive(:request_enroll) do |applicant|
ADDR = Idp::Constants::MOCK_IDV_APPLICANT_STATE_ID_ADDRESS
expect(applicant).to have_attributes(
address: "#{
Idp::Constants::MOCK_IDV_APPLICANT_STATE_ID_ADDRESS[:state_id_address1]
} #{
Idp::Constants::MOCK_IDV_APPLICANT_STATE_ID_ADDRESS[:state_id_address2]
}",
city: Idp::Constants::MOCK_IDV_APPLICANT_STATE_ID_ADDRESS[:state_id_city],
state: Idp::Constants::MOCK_IDV_APPLICANT_STATE_ID_ADDRESS[
:state_id_jurisdiction
],
zip_code: Idp::Constants::MOCK_IDV_APPLICANT_STATE_ID_ADDRESS[:state_id_zipcode],
)
UspsInPersonProofing::Mock::Proofer.new.request_enroll(applicant)
end

subject.schedule_in_person_enrollment(user, pii)
end
end

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

it 'does not map enrollment address fields' do
expect(proofer).to receive(:request_enroll) do |applicant|
expect(applicant).to have_attributes(
address: Idp::Constants::MOCK_IDV_APPLICANT[:address1],
city: Idp::Constants::MOCK_IDV_APPLICANT[:city],
state: Idp::Constants::MOCK_IDV_APPLICANT[:state],
zip_code: Idp::Constants::MOCK_IDV_APPLICANT[:zipcode],
)
UspsInPersonProofing::Mock::Proofer.new.request_enroll(applicant)
end

subject.schedule_in_person_enrollment(user, pii)
end
end
end
end

context 'transliteration enabled' do
let(:usps_ipp_transliteration_enabled) { true }

it 'creates usps enrollment while using transliteration' do
mock_proofer = double(UspsInPersonProofing::Mock::Proofer)
expect(subject).to receive(:usps_proofer).and_return(mock_proofer)

first_name = Idp::Constants::MOCK_IDV_APPLICANT[:first_name]
last_name = Idp::Constants::MOCK_IDV_APPLICANT[:last_name]
address = Idp::Constants::MOCK_IDV_APPLICANT[:address1]
Expand All @@ -113,7 +170,7 @@
expect(transliterator).to receive(:transliterate).
with(city).and_return(transliterated(city))

expect(mock_proofer).to receive(:request_enroll) do |applicant|
expect(proofer).to receive(:request_enroll) do |applicant|
expect(applicant.first_name).to eq(first_name)
expect(applicant.last_name).to eq("transliterated_#{last_name}")
expect(applicant.address).to eq(address)
Expand All @@ -133,10 +190,7 @@
context 'when the enrollment does not have a unique ID' do
it 'uses the deprecated InPersonEnrollment#usps_unique_id value to create the enrollment' do
enrollment.update(unique_id: nil)
mock_proofer = double(UspsInPersonProofing::Mock::Proofer)
expect(subject).to receive(:usps_proofer).and_return(mock_proofer)

expect(mock_proofer).to receive(:request_enroll) do |applicant|
expect(proofer).to receive(:request_enroll) do |applicant|
expect(applicant.unique_id).to eq(enrollment.usps_unique_id)

UspsInPersonProofing::Mock::Proofer.new.request_enroll(applicant)
Expand All @@ -155,6 +209,7 @@
end

context 'event logging' do
let(:proofer) { UspsInPersonProofing::Mock::Proofer.new }
context 'with no service provider' do
it 'logs event' do
subject.schedule_in_person_enrollment(user, pii)
Expand Down