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

[1115] poc: add logic to verify a PDF was properly stamped #16051

Merged
merged 15 commits into from
Mar 26, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ def self.stamp_pdf(stamped_template_path, form, current_loa)
end
stamp_text = SUBMISSION_TEXT + current_time
desired_stamps = [[10, 10, stamp_text]]
stamp(desired_stamps, stamped_template_path, auth_text, text_only: false)
verify(stamped_template_path) { stamp(desired_stamps, stamped_template_path, auth_text, text_only: false) }

stamp_submission_date(stamped_template_path, form.submission_date_config)
end

def self.stamp107959f1(stamped_template_path, form)
desired_stamps = [[26, 82.5, form.data['statement_of_truth_signature']]]
append_to_stamp = false
stamp(desired_stamps, stamped_template_path, append_to_stamp)
verify(stamped_template_path) { stamp(desired_stamps, stamped_template_path, append_to_stamp) }
end

def self.stamp264555(stamped_template_path, form)
Expand All @@ -55,7 +55,7 @@ def self.stamp214142(stamped_template_path, form)
{ type: :new_page }
]

multistamp(stamped_template_path, signature_text, page_configuration)
verified_multistamp(stamped_template_path, signature_text, page_configuration)

# This is a one-off case where we need to stamp a date on the first page of 21-4142 when resubmitting
if form.data['in_progress_form_created_at']
Expand All @@ -74,15 +74,15 @@ def self.stamp214142_date_stamp_for_resubmission(stamped_template_path, date_tit
{ type: :new_page }
]

multistamp(stamped_template_path, date_title, page_configuration, 12)
verified_multistamp(stamped_template_path, date_title, page_configuration, 12)

page_configuration = [
{ type: :text, position: date_text_stamp_position },
{ type: :new_page },
{ type: :new_page }
]

multistamp(stamped_template_path, date_text, page_configuration, 12)
verified_multistamp(stamped_template_path, date_text, page_configuration, 12)
end

def self.stamp2110210(stamped_template_path, form)
Expand All @@ -94,7 +94,7 @@ def self.stamp2110210(stamped_template_path, form)
{ type: :text, position: desired_stamps[0] }
]

multistamp(stamped_template_path, signature_text, page_configuration)
verified_multistamp(stamped_template_path, signature_text, page_configuration)
end

def self.stamp210845(stamped_template_path, form)
Expand All @@ -106,7 +106,7 @@ def self.stamp210845(stamped_template_path, form)
{ type: :text, position: desired_stamps[0] }
]

multistamp(stamped_template_path, signature_text, page_configuration)
verified_multistamp(stamped_template_path, signature_text, page_configuration)
end

def self.stamp21p0847(stamped_template_path, form)
Expand All @@ -117,7 +117,7 @@ def self.stamp21p0847(stamped_template_path, form)
{ type: :text, position: desired_stamps[0] }
]

multistamp(stamped_template_path, signature_text, page_configuration)
verified_multistamp(stamped_template_path, signature_text, page_configuration)
end

def self.stamp210972(stamped_template_path, form)
Expand All @@ -129,7 +129,7 @@ def self.stamp210972(stamped_template_path, form)
{ type: :text, position: desired_stamps[0] }
]

multistamp(stamped_template_path, signature_text, page_configuration)
verified_multistamp(stamped_template_path, signature_text, page_configuration)
end

def self.stamp210966(stamped_template_path, form)
Expand All @@ -140,7 +140,7 @@ def self.stamp210966(stamped_template_path, form)
{ type: :text, position: desired_stamps[0] }
]

multistamp(stamped_template_path, signature_text, page_configuration)
verified_multistamp(stamped_template_path, signature_text, page_configuration)
end

def self.stamp2010207(stamped_template_path, form)
Expand All @@ -162,7 +162,7 @@ def self.stamp2010207(stamped_template_path, form)
{ type: :text, position: desired_stamps[0] }
]

multistamp(stamped_template_path, signature_text, page_configuration)
verified_multistamp(stamped_template_path, signature_text, page_configuration)
end

def self.stamp4010007_uuid(uuid)
Expand All @@ -173,7 +173,7 @@ def self.stamp4010007_uuid(uuid)
{ type: :text, position: desired_stamps[0] }
]

multistamp(stamped_template_path, uuid, page_configuration, 7)
verified_multistamp(stamped_template_path, uuid, page_configuration, 7)
end

