From dce723c1fb968ec13baae1e0ed77b2315e5f6ca9 Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Tue, 1 Jul 2025 18:13:25 -0400 Subject: [PATCH] Fix checksum path handling --- src/password_manager/entry_management.py | 7 ++-- .../test_entry_management_checksum_path.py | 41 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 src/tests/test_entry_management_checksum_path.py diff --git a/src/password_manager/entry_management.py b/src/password_manager/entry_management.py index 598b3a0..b227bf9 100644 --- a/src/password_manager/entry_management.py +++ b/src/password_manager/entry_management.py @@ -344,8 +344,8 @@ class EntryManager: json_content = json.dumps(data, indent=4) checksum = hashlib.sha256(json_content.encode("utf-8")).hexdigest() - # Construct the full path for the checksum file - checksum_path = self.fingerprint_dir / self.checksum_file + # The checksum file path already includes the fingerprint directory + checksum_path = self.checksum_file with open(checksum_path, "w") as f: f.write(checksum) @@ -363,7 +363,8 @@ class EntryManager: Creates a backup of the encrypted JSON index file to prevent data loss. """ try: - index_file_path = self.fingerprint_dir / self.index_file + # self.index_file already includes the fingerprint directory + index_file_path = self.index_file if not index_file_path.exists(): logger.warning( f"Index file '{index_file_path}' does not exist. No backup created." diff --git a/src/tests/test_entry_management_checksum_path.py b/src/tests/test_entry_management_checksum_path.py new file mode 100644 index 0000000..0a6b914 --- /dev/null +++ b/src/tests/test_entry_management_checksum_path.py @@ -0,0 +1,41 @@ +import sys +from pathlib import Path +from tempfile import TemporaryDirectory +from cryptography.fernet import Fernet + +sys.path.append(str(Path(__file__).resolve().parents[1])) + +from password_manager.encryption import EncryptionManager +from password_manager.vault import Vault +from password_manager.entry_management import EntryManager + + +def test_update_checksum_writes_to_expected_path(): + with TemporaryDirectory() as tmpdir: + tmp_path = Path(tmpdir) + key = Fernet.generate_key() + enc_mgr = EncryptionManager(key, tmp_path) + vault = Vault(enc_mgr, tmp_path) + entry_mgr = EntryManager(vault, tmp_path) + + # create an empty index file + vault.save_index({"passwords": {}}) + entry_mgr.update_checksum() + + expected = tmp_path / "seedpass_passwords_db_checksum.txt" + assert expected.exists() + + +def test_backup_index_file_creates_backup_in_directory(): + with TemporaryDirectory() as tmpdir: + tmp_path = Path(tmpdir) + key = Fernet.generate_key() + enc_mgr = EncryptionManager(key, tmp_path) + vault = Vault(enc_mgr, tmp_path) + entry_mgr = EntryManager(vault, tmp_path) + + vault.save_index({"passwords": {}}) + entry_mgr.backup_index_file() + + backups = list(tmp_path.glob("passwords_db_backup_*.json.enc")) + assert len(backups) == 1