authentik.lib.tests.test_utils_time
Test time utils
1"""Test time utils""" 2 3from datetime import timedelta 4from unittest.mock import patch 5 6from django.core.exceptions import ValidationError 7from django.test import TestCase 8 9from authentik.lib.utils.time import fqdn_rand, timedelta_from_string, timedelta_string_validator 10 11 12class TestTimeUtils(TestCase): 13 """Test time-utils""" 14 15 def test_valid(self): 16 """Test valid expression""" 17 expr = "hours=3;minutes=1" 18 expected = timedelta(hours=3, minutes=1) 19 self.assertEqual(timedelta_from_string(expr), expected) 20 21 def test_invalid(self): 22 """Test invalid expression""" 23 with self.assertRaises(ValueError): 24 timedelta_from_string("foo") 25 with self.assertRaises(ValueError): 26 timedelta_from_string("bar=baz") 27 28 def test_validation(self): 29 """Test Django model field validator""" 30 with self.assertRaises(ValidationError): 31 timedelta_string_validator("foo") 32 33 def test_validation_overflow(self): 34 """Reject durations outside the range supported by timedelta.""" 35 for value in ("days=1000000000", "days=-1000000000", "seconds=inf"): 36 with self.subTest(value=value), self.assertRaises(ValidationError): 37 timedelta_string_validator(value) 38 39 @patch("authentik.lib.utils.time.socket.gethostname", return_value="test-host") 40 def test_fqdn_rand_deterministic(self, _gethostname): 41 """Test schedule entropy is stable for a hostname and task""" 42 self.assertEqual(fqdn_rand("test-task"), fqdn_rand("test-task")) 43 44 @patch("authentik.lib.utils.time.socket.gethostname", return_value="test-host") 45 def test_fqdn_rand_range(self, _gethostname): 46 """Test schedule entropy remains within the requested range""" 47 self.assertGreaterEqual(fqdn_rand("test-task"), 0) 48 self.assertLess(fqdn_rand("test-task"), 60) 49 self.assertGreaterEqual(fqdn_rand("test-task", 7), 0) 50 self.assertLess(fqdn_rand("test-task", 7), 7) 51 52 def test_fqdn_rand_does_not_resolve_hostname(self): 53 """Test schedule entropy does not depend on DNS resolver functions""" 54 with ( 55 patch("authentik.lib.utils.time.socket.gethostname", return_value="test-host"), 56 patch( 57 "authentik.lib.utils.time.socket.getfqdn", 58 side_effect=AssertionError("getfqdn must not be called"), 59 ) as getfqdn, 60 patch( 61 "authentik.lib.utils.time.socket.getaddrinfo", 62 side_effect=AssertionError("getaddrinfo must not be called"), 63 ) as getaddrinfo, 64 patch( 65 "authentik.lib.utils.time.socket.gethostbyname", 66 side_effect=AssertionError("gethostbyname must not be called"), 67 ) as gethostbyname, 68 patch( 69 "authentik.lib.utils.time.socket.gethostbyaddr", 70 side_effect=AssertionError("gethostbyaddr must not be called"), 71 ) as gethostbyaddr, 72 ): 73 self.assertEqual(fqdn_rand("test-task"), fqdn_rand("test-task")) 74 75 getfqdn.assert_not_called() 76 getaddrinfo.assert_not_called() 77 gethostbyname.assert_not_called() 78 gethostbyaddr.assert_not_called()
class
TestTimeUtils(django.test.testcases.TestCase):
13class TestTimeUtils(TestCase): 14 """Test time-utils""" 15 16 def test_valid(self): 17 """Test valid expression""" 18 expr = "hours=3;minutes=1" 19 expected = timedelta(hours=3, minutes=1) 20 self.assertEqual(timedelta_from_string(expr), expected) 21 22 def test_invalid(self): 23 """Test invalid expression""" 24 with self.assertRaises(ValueError): 25 timedelta_from_string("foo") 26 with self.assertRaises(ValueError): 27 timedelta_from_string("bar=baz") 28 29 def test_validation(self): 30 """Test Django model field validator""" 31 with self.assertRaises(ValidationError): 32 timedelta_string_validator("foo") 33 34 def test_validation_overflow(self): 35 """Reject durations outside the range supported by timedelta.""" 36 for value in ("days=1000000000", "days=-1000000000", "seconds=inf"): 37 with self.subTest(value=value), self.assertRaises(ValidationError): 38 timedelta_string_validator(value) 39 40 @patch("authentik.lib.utils.time.socket.gethostname", return_value="test-host") 41 def test_fqdn_rand_deterministic(self, _gethostname): 42 """Test schedule entropy is stable for a hostname and task""" 43 self.assertEqual(fqdn_rand("test-task"), fqdn_rand("test-task")) 44 45 @patch("authentik.lib.utils.time.socket.gethostname", return_value="test-host") 46 def test_fqdn_rand_range(self, _gethostname): 47 """Test schedule entropy remains within the requested range""" 48 self.assertGreaterEqual(fqdn_rand("test-task"), 0) 49 self.assertLess(fqdn_rand("test-task"), 60) 50 self.assertGreaterEqual(fqdn_rand("test-task", 7), 0) 51 self.assertLess(fqdn_rand("test-task", 7), 7) 52 53 def test_fqdn_rand_does_not_resolve_hostname(self): 54 """Test schedule entropy does not depend on DNS resolver functions""" 55 with ( 56 patch("authentik.lib.utils.time.socket.gethostname", return_value="test-host"), 57 patch( 58 "authentik.lib.utils.time.socket.getfqdn", 59 side_effect=AssertionError("getfqdn must not be called"), 60 ) as getfqdn, 61 patch( 62 "authentik.lib.utils.time.socket.getaddrinfo", 63 side_effect=AssertionError("getaddrinfo must not be called"), 64 ) as getaddrinfo, 65 patch( 66 "authentik.lib.utils.time.socket.gethostbyname", 67 side_effect=AssertionError("gethostbyname must not be called"), 68 ) as gethostbyname, 69 patch( 70 "authentik.lib.utils.time.socket.gethostbyaddr", 71 side_effect=AssertionError("gethostbyaddr must not be called"), 72 ) as gethostbyaddr, 73 ): 74 self.assertEqual(fqdn_rand("test-task"), fqdn_rand("test-task")) 75 76 getfqdn.assert_not_called() 77 getaddrinfo.assert_not_called() 78 gethostbyname.assert_not_called() 79 gethostbyaddr.assert_not_called()
Test time-utils
def
test_valid(self):
16 def test_valid(self): 17 """Test valid expression""" 18 expr = "hours=3;minutes=1" 19 expected = timedelta(hours=3, minutes=1) 20 self.assertEqual(timedelta_from_string(expr), expected)
Test valid expression
def
test_invalid(self):
22 def test_invalid(self): 23 """Test invalid expression""" 24 with self.assertRaises(ValueError): 25 timedelta_from_string("foo") 26 with self.assertRaises(ValueError): 27 timedelta_from_string("bar=baz")
Test invalid expression
def
test_validation(self):
29 def test_validation(self): 30 """Test Django model field validator""" 31 with self.assertRaises(ValidationError): 32 timedelta_string_validator("foo")
Test Django model field validator
def
test_validation_overflow(self):
34 def test_validation_overflow(self): 35 """Reject durations outside the range supported by timedelta.""" 36 for value in ("days=1000000000", "days=-1000000000", "seconds=inf"): 37 with self.subTest(value=value), self.assertRaises(ValidationError): 38 timedelta_string_validator(value)
Reject durations outside the range supported by timedelta.
@patch('authentik.lib.utils.time.socket.gethostname', return_value='test-host')
def
test_fqdn_rand_deterministic(self, _gethostname):
40 @patch("authentik.lib.utils.time.socket.gethostname", return_value="test-host") 41 def test_fqdn_rand_deterministic(self, _gethostname): 42 """Test schedule entropy is stable for a hostname and task""" 43 self.assertEqual(fqdn_rand("test-task"), fqdn_rand("test-task"))
Test schedule entropy is stable for a hostname and task
@patch('authentik.lib.utils.time.socket.gethostname', return_value='test-host')
def
test_fqdn_rand_range(self, _gethostname):
45 @patch("authentik.lib.utils.time.socket.gethostname", return_value="test-host") 46 def test_fqdn_rand_range(self, _gethostname): 47 """Test schedule entropy remains within the requested range""" 48 self.assertGreaterEqual(fqdn_rand("test-task"), 0) 49 self.assertLess(fqdn_rand("test-task"), 60) 50 self.assertGreaterEqual(fqdn_rand("test-task", 7), 0) 51 self.assertLess(fqdn_rand("test-task", 7), 7)
Test schedule entropy remains within the requested range
def
test_fqdn_rand_does_not_resolve_hostname(self):
53 def test_fqdn_rand_does_not_resolve_hostname(self): 54 """Test schedule entropy does not depend on DNS resolver functions""" 55 with ( 56 patch("authentik.lib.utils.time.socket.gethostname", return_value="test-host"), 57 patch( 58 "authentik.lib.utils.time.socket.getfqdn", 59 side_effect=AssertionError("getfqdn must not be called"), 60 ) as getfqdn, 61 patch( 62 "authentik.lib.utils.time.socket.getaddrinfo", 63 side_effect=AssertionError("getaddrinfo must not be called"), 64 ) as getaddrinfo, 65 patch( 66 "authentik.lib.utils.time.socket.gethostbyname", 67 side_effect=AssertionError("gethostbyname must not be called"), 68 ) as gethostbyname, 69 patch( 70 "authentik.lib.utils.time.socket.gethostbyaddr", 71 side_effect=AssertionError("gethostbyaddr must not be called"), 72 ) as gethostbyaddr, 73 ): 74 self.assertEqual(fqdn_rand("test-task"), fqdn_rand("test-task")) 75 76 getfqdn.assert_not_called() 77 getaddrinfo.assert_not_called() 78 gethostbyname.assert_not_called() 79 gethostbyaddr.assert_not_called()
Test schedule entropy does not depend on DNS resolver functions