From 48dc00c9e277c467bec2720eba5ac73ccc376573 Mon Sep 17 00:00:00 2001 From: svalexander Date: Wed, 10 May 2023 17:18:53 -0400 Subject: [PATCH 1/4] log nontransliterable chars --- app/services/analytics_events.rb | 11 +++++++++++ .../transliterable_validator.rb | 19 ++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/app/services/analytics_events.rb b/app/services/analytics_events.rb index 451701ee8f7..007140abdc9 100644 --- a/app/services/analytics_events.rb +++ b/app/services/analytics_events.rb @@ -664,6 +664,17 @@ def idv_in_person_prepare_submitted(flow_path:, in_person_cta_variant:, **extra) ) end + # @param [String] nontransliterable_characters + # Nontransliterable characters submitted by user + def idv_in_person_proofing_nontransliterable_characters_submitted(nontransliterable_characters:, + **extra) + track_event( + 'IdV: in person proofing characters submitted could not be transliterated', + nontransliterable_characters: nontransliterable_characters, + **extra, + ) + end + def idv_in_person_proofing_residential_address_submitted(**extra) track_event('IdV: in person proofing residential address submitted', **extra) end diff --git a/app/services/usps_in_person_proofing/transliterable_validator.rb b/app/services/usps_in_person_proofing/transliterable_validator.rb index 7ab49ca5f00..68719523c9b 100644 --- a/app/services/usps_in_person_proofing/transliterable_validator.rb +++ b/app/services/usps_in_person_proofing/transliterable_validator.rb @@ -32,7 +32,7 @@ def initialize(options) # @param [ActiveModel::Validations] record def validate(record) return unless IdentityConfig.store.usps_ipp_transliteration_enabled - + nontransliterable_chars = [] @fields.each do |field| next unless record.respond_to?(field) @@ -42,12 +42,19 @@ def validate(record) invalid_chars = get_invalid_chars(value) next unless invalid_chars.present? + nontransliterable_chars += invalid_chars + record.errors.add( field, :nontransliterable_field, message: get_error_message(invalid_chars), ) end + + if nontransliterable_chars.present? + nontransliterable_chars = nontransliterable_chars.sort.uniq + log_nontransliterable_characters(nontransliterable_chars) + end end def transliterator @@ -87,5 +94,15 @@ def get_invalid_chars(value) # Create sorted list of unique unsupported characters (result.unsupported_chars + additional_chars).sort.uniq end + + def analytics(user: AnonymousUser.new) + Analytics.new(user: user, request: nil, session: {}, sp: nil) + end + + def log_nontransliterable_characters(result) + analytics.idv_in_person_proofing_nontransliterable_characters_submitted( + nontransliterable_characters: result, + ) + end end end From 98b72c810d524492e657e45a6a4ce9e8868f450c Mon Sep 17 00:00:00 2001 From: svalexander Date: Thu, 11 May 2023 11:25:06 -0400 Subject: [PATCH 2/4] refactor --- app/services/analytics_events.rb | 6 ++++-- .../transliterable_validator.rb | 14 +++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/app/services/analytics_events.rb b/app/services/analytics_events.rb index 007140abdc9..cae8c5882f6 100644 --- a/app/services/analytics_events.rb +++ b/app/services/analytics_events.rb @@ -666,8 +666,10 @@ def idv_in_person_prepare_submitted(flow_path:, in_person_cta_variant:, **extra) # @param [String] nontransliterable_characters # Nontransliterable characters submitted by user - def idv_in_person_proofing_nontransliterable_characters_submitted(nontransliterable_characters:, - **extra) + def idv_in_person_proofing_nontransliterable_characters_submitted( + nontransliterable_characters:, + **extra + ) track_event( 'IdV: in person proofing characters submitted could not be transliterated', nontransliterable_characters: nontransliterable_characters, diff --git a/app/services/usps_in_person_proofing/transliterable_validator.rb b/app/services/usps_in_person_proofing/transliterable_validator.rb index 68719523c9b..a0de7cc6ecc 100644 --- a/app/services/usps_in_person_proofing/transliterable_validator.rb +++ b/app/services/usps_in_person_proofing/transliterable_validator.rb @@ -32,7 +32,7 @@ def initialize(options) # @param [ActiveModel::Validations] record def validate(record) return unless IdentityConfig.store.usps_ipp_transliteration_enabled - nontransliterable_chars = [] + nontransliterable_chars = Set.new @fields.each do |field| next unless record.respond_to?(field) @@ -52,8 +52,10 @@ def validate(record) end if nontransliterable_chars.present? - nontransliterable_chars = nontransliterable_chars.sort.uniq - log_nontransliterable_characters(nontransliterable_chars) + nontransliterable_chars = nontransliterable_chars.sort + analytics.idv_in_person_proofing_nontransliterable_characters_submitted( + nontransliterable_characters: nontransliterable_chars, + ) end end @@ -98,11 +100,5 @@ def get_invalid_chars(value) def analytics(user: AnonymousUser.new) Analytics.new(user: user, request: nil, session: {}, sp: nil) end - - def log_nontransliterable_characters(result) - analytics.idv_in_person_proofing_nontransliterable_characters_submitted( - nontransliterable_characters: result, - ) - end end end From 707d38e1b5e8e885103b021292d87bdbe52aae7c Mon Sep 17 00:00:00 2001 From: svalexander Date: Thu, 11 May 2023 21:31:01 -0400 Subject: [PATCH 3/4] add spec --- .../transliterable_validator.rb | 3 +-- .../transliterable_validator_spec.rb | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/app/services/usps_in_person_proofing/transliterable_validator.rb b/app/services/usps_in_person_proofing/transliterable_validator.rb index a0de7cc6ecc..ce966221caa 100644 --- a/app/services/usps_in_person_proofing/transliterable_validator.rb +++ b/app/services/usps_in_person_proofing/transliterable_validator.rb @@ -52,9 +52,8 @@ def validate(record) end if nontransliterable_chars.present? - nontransliterable_chars = nontransliterable_chars.sort analytics.idv_in_person_proofing_nontransliterable_characters_submitted( - nontransliterable_characters: nontransliterable_chars, + nontransliterable_characters: nontransliterable_chars.sort, ) end end diff --git a/spec/services/usps_in_person_proofing/transliterable_validator_spec.rb b/spec/services/usps_in_person_proofing/transliterable_validator_spec.rb index e06a8ff13a8..cb016ac3027 100644 --- a/spec/services/usps_in_person_proofing/transliterable_validator_spec.rb +++ b/spec/services/usps_in_person_proofing/transliterable_validator_spec.rb @@ -107,8 +107,10 @@ context 'failing transliteration' do let(:invalid_field) { 'def' } + let(:analytics) { FakeAnalytics.new } before do + allow(validator).to receive(:analytics).and_return(analytics) allow(validator.transliterator).to receive(:transliterate).with('def'). and_return( UspsInPersonProofing::Transliterator::TransliterationResult.new( @@ -128,6 +130,15 @@ expect(error.type).to eq(:nontransliterable_field) expect(error.options[:message]).to eq(message) end + + it 'logs nontransliterable characters' do + validator.validate(model) + + expect(analytics).to have_logged_event( + 'IdV: in person proofing characters submitted could not be transliterated', + nontransliterable_characters: ['*', '3', 'C'], + ) + end end context 'with callable error message' do @@ -210,6 +221,11 @@ let(:fields) { [:other_invalid_field, :valid_field, :invalid_field] } let(:invalid_field) { '123' } let(:other_invalid_field) { "\#@$%" } + let(:analytics) { FakeAnalytics.new } + + before do + allow(validator).to receive(:analytics).and_return(analytics) + end it 'sets multiple validation messages' do validator.validate(model) @@ -217,6 +233,15 @@ expect(errors).to include(:invalid_field) expect(errors).to include(:other_invalid_field) end + + it 'logs nontransliterable characters' do + validator.validate(model) + + expect(analytics).to have_logged_event( + 'IdV: in person proofing characters submitted could not be transliterated', + nontransliterable_characters: ['#', '$', '%', '1', '2', '3', '@'], + ) + end end end From 9c30bba3344d556c08ba83ac17fbe2c99b025de6 Mon Sep 17 00:00:00 2001 From: svalexander Date: Fri, 12 May 2023 09:40:12 -0400 Subject: [PATCH 4/4] changelog: Internal, Transliterable Validator, log nontransliterable characters