mirror of
https://github.com/ansible-collections/community.crypto.git
synced 2026-05-07 13:53:06 +00:00
Ensure that *everything* is typed in community.crypto (#917)
* Ensure that *everything* is typed in community.crypto. * Fix comment. * Ignore type definitions/imports and AssertionErrors for code coverage.
This commit is contained in:
@@ -21,10 +21,20 @@ from ..test__time import TIMEZONES, cartesian_product
|
||||
|
||||
|
||||
if t.TYPE_CHECKING:
|
||||
from ansible_collections.community.crypto.plugins.module_utils._acme.backends import (
|
||||
from ansible_collections.community.crypto.plugins.module_utils._acme.backends import ( # pragma: no cover
|
||||
Criterium,
|
||||
)
|
||||
|
||||
class DatetimeKwarg(t.TypedDict): # pragma: no cover
|
||||
year: int
|
||||
month: int
|
||||
day: int
|
||||
hour: t.NotRequired[int]
|
||||
minute: t.NotRequired[int]
|
||||
second: t.NotRequired[int]
|
||||
microsecond: t.NotRequired[int]
|
||||
tzinfo: t.NotRequired[datetime.timezone | None]
|
||||
|
||||
|
||||
def load_fixture(name: str) -> str:
|
||||
with open(
|
||||
@@ -133,7 +143,7 @@ TEST_CERT_INFO: list[tuple[str, CertificateInformation, str]] = [
|
||||
]
|
||||
|
||||
|
||||
TEST_PARSE_ACME_TIMESTAMP: list[tuple[datetime.timedelta, str, dict[str, int]]] = (
|
||||
TEST_PARSE_ACME_TIMESTAMP: list[tuple[datetime.timedelta, str, DatetimeKwarg]] = (
|
||||
cartesian_product(
|
||||
TIMEZONES,
|
||||
[
|
||||
@@ -201,7 +211,7 @@ TEST_PARSE_ACME_TIMESTAMP: list[tuple[datetime.timedelta, str, dict[str, int]]]
|
||||
|
||||
|
||||
TEST_INTERPOLATE_TIMESTAMP: list[
|
||||
tuple[datetime.timedelta, dict[str, int], dict[str, int], float, dict[str, int]]
|
||||
tuple[datetime.timedelta, DatetimeKwarg, DatetimeKwarg, float, DatetimeKwarg]
|
||||
] = cartesian_product(
|
||||
TIMEZONES,
|
||||
[
|
||||
@@ -233,17 +243,17 @@ class FakeBackend(CryptoBackend):
|
||||
*,
|
||||
key_file: str | os.PathLike | None = None,
|
||||
key_content: str | None = None,
|
||||
passphrase=None,
|
||||
passphrase: str | None = None,
|
||||
) -> t.NoReturn:
|
||||
raise BackendException("Not implemented in fake backend")
|
||||
raise BackendException("Not implemented in fake backend") # pragma: no cover
|
||||
|
||||
def sign(
|
||||
self, *, payload64: str, protected64: str, key_data: dict[str, t.Any] | None
|
||||
) -> t.NoReturn:
|
||||
raise BackendException("Not implemented in fake backend")
|
||||
raise BackendException("Not implemented in fake backend") # pragma: no cover
|
||||
|
||||
def create_mac_key(self, *, alg: str, key: str) -> t.NoReturn:
|
||||
raise BackendException("Not implemented in fake backend")
|
||||
raise BackendException("Not implemented in fake backend") # pragma: no cover
|
||||
|
||||
def get_ordered_csr_identifiers(
|
||||
self,
|
||||
@@ -251,7 +261,7 @@ class FakeBackend(CryptoBackend):
|
||||
csr_filename: str | os.PathLike | None = None,
|
||||
csr_content: str | bytes | None = None,
|
||||
) -> t.NoReturn:
|
||||
raise BackendException("Not implemented in fake backend")
|
||||
raise BackendException("Not implemented in fake backend") # pragma: no cover
|
||||
|
||||
def get_csr_identifiers(
|
||||
self,
|
||||
@@ -259,7 +269,7 @@ class FakeBackend(CryptoBackend):
|
||||
csr_filename: str | os.PathLike | None = None,
|
||||
csr_content: str | bytes | None = None,
|
||||
) -> t.NoReturn:
|
||||
raise BackendException("Not implemented in fake backend")
|
||||
raise BackendException("Not implemented in fake backend") # pragma: no cover
|
||||
|
||||
def get_cert_days(
|
||||
self,
|
||||
@@ -268,10 +278,10 @@ class FakeBackend(CryptoBackend):
|
||||
cert_content: str | bytes | None = None,
|
||||
now: datetime.datetime | None = None,
|
||||
) -> t.NoReturn:
|
||||
raise BackendException("Not implemented in fake backend")
|
||||
raise BackendException("Not implemented in fake backend") # pragma: no cover
|
||||
|
||||
def create_chain_matcher(self, *, criterium: Criterium) -> t.NoReturn:
|
||||
raise BackendException("Not implemented in fake backend")
|
||||
raise BackendException("Not implemented in fake backend") # pragma: no cover
|
||||
|
||||
def get_cert_information(
|
||||
self,
|
||||
@@ -279,4 +289,4 @@ class FakeBackend(CryptoBackend):
|
||||
cert_filename: str | os.PathLike | None = None,
|
||||
cert_content: str | bytes | None = None,
|
||||
) -> t.NoReturn:
|
||||
raise BackendException("Not implemented in fake backend")
|
||||
raise BackendException("Not implemented in fake backend") # pragma: no cover
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
import pathlib
|
||||
import typing as t
|
||||
from unittest.mock import (
|
||||
MagicMock,
|
||||
@@ -37,10 +38,12 @@ from .backend_data import (
|
||||
|
||||
|
||||
if t.TYPE_CHECKING:
|
||||
from ansible_collections.community.crypto.plugins.module_utils._acme.backends import (
|
||||
from ansible_collections.community.crypto.plugins.module_utils._acme.backends import ( # pragma: no cover
|
||||
CertificateInformation,
|
||||
)
|
||||
|
||||
from .backend_data import DatetimeKwarg # pragma: no cover
|
||||
|
||||
|
||||
if not HAS_CURRENT_CRYPTOGRAPHY:
|
||||
pytest.skip("cryptography not found")
|
||||
@@ -48,10 +51,10 @@ if not HAS_CURRENT_CRYPTOGRAPHY:
|
||||
|
||||
@pytest.mark.parametrize("pem, result, dummy", TEST_KEYS)
|
||||
def test_eckeyparse_cryptography(
|
||||
pem: str, result: dict[str, t.Any], dummy: str, tmpdir
|
||||
pem: str, result: dict[str, t.Any], dummy: str, tmp_path: pathlib.Path
|
||||
) -> None:
|
||||
fn = tmpdir / "test.pem"
|
||||
fn.write(pem)
|
||||
fn = tmp_path / "test.pem"
|
||||
fn.write_text(pem)
|
||||
module = MagicMock()
|
||||
backend = CryptographyBackend(module=module)
|
||||
key = backend.parse_key(key_file=str(fn))
|
||||
@@ -64,10 +67,10 @@ def test_eckeyparse_cryptography(
|
||||
|
||||
@pytest.mark.parametrize("csr, result, openssl_output", TEST_CSRS)
|
||||
def test_csridentifiers_cryptography(
|
||||
csr: str, result: set[tuple[str, str]], openssl_output: str, tmpdir
|
||||
csr: str, result: set[tuple[str, str]], openssl_output: str, tmp_path: pathlib.Path
|
||||
) -> None:
|
||||
fn = tmpdir / "test.csr"
|
||||
fn.write(csr)
|
||||
fn = tmp_path / "test.csr"
|
||||
fn.write_text(csr)
|
||||
module = MagicMock()
|
||||
backend = CryptographyBackend(module=module)
|
||||
identifiers = backend.get_csr_identifiers(csr_filename=str(fn))
|
||||
@@ -78,11 +81,14 @@ def test_csridentifiers_cryptography(
|
||||
|
||||
@pytest.mark.parametrize("timezone, now, expected_days", TEST_CERT_DAYS)
|
||||
def test_certdays_cryptography(
|
||||
timezone: datetime.timedelta, now: datetime.datetime, expected_days: int, tmpdir
|
||||
timezone: datetime.timedelta,
|
||||
now: datetime.datetime,
|
||||
expected_days: int,
|
||||
tmp_path: pathlib.Path,
|
||||
) -> None:
|
||||
with freeze_time("2024-02-03 04:05:06", tz_offset=timezone):
|
||||
fn = tmpdir / "test-cert.pem"
|
||||
fn.write(TEST_CERT)
|
||||
fn = tmp_path / "test-cert.pem"
|
||||
fn.write_text(TEST_CERT)
|
||||
module = MagicMock()
|
||||
backend = CryptographyBackend(module=module)
|
||||
days = backend.get_cert_days(cert_filename=str(fn), now=now)
|
||||
@@ -98,10 +104,10 @@ def test_get_cert_information(
|
||||
cert_content: str,
|
||||
expected_cert_info: CertificateInformation,
|
||||
openssl_output: str,
|
||||
tmpdir,
|
||||
tmp_path: pathlib.Path,
|
||||
) -> None:
|
||||
fn = tmpdir / "test-cert.pem"
|
||||
fn.write(cert_content)
|
||||
fn = tmp_path / "test-cert.pem"
|
||||
fn.write_text(cert_content)
|
||||
module = MagicMock()
|
||||
backend = CryptographyBackend(module=module)
|
||||
|
||||
@@ -138,7 +144,7 @@ def test_now(timezone: datetime.timedelta) -> None:
|
||||
|
||||
@pytest.mark.parametrize("timezone, timestamp_str, expected", TEST_PARSE_ACME_TIMESTAMP)
|
||||
def test_parse_acme_timestamp(
|
||||
timezone: datetime.timedelta, timestamp_str: str, expected: dict[str, int]
|
||||
timezone: datetime.timedelta, timestamp_str: str, expected: DatetimeKwarg
|
||||
) -> None:
|
||||
with freeze_time("2024-02-03 04:05:06 +00:00", tz_offset=timezone):
|
||||
module = MagicMock()
|
||||
@@ -153,10 +159,10 @@ def test_parse_acme_timestamp(
|
||||
)
|
||||
def test_interpolate_timestamp(
|
||||
timezone: datetime.timedelta,
|
||||
start: dict[str, int],
|
||||
end: dict[str, int],
|
||||
start: DatetimeKwarg,
|
||||
end: DatetimeKwarg,
|
||||
percentage: float,
|
||||
expected: dict[str, int],
|
||||
expected: DatetimeKwarg,
|
||||
) -> None:
|
||||
with freeze_time("2024-02-03 04:05:06", tz_offset=timezone):
|
||||
module = MagicMock()
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
import pathlib
|
||||
import typing as t
|
||||
from unittest.mock import (
|
||||
MagicMock,
|
||||
@@ -33,10 +34,12 @@ from .backend_data import (
|
||||
|
||||
|
||||
if t.TYPE_CHECKING:
|
||||
from ansible_collections.community.crypto.plugins.module_utils._acme.backends import (
|
||||
from ansible_collections.community.crypto.plugins.module_utils._acme.backends import ( # pragma: no cover
|
||||
CertificateInformation,
|
||||
)
|
||||
|
||||
from .backend_data import DatetimeKwarg # pragma: no cover
|
||||
|
||||
|
||||
# from ..test_time import TIMEZONES
|
||||
|
||||
@@ -55,10 +58,10 @@ TEST_IPS = [
|
||||
|
||||
@pytest.mark.parametrize("pem, result, openssl_output", TEST_KEYS)
|
||||
def test_eckeyparse_openssl(
|
||||
pem: str, result: dict[str, t.Any], openssl_output: str, tmpdir
|
||||
pem: str, result: dict[str, t.Any], openssl_output: str, tmp_path: pathlib.Path
|
||||
) -> None:
|
||||
fn = tmpdir / "test.key"
|
||||
fn.write(pem)
|
||||
fn = tmp_path / "test.key"
|
||||
fn.write_text(pem)
|
||||
module = MagicMock()
|
||||
module.run_command = MagicMock(return_value=(0, openssl_output, 0))
|
||||
backend = OpenSSLCLIBackend(module=module, openssl_binary="openssl")
|
||||
@@ -69,10 +72,10 @@ def test_eckeyparse_openssl(
|
||||
|
||||
@pytest.mark.parametrize("csr, result, openssl_output", TEST_CSRS)
|
||||
def test_csridentifiers_openssl(
|
||||
csr: str, result: set[tuple[str, str]], openssl_output: str, tmpdir
|
||||
csr: str, result: set[tuple[str, str]], openssl_output: str, tmp_path: pathlib.Path
|
||||
) -> None:
|
||||
fn = tmpdir / "test.csr"
|
||||
fn.write(csr)
|
||||
fn = tmp_path / "test.csr"
|
||||
fn.write_text(csr)
|
||||
module = MagicMock()
|
||||
module.run_command = MagicMock(return_value=(0, openssl_output, 0))
|
||||
backend = OpenSSLCLIBackend(module=module, openssl_binary="openssl")
|
||||
@@ -89,11 +92,14 @@ def test_normalize_ip(ip: str, result: str) -> None:
|
||||
|
||||
@pytest.mark.parametrize("timezone, now, expected_days", TEST_CERT_DAYS)
|
||||
def test_certdays_cryptography(
|
||||
timezone: datetime.timedelta, now: datetime.datetime, expected_days: int, tmpdir
|
||||
timezone: datetime.timedelta,
|
||||
now: datetime.datetime,
|
||||
expected_days: int,
|
||||
tmp_path: pathlib.Path,
|
||||
) -> None:
|
||||
with freeze_time("2024-02-03 04:05:06", tz_offset=timezone):
|
||||
fn = tmpdir / "test-cert.pem"
|
||||
fn.write(TEST_CERT)
|
||||
fn = tmp_path / "test-cert.pem"
|
||||
fn.write_text(TEST_CERT)
|
||||
module = MagicMock()
|
||||
module.run_command = MagicMock(return_value=(0, TEST_CERT_OPENSSL_OUTPUT, 0))
|
||||
backend = OpenSSLCLIBackend(module=module, openssl_binary="openssl")
|
||||
@@ -110,10 +116,10 @@ def test_get_cert_information(
|
||||
cert_content: str,
|
||||
expected_cert_info: CertificateInformation,
|
||||
openssl_output: str,
|
||||
tmpdir,
|
||||
tmp_path: pathlib.Path,
|
||||
) -> None:
|
||||
fn = tmpdir / "test-cert.pem"
|
||||
fn.write(cert_content)
|
||||
fn = tmp_path / "test-cert.pem"
|
||||
fn.write_text(cert_content)
|
||||
module = MagicMock()
|
||||
module.run_command = MagicMock(return_value=(0, openssl_output, 0))
|
||||
backend = OpenSSLCLIBackend(module=module, openssl_binary="openssl")
|
||||
@@ -144,7 +150,7 @@ def test_now(timezone: datetime.timedelta) -> None:
|
||||
|
||||
@pytest.mark.parametrize("timezone, timestamp_str, expected", TEST_PARSE_ACME_TIMESTAMP)
|
||||
def test_parse_acme_timestamp(
|
||||
timezone: datetime.timedelta, timestamp_str: str, expected: dict[str, int]
|
||||
timezone: datetime.timedelta, timestamp_str: str, expected: DatetimeKwarg
|
||||
) -> None:
|
||||
with freeze_time("2024-02-03 04:05:06", tz_offset=timezone):
|
||||
module = MagicMock()
|
||||
@@ -159,10 +165,10 @@ def test_parse_acme_timestamp(
|
||||
)
|
||||
def test_interpolate_timestamp(
|
||||
timezone: datetime.timedelta,
|
||||
start: dict[str, int],
|
||||
end: dict[str, int],
|
||||
start: DatetimeKwarg,
|
||||
end: DatetimeKwarg,
|
||||
percentage: float,
|
||||
expected: dict[str, int],
|
||||
expected: DatetimeKwarg,
|
||||
) -> None:
|
||||
with freeze_time("2024-02-03 04:05:06", tz_offset=timezone):
|
||||
module = MagicMock()
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pathlib
|
||||
from unittest.mock import (
|
||||
MagicMock,
|
||||
)
|
||||
@@ -18,14 +19,14 @@ TEST_TEXT = r"""1234
|
||||
5678"""
|
||||
|
||||
|
||||
def test_read_file(tmpdir) -> None:
|
||||
fn = tmpdir / "test.txt"
|
||||
fn.write(TEST_TEXT)
|
||||
def test_read_file(tmp_path: pathlib.Path) -> None:
|
||||
fn = tmp_path / "test.txt"
|
||||
fn.write_text(TEST_TEXT)
|
||||
assert read_file(str(fn)) == TEST_TEXT.encode("utf-8")
|
||||
|
||||
|
||||
def test_write_file(tmpdir) -> None:
|
||||
fn = tmpdir / "test.txt"
|
||||
def test_write_file(tmp_path: pathlib.Path) -> None:
|
||||
fn = tmp_path / "test.txt"
|
||||
module = MagicMock()
|
||||
write_file(module=module, dest=str(fn), content=TEST_TEXT.encode("utf-8"))
|
||||
assert fn.read() == TEST_TEXT
|
||||
assert fn.read_text() == TEST_TEXT
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
import pathlib
|
||||
import typing as t
|
||||
|
||||
import pytest
|
||||
@@ -100,9 +101,9 @@ def test_nopad_b64(value: str, result: str) -> None:
|
||||
|
||||
|
||||
@pytest.mark.parametrize("pem, der", TEST_PEM_DERS)
|
||||
def test_pem_to_der(pem: str, der: bytes, tmpdir):
|
||||
fn = tmpdir / "test.pem"
|
||||
fn.write(pem)
|
||||
def test_pem_to_der(pem: str, der: bytes, tmp_path: pathlib.Path) -> None:
|
||||
fn = tmp_path / "test.pem"
|
||||
fn.write_text(pem)
|
||||
assert pem_to_der(pem_filename=str(fn)) == der
|
||||
assert pem_to_der(pem_content=pem) == der
|
||||
|
||||
@@ -111,9 +112,9 @@ def test_pem_to_der(pem: str, der: bytes, tmpdir):
|
||||
def test_process_links(
|
||||
value: dict[str, t.Any], expected_result: list[tuple[str, str]]
|
||||
) -> None:
|
||||
data = []
|
||||
data: list[tuple[str, str]] = []
|
||||
|
||||
def callback(url, rel):
|
||||
def callback(url: str, rel: str) -> None:
|
||||
data.append((url, rel))
|
||||
|
||||
process_links(info=value, callback=callback)
|
||||
|
||||
Reference in New Issue
Block a user