From 2d288b247a2d619bf90943370aae699a5c14c841 Mon Sep 17 00:00:00 2001 From: djmore4 Date: Tue, 11 Oct 2022 17:11:22 -0400 Subject: [PATCH] Try some new things --- defender/tests.py | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/defender/tests.py b/defender/tests.py index bf35219..fff91ac 100644 --- a/defender/tests.py +++ b/defender/tests.py @@ -286,7 +286,7 @@ class AccessAttemptTest(DefenderTestCase): """ self.test_failure_limit_by_ip_once() # Wait for the cooling off period - time.sleep(config.ATTEMPT_COOLOFF_TIME) + time.sleep(config.LOCKOUT_COOLOFF_TIMES[0]) if config.MOCK_REDIS: # mock redis require that we expire on our own @@ -427,6 +427,7 @@ class AccessAttemptTest(DefenderTestCase): self.assertTemplateUsed(response, "defender/lockout.html") @patch("defender.config.COOLOFF_TIME", 0) + @patch("defender.config.LOCKOUT_COOLOFF_TIMES", [0]) def test_failed_login_no_cooloff(self): """ failed login no cooloff """ for i in range(0, config.FAILURE_LIMIT): @@ -950,39 +951,57 @@ class AccessAttemptTest(DefenderTestCase): def test_lockout_cooloff_correctly_scales_with_ip_when_set(self): self.test_failure_limit_by_ip_once() time.sleep(config.LOCKOUT_COOLOFF_TIMES[0]) - self.test_valid_login() + if config.MOCK_REDIS: + # mock redis require that we expire on our own + get_redis_connection().do_expire() # pragma: no cover + self.assertFalse(utils.is_source_ip_already_locked("127.0.0.1")) self.test_failure_limit_by_ip_once() time.sleep(config.LOCKOUT_COOLOFF_TIMES[0]) - self.test_blocked_ip_cannot_login() + self.assertTrue(utils.is_source_ip_already_locked("127.0.0.1")) utils.reset_failed_attempts(ip_address="127.0.0.1") @patch("defender.config.LOCKOUT_COOLOFF_TIMES", [2, 4]) def test_lockout_cooloff_correctly_scales_with_username_when_set(self): self.test_failure_limit_by_username_once() time.sleep(config.LOCKOUT_COOLOFF_TIMES[0]) - self.test_valid_login() + if config.MOCK_REDIS: + # mock redis require that we expire on our own + get_redis_connection().do_expire() # pragma: no cover + self.assertFalse(utils.is_user_already_locked(VALID_USERNAME)) self.test_failure_limit_by_username_once() time.sleep(config.LOCKOUT_COOLOFF_TIMES[0]) - self.test_blocked_username_cannot_login() + self.assertTrue(utils.is_user_already_locked(VALID_USERNAME)) utils.reset_failed_attempts(username=VALID_USERNAME) @patch("defender.config.LOCKOUT_COOLOFF_TIMES", [2, 4]) def test_lockout_correctly_releases_after_scaling_with_ip(self): self.test_failure_limit_by_ip_once() time.sleep(config.LOCKOUT_COOLOFF_TIMES[0]) - self.test_valid_login() + if config.MOCK_REDIS: + # mock redis require that we expire on our own + get_redis_connection().do_expire() # pragma: no cover + self.assertFalse(utils.is_source_ip_already_locked("127.0.0.1")) self.test_failure_limit_by_ip_once() time.sleep(config.LOCKOUT_COOLOFF_TIMES[1]) - self.test_valid_login() + if config.MOCK_REDIS: + # mock redis require that we expire on our own + get_redis_connection().do_expire() # pragma: no cover + self.assertFalse(utils.is_source_ip_already_locked("127.0.0.1")) @patch("defender.config.LOCKOUT_COOLOFF_TIMES", [2, 4]) def test_lockout_correctly_releases_after_scaling_with_username(self): self.test_failure_limit_by_username_once() time.sleep(config.LOCKOUT_COOLOFF_TIMES[0]) - self.test_valid_login() + if config.MOCK_REDIS: + # mock redis require that we expire on our own + get_redis_connection().do_expire() # pragma: no cover + self.assertFalse(utils.is_user_already_locked(VALID_USERNAME)) self.test_failure_limit_by_username_once() time.sleep(config.LOCKOUT_COOLOFF_TIMES[1]) - self.test_valid_login() + if config.MOCK_REDIS: + # mock redis require that we expire on our own + get_redis_connection().do_expire() # pragma: no cover + self.assertFalse(utils.is_user_already_locked(VALID_USERNAME)) class SignalTest(DefenderTestCase):