diff --git a/dnstapir/key_resolver.py b/dnstapir/key_resolver.py index b70dac7..ff8ed7a 100644 --- a/dnstapir/key_resolver.py +++ b/dnstapir/key_resolver.py @@ -32,7 +32,6 @@ def key_resolver_from_client_database(client_database: str, key_cache: KeyCache class KeyResolver: - def __init__(self): self.logger = logging.getLogger(__name__).getChild(self.__class__.__name__) diff --git a/tests/test_key_resolver.py b/tests/test_key_resolver.py index 6742a51..144c8df 100644 --- a/tests/test_key_resolver.py +++ b/tests/test_key_resolver.py @@ -1,9 +1,32 @@ +from pathlib import Path +from tempfile import TemporaryDirectory + import pytest from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.asymmetric import ed25519 from pytest_httpx import HTTPXMock -from dnstapir.key_resolver import UrlKeyResolver +from dnstapir.key_resolver import FileKeyResolver, UrlKeyResolver + + +def test_file_key_resolver(httpx_mock: HTTPXMock): + key_id = "xyzzy" + public_key = ed25519.Ed25519PrivateKey.generate().public_key() + public_key_pem = public_key.public_bytes( + encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo + ) + + with TemporaryDirectory(prefix="dnstapir") as directory: + pem_filename = Path(directory) / f"{key_id}.pem" + with open(pem_filename, "wb") as fp: + fp.write(public_key_pem) + + resolver = FileKeyResolver(client_database_directory=directory) + res = resolver.resolve_public_key(key_id) + assert res == public_key + + with pytest.raises(KeyError): + _ = resolver.resolve_public_key("unknown") def test_url_key_resolver(httpx_mock: HTTPXMock): @@ -21,7 +44,7 @@ def test_url_key_resolver(httpx_mock: HTTPXMock): assert res == public_key with pytest.raises(KeyError): - res = resolver.resolve_public_key("unknown") + _ = resolver.resolve_public_key("unknown") def test_url_key_resolver_pattern(httpx_mock: HTTPXMock): @@ -39,7 +62,7 @@ def test_url_key_resolver_pattern(httpx_mock: HTTPXMock): assert res == public_key with pytest.raises(KeyError): - res = resolver.resolve_public_key("unknown") + _ = resolver.resolve_public_key("unknown") def test_url_key_resolver_contextlib(httpx_mock: HTTPXMock): @@ -50,7 +73,11 @@ def test_url_key_resolver_contextlib(httpx_mock: HTTPXMock): ) httpx_mock.add_response(url=f"https://keys/{key_id}.pem", content=public_key_pem) + httpx_mock.add_response(url="https://keys/unknown.pem", status_code=404) with UrlKeyResolver(client_database_base_url="https://keys") as resolver: res = resolver.resolve_public_key(key_id) assert res == public_key + + with pytest.raises(KeyError): + _ = resolver.resolve_public_key("unknown")