Пример #1
0
    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)
Пример #2
0
 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')
Пример #3
0
 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)
Пример #4
0
 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)
Пример #5
0
 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)
Пример #6
0
 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)
Пример #7
0
 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)
Пример #8
0
 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')
Пример #9
0
 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')
Пример #10
0
    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)
Пример #11
0
    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()
Пример #12
0
    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'])
Пример #13
0
    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)
Пример #14
0
 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()
Пример #15
0
 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()
Пример #16
0
 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)
Пример #17
0
 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)
Пример #18
0
    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'])
Пример #19
0
    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)
Пример #20
0
    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)
Пример #21
0
    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])
Пример #22
0
 def test_parse_data_fields(self):
     pdftk = PDFTKWrapper()
     results = list(pdftk.parse_data_fields(
         DATA_FIELDS_STR_SAMPLE))
     self.assertListEqual(results, PARSED_DATA_FIELDS)
Пример #23
0
 def test_parse_data_fields(self):
     pdftk = PDFTKWrapper()
     results = list(pdftk.parse_data_fields(DATA_FIELDS_STR_SAMPLE))
     self.assertListEqual(results, PARSED_DATA_FIELDS)
Пример #24
0
    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)
Пример #25
0
 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()
Пример #26
0
 def test_parse_fdf(self):
     pdftk = PDFTKWrapper()
     results = list(pdftk.parse_fdf_fields(FDF_STR_SAMPLE))
     self.assertListEqual(results, PARSED_FDF_FIELDS)
Пример #27
0
 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()
Пример #28
0
 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()
Пример #29
0
 def test_fill_signature(self):
     path = self.field_pdfs['signature']
     pdftk = PDFTKWrapper()
     field_data = pdftk.get_field_data(path)
     print(field_data)
Пример #30
0
 def test_get_field_data(self):
     pdftk = PDFTKWrapper()
     results = pdftk.get_field_data(self.sample_form_path)
     self.assertListEqual(results, FIELD_DATA)
Пример #31
0
 def test_parse_fdf(self):
     pdftk = PDFTKWrapper()
     results = list(pdftk.parse_fdf_fields(FDF_STR_SAMPLE))
     self.assertListEqual(results, PARSED_FDF_FIELDS)