test: align legacy migration handling

This commit is contained in:
thePR0M3TH3AN
2025-08-11 19:35:03 -04:00
parent 294eef9725
commit 28f552313f
10 changed files with 36 additions and 33 deletions

View File

@@ -8,6 +8,7 @@ sys.path.append(str(Path(__file__).resolve().parents[1]))
import pytest
import seedpass.core.encryption as enc_module
import seedpass.core.vault as vault_module
from helpers import TEST_PASSWORD
from seedpass.core.encryption import (
EncryptionManager,
@@ -15,12 +16,13 @@ from seedpass.core.encryption import (
)
from seedpass.core.config_manager import ConfigManager
from seedpass.core.vault import Vault
from seedpass.core.migrations import LATEST_VERSION
def _setup_legacy_file(tmp_path: Path, iterations: int) -> None:
legacy_key = _derive_legacy_key_from_password(TEST_PASSWORD, iterations=iterations)
mgr = EncryptionManager(legacy_key, tmp_path)
data = {"entries": {"0": {"kind": "test"}}}
data = {"schema_version": LATEST_VERSION, "entries": {"0": {"kind": "test"}}}
json_bytes = json.dumps(data, separators=(",", ":")).encode("utf-8")
legacy_encrypted = mgr.fernet.encrypt(json_bytes)
(tmp_path / "seedpass_entries_db.json.enc").write_bytes(legacy_encrypted)
@@ -32,6 +34,7 @@ def test_migrate_iterations(tmp_path, monkeypatch, iterations):
new_key = base64.urlsafe_b64encode(b"B" * 32)
mgr = EncryptionManager(new_key, tmp_path)
vault = Vault(mgr, tmp_path)
prompts: list[int] = []
@@ -40,6 +43,7 @@ def test_migrate_iterations(tmp_path, monkeypatch, iterations):
return TEST_PASSWORD
monkeypatch.setattr(enc_module, "prompt_existing_password", fake_prompt)
monkeypatch.setattr(vault_module, "prompt_existing_password", fake_prompt)
monkeypatch.setattr("builtins.input", lambda *_a, **_k: "2")
calls: list[int] = []
@@ -51,15 +55,15 @@ def test_migrate_iterations(tmp_path, monkeypatch, iterations):
monkeypatch.setattr(enc_module, "_derive_legacy_key_from_password", tracking_derive)
mgr.load_json_data()
vault.load_index()
# Loading again should not prompt for password or attempt legacy counts
mgr.load_json_data()
vault.load_index()
assert prompts == [1]
expected = [50_000] if iterations == 50_000 else [50_000, 100_000]
assert calls == expected
cfg = ConfigManager(Vault(mgr, tmp_path), tmp_path)
cfg = ConfigManager(vault, tmp_path)
assert cfg.get_kdf_iterations() == iterations
content = (tmp_path / "seedpass_entries_db.json.enc").read_bytes()