def self.multistamp(stamped_template_path, signature_text, page_configuration, font_size = 16)
Expand All @@ -200,9 +200,8 @@ def self.multistamp(stamped_template_path, signature_text, page_configuration, f
def self.stamp(desired_stamps, stamped_template_path, append_to_stamp, text_only: true)
current_file_path = stamped_template_path
desired_stamps.each do |x, y, text|
out_path = CentralMail::DatestampPdf.new(current_file_path, append_to_stamp:).run(text:, x:, y:, text_only:,
size: 9)
current_file_path = out_path
datestamp_instance = CentralMail::DatestampPdf.new(current_file_path, append_to_stamp:)
current_file_path = datestamp_instance.run(text:, x:, y:, text_only:, size: 9)
pennja marked this conversation as resolved.
Show resolved Hide resolved
end
File.rename(current_file_path, stamped_template_path)
end
Expand All @@ -225,16 +224,32 @@ def self.stamp_submission_date(stamped_template_path, config)
page_configuration = default_page_configuration
page_configuration[config[:page_number]] = { type: :text, position: date_title_stamp_position }

multistamp(stamped_template_path, SUBMISSION_DATE_TITLE, page_configuration, 12)
verified_multistamp(stamped_template_path, SUBMISSION_DATE_TITLE, page_configuration, 12)

page_configuration = default_page_configuration
page_configuration[config[:page_number]] = { type: :text, position: date_text_stamp_position }

multistamp(stamped_template_path, Time.current.in_time_zone('UTC').strftime('%H:%M %Z %D'), page_configuration,
12)
current_time = Time.current.in_time_zone('UTC').strftime('%H:%M %Z %D')
verified_multistamp(stamped_template_path, current_time, page_configuration, 12)
end
end

def self.verify(template_path)
orig_size = File.size(template_path)
yield
pennja marked this conversation as resolved.
Show resolved Hide resolved
stamped_size = File.size(template_path)

raise StandardError, 'The PDF remained unchanged upon stamping.' unless stamped_size > orig_size
rescue => e
raise StandardError, "An error occurred while verifying stamp: #{e}"
end

def self.verified_multistamp(stamped_template_path, stamp_text, page_configuration, *)
raise StandardError, 'The provided stamp content was empty.' if stamp_text.blank?

verify(stamped_template_path) { multistamp(stamped_template_path, stamp_text, page_configuration, *) }
end

def self.default_page_configuration
[
{ type: :new_page },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
"relationship_to_veteran": "other",
"otherRelationship_to_veteran": "friend of a friend"
},
"additional_information": "Lots of \"extra\" stuff here"
"additional_information": "Lots of \"extra\" stuff here",
"statement_of_truth_signature": "John Veteran"
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@
"release_duration": "untilDate",
"release_end_date": "2033-06-16",
"security_question": "motherBirthplace",
"security_answer": "Las Vegas, NV"
}
"security_answer": "Las Vegas, NV",
"statement_of_truth_signature": "John Veteran"
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@
},
"witness_other_relationship_to_claimant": "Other text",
"claimant_type": "non-veteran",
"claim_ownership": "third-party"
}
"claim_ownership": "third-party",
"statement_of_truth_signature": "John Veteran"
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,6 @@
"preparer_organization": "Top Org",
"court_appointment_info": "Representing \"court stuff\" like...Representing court stuff like...Representing court stuff like...Representing court stuff like...Representing court stuff like...Representing court stuff like...",
"relationship_to_veteran": "Veteran Service Officer"
}
},
"statement_of_truth_signature": "John Veteran"
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@
"postal_code": "54890"
}
},
"remarks": "Lengthy \"remarks\" here \nabout what is needed\tand such"
"remarks": "Lengthy \"remarks\" here \nabout what is needed\tand such",
"statement_of_truth_signature": "John Veteran"
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@
"release_duration": "untilDate",
"release_end_date": "2033-06-16",
"security_question": "motherBirthplace",
"security_answer": "Las Vegas, NV"
"security_answer": "Las Vegas, NV",
"statement_of_truth_signature": "John M Veteran"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
"state": "CA",
"postal_code": "12345"
},
"veteran_phone": "555-555-5557"
}
"veteran_phone": "555-555-5557",
"statement_of_truth_signature": "Arthur C Preparer"
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@
},
"witness_other_relationship_to_claimant": "Other text",
"claimant_type": "non-veteran",
"claim_ownership": "third-party"
}
"claim_ownership": "third-party",
"statement_of_truth_signature": "Joe Center Claimant"
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
"veteran_ssn": "999442222",
"relationship_to_deceased_claimant": {
"relationship_to_veteran": "spouse"
}
},
"statement_of_truth_signature": "Arthur C Preparer"
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
"relationship_to_veteran": "other",
"otherRelationship_to_veteran": "friend of a friend"
},
"additional_information": "Lots of extra stuff here"
"additional_information": "Lots of extra stuff here",
"statement_of_truth_signature": "Arthur C Preparer"
}
23 changes: 13 additions & 10 deletions modules/simple_forms_api/spec/services/pdf_filler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@

describe SimpleFormsApi::PdfFiller do
def self.test_pdf_fill(form_number, test_payload = form_number)
it 'fills out a PDF from a templated JSON file' do
expected_pdf_path = "tmp/#{form_number}-tmp.pdf"
form_name = form_number.split(Regexp.union(%w[vba_ vha_]))[1].gsub('_', '-')
context "when filling the pdf for form #{form_name} given template #{test_payload}" do
it 'fills out a PDF from a templated JSON file' do
expected_pdf_path = "tmp/#{form_number}-tmp.pdf"

# remove the pdf if it already exists
FileUtils.rm_f(expected_pdf_path)
# remove the pdf if it already exists
FileUtils.rm_f(expected_pdf_path)

# fill the PDF
data = JSON.parse(File.read("modules/simple_forms_api/spec/fixtures/form_json/#{test_payload}.json"))
form = "SimpleFormsApi::#{form_number.titleize.gsub(' ', '')}".constantize.new(data)
filler = SimpleFormsApi::PdfFiller.new(form_number:, form:)
filler.generate
expect(File.exist?(expected_pdf_path)).to eq(true)
# fill the PDF
data = JSON.parse(File.read("modules/simple_forms_api/spec/fixtures/form_json/#{test_payload}.json"))
form = "SimpleFormsApi::#{form_number.titleize.gsub(' ', '')}".constantize.new(data)
filler = SimpleFormsApi::PdfFiller.new(form_number:, form:)
filler.generate
expect(File.exist?(expected_pdf_path)).to eq(true)
end
end
end

Expand Down
Loading
Loading