Skip to content

Commit

Permalink
move utils tests to pytest, deprecate some unused functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasertl committed Jun 27, 2024
1 parent 8d60e96 commit dcf5b2f
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 275 deletions.
22 changes: 20 additions & 2 deletions ca/django_ca/deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ class RemovedInDjangoCA220Warning(PendingDeprecationWarning):
]


def deprecate_function(category: DeprecationWarningType, stacklevel: int = 2) -> typing.Callable[[F], F]:
"""Decorator to deprecate an entire function."""

def decorator_deprecate(func: F) -> F:
@functools.wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
warnings.warn(
f"{func.__name__}() is deprecated and will be removed in django-ca {category.version}.",
category=category,
stacklevel=stacklevel,
)
return func(*args, **kwargs)

return typing.cast(F, wrapper)

return decorator_deprecate


def deprecate_argument(
arg: str, category: DeprecationWarningType, stacklevel: int = 2
) -> typing.Callable[[F], F]:
Expand All @@ -67,7 +85,7 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
bound = sig.bind(*args, **kwargs)
if arg in bound.arguments:
warnings.warn(
f"Argument {arg} is deprecated and will be removed in django ca {category.version}.",
f"Argument {arg} is deprecated and will be removed in django-ca {category.version}.",
category=category,
stacklevel=stacklevel,
)
Expand Down Expand Up @@ -95,7 +113,7 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
if arg in bound.arguments and isinstance(bound.arguments.get(arg), types):
name = type(bound.arguments.get(arg)).__name__
warnings.warn(
f"Passing {name} for {arg} is deprecated and will be removed in django ca {category.version}.", # NOQA: E501
f"Passing {name} for {arg} is deprecated and will be removed in django-ca {category.version}.", # NOQA: E501
category=category,
stacklevel=stacklevel,
)
Expand Down
8 changes: 7 additions & 1 deletion ca/django_ca/migration_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""

import typing
import warnings
from typing import Optional

from cryptography import x509
Expand Down Expand Up @@ -75,7 +76,12 @@ def issuer_alt_name_to_sign_issuer_alternative_name(ca: MigratingCertificateAuth
# This is how this value was parsed until 1.27.0. De facto, it was a single text input field, and
# no documented input method (admin or command-line interface) allowed multiple values. The
# maximum length was only 255 characters. Together, this makes multiple values unlikely.
alternative_names = [parse_general_name(name) for name in split_str(ca.issuer_alt_name.strip(), ",")]
with warnings.catch_warnings():
warnings.simplefilter("ignore")
alternative_names = [
parse_general_name(name) for name in split_str(ca.issuer_alt_name.strip(), ",")
]

if not alternative_names:
ca.sign_issuer_alternative_name = None
return
Expand Down
10 changes: 5 additions & 5 deletions ca/django_ca/tests/commands/test_regenerate_ocsp_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from django_ca.tests.base.constants import CERT_DATA
from django_ca.tests.base.mixins import TestCaseMixin
from django_ca.tests.base.utils import cmd, cmd_e2e, override_tmpcadir
from django_ca.utils import add_colons, file_exists, read_file
from django_ca.utils import add_colons, get_storage, read_file


def regenerate_ocsp_keys(*serials: str, **kwargs: Any) -> tuple[str, str]:
Expand Down Expand Up @@ -61,8 +61,8 @@ def assertKey( # pylint: disable=invalid-name
priv_path = f"ocsp/{ca.serial}.key"
cert_path = f"ocsp/{ca.serial}.pem"

self.assertTrue(file_exists(priv_path))
self.assertTrue(file_exists(cert_path))
self.assertTrue(get_storage().exists(priv_path))
self.assertTrue(get_storage().exists(cert_path))
if key_type is None:
ca_key = ca.key_backend.get_key( # type: ignore[attr-defined] # we assume StoragesBackend
ca, UsePrivateKeyOptions(password=None)
Expand Down Expand Up @@ -110,8 +110,8 @@ def assertHasNoKey(self, serial: str) -> None: # pylint: disable=invalid-name
"""Assert that the key is **not** present."""
priv_path = f"ocsp/{serial}.key"
cert_path = f"ocsp/{serial}.pem"
self.assertFalse(file_exists(priv_path))
self.assertFalse(file_exists(cert_path))
self.assertFalse(get_storage().exists(priv_path))
self.assertFalse(get_storage().exists(cert_path))

@override_tmpcadir(CA_USE_CELERY=False) # CA_USE_CELERY=False is set anyway, but just to be sure
def test_basic(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion ca/django_ca/tests/test_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ def test_init_with_deprecated_expires(
) -> None:
"""Test a deprecated expired type."""
name = type(expires).__name__
msg = rf"^Passing {name} for expires is deprecated and will be removed in django ca 2.0.$"
msg = rf"^Passing {name} for expires is deprecated and will be removed in django-ca 2.0.$"
with assert_create_ca_signals(), assert_removed_in_200(msg):
ca = CertificateAuthority.objects.init(
ca_name,
Expand Down
4 changes: 2 additions & 2 deletions ca/django_ca/tests/test_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def test_init_expires() -> None:

def test_init_expires_with_deprecated_type() -> None:
"""Test the `expire` parameter."""
msg = r"^Passing int for expires is deprecated and will be removed in django ca 2\.0\.$"
msg = r"^Passing int for expires is deprecated and will be removed in django-ca 2\.0\.$"
with assert_removed_in_200(msg):
prof = Profile("example", expires=30) # type: ignore[arg-type]
assert prof.expires == timedelta(days=30)
Expand Down Expand Up @@ -699,7 +699,7 @@ def test_create_cert_with_deprecated_expires_type(
) -> None:
"""Create a certificate with an int for expires (which is deprecated)."""
csr = CERT_DATA["child-cert"]["csr"]["parsed"]
msg = r"^Passing int for expires is deprecated and will be removed in django ca 2\.0\.$"
msg = r"^Passing int for expires is deprecated and will be removed in django-ca 2\.0\.$"

prof = Profile("example")
with mock_signal(pre_sign_cert) as pre, assert_removed_in_200(msg):
Expand Down
Loading

0 comments on commit dcf5b2f

Please sign in to comment.