mirror of
https://github.com/jazzband/django-configurations.git
synced 2026-03-16 22:20:27 +00:00
add tests for example generators
This commit is contained in:
parent
5cd3a86106
commit
3b96d16510
2 changed files with 52 additions and 5 deletions
|
|
@ -1,8 +1,17 @@
|
|||
from typing import Callable
|
||||
import secrets
|
||||
import base64
|
||||
|
||||
import django
|
||||
from django.core.management.utils import get_random_secret_key
|
||||
from django.utils.crypto import get_random_string, RANDOM_STRING_CHARS
|
||||
from django.utils.crypto import get_random_string
|
||||
|
||||
if django.VERSION[0] > 3 or \
|
||||
(django.VERSION[0] == 3 and django.VERSION[1] >= 2):
|
||||
# RANDOM_STRING_CHARS was only introduced in django 3.2
|
||||
from django.utils.crypto import RANDOM_STRING_CHARS
|
||||
else:
|
||||
RANDOM_STRING_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" # pragma: no cover
|
||||
|
||||
|
||||
def gen_django_secret_key() -> str:
|
||||
|
|
@ -17,9 +26,11 @@ def gen_random_string(length: int, allowed_chars: str = RANDOM_STRING_CHARS) ->
|
|||
Create a parameterized generator which generates a cryptographically secure random string of the given length
|
||||
containing the given characters.
|
||||
"""
|
||||
def generate() -> str:
|
||||
|
||||
def _gen_random_string() -> str:
|
||||
return get_random_string(length, allowed_chars)
|
||||
return generate
|
||||
|
||||
return _gen_random_string
|
||||
|
||||
|
||||
def gen_bytes(length: int, encoding: str) -> Callable[[], str]:
|
||||
|
|
@ -36,7 +47,7 @@ def gen_bytes(length: int, encoding: str) -> Callable[[], str]:
|
|||
raise ValueError(f"Cannot gen_bytes with encoding '{encoding}'. Valid encodings are 'base64', 'base64_urlsafe'"
|
||||
f" and 'hex'")
|
||||
|
||||
def generate() -> str:
|
||||
def _gen_bytes() -> str:
|
||||
b = secrets.token_bytes(length)
|
||||
if encoding == "base64":
|
||||
return base64.standard_b64encode(b).decode("ASCII")
|
||||
|
|
@ -44,4 +55,5 @@ def gen_bytes(length: int, encoding: str) -> Callable[[], str]:
|
|||
return base64.urlsafe_b64encode(b).decode("ASCII")
|
||||
elif encoding == "hex":
|
||||
return b.hex().upper()
|
||||
return generate
|
||||
|
||||
return _gen_bytes
|
||||
|
|
|
|||
35
tests/test_example_generators.py
Normal file
35
tests/test_example_generators.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import base64
|
||||
from django.test import TestCase
|
||||
from configurations.example_generators import gen_bytes, gen_random_string, gen_django_secret_key
|
||||
|
||||
|
||||
class ExampleGeneratorsTestCase(TestCase):
|
||||
def test_generators_dont_raise_exceptions(self):
|
||||
for gen in [gen_bytes(64, "hex"), gen_bytes(64, "base64"), gen_bytes(64, "base64_urlsafe"),
|
||||
gen_random_string(16, "ab"), gen_random_string(5),
|
||||
gen_django_secret_key]:
|
||||
with self.subTest(gen.__name__):
|
||||
gen()
|
||||
|
||||
# gen_django_secret_key() and gen_random_string() are not tested beyond the above general test case
|
||||
# because they are just wrappers around existing django utilities.
|
||||
# They are thus assumed to work.
|
||||
|
||||
def test_gen_bytes(self):
|
||||
with self.subTest("base64"):
|
||||
result = gen_bytes(64, "base64")()
|
||||
b = base64.standard_b64decode(result.encode("ASCII"))
|
||||
self.assertEqual(len(b), 64)
|
||||
|
||||
with self.subTest("base64_urlsafe"):
|
||||
result = gen_bytes(64, "base64_urlsafe")()
|
||||
b = base64.urlsafe_b64decode(result.encode("ASCII"))
|
||||
self.assertEqual(len(b), 64)
|
||||
|
||||
with self.subTest("hex"):
|
||||
result = gen_bytes(64, "hex")()
|
||||
b = bytes.fromhex(result)
|
||||
self.assertEqual(len(b), 64)
|
||||
|
||||
with self.subTest("invalid"):
|
||||
self.assertRaises(ValueError, gen_bytes, 64, "invalid")
|
||||
Loading…
Reference in a new issue