chore(keyringctl): add missing type annotations for tests

This commit is contained in:
Levente Polyak 2021-11-01 18:37:00 +01:00
parent c807a81a57
commit 86eb172ac3
No known key found for this signature in database
GPG Key ID: FC1B547C8D8172C8
4 changed files with 66 additions and 28 deletions

View File

@ -32,6 +32,7 @@ from .types import Fingerprint
from .types import Trust
from .types import Uid
from .types import Username
from .util import filter_fingerprints_by_trust
from .util import get_cert_paths
from .util import system
from .util import transform_fd_to_tmpfile
@ -605,12 +606,7 @@ def export_ownertrust(certs: List[Path], output: Path) -> List[Fingerprint]:
"""
main_trusts = certificate_trust_from_paths(sources=certs, main_keys=get_fingerprints_from_paths(sources=certs))
trusted_certs: List[Fingerprint] = list(
map(
lambda item: item[0],
filter(lambda item: Trust.full == item[1], main_trusts.items()),
)
)
trusted_certs: List[Fingerprint] = filter_fingerprints_by_trust(main_trusts, Trust.full)
with open(file=output, mode="w") as trusted_certs_file:
for cert in sorted(set(trusted_certs)):
@ -636,12 +632,7 @@ def export_revoked(certs: List[Path], main_keys: Set[Fingerprint], output: Path)
"""
certificate_trusts = certificate_trust_from_paths(sources=certs, main_keys=main_keys)
revoked_certs: List[Fingerprint] = list(
map(
lambda item: item[0],
filter(lambda item: Trust.revoked == item[1], certificate_trusts.items()),
)
)
revoked_certs: List[Fingerprint] = filter_fingerprints_by_trust(certificate_trusts, Trust.revoked)
with open(file=output, mode="w") as revoked_certs_file:
for cert in sorted(set(revoked_certs)):

View File

@ -16,12 +16,14 @@ from tempfile import mkstemp
from traceback import print_stack
from typing import IO
from typing import AnyStr
from typing import Dict
from typing import List
from typing import Optional
from typing import Set
from typing import Union
from libkeyringctl.types import Fingerprint
from libkeyringctl.types import Trust
@contextmanager
@ -198,4 +200,37 @@ def get_parent_cert_paths(paths: Iterable[Path]) -> Set[Path]:
def contains_fingerprint(fingerprints: Iterable[Fingerprint], fingerprint: Fingerprint) -> bool:
"""Returns weather an iterable structure of fingerprints contains a specific fingerprint
Parameters
----------
fingerprints: Iteratable structure of fingerprints that should be searched
fingerprint: Fingerprint to search for
Returns
-------
Weather an iterable structure of fingerprints contains a specific fingerprint
"""
return any(filter(lambda e: str(e).endswith(fingerprint), fingerprints))
def filter_fingerprints_by_trust(trusts: Dict[Fingerprint, Trust], trust: Trust) -> List[Fingerprint]:
"""Filters a dict of Fingerprint to Trust by a passed Trust parameter and returns the matching fingerprints.
Parameters
----------
trusts: Dict of Fingerprint to Trust that should be filtered based on the trust parameter
trust: Trust that should be used to filter the trusts dict
Returns
-------
The matching fingerprints of the dict filtered by trust
"""
return list(
map(
lambda item: item[0],
filter(lambda item: trust == item[1], trusts.items()),
)
)

View File

@ -3,8 +3,12 @@ from functools import wraps
from pathlib import Path
from shutil import copytree
from tempfile import TemporaryDirectory
from typing import Any
from typing import Callable
from typing import Dict
from typing import Generator
from typing import List
from typing import Optional
from typing import Set
from pytest import fixture
@ -26,17 +30,22 @@ test_main_fingerprints: Set[Fingerprint] = set()
@fixture(autouse=True)
def reset_storage():
def reset_storage() -> None:
test_keys.clear()
test_certificates.clear()
test_keyring_certificates.clear()
test_main_fingerprints.clear()
def create_certificate(username: Username, uids: List[Uid], keyring_type: str = "packager", func=None):
def decorator(decorated_func):
def create_certificate(
username: Username,
uids: List[Uid],
keyring_type: str = "packager",
func: Optional[Callable[..., Any]] = None,
) -> Callable[..., Any]:
def decorator(decorated_func: Callable[..., None]) -> Callable[..., Any]:
@wraps(decorated_func)
def wrapper(working_dir: Path, *args, **kwargs):
def wrapper(working_dir: Path, *args: Any, **kwargs: Any) -> None:
print(username)
key_directory = working_dir / "secret" / f"{id}"
@ -80,10 +89,12 @@ def create_certificate(username: Username, uids: List[Uid], keyring_type: str =
return decorator(func)
def create_uid_certification(issuer: Username, certified: Username, uid: Uid, func=None):
def decorator(decorated_func):
def create_uid_certification(
issuer: Username, certified: Username, uid: Uid, func: Optional[Callable[[Any], None]] = None
) -> Callable[..., Any]:
def decorator(decorated_func: Callable[..., None]) -> Callable[..., Any]:
@wraps(decorated_func)
def wrapper(working_dir: Path, *args, **kwargs):
def wrapper(working_dir: Path, *args: Any, **kwargs: Any) -> None:
key: Path = test_keys[issuer][0]
certificate: Path = test_certificates[certified][0]
fingerprint: Fingerprint = Fingerprint(test_keyring_certificates[certified][0].name)
@ -115,12 +126,13 @@ def create_uid_certification(issuer: Username, certified: Username, uid: Uid, fu
@fixture(scope="function")
def working_dir():
def working_dir() -> Generator[Path, None, None]:
with TemporaryDirectory(prefix="arch-keyringctl-test-") as tempdir:
with cwd(tempdir):
yield Path(tempdir)
path: Path = Path(tempdir)
with cwd(path):
yield path
@fixture(scope="function")
def keyring_dir(working_dir: Path):
def keyring_dir(working_dir: Path) -> Generator[Path, None, None]:
yield working_dir / "keyring"

View File

@ -12,7 +12,7 @@ from .conftest import test_main_fingerprints
@create_certificate(username=Username("foobar"), uids=[Uid("foobar <foo@bar.xyz>")], keyring_type="main")
def test_certificate_trust_main_key_has_full_trust(working_dir: Path, keyring_dir: Path):
def test_certificate_trust_main_key_has_full_trust(working_dir: Path, keyring_dir: Path) -> None:
trust = certificate_trust(
test_keyring_certificates[Username("foobar")][0],
test_main_fingerprints,
@ -22,7 +22,7 @@ def test_certificate_trust_main_key_has_full_trust(working_dir: Path, keyring_di
@create_certificate(username=Username("main"), uids=[Uid("main <foo@bar.xyz>")])
@create_certificate(username=Username("foobar"), uids=[Uid("foobar <foo@bar.xyz>")])
def test_certificate_trust_no_signature_is_unknown(working_dir: Path, keyring_dir: Path):
def test_certificate_trust_no_signature_is_unknown(working_dir: Path, keyring_dir: Path) -> None:
trust = certificate_trust(
test_keyring_certificates[Username("foobar")][0],
test_main_fingerprints,
@ -33,7 +33,7 @@ def test_certificate_trust_no_signature_is_unknown(working_dir: Path, keyring_di
@create_certificate(username=Username("main"), uids=[Uid("main <foo@bar.xyz>")], keyring_type="main")
@create_certificate(username=Username("foobar"), uids=[Uid("foobar <foo@bar.xyz>")])
@create_uid_certification(issuer=Username("main"), certified=Username("foobar"), uid=Uid("foobar <foo@bar.xyz>"))
def test_certificate_trust_one_signature_is_marginal(working_dir: Path, keyring_dir: Path):
def test_certificate_trust_one_signature_is_marginal(working_dir: Path, keyring_dir: Path) -> None:
trust = certificate_trust(
test_keyring_certificates[Username("foobar")][0],
test_main_fingerprints,
@ -45,7 +45,7 @@ def test_certificate_trust_one_signature_is_marginal(working_dir: Path, keyring_
@create_certificate(username=Username("not_main"), uids=[Uid("main <foo@bar.xyz>")])
@create_certificate(username=Username("foobar"), uids=[Uid("foobar <foo@bar.xyz>")])
@create_uid_certification(issuer=Username("not_main"), certified=Username("foobar"), uid=Uid("foobar <foo@bar.xyz>"))
def test_certificate_trust_one_none_main_signature_gives_no_trust(working_dir: Path, keyring_dir: Path):
def test_certificate_trust_one_none_main_signature_gives_no_trust(working_dir: Path, keyring_dir: Path) -> None:
trust = certificate_trust(
test_keyring_certificates[Username("foobar")][0],
test_main_fingerprints,
@ -60,7 +60,7 @@ def test_certificate_trust_one_none_main_signature_gives_no_trust(working_dir: P
@create_uid_certification(issuer=Username("main1"), certified=Username("foobar"), uid=Uid("foobar <foo@bar.xyz>"))
@create_uid_certification(issuer=Username("main2"), certified=Username("foobar"), uid=Uid("foobar <foo@bar.xyz>"))
@create_uid_certification(issuer=Username("main3"), certified=Username("foobar"), uid=Uid("foobar <foo@bar.xyz>"))
def test_certificate_trust_three_main_signature_gives_full_trust(working_dir: Path, keyring_dir: Path):
def test_certificate_trust_three_main_signature_gives_full_trust(working_dir: Path, keyring_dir: Path) -> None:
trust = certificate_trust(
test_keyring_certificates[Username("foobar")][0],
test_main_fingerprints,