test: cover legacy migration prompt and sync

This commit is contained in:
thePR0M3TH3AN
2025-08-03 11:29:23 -04:00
parent 42aa945b00
commit 44ce005cdc
4 changed files with 96 additions and 2 deletions

View File

@@ -5,9 +5,13 @@ from pathlib import Path
from helpers import create_vault, TEST_SEED, TEST_PASSWORD
from utils.key_derivation import derive_index_key
from cryptography.fernet import Fernet
from types import SimpleNamespace
from seedpass.core.manager import PasswordManager, EncryptionMode
from seedpass.core.vault import Vault
def test_legacy_index_migrates(tmp_path: Path):
def test_legacy_index_migrates(monkeypatch, tmp_path: Path):
vault, _ = create_vault(tmp_path, TEST_SEED, TEST_PASSWORD)
key = derive_index_key(TEST_SEED)
@@ -33,6 +37,8 @@ def test_legacy_index_migrates(tmp_path: Path):
hashlib.sha256(enc).hexdigest()
)
monkeypatch.setattr("builtins.input", lambda *_a, **_k: "y")
loaded = vault.load_index()
assert loaded == data
@@ -40,3 +46,38 @@ def test_legacy_index_migrates(tmp_path: Path):
assert new_file.exists()
assert not legacy_file.exists()
assert not (tmp_path / "seedpass_passwords_db_checksum.txt").exists()
backup = tmp_path / "legacy_backups" / "seedpass_passwords_db.json.enc"
assert backup.exists()
def test_migration_triggers_sync(monkeypatch, tmp_path: Path):
vault, enc_mgr = create_vault(tmp_path, TEST_SEED, TEST_PASSWORD)
key = derive_index_key(TEST_SEED)
data = {"schema_version": 4, "entries": {}}
enc = Fernet(key).encrypt(json.dumps(data).encode())
legacy_file = tmp_path / "seedpass_passwords_db.json.enc"
legacy_file.write_bytes(enc)
monkeypatch.setattr("builtins.input", lambda *_a, **_k: "y")
pm = PasswordManager.__new__(PasswordManager)
pm.encryption_mode = EncryptionMode.SEED_ONLY
pm.encryption_manager = enc_mgr
pm.vault = Vault(enc_mgr, tmp_path)
pm.parent_seed = TEST_SEED
pm.fingerprint_dir = tmp_path
pm.current_fingerprint = tmp_path.name
pm.bip85 = SimpleNamespace()
calls = {"sync": 0}
pm.start_background_vault_sync = lambda *a, **k: calls.__setitem__(
"sync", calls["sync"] + 1
)
monkeypatch.setattr(
"seedpass.core.manager.NostrClient", lambda *a, **k: SimpleNamespace()
)
pm.initialize_managers()
assert calls["sync"] == 1