mirror of
https://github.com/PR0M3TH3AN/SeedPass.git
synced 2026-11-06 04:42:35 +00:00
Merge pull request #878 from PR0M3TH3AN/test-key-validation-coverage-8412470545867749142
🧪 Add error handling tests for key validation utils
This commit is contained in:
2 files changed
+49
-5
No files matched your search
@@ -0,0 +1,44 @@
|
||||
import sys
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
from unittest.mock import Mock, MagicMock
|
||||
|
||||
# Add src to sys.path
|
||||
sys.path.append(str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
from utils.key_validation import (
|
||||
validate_totp_secret,
|
||||
validate_ssh_key_pair,
|
||||
validate_pgp_private_key,
|
||||
validate_nostr_keys,
|
||||
validate_seed_phrase,
|
||||
)
|
||||
|
||||
def test_validate_totp_secret_exception(monkeypatch):
|
||||
"""Test that validate_totp_secret returns False when an exception occurs."""
|
||||
monkeypatch.setattr("utils.key_validation.pyotp.TOTP", Mock(side_effect=Exception("TOTP Error")))
|
||||
assert validate_totp_secret("any_secret") is False
|
||||
|
||||
def test_validate_ssh_key_pair_exception(monkeypatch):
|
||||
"""Test that validate_ssh_key_pair returns False when an exception occurs."""
|
||||
monkeypatch.setattr("utils.key_validation.serialization.load_pem_private_key", Mock(side_effect=Exception("SSH Error")))
|
||||
assert validate_ssh_key_pair("priv", "pub") is False
|
||||
|
||||
def test_validate_pgp_private_key_exception(monkeypatch):
|
||||
"""Test that validate_pgp_private_key returns False when an exception occurs."""
|
||||
monkeypatch.setattr("utils.key_validation.PGPKey.from_blob", Mock(side_effect=Exception("PGP Error")))
|
||||
assert validate_pgp_private_key("priv", "fp") is False
|
||||
|
||||
def test_validate_nostr_keys_exception(monkeypatch):
|
||||
"""Test that validate_nostr_keys returns False when an exception occurs."""
|
||||
# The function calls Keys.bech32_to_hex(nsec) first
|
||||
monkeypatch.setattr("utils.key_validation.Keys.bech32_to_hex", Mock(side_effect=Exception("Nostr Error")))
|
||||
assert validate_nostr_keys("npub", "nsec") is False
|
||||
|
||||
def test_validate_seed_phrase_exception(monkeypatch):
|
||||
"""Test that validate_seed_phrase returns False when an exception occurs."""
|
||||
# The function calls Mnemonic("english").check(mnemonic)
|
||||
mock_instance = MagicMock()
|
||||
mock_instance.check.side_effect = Exception("Seed Error")
|
||||
monkeypatch.setattr("utils.key_validation.Mnemonic", Mock(return_value=mock_instance))
|
||||
assert validate_seed_phrase("mnemonic") is False
|
||||
@@ -15,7 +15,7 @@ def validate_totp_secret(secret: str) -> bool:
|
||||
try:
|
||||
pyotp.TOTP(secret).at(0)
|
||||
return True
|
||||
except Exception as e: # pragma: no cover - pyotp errors vary
|
||||
except Exception as e:
|
||||
logger.debug(f"Invalid TOTP secret: {e}")
|
||||
return False
|
||||
|
||||
@@ -33,7 +33,7 @@ def validate_ssh_key_pair(priv_pem: str, pub_pem: str) -> bool:
|
||||
.decode()
|
||||
)
|
||||
return derived == pub_pem
|
||||
except Exception as e: # pragma: no cover - serialization errors vary
|
||||
except Exception as e:
|
||||
logger.debug(f"SSH key validation failed: {e}")
|
||||
return False
|
||||
|
||||
@@ -43,7 +43,7 @@ def validate_pgp_private_key(priv_key: str, fingerprint: str) -> bool:
|
||||
try:
|
||||
key, _ = PGPKey.from_blob(priv_key)
|
||||
return key.fingerprint == fingerprint
|
||||
except Exception as e: # pragma: no cover - pgpy errors vary
|
||||
except Exception as e:
|
||||
logger.debug(f"PGP key validation failed: {e}")
|
||||
return False
|
||||
|
||||
@@ -55,7 +55,7 @@ def validate_nostr_keys(npub: str, nsec: str) -> bool:
|
||||
derived = Keys(priv_k=priv_hex)
|
||||
encoded = Keys.hex_to_bech32(derived.public_key_hex(), "npub")
|
||||
return encoded == npub
|
||||
except Exception as e: # pragma: no cover - nostr errors vary
|
||||
except Exception as e:
|
||||
logger.debug(f"Nostr key validation failed: {e}")
|
||||
return False
|
||||
|
||||
@@ -64,6 +64,6 @@ def validate_seed_phrase(mnemonic: str) -> bool:
|
||||
"""Return True if ``mnemonic`` is a valid BIP-39 seed phrase."""
|
||||
try:
|
||||
return Mnemonic("english").check(mnemonic)
|
||||
except Exception as e: # pragma: no cover - mnemonic errors vary
|
||||
except Exception as e:
|
||||
logger.debug(f"Seed phrase validation failed: {e}")
|
||||
return False
|
||||
Reference in new issue
Block a user