|
11 | 11 | test_data_types: %w[simple], |
12 | 12 | run_at: '2025-10-24T18:48:27Z' |
13 | 13 | } |
| 14 | + |
| 15 | + describe '.stamp_signature' do |
| 16 | + let(:pdf_path) { 'tmp/test_form.pdf' } |
| 17 | + let(:signature_text) { 'John Doe' } |
| 18 | + let(:form_data) do |
| 19 | + { |
| 20 | + 'generalInformation' => { |
| 21 | + 'signature' => signature_text, |
| 22 | + 'nursingOfficialName' => 'Jane Smith' |
| 23 | + } |
| 24 | + } |
| 25 | + end |
| 26 | + |
| 27 | + before do |
| 28 | + allow(PDFUtilities::DatestampPdf).to receive(:new).and_return( |
| 29 | + double(run: 'tmp/stamped.pdf') |
| 30 | + ) |
| 31 | + end |
| 32 | + |
| 33 | + context 'with signature in generalInformation.signature' do |
| 34 | + it 'stamps the signature on the PDF' do |
| 35 | + expect(PDFUtilities::DatestampPdf).to receive(:new).with(pdf_path).and_return( |
| 36 | + double(run: 'tmp/stamped.pdf') |
| 37 | + ) |
| 38 | + |
| 39 | + result = described_class.stamp_signature(pdf_path, form_data) |
| 40 | + expect(result).to eq('tmp/stamped.pdf') |
| 41 | + end |
| 42 | + |
| 43 | + it 'calls DatestampPdf with correct parameters' do |
| 44 | + datestamp_double = instance_double(PDFUtilities::DatestampPdf) |
| 45 | + allow(PDFUtilities::DatestampPdf).to receive(:new).with(pdf_path).and_return(datestamp_double) |
| 46 | + |
| 47 | + expect(datestamp_double).to receive(:run).with( |
| 48 | + text: signature_text, |
| 49 | + x: described_class::SIGNATURE_X, |
| 50 | + y: described_class::SIGNATURE_Y, |
| 51 | + page_number: described_class::SIGNATURE_PAGE, |
| 52 | + size: described_class::SIGNATURE_SIZE, |
| 53 | + text_only: true, |
| 54 | + timestamp: '', |
| 55 | + template: pdf_path, |
| 56 | + multistamp: true |
| 57 | + ).and_return('tmp/stamped.pdf') |
| 58 | + |
| 59 | + described_class.stamp_signature(pdf_path, form_data) |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + context 'with no signature data' do |
| 64 | + let(:form_data) { { 'generalInformation' => {} } } |
| 65 | + |
| 66 | + it 'returns the original PDF path without stamping' do |
| 67 | + expect(PDFUtilities::DatestampPdf).not_to receive(:new) |
| 68 | + result = described_class.stamp_signature(pdf_path, form_data) |
| 69 | + expect(result).to eq(pdf_path) |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + context 'with blank signature' do |
| 74 | + let(:form_data) do |
| 75 | + { 'generalInformation' => { 'signature' => ' ' } } |
| 76 | + end |
| 77 | + |
| 78 | + it 'returns the original PDF path without stamping' do |
| 79 | + expect(PDFUtilities::DatestampPdf).not_to receive(:new) |
| 80 | + result = described_class.stamp_signature(pdf_path, form_data) |
| 81 | + expect(result).to eq(pdf_path) |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + context 'when stamping raises an error' do |
| 86 | + let(:error_message) { 'PDF stamping failed' } |
| 87 | + |
| 88 | + before do |
| 89 | + allow(PDFUtilities::DatestampPdf).to receive(:new).and_raise(StandardError, error_message) |
| 90 | + end |
| 91 | + |
| 92 | + it 'logs the error and returns the original PDF path' do |
| 93 | + expect(Rails.logger).to receive(:error).with( |
| 94 | + 'Form210779: Error stamping signature', |
| 95 | + hash_including(error: error_message) |
| 96 | + ) |
| 97 | + |
| 98 | + result = described_class.stamp_signature(pdf_path, form_data) |
| 99 | + expect(result).to eq(pdf_path) |
| 100 | + end |
| 101 | + end |
| 102 | + end |
14 | 103 | end |
0 commit comments