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
46 changes: 26 additions & 20 deletions app/forms/idv/api_image_upload_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ def submit
doc_pii_response = validate_pii_from_doc(client_response) if client_response.success?
end

determine_response(
response = determine_response(
form_response: form_response,
client_response: client_response,
doc_pii_response: doc_pii_response,
)

track_event(response)

response
end

private
Expand Down Expand Up @@ -98,25 +102,6 @@ def validate_pii_from_doc(client_response)

analytics.idv_doc_auth_submitted_pii_validation(**response.to_h)

pii_from_doc = response.pii_from_doc || {}
stored_image_result = store_encrypted_images_if_required

irs_attempts_api_tracker.idv_document_upload_submitted(
success: response.success?,
document_state: pii_from_doc[:state],
document_number: pii_from_doc[:state_id_number],
document_issued: pii_from_doc[:state_id_issued],
document_expiration: pii_from_doc[:state_id_expiration],
document_front_image_filename: stored_image_result&.front_filename,
document_back_image_filename: stored_image_result&.back_filename,
document_image_encryption_key: stored_image_result&.encryption_key,
first_name: pii_from_doc[:first_name],
last_name: pii_from_doc[:last_name],
date_of_birth: pii_from_doc[:dob],
address: pii_from_doc[:address1],
failure_reason: response.errors&.except(:hints)&.presence,
)

store_pii(client_response) if client_response.success? && response.success?

response
Expand Down Expand Up @@ -309,5 +294,26 @@ def throttle
throttle_type: :idv_doc_auth,
)
end

def track_event(response)
pii_from_doc = response.pii_from_doc || {}
stored_image_result = store_encrypted_images_if_required

irs_attempts_api_tracker.idv_document_upload_submitted(
success: response.success?,
document_state: pii_from_doc[:state],
document_number: pii_from_doc[:state_id_number],
document_issued: pii_from_doc[:state_id_issued],
document_expiration: pii_from_doc[:state_id_expiration],
document_front_image_filename: stored_image_result&.front_filename,
document_back_image_filename: stored_image_result&.back_filename,
document_image_encryption_key: stored_image_result&.encryption_key,
first_name: pii_from_doc[:first_name],
last_name: pii_from_doc[:last_name],
date_of_birth: pii_from_doc[:dob],
address: pii_from_doc[:address1],
failure_reason: response.errors&.except(:hints)&.presence,
)
end
end
end
40 changes: 35 additions & 5 deletions spec/controllers/idv/image_uploads_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
any_args,
)

expect(@irs_attempts_api_tracker).not_to receive(:track_event).with(
expect(@irs_attempts_api_tracker).to receive(:track_event).with(
:idv_document_upload_submitted,
any_args,
)
Expand Down Expand Up @@ -129,9 +129,21 @@
flow_path: 'standard',
)

expect(@irs_attempts_api_tracker).not_to receive(:track_event).with(
expect(@irs_attempts_api_tracker).to receive(:track_event).with(
:idv_document_upload_submitted,
any_args,
{ address: nil,
date_of_birth: nil,
document_back_image_filename: nil,
document_expiration: nil,
document_front_image_filename: nil,
document_image_encryption_key: nil,
document_issued: nil,
document_number: nil,
document_state: nil,
failure_reason: { front: ['The selection was not a valid file.'] },
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Opinion question: I think the academically-correct thing to do here would be to re-use the front: [I18n.t('doc_auth.errors.not_a_file')] that's set up above, maybe moving it to a little let(:error_msg) assignment.

I have a slight preference for using the actual English string here, though, to better highlight the unusual (compared to other events) behavior where we are sending the actual translated string across. I think this makes it clearer.

Anyone feel strongly one way or the other?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i think it's fine to hardcode the string like this, makes it easiest to change if something else changes

first_name: nil,
last_name: nil,
success: false },
)

expect(@analytics).not_to receive(:track_event).with(
Expand Down Expand Up @@ -229,9 +241,27 @@
flow_path: 'standard',
)

expect(@irs_attempts_api_tracker).not_to receive(:track_event).with(
expect(@irs_attempts_api_tracker).to receive(:track_event).with(
:idv_document_upload_rate_limited,
)

# This is the last upload which triggers the rate limit, apparently.
# I do find this moderately confusing.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Someone want to check my logic here? I wasn't expecting both events initially, but that seems to be what happens with analytics events.

Want to make sure I'm enshrining a new bug in tests.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think there's the attempt, and then that triggers the limit? I think that's fine

expect(@irs_attempts_api_tracker).to receive(:track_event).with(
:idv_document_upload_submitted,
any_args,
{ address: nil,
date_of_birth: nil,
document_back_image_filename: nil,
document_expiration: nil,
document_front_image_filename: nil,
document_image_encryption_key: nil,
document_issued: nil,
document_number: nil,
document_state: nil,
failure_reason: { limit: ['We could not verify your ID'] },
first_name: nil,
last_name: nil,
success: false },
)

expect(@analytics).not_to receive(:track_event).with(
Expand Down