From 95917a951ea6a506b34c79d3bd3ff9eaa02b70dc Mon Sep 17 00:00:00 2001 From: Jack Sullivan Date: Wed, 26 Apr 2017 12:49:44 -0700 Subject: [PATCH] In tests, only set cooldown if testing it The results for the cache unit tests were inconsistent, sometimes blocking and other time allowing. The source of the non-determinism was the COOLDOWN_TIME set to 2 seconds in the test. If a test took slightly longer than the cooldown time, it would fail. Testing times on Travis CI vary with each build, and would produce unreliable results. Now all tests have no cooldown period, except when the cooldown itself is being tested. This ensures accurate and predicable test results. --- axes/test_settings.py | 3 --- axes/tests.py | 8 ++++++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/axes/test_settings.py b/axes/test_settings.py index b8a47d4..46ba0b5 100644 --- a/axes/test_settings.py +++ b/axes/test_settings.py @@ -1,5 +1,3 @@ -from datetime import timedelta - DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', @@ -55,4 +53,3 @@ USE_TZ = False LOGIN_REDIRECT_URL = '/admin/' AXES_LOGIN_FAILURE_LIMIT = 10 -AXES_COOLOFF_TIME = timedelta(seconds=2) diff --git a/axes/tests.py b/axes/tests.py index 1ce899e..f12702f 100644 --- a/axes/tests.py +++ b/axes/tests.py @@ -16,13 +16,15 @@ from django.utils import six from django.test.client import RequestFactory from axes.decorators import get_ip, get_cache_key -from axes.settings import COOLOFF_TIME from axes.settings import FAILURE_LIMIT from axes.models import AccessAttempt, AccessLog from axes.signals import user_locked_out from axes.utils import reset, iso8601 +TEST_COOLOFF_TIME = datetime.timedelta(seconds=2) + + class MockRequest: def __init__(self): self.META = dict() @@ -140,17 +142,19 @@ class AccessAttemptTest(TestCase): self.assertNotEquals(AccessLog.objects.latest('id').logout_time, None) self.assertContains(response, 'Logged out') + @patch('axes.decorators.COOLOFF_TIME', TEST_COOLOFF_TIME) def test_cooling_off(self): """Tests if the cooling time allows a user to login """ self.test_failure_limit_once() # Wait for the cooling off period - time.sleep(COOLOFF_TIME.total_seconds()) + time.sleep(TEST_COOLOFF_TIME.total_seconds()) # It should be possible to login again, make sure it is. self.test_valid_login() + @patch('axes.decorators.COOLOFF_TIME', TEST_COOLOFF_TIME) def test_cooling_off_for_trusted_user(self): """Test the cooling time for a trusted user """