-
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
49 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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) |