Skip to content

Commit

Permalink
Add test: test_load_protected_file_wrong_password
Browse files Browse the repository at this point in the history
  • Loading branch information
dantehemerson committed Nov 10, 2023
1 parent febd712 commit 4fcca50
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 5 additions & 3 deletions py_pdf_parser/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,24 @@ def load_file(
def load(
pdf_file: IO,
pdf_file_path: Optional[str] = None,
la_params: Optional[Dict] = None,
password: Optional[str] = None,
la_params: Optional[Dict] = None,
**kwargs: Any,
) -> PDFDocument:
"""
Loads the pdf file into a PDFDocument.
Args:
pdf_file (io): The PDF file.
pdf_file_path (str, optional): Passed to `PDFDocument`. See the documentation
for `PDFDocument`.
password (str, optional): Password for the encrypted PDF. Required if the
PDF is encrypted.
la_params (dict): The layout parameters passed to PDF Miner for analysis. See
the PDFMiner documentation here:
https://pdfminersix.readthedocs.io/en/latest/reference/composable.html#laparams.
Note that py_pdf_parser will re-order the elements it receives from PDFMiner
so options relating to element ordering will have no effect.
pdf_file_path (str, optional): Passed to `PDFDocument`. See the documentation
for `PDFDocument`.
kwargs: Passed to `PDFDocument`. See the documentation for `PDFDocument`.
Returns:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_loaders.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
from unittest import TestCase

from pdfminer.pdfdocument import PDFPasswordIncorrect

from py_pdf_parser.components import PDFDocument
from py_pdf_parser.loaders import load, load_file

Expand All @@ -18,6 +20,13 @@ def test_load_protected_file(self):
document = load_file(file_path, password="p4ssword")
self.assertIsInstance(document, PDFDocument)

def test_load_protected_file_wrong_password(self):
file_path = os.path.join(
os.path.dirname(__file__), "data", "pdfs", "test_protected.pdf"
)
with self.assertRaises(PDFPasswordIncorrect):
load_file(file_path, password="wrong_password")

def test_load(self):
file_path = os.path.join(os.path.dirname(__file__), "data", "pdfs", "test.pdf")
with open(file_path, "rb") as in_file:
Expand Down

0 comments on commit 4fcca50

Please sign in to comment.