Skip to content

Commit

Permalink
chore(keyringctl): add missing type annotations for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anthraxx committed Nov 30, 2021
1 parent c807a81 commit 86eb172
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 28 deletions.
15 changes: 3 additions & 12 deletions libkeyringctl/keyring.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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
Expand Down Expand Up @@ -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)):
Expand All @@ -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)):
Expand Down
35 changes: 35 additions & 0 deletions libkeyringctl/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
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
Expand Down Expand Up @@ -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()),
)
)
34 changes: 23 additions & 11 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
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
Expand All @@ -26,17 +30,22 @@


@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}"
Expand Down Expand Up @@ -80,10 +89,12 @@ def wrapper(working_dir: Path, *args, **kwargs):
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)
Expand Down Expand Up @@ -115,12 +126,13 @@ def wrapper(working_dir: Path, *args, **kwargs):


@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"
10 changes: 5 additions & 5 deletions tests/test_trust.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


@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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 86eb172

Please sign in to comment.