Skip to content

Commit

Permalink
test: use AXES_LOCKOUT_PARAMETERS in handlers test
Browse files Browse the repository at this point in the history
  • Loading branch information
hirotasoshu authored and wannacfuture committed May 13, 2023
1 parent b6c5fc5 commit eb2465a
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,10 @@ def test_handler_remove_out_of_limit_failure_logs(self):
_more = 10
for i in range(settings.AXES_ACCESS_FAILURE_LOG_PER_USER_LIMIT + _more):
self.create_failure_log()
self.assertEqual(_more, AxesProxyHandler.remove_out_of_limit_failure_logs(username=self.username))
self.assertEqual(
_more,
AxesProxyHandler.remove_out_of_limit_failure_logs(username=self.username),
)

@override_settings(AXES_RESET_ON_SUCCESS=True)
def test_handler(self):
Expand Down Expand Up @@ -296,7 +299,7 @@ def test_empty_request(self, log):
def test_whitelist(self, log):
self.check_whitelist(log)

@override_settings(AXES_ONLY_USER_FAILURES=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=["username"])
@patch("axes.handlers.database.log")
def test_user_login_failed_only_user_failures_with_none_username(self, log):
credentials = {"username": None, "password": "test"}
Expand All @@ -305,7 +308,7 @@ def test_user_login_failed_only_user_failures_with_none_username(self, log):
attempt = AccessAttempt.objects.all()
self.assertEqual(0, AccessAttempt.objects.count())
log.warning.assert_called_with(
"AXES: Username is None and AXES_ONLY_USER_FAILURES is enabled, new record will NOT be created."
"AXES: Username is None and username is the only one lockout parameter, new record will NOT be created."
)

def test_user_login_failed_with_none_username(self):
Expand All @@ -318,22 +321,37 @@ def test_user_login_failed_with_none_username(self):
def test_user_login_failed_multiple_username(self):
configurations = (
(2, 1, {}, ["admin", "admin1"]),
(2, 1, {"AXES_USE_USER_AGENT": True}, ["admin", "admin1"]),
(2, 1, {"AXES_ONLY_USER_FAILURES": True}, ["admin", "admin1"]),
(
2,
1,
{"AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP": True},
{"AXES_LOCKOUT_PARAMETERS": [("ip_address", "user_agent")]},
["admin", "admin1"],
),
(2, 1, {"AXES_LOCKOUT_PARAMETERS": ["username"]}, ["admin", "admin1"]),
(
2,
1,
{"AXES_LOCKOUT_PARAMETERS": [("username", "ip_address")]},
["admin", "admin1"],
),
(
1,
2,
{"AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP": True},
{"AXES_LOCKOUT_PARAMETERS": [("username", "ip_address")]},
["admin", "admin"],
),
(1, 2, {"AXES_LOCK_OUT_BY_USER_OR_IP": True}, ["admin", "admin"]),
(2, 1, {"AXES_LOCK_OUT_BY_USER_OR_IP": True}, ["admin", "admin1"]),
(
1,
2,
{"AXES_LOCKOUT_PARAMETERS": ["username", "ip_address"]},
["admin", "admin"],
),
(
2,
1,
{"AXES_LOCKOUT_PARAMETERS": ["username", "ip_address"]},
["admin", "admin1"],
),
)

for (
Expand Down Expand Up @@ -400,7 +418,7 @@ def test_handler_reset_attempts(self):
with self.assertRaises(NotImplementedError):
AxesProxyHandler.reset_attempts()

@override_settings(AXES_ONLY_USER_FAILURES=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=["username"])
def test_handler_reset_attempts_username(self):
self.set_up_login_attempts()
self.assertEqual(
Expand Down Expand Up @@ -436,7 +454,7 @@ def test_handler_reset_attempts_ip(self):
self.check_failures(0, ip_address=self.IP_1)
self.check_failures(2, ip_address=self.IP_2)

@override_settings(AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=[("username", "ip_address")])
def test_handler_reset_attempts_ip_and_username(self):
self.set_up_login_attempts()
self.check_failures(1, username=self.USERNAME_1, ip_address=self.IP_1)
Expand Down Expand Up @@ -482,7 +500,7 @@ def test_empty_request(self, log):
def test_whitelist(self, log):
self.check_whitelist(log)

@override_settings(AXES_ONLY_USER_FAILURES=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=["username"])
@patch.object(cache, "set")
@patch("axes.handlers.cache.log")
def test_user_login_failed_only_user_failures_with_none_username(
Expand All @@ -493,7 +511,7 @@ def test_user_login_failed_only_user_failures_with_none_username(
AxesProxyHandler.user_login_failed(sender, credentials, self.request)
self.assertFalse(cache_set.called)
log.warning.assert_called_with(
"AXES: Username is None and AXES_ONLY_USER_FAILURES is enabled, new record will NOT be created."
"AXES: Username is None and username is the only one lockout parameter, new record will NOT be created."
)

@patch.object(cache, "add")
Expand Down

0 comments on commit eb2465a

Please sign in to comment.