From 35d35e4b32d6a2242b1d5363407847e9a83b9fa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksi=20H=C3=A4kli?= Date: Wed, 6 Nov 2019 20:26:30 +0200 Subject: [PATCH] Refactor database handler test code (#516) --- axes/tests/test_handlers.py | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/axes/tests/test_handlers.py b/axes/tests/test_handlers.py index f910099..5cac414 100644 --- a/axes/tests/test_handlers.py +++ b/axes/tests/test_handlers.py @@ -200,27 +200,32 @@ class AxesDatabaseHandlerTestCase(AxesHandlerBaseTestCase): self.assertEqual(1, is_whitelisted.call_count) def test_user_login_failed_multiple_username(self): - tests = [ # (settings, Second login attempt failures_since_start) - ({}, 2), - ({"AXES_ONLY_USER_FAILURES": True}, 1), - ({"AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP": True}, 1), - ({"AXES_USE_USER_AGENT": True}, 2), - ] - for setting_dict, failures_since_start in tests: - with self.settings(**setting_dict): + configurations = ( + (1, 2, {}), + (1, 2, {"AXES_USE_USER_AGENT": True}), + (2, 1, {"AXES_ONLY_USER_FAILURES": True}), + (2, 1, {"AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP": True}), + ) + + for total_attempts_count, failures_since_start, overrides in configurations: + with self.settings(**overrides): with self.subTest( - settings=setting_dict, failures_since_start=failures_since_start + total_attempts_count=total_attempts_count, + failures_since_start=failures_since_start, + settings=overrides, ): self.login(username="admin") - attempts = AccessAttempt.objects.filter(username="admin") - self.assertEqual(1, attempts.count()) - self.assertEqual(1, attempts.first().failures_since_start) + attempt = AccessAttempt.objects.get(username="admin") + self.assertEqual(1, attempt.failures_since_start) + # check the number of failures associated to the attempt self.login(username="other_admin") - attempts = AccessAttempt.objects.filter(username="other_admin") - self.assertEqual(1, attempts.count()) + attempt = AccessAttempt.objects.get(username="other_admin") + self.assertEqual(failures_since_start, attempt.failures_since_start) + + # check the number of distinct attempts self.assertEqual( - failures_since_start, attempts.first().failures_since_start + total_attempts_count, AccessAttempt.objects.count() ) AccessAttempt.objects.all().delete()