Skip to content

Commit

Permalink
refactor(pdf): decouple unlock from PdfDocument __init__
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-awd committed Sep 7, 2024
1 parent 324e1c5 commit e7eb85f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/monopoly/banks/detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from functools import cached_property
from typing import TYPE_CHECKING, Any, Type

from monopoly.identifiers import Identifier, TextIdentifier
from monopoly.identifiers import Identifier, MetadataIdentifier, TextIdentifier
from monopoly.pdf import PdfDocument

if TYPE_CHECKING:
Expand All @@ -21,9 +21,9 @@ def metadata_items(self) -> list[Any]:
"""
Retrieves encryption and metadata identifiers from a bank statement PDF
"""
identifiers: list[Identifier] = []
if metadata_identifier := self.document.metadata_identifier:
identifiers.append(metadata_identifier)
identifiers: list[MetadataIdentifier] = []
if identifier := self.document.metadata_identifier:
identifiers.append(identifier)

if not identifiers:
raise ValueError("Could not get identifier")
Expand Down
1 change: 1 addition & 0 deletions src/monopoly/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def process_statement(

try:
document = PdfDocument(file)
document.unlock_document()
analyzer = BankDetector(document)
bank = analyzer.detect_bank(banks) or GenericBank
parser = PdfParser(bank, document)
Expand Down
9 changes: 4 additions & 5 deletions src/monopoly/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,11 @@ def __init__(
args = {"filename": self.file_path, "stream": self.file_bytes}
super().__init__(**args)

if self.is_encrypted:
self._unlock_document()

self.metadata_identifier = MetadataIdentifier(**self.metadata)
@cached_property
def metadata_identifier(self):
return MetadataIdentifier(**self.metadata)

def _unlock_document(self):
def unlock_document(self):
"""Attempt to unlock the document using the provided passwords."""
if not self.is_encrypted:
return self
Expand Down
14 changes: 7 additions & 7 deletions tests/integration/test_pdf_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_can_open_protected(pdf_document: PdfDocument):
pdf_document = PdfDocument(
passwords=None, file_path=fixture_directory / "protected.pdf"
)
pdf_document._unlock_document()
pdf_document.unlock_document()


def test_wrong_password_raises_error():
Expand All @@ -52,15 +52,15 @@ def test_wrong_password_raises_error():
passwords=[SecretStr("wrongpw_123")],
file_path=fixture_directory / "protected.pdf",
)
pdf_document._unlock_document()
pdf_document.unlock_document()


def test_override_password():
pdf_document: PdfDocument = PdfDocument(
passwords=[SecretStr("foobar123")],
file_path=fixture_directory / "protected.pdf",
)
pdf_document._unlock_document()
pdf_document.unlock_document()
assert not pdf_document.is_encrypted


Expand All @@ -75,7 +75,7 @@ def test_missing_password_raises_error():
pdf_document = PdfDocument(
passwords=None, file_path=fixture_directory / "protected.pdf"
)
pdf_document._unlock_document()
pdf_document.unlock_document()


def test_null_password_raises_error():
Expand All @@ -89,7 +89,7 @@ def test_null_password_raises_error():
pdf_document = PdfDocument(
passwords=None, file_path=fixture_directory / "protected.pdf"
)
pdf_document._unlock_document()
pdf_document.unlock_document()


def test_invalid_password_type_raises_error():
Expand All @@ -103,7 +103,7 @@ def test_invalid_password_type_raises_error():
pdf_document = PdfDocument(
passwords=None, file_path=fixture_directory / "protected.pdf"
)
pdf_document._unlock_document()
pdf_document.unlock_document()


def test_plain_text_passwords_raises_error(pdf_document: PdfDocument):
Expand All @@ -117,4 +117,4 @@ def test_plain_text_passwords_raises_error(pdf_document: PdfDocument):
pdf_document = PdfDocument(
passwords=None, file_path=fixture_directory / "protected.pdf"
)
pdf_document._unlock_document()
pdf_document.unlock_document()
4 changes: 1 addition & 3 deletions tests/unit/test_bank_identifier/test_get_identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ def mock_non_encrypted_document():


def test_metadata_identifier(mock_non_encrypted_document):
with patch.object(
PdfDocument, "_unlock_document", new_callable=Mock
) as mock_unlock:
with patch.object(PdfDocument, "unlock_document", new_callable=Mock) as mock_unlock:
mock_unlock.return_value = mock_non_encrypted_document

expected_identifier = MetadataIdentifier(
Expand Down

0 comments on commit e7eb85f

Please sign in to comment.