Add unit tests for utilities

This commit is contained in:
thePR0M3TH3AN
2025-07-01 00:13:08 -04:00
parent 58302484e6
commit e105c1a2b4
3 changed files with 92 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
import hashlib
from pathlib import Path
from utils import checksum
def test_calculate_checksum(tmp_path):
file = tmp_path / "data.txt"
content = "hello world"
file.write_text(content)
expected = hashlib.sha256(content.encode()).hexdigest()
result = checksum.calculate_checksum(str(file))
assert result == expected
def test_calculate_checksum_missing(tmp_path):
missing = tmp_path / "missing.txt"
assert checksum.calculate_checksum(str(missing)) is None
def test_verify_and_update(tmp_path):
chk_file = tmp_path / "chk.txt"
chk_file.write_text("abc")
assert checksum.verify_checksum("abc", str(chk_file))
assert not checksum.verify_checksum("def", str(chk_file))
assert checksum.update_checksum("payload", str(chk_file))
expected = hashlib.sha256("payload".encode()).hexdigest()
assert chk_file.read_text() == expected
def test_initialize_checksum(tmp_path):
data = tmp_path / "file.bin"
data.write_text("payload")
chk_file = tmp_path / "chk2.txt"
assert checksum.initialize_checksum(str(data), str(chk_file))
expected = hashlib.sha256("payload".encode()).hexdigest()
assert chk_file.read_text() == expected

View File

@@ -0,0 +1,20 @@
from utils.fingerprint_manager import FingerprintManager
def test_add_and_remove_fingerprint(tmp_path):
mgr = FingerprintManager(tmp_path)
phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
fp = mgr.add_fingerprint(phrase)
assert fp in mgr.list_fingerprints()
dir_path = mgr.get_fingerprint_directory(fp)
assert dir_path and dir_path.exists()
assert mgr.select_fingerprint(fp)
assert mgr.get_current_fingerprint_dir() == dir_path
assert mgr.remove_fingerprint(fp)
assert fp not in mgr.list_fingerprints()
assert not dir_path.exists()
def test_remove_nonexistent_fingerprint(tmp_path):
mgr = FingerprintManager(tmp_path)
assert not mgr.remove_fingerprint("UNKNOWN")

View File

@@ -0,0 +1,34 @@
import builtins
from itertools import cycle
import pytest
from utils import password_prompt
def test_prompt_new_password(monkeypatch):
responses = cycle(["goodpass", "goodpass"])
monkeypatch.setattr(
password_prompt.getpass, "getpass", lambda prompt: next(responses)
)
result = password_prompt.prompt_new_password()
assert result == "goodpass"
def test_prompt_new_password_retry(monkeypatch):
seq = iter(["pass1", "pass2", "passgood", "passgood"])
monkeypatch.setattr(password_prompt.getpass, "getpass", lambda prompt: next(seq))
result = password_prompt.prompt_new_password()
assert result == "passgood"
def test_prompt_existing_password(monkeypatch):
monkeypatch.setattr(password_prompt.getpass, "getpass", lambda prompt: "mypassword")
assert password_prompt.prompt_existing_password() == "mypassword"
def test_confirm_action_yes_no(monkeypatch):
monkeypatch.setattr(builtins, "input", lambda _: "Y")
assert password_prompt.confirm_action()
monkeypatch.setattr(builtins, "input", lambda _: "n")
assert not password_prompt.confirm_action()