diff --git a/app/controllers/idv/otp_verification_controller.rb b/app/controllers/idv/otp_verification_controller.rb index 335b0b8ff11..a534637b9a6 100644 --- a/app/controllers/idv/otp_verification_controller.rb +++ b/app/controllers/idv/otp_verification_controller.rb @@ -19,10 +19,15 @@ def show def update result = phone_confirmation_otp_verification_form.submit(code: params[:code]) analytics.idv_phone_confirmation_otp_submitted(**result.to_h) + + parsed_failure_reason = + (result.extra.slice(:code_expired) if result.extra[:code_expired]) || + (result.extra.slice(:code_matches) if !result.success? && !result.extra[:code_matches]) || + {} irs_attempts_api_tracker.idv_phone_otp_submitted( success: result.success?, phone_number: idv_session.user_phone_confirmation_session.phone, - failure_reason: result.success? ? {} : result.extra.slice(:code_expired, :code_matches), + failure_reason: parsed_failure_reason, ) if result.success? diff --git a/spec/controllers/idv/otp_verification_controller_spec.rb b/spec/controllers/idv/otp_verification_controller_spec.rb index e14420270d4..8f7cbcd326a 100644 --- a/spec/controllers/idv/otp_verification_controller_spec.rb +++ b/spec/controllers/idv/otp_verification_controller_spec.rb @@ -5,15 +5,19 @@ let(:phone) { '2255555000' } let(:user_phone_confirmation) { false } - let(:phone_confirmation_otp_delivery_method) { 'sms' } let(:phone_confirmation_otp_code) { '777777' } let(:phone_confirmation_otp_sent_at) { Time.zone.now } - let(:user_phone_confirmation_session) do - Idv::PhoneConfirmationSession.new( + let(:phone_confirmation_session_properties) do + { code: phone_confirmation_otp_code, phone: phone, + delivery_method: :sms, + } + end + let(:user_phone_confirmation_session) do + Idv::PhoneConfirmationSession.new( + **phone_confirmation_session_properties, sent_at: phone_confirmation_otp_sent_at, - delivery_method: phone_confirmation_otp_delivery_method.to_sym, ) end @@ -66,11 +70,12 @@ end describe '#update' do + let(:otp_code_param) { { code: phone_confirmation_otp_code } } context 'the user has not been sent an otp' do let(:user_phone_confirmation_session) { nil } it 'redirects to otp delivery method selection' do - put :update, params: { code: phone_confirmation_otp_code } + put :update, params: otp_code_param expect(response).to redirect_to(idv_otp_delivery_method_path) end end @@ -79,13 +84,13 @@ let(:user_phone_confirmation) { true } it 'redirects to the review step' do - put :update, params: { code: phone_confirmation_otp_code } + put :update, params: otp_code_param expect(response).to redirect_to(idv_review_path) end end it 'tracks an analytics event' do - put :update, params: { code: phone_confirmation_otp_code } + put :update, params: otp_code_param expected_result = { success: true, @@ -104,30 +109,56 @@ end describe 'track irs analytics event' do + let(:phone_property) { { phone_number: phone } } context 'when the phone otp code is valid' do it 'captures success event' do expect(@irs_attempts_api_tracker).to receive(:idv_phone_otp_submitted).with( success: true, - phone_number: phone, + **phone_property, failure_reason: {}, ) - put :update, params: { code: phone_confirmation_otp_code } + put :update, params: otp_code_param end end context 'when the phone otp code is invalid' do + let(:invalid_otp_code_param) { { code: '000' } } it 'captures failure event' do expect(@irs_attempts_api_tracker).to receive(:idv_phone_otp_submitted).with( success: false, - phone_number: phone, + **phone_property, failure_reason: { code_matches: false, - code_expired: false, }, ) - put :update, params: { code: '000' } + put :update, params: invalid_otp_code_param + end + end + + context 'when the phone otp code has expired' do + let(:expired_phone_confirmation_otp_sent_at) do + # Set time to a long time ago + phone_confirmation_otp_sent_at - 900000000 + end + let(:user_phone_confirmation_session) do + Idv::PhoneConfirmationSession.new( + **phone_confirmation_session_properties, + sent_at: expired_phone_confirmation_otp_sent_at, + ) + end + + it 'captures failure event' do + expect(@irs_attempts_api_tracker).to receive(:idv_phone_otp_submitted).with( + success: false, + **phone_property, + failure_reason: { + code_expired: true, + }, + ) + + put :update, params: otp_code_param end end end