def test_run_command(self, subprocess): pdftk = PDFTKWrapper() comm_err = Mock(return_value=(b'', b'an error')) comm_out = Mock(return_value=(b'success', b'')) proc_out = Mock(communicate=comm_out) proc_err = Mock(communicate=comm_err) popen_yes = Mock(return_value=proc_out) popen_bad = Mock(return_value=proc_err) # check the good case subprocess.Popen = popen_yes args = ['pdftk', 'go'] result = pdftk.run_command(args) self.assertEqual('success', result) popen_yes.assert_called_once_with(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) comm_out.assert_called_once_with() proc_out.assert_not_called() # check the arg fixing popen_yes.reset_mock() result = pdftk.run_command(['dostuff']) popen_yes.assert_called_once_with(['pdftk','dostuff'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) # check the bad case subprocess.reset_mock() subprocess.Popen = popen_bad args = ['go'] with self.assertRaises(PdftkError): pdftk.run_command(args) proc_err.assert_not_called() comm_err.assert_called_once_with() popen_bad.assert_called_once_with(['pdftk','go'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def test_init(self): pdftk = PDFTKWrapper() self.assertEqual(pdftk.encoding, 'latin-1') self.assertEqual(pdftk.TEMP_FOLDER_PATH, None) pdftk = PDFTKWrapper(encoding='utf-8', tmp_path='data') self.assertEqual(pdftk.encoding, 'utf-8') self.assertEqual(pdftk.TEMP_FOLDER_PATH, 'data')
def test_fill_radio(self): path = self.field_pdfs['radio'] pdftk = PDFTKWrapper() results = pdftk.get_field_data(path) self.assertListEqual(results, RADIO_SAMPLE) sample_answers = {'Radio Buttons': 'yellow'} filled_pdf = pdftk.fill_pdf(path, sample_answers) filled_sample = open('data/sample_output/fields/radio.pdf', 'rb').read() self.assertEqual(filled_pdf, filled_sample)
def test_fill_checkbox(self): path = self.field_pdfs['checkbox'] pdftk = PDFTKWrapper() results = pdftk.get_field_data(path) self.assertListEqual(results, CHECKBOX_SAMPLE) sample_answers = {'Check Box2': 'Off', 'Check Box3': 'Yes'} filled_pdf = pdftk.fill_pdf(path, sample_answers) filled_sample = open('data/sample_output/fields/checkbox.pdf', 'rb').read() self.assertEqual(filled_pdf, filled_sample)
def test_fill_many(self): pdftk = PDFTKWrapper() fields = pdftk.get_field_data(self.sample_form_path) answers = example_pdf_answers(3) results = pdftk.fill_pdf_many(self.sample_form_path, answers) for field in fields: field_type = field['type'] if field_type == 'text': field_name = field['name'] for answer in answers: answer_text = answer[field_name] search_term = format_pdf_search_term(answer_text) self.assertIn(search_term, results)
def test_combine_pdfs(self): pdftk = PDFTKWrapper() # get all the paths of filled field samples pdfs = self.field_pdfs del pdfs['signature'] paths = [p.replace('pdfs', 'output') for p in pdfs.values()] paths.sort() combined_pdf = pdftk.join_pdfs(paths) sample_combined = open('data/sample_output/fields/all.pdf', 'rb').read() # check that there is less than 1kb difference in length # between result and sample self.assertLess(abs(len(combined_pdf) - len(sample_combined)), 1000)
def test_fill_text(self): path = self.field_pdfs['text'] pdftk = PDFTKWrapper() results = pdftk.get_field_data(path) self.assertListEqual(results, TEXT_SAMPLE) sample_answers = { 'multiline': 'So\nmany\nlines', 'single': 'Hello pdf world' } filled_pdf = pdftk.fill_pdf(path, sample_answers) for key, value in sample_answers.items(): pdf_friendly_search_term = format_pdf_search_term( value, pdftk.encoding) self.assertIn(pdf_friendly_search_term, filled_pdf)
def test_get_file_contents(self, open): pdftk = PDFTKWrapper(encoding='utf-2000') decoder = Mock(return_value='decoded') # check with no decode mock_bytestring = Mock(decode=decoder) open.return_value.read.return_value = mock_bytestring result = pdftk._get_file_contents('path') self.assertEqual(result, mock_bytestring) open.assert_called_once_with('path', 'rb') decoder.assert_not_called() # check with decode open.reset_mock() open.return_value.read.return_value = mock_bytestring result = pdftk._get_file_contents('path', decode=True) self.assertEqual(result, 'decoded') open.assert_called_once_with('path', 'rb') decoder.assert_called_once_with('utf-2000')
def test_fill_example_pdf(self): pdftk = PDFTKWrapper() fields = pdftk.get_field_data(self.sample_form_path) sample_answers = { 'Address City': 'Little Town', 'Address State': 'CA', 'Address Street': '111 Main Street', 'Address Zip': '01092', 'Arrested outside SF': 'No', 'Cell phone number': '999-999-9999', 'Charged with a crime': 'No', 'Date': '09/09/2016', 'Date of Birth': '09/09/9999', 'Dates arrested outside SF': '', 'Drivers License': 'D9999999', 'Email Address': '*****@*****.**', 'Employed': 'No', 'First Name': 'Berry', 'Home phone number': '', 'How did you hear about the Clean Slate Program': 'From a wonderful friend', 'If probation where and when?': '', 'Last Name': 'Manatee', 'MI': 'H', 'May we leave voicemail': 'Yes', 'May we send mail here': 'Yes', 'Monthly expenses': '1000', 'On probation or parole': 'No', 'Other phone number': '', 'Serving a sentence': 'No', 'Social Security Number': '999-99-9999', 'US Citizen': 'Yes', 'What is your monthly income': '0', 'Work phone number': '', } filled_pdf = pdftk.fill_pdf(self.sample_form_path, sample_answers) for field in fields: field_type = field['type'] if field_type == 'text': field_name = field['name'] answer_text = sample_answers[field_name] search_term = format_pdf_search_term(answer_text) self.assertIn(search_term, filled_pdf)
def test_fill_pdf(self, fake_open): pdftk = PDFTKWrapper() fake_answers = Mock() fake_path = "some/fake/path.pdf" coerce_to_file_path = Mock(return_value=fake_path) pdftk._coerce_to_file_path = coerce_to_file_path fake_insertions = Mock() gen_answer_insertions = Mock(return_value=fake_insertions) pdftk._generate_answer_insertions = gen_answer_insertions fake_fdf_str = Mock() patch_fdf_with_insertions = Mock(return_value=fake_fdf_str) pdftk._patch_fdf_with_insertions = patch_fdf_with_insertions fake_output_path = Mock() load_patched_fdf = Mock(return_value=fake_output_path) pdftk._load_patched_fdf_into_pdf = load_patched_fdf fake_read_results = Mock() fake_open.return_value = Mock( read=Mock( return_value=fake_read_results) ) clean_up_tmp_files = Mock() pdftk.clean_up_tmp_files = clean_up_tmp_files # run the method result = pdftk.fill_pdf(fake_path, fake_answers) coerce_to_file_path.assert_called_with(fake_path) gen_answer_insertions.assert_called_with(fake_path, fake_answers) patch_fdf_with_insertions.assert_called_with(fake_insertions) load_patched_fdf.assert_called_with(fake_path, fake_fdf_str) fake_open.assert_called_with(fake_output_path, 'rb') clean_up_tmp_files.assert_called_with() self.assertEqual(result, fake_read_results) pdftk.clean_up = False pdftk.fill_pdf(fake_path, fake_answers) clean_up_tmp_files.reset_mock() clean_up_tmp_files.assert_not_called()
def test_write_tmp_file(self, open, mkstemp): mkstemp.return_value = ('os.file', 'filepath') mock_file = Mock() # check with file_object pdftk = PDFTKWrapper() path = pdftk._write_tmp_file(mock_file) mkstemp.assert_called_once_with(dir=pdftk.TEMP_FOLDER_PATH) open.assert_called_once_with('filepath', 'wb') mock_file.read.assert_called_once_with() self.assertEqual(path, 'filepath') self.assertListEqual(pdftk._tmp_files, ['filepath']) # check with bytes pdftk = PDFTKWrapper() mkstemp.reset_mock() open.reset_mock() path = pdftk._write_tmp_file(bytestring=b'content') open.assert_called_once_with('filepath', 'wb') self.assertEqual(path, 'filepath') self.assertListEqual(pdftk._tmp_files, ['filepath'])
def test_coerce_to_file_path(self): pdftk = PDFTKWrapper() wrt_tmp = Mock(return_value='path') pdftk._write_tmp_file = wrt_tmp # check with a string input result = pdftk._coerce_to_file_path('goodpath') self.assertEqual(result, 'goodpath') wrt_tmp.assert_not_called() # check with a bytestring input bstring = b'foo' result = pdftk._coerce_to_file_path(bstring) self.assertEqual(result, 'path') wrt_tmp.assert_called_once_with(bytestring=bstring) # check with a not string input wrt_tmp.reset_mock() not_string = Mock() result = pdftk._coerce_to_file_path(not_string) self.assertEqual(result, 'path') wrt_tmp.assert_called_once_with(file_obj=not_string)
def test_clean_up_tmp_files(self, remove): pdftk = PDFTKWrapper() paths = [c for c in 'hello'] pdftk._tmp_files = paths pdftk.clean_up_tmp_files() for p in paths: remove.assert_any_call(p) self.assertListEqual(pdftk._tmp_files, []) # test with no files remove.reset_mock() pdftk.clean_up_tmp_files() remove.assert_not_called()
def test_get_fdf(self): pdftk = PDFTKWrapper() coercer = Mock(return_value='path.pdf') pdftk._coerce_to_file_path = coercer writer = Mock(return_value='tmp_path.fdf') pdftk._write_tmp_file = writer runner = Mock() pdftk.run_command = runner contents_getter = Mock() pdftk._get_file_contents = contents_getter results = pdftk.get_fdf('something.pdf') coercer.assert_called_once_with('something.pdf') writer.assert_called_once_with() runner.assert_called_once_with( ['path.pdf', 'generate_fdf', 'output', 'tmp_path.fdf']) contents_getter.assert_called_once_with('tmp_path.fdf', decode=True)
def test_get_fdf(self): pdftk = PDFTKWrapper() coercer = Mock(return_value='path.pdf') pdftk._coerce_to_file_path = coercer writer = Mock(return_value='tmp_path.fdf') pdftk._write_tmp_file = writer runner = Mock() pdftk.run_command = runner contents_getter = Mock() pdftk._get_file_contents = contents_getter results = pdftk.get_fdf('something.pdf') coercer.assert_called_once_with('something.pdf') writer.assert_called_once_with() runner.assert_called_once_with([ 'path.pdf', 'generate_fdf', 'output', 'tmp_path.fdf']) contents_getter.assert_called_once_with( 'tmp_path.fdf', decode=True)
def test_run_command(self, subprocess): pdftk = PDFTKWrapper() comm_err = Mock(return_value=(b'', b'an error')) comm_out = Mock(return_value=(b'success', b'')) proc_out = Mock(communicate=comm_out) proc_err = Mock(communicate=comm_err) popen_yes = Mock(return_value=proc_out) popen_bad = Mock(return_value=proc_err) # check the good case subprocess.Popen = popen_yes args = ['pdftk', 'go'] result = pdftk.run_command(args) self.assertEqual('success', result) popen_yes.assert_called_once_with(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) comm_out.assert_called_once_with() proc_out.assert_not_called() # check the arg fixing popen_yes.reset_mock() result = pdftk.run_command(['dostuff']) popen_yes.assert_called_once_with(['pdftk', 'dostuff'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) # check the bad case subprocess.reset_mock() subprocess.Popen = popen_bad args = ['go'] with self.assertRaises(PdftkError): pdftk.run_command(args) proc_err.assert_not_called() comm_err.assert_called_once_with() popen_bad.assert_called_once_with(['pdftk', 'go'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def test_fill_pdf_many(self): #vars pdftk = PDFTKWrapper() fake_answer = Mock() fake_multiple_answers = [fake_answer] #ensure self.clean_up value is preserved fake_clean_up = pdftk.clean_up _fake_clean_up_setting = fake_clean_up fake_clean_up = False fake_clean_up = _fake_clean_up_setting #pdf path (should be same as in fill_pdf) fake_path = "some/fake/path.pdf" coerce_to_file_path = Mock(return_value=fake_path) pdftk._coerce_to_file_path = coerce_to_file_path #for loop fake_fill_pdf = Mock() pdftk.fill_pdf = fake_fill_pdf fake_filled_pdf = Mock() pdftk.fill_pdf.return_value = fake_filled_pdf fake_write_tmp_file = Mock(return_value=fake_path) pdftk._write_tmp_file = fake_write_tmp_file #join_pdfs fake_join_pdfs = Mock() pdftk.join_pdfs = fake_join_pdfs #run the method result = pdftk.fill_pdf_many(fake_path, fake_multiple_answers) self.assertEqual(pdftk.clean_up, fake_clean_up) coerce_to_file_path.assert_called_with(fake_path) fake_fill_pdf.assert_called_with(fake_path, fake_answer) fake_write_tmp_file.assert_called_with(bytestring=fake_filled_pdf) fake_join_pdfs.assert_called_with([fake_path])
def test_parse_data_fields(self): pdftk = PDFTKWrapper() results = list(pdftk.parse_data_fields( DATA_FIELDS_STR_SAMPLE)) self.assertListEqual(results, PARSED_DATA_FIELDS)
def test_parse_data_fields(self): pdftk = PDFTKWrapper() results = list(pdftk.parse_data_fields(DATA_FIELDS_STR_SAMPLE)) self.assertListEqual(results, PARSED_DATA_FIELDS)
current_app, send_file, redirect) from sqlalchemy.engine.reflection import Inspector import io, os, glob, json, datetime from src.main import db from src.pdfhook import ( blueprint, serializers, models ) from src.pdftk_wrapper import PDFTKWrapper from src.settings import PROJECT_ROOT pdf_dumper = serializers.PDFFormDumper() pdf_list_dumper = serializers.PDFFormIndexDumper() pdf_loader = serializers.PDFFormLoader() pdftk = PDFTKWrapper(clean_up=False) def request_wants_json(): best = request.accept_mimetypes \ .best_match(['application/json', 'text/html']) return best == 'application/json' and \ request.accept_mimetypes[best] > \ request.accept_mimetypes['text/html'] @blueprint.before_app_first_request def make_sure_there_is_a_working_database(*args, **kwargs): if current_app.config.get('ENV') != 'dev': return inspector = Inspector.from_engine(db.engine)
def test_get_fdf(self): pdftk = PDFTKWrapper() results = pdftk.get_fdf(self.sample_form_path) self.assertEqual(results, FDF_STR_SAMPLE) pdftk.clean_up_tmp_files()
def test_parse_fdf(self): pdftk = PDFTKWrapper() results = list(pdftk.parse_fdf_fields(FDF_STR_SAMPLE)) self.assertListEqual(results, PARSED_FDF_FIELDS)
def test_get_data_fields(self): pdftk = PDFTKWrapper() results = pdftk.get_data_fields(self.sample_form_path) self.assertEqual(results, DATA_FIELDS_STR_SAMPLE) pdftk.clean_up_tmp_files()
def test_get_full_form_field_data(self): pdftk = PDFTKWrapper() results = pdftk._get_full_form_field_data(self.sample_form_path) self.assertDictEqual(results, FIELD_DATA_MAP_SAMPLE) pdftk.clean_up_tmp_files()
def test_fill_signature(self): path = self.field_pdfs['signature'] pdftk = PDFTKWrapper() field_data = pdftk.get_field_data(path) print(field_data)
def test_get_field_data(self): pdftk = PDFTKWrapper() results = pdftk.get_field_data(self.sample_form_path) self.assertListEqual(results, FIELD_DATA)