Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip fileno before validating #18497

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def validate_metadata(metadata_input, consumer_id, upload_guid, submission_versi
if rejected.present?
raise VBADocuments::UploadError.new(code: 'DOC102', detail: "Non-string values for keys: #{rejected.join(',')}")
end
if (FILE_NUMBER_REGEX =~ metadata['fileNumber']).nil?
if (FILE_NUMBER_REGEX =~ metadata['fileNumber']&.strip).nil?
Copy link
Contributor

Choose a reason for hiding this comment

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

The safe navigation operator (&.) isn't needed here because L48 will raise an exception if there are missing keys, and L53 will raise an exception if the value isn't a string. Therefore metadata['fileNumber'] won't ever be nil if L55 is reached.

raise VBADocuments::UploadError.new(code: 'DOC102', detail: 'Non-numeric or invalid-length fileNumber')
end

Expand All @@ -76,6 +76,7 @@ def validate_documents(parts, pdf_validator_options = VBADocuments::DocumentRequ

def perfect_metadata(model, parts, timestamp)
metadata = JSON.parse(parts['metadata'])
metadata['fileNumber'] = metadata['fileNumber']&.strip if metadata.key?('fileNumber')
Copy link
Contributor

Choose a reason for hiding this comment

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

Since execution won't reach perfect_metadata if a fileNumber key is not provided and confirmed valid in the metadata (validate_metadata runs before perfect_metadata), if metadata.key?('fileNumber') and the safe navigation operator (&.) aren't needed and this can be simplified to:

metadata['fileNumber'] = metadata['fileNumber'].strip

metadata['source'] = "#{model.consumer_name} via VA API"
metadata['receiveDt'] = timestamp.in_time_zone('US/Central').strftime('%Y-%m-%d %H:%M:%S')
metadata['uuid'] = model.guid
Expand Down
2 changes: 1 addition & 1 deletion modules/vba_documents/spec/fixtures/valid_metadata.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"veteranFirstName": "Jane",
"veteranLastName": "Doe",
"fileNumber": "012345678",
"fileNumber": " 012345678 ",
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than modifying this file which is intended to be the "model" version of metadata, have you considered modifying the value in the spec file prior to using it instead? Something like this:

# code that sets up the test, including the metadata (using valid_metadata.json)
metadata['fileNumber'] = "  012345678 "
# code that runs the UploadProcessor and confirms after that the fileNumber was sent as "012345678"

"zipCode": "94402",
"source": "Vets.gov",
"docType": "21-22",
Expand Down
2 changes: 2 additions & 0 deletions modules/vba_documents/spec/sidekiq/upload_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@
expect(capture_body).to have_key('document')
metadata = JSON.parse(capture_body['metadata'])
expect(metadata['uuid']).to eq(upload.guid)
# leading\trailing whitespace should have been removed
expect(metadata['fileNumber']).to eq(metadata['fileNumber']&.strip)
Copy link
Contributor

Choose a reason for hiding this comment

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

Since having whitespace in the fileNumber is an edge case and the test here is for a happy path, I think that testing the edge case deserves its own it block (and context).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea, i had the same thought..i was trying to use an existing test to save unit test run time(I've noticed that PRs are starting to time out on unit tests in vets-api), but, yea, you pushed me back to a distinct unit test for file number with leading\trailing white space....more unit test power Scotty

updated = VBADocuments::UploadSubmission.find_by(guid: upload.guid)
expect(updated.status).to eq('received')
end
Expand Down
Loading