Skip to content

Commit

Permalink
test: use AXES_LOCKOUT_PARAMETERS in login 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 a90c3ae commit 83a009b
Showing 1 changed file with 38 additions and 33 deletions.
71 changes: 38 additions & 33 deletions tests/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,11 @@ def test_reset_on_success_true(self):
self.assertContains(response, self.LOCKED_MESSAGE, status_code=self.BLOCKED)
self.assertTrue(self.attempt_count())

@override_settings(AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=[("username", "ip_address")])
def test_lockout_by_combination_user_and_ip(self):
"""
Test login failure when AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP is True.
Test login failure when lockout parameters is combination
of username and ip_address.
"""

# test until one try before the limit
Expand All @@ -199,10 +200,10 @@ def test_lockout_by_combination_user_and_ip(self):
response = self.login(is_valid_username=True, is_valid_password=False)
self.assertContains(response, self.LOCKED_MESSAGE, status_code=429)

@override_settings(AXES_ONLY_USER_FAILURES=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=["username"])
def test_lockout_by_only_user_failures(self):
"""
Test login failure when AXES_ONLY_USER_FAILURES is True.
Test login failure when lockout parameter is username.
"""

# test until one try before the limit
Expand Down Expand Up @@ -274,7 +275,7 @@ def test_lockout_by_ip_allows_when_diff_user_diff_ip_without_cache(self):

# Test for true and false positives when blocking by user only.
# Cache disabled. When AXES_ONLY_USER_FAILURES = True
@override_settings(AXES_ONLY_USER_FAILURES=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=["username"])
def test_lockout_by_user_blocks_when_same_user_same_ip_without_cache(self):
# User 1 is locked out from IP 1.
self._lockout_user1_from_ip1()
Expand All @@ -283,7 +284,7 @@ def test_lockout_by_user_blocks_when_same_user_same_ip_without_cache(self):
response = self._login(self.USER_1, self.VALID_PASSWORD, ip_addr=self.IP_1)
self.assertEqual(response.status_code, self.BLOCKED)

@override_settings(AXES_ONLY_USER_FAILURES=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=["username"])
def test_lockout_by_user_blocks_when_same_user_diff_ip_without_cache(self):
# User 1 is locked out from IP 1.
self._lockout_user1_from_ip1()
Expand All @@ -292,7 +293,7 @@ def test_lockout_by_user_blocks_when_same_user_diff_ip_without_cache(self):
response = self._login(self.USER_1, self.VALID_PASSWORD, ip_addr=self.IP_2)
self.assertEqual(response.status_code, self.BLOCKED)

@override_settings(AXES_ONLY_USER_FAILURES=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=["username"])
def test_lockout_by_user_allows_when_diff_user_same_ip_without_cache(self):
# User 1 is locked out from IP 1.
self._lockout_user1_from_ip1()
Expand All @@ -301,7 +302,7 @@ def test_lockout_by_user_allows_when_diff_user_same_ip_without_cache(self):
response = self._login(self.USER_2, self.VALID_PASSWORD, ip_addr=self.IP_1)
self.assertEqual(response.status_code, self.ALLOWED)

@override_settings(AXES_ONLY_USER_FAILURES=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=["username"])
def test_lockout_by_user_allows_when_diff_user_diff_ip_without_cache(self):
# User 1 is locked out from IP 1.
self._lockout_user1_from_ip1()
Expand All @@ -310,7 +311,7 @@ def test_lockout_by_user_allows_when_diff_user_diff_ip_without_cache(self):
response = self._login(self.USER_2, self.VALID_PASSWORD, ip_addr=self.IP_2)
self.assertEqual(response.status_code, self.ALLOWED)

@override_settings(AXES_ONLY_USER_FAILURES=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=["username"])
def test_lockout_by_user_with_empty_username_allows_other_users_without_cache(self):
# User with empty username is locked out from IP 1.
self._lockout_user_from_ip(username="", ip_addr=self.IP_1)
Expand All @@ -321,7 +322,7 @@ def test_lockout_by_user_with_empty_username_allows_other_users_without_cache(se

# Test for true and false positives when blocking by user and IP together.
# Cache disabled. When LOCK_OUT_BY_COMBINATION_USER_AND_IP = True
@override_settings(AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=[("username", "ip_address")])
def test_lockout_by_user_and_ip_blocks_when_same_user_same_ip_without_cache(self):
# User 1 is locked out from IP 1.
self._lockout_user1_from_ip1()
Expand All @@ -330,7 +331,7 @@ def test_lockout_by_user_and_ip_blocks_when_same_user_same_ip_without_cache(self
response = self._login(self.USER_1, self.VALID_PASSWORD, ip_addr=self.IP_1)
self.assertEqual(response.status_code, self.BLOCKED)

@override_settings(AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=[("username", "ip_address")])
def test_lockout_by_user_and_ip_allows_when_same_user_diff_ip_without_cache(self):
# User 1 is locked out from IP 1.
self._lockout_user1_from_ip1()
Expand All @@ -339,7 +340,7 @@ def test_lockout_by_user_and_ip_allows_when_same_user_diff_ip_without_cache(self
response = self._login(self.USER_1, self.VALID_PASSWORD, ip_addr=self.IP_2)
self.assertEqual(response.status_code, self.ALLOWED)

@override_settings(AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=[("username", "ip_address")])
def test_lockout_by_user_and_ip_allows_when_diff_user_same_ip_without_cache(self):
# User 1 is locked out from IP 1.
self._lockout_user1_from_ip1()
Expand All @@ -348,7 +349,7 @@ def test_lockout_by_user_and_ip_allows_when_diff_user_same_ip_without_cache(self
response = self._login(self.USER_2, self.VALID_PASSWORD, ip_addr=self.IP_1)
self.assertEqual(response.status_code, self.ALLOWED)

@override_settings(AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=[("username", "ip_address")])
def test_lockout_by_user_and_ip_allows_when_diff_user_diff_ip_without_cache(self):
# User 1 is locked out from IP 1.
self._lockout_user1_from_ip1()
Expand All @@ -357,7 +358,7 @@ def test_lockout_by_user_and_ip_allows_when_diff_user_diff_ip_without_cache(self
response = self._login(self.USER_2, self.VALID_PASSWORD, ip_addr=self.IP_2)
self.assertEqual(response.status_code, self.ALLOWED)

@override_settings(AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=[("username", "ip_address")])
def test_lockout_by_user_and_ip_with_empty_username_allows_other_users_without_cache(
self,
):
Expand All @@ -368,7 +369,7 @@ def test_lockout_by_user_and_ip_with_empty_username_allows_other_users_without_c
response = self.client.get(reverse("admin:login"), REMOTE_ADDR=self.IP_1)
self.assertContains(response, self.LOGIN_FORM_KEY, status_code=200, html=True)

@override_settings(AXES_USE_USER_AGENT=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=[("ip_address", "user_agent")])
def test_lockout_by_user_still_allows_login_with_differnet_user_agent(self):
# User with empty username is locked out with "test-browser" user agent.
self._lockout_user_from_ip(username="username", ip_addr=self.IP_1, user_agent="test-browser")
Expand Down Expand Up @@ -415,7 +416,7 @@ def test_lockout_by_ip_allows_when_diff_user_diff_ip_using_cache(self):
response = self._login(self.USER_2, self.VALID_PASSWORD, ip_addr=self.IP_2)
self.assertEqual(response.status_code, self.ALLOWED)

@override_settings(AXES_ONLY_USER_FAILURES=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=["username"])
def test_lockout_by_user_with_empty_username_allows_other_users_using_cache(self):
# User with empty username is locked out from IP 1.
self._lockout_user_from_ip(username="", ip_addr=self.IP_1)
Expand All @@ -426,7 +427,7 @@ def test_lockout_by_user_with_empty_username_allows_other_users_using_cache(self

# Test for true and false positives when blocking by user only.
# With cache enabled. When AXES_ONLY_USER_FAILURES = True
@override_settings(AXES_ONLY_USER_FAILURES=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=["username"])
def test_lockout_by_user_blocks_when_same_user_same_ip_using_cache(self):
# User 1 is locked out from IP 1.
self._lockout_user1_from_ip1()
Expand All @@ -435,7 +436,7 @@ def test_lockout_by_user_blocks_when_same_user_same_ip_using_cache(self):
response = self._login(self.USER_1, self.VALID_PASSWORD, ip_addr=self.IP_1)
self.assertEqual(response.status_code, self.BLOCKED)

@override_settings(AXES_ONLY_USER_FAILURES=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=["username"])
def test_lockout_by_user_blocks_when_same_user_diff_ip_using_cache(self):
# User 1 is locked out from IP 1.
self._lockout_user1_from_ip1()
Expand All @@ -444,7 +445,7 @@ def test_lockout_by_user_blocks_when_same_user_diff_ip_using_cache(self):
response = self._login(self.USER_1, self.VALID_PASSWORD, ip_addr=self.IP_2)
self.assertEqual(response.status_code, self.BLOCKED)

@override_settings(AXES_ONLY_USER_FAILURES=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=["username"])
def test_lockout_by_user_allows_when_diff_user_same_ip_using_cache(self):
# User 1 is locked out from IP 1.
self._lockout_user1_from_ip1()
Expand All @@ -453,7 +454,7 @@ def test_lockout_by_user_allows_when_diff_user_same_ip_using_cache(self):
response = self._login(self.USER_2, self.VALID_PASSWORD, ip_addr=self.IP_1)
self.assertEqual(response.status_code, self.ALLOWED)

@override_settings(AXES_ONLY_USER_FAILURES=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=["username"])
def test_lockout_by_user_allows_when_diff_user_diff_ip_using_cache(self):
# User 1 is locked out from IP 1.
self._lockout_user1_from_ip1()
Expand All @@ -464,7 +465,7 @@ def test_lockout_by_user_allows_when_diff_user_diff_ip_using_cache(self):

# Test for true and false positives when blocking by user and IP together.
# With cache enabled. When LOCK_OUT_BY_COMBINATION_USER_AND_IP = True
@override_settings(AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=[("username", "ip_address")])
def test_lockout_by_user_and_ip_blocks_when_same_user_same_ip_using_cache(self):
# User 1 is locked out from IP 1.
self._lockout_user1_from_ip1()
Expand All @@ -473,7 +474,7 @@ def test_lockout_by_user_and_ip_blocks_when_same_user_same_ip_using_cache(self):
response = self._login(self.USER_1, self.VALID_PASSWORD, ip_addr=self.IP_1)
self.assertEqual(response.status_code, self.BLOCKED)

@override_settings(AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=[("username", "ip_address")])
def test_lockout_by_user_and_ip_allows_when_same_user_diff_ip_using_cache(self):
# User 1 is locked out from IP 1.
self._lockout_user1_from_ip1()
Expand All @@ -482,7 +483,7 @@ def test_lockout_by_user_and_ip_allows_when_same_user_diff_ip_using_cache(self):
response = self._login(self.USER_1, self.VALID_PASSWORD, ip_addr=self.IP_2)
self.assertEqual(response.status_code, self.ALLOWED)

@override_settings(AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=[("username", "ip_address")])
def test_lockout_by_user_and_ip_allows_when_diff_user_same_ip_using_cache(self):
# User 1 is locked out from IP 1.
self._lockout_user1_from_ip1()
Expand All @@ -491,7 +492,7 @@ def test_lockout_by_user_and_ip_allows_when_diff_user_same_ip_using_cache(self):
response = self._login(self.USER_2, self.VALID_PASSWORD, ip_addr=self.IP_1)
self.assertEqual(response.status_code, self.ALLOWED)

@override_settings(AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=[("username", "ip_address")])
def test_lockout_by_user_and_ip_allows_when_diff_user_diff_ip_using_cache(self):
# User 1 is locked out from IP 1.
self._lockout_user1_from_ip1()
Expand All @@ -501,7 +502,7 @@ def test_lockout_by_user_and_ip_allows_when_diff_user_diff_ip_using_cache(self):
self.assertEqual(response.status_code, self.ALLOWED)

@override_settings(
AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP=True, AXES_FAILURE_LIMIT=2
AXES_LOCKOUT_PARAMETERS=[("username", "ip_address")], AXES_FAILURE_LIMIT=2
)
def test_lockout_by_user_and_ip_allows_when_diff_user_same_ip_using_cache_multiple_attempts(
self,
Expand Down Expand Up @@ -530,7 +531,7 @@ def test_lockout_by_user_and_ip_allows_when_diff_user_same_ip_using_cache_multip
response = self._login(self.USER_2, self.VALID_PASSWORD, ip_addr=self.IP_2)
self.assertEqual(response.status_code, self.ALLOWED)

@override_settings(AXES_LOCK_OUT_BY_COMBINATION_USER_AND_IP=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=[("username", "ip_address")])
def test_lockout_by_user_and_ip_with_empty_username_allows_other_users_using_cache(
self,
):
Expand All @@ -543,7 +544,7 @@ def test_lockout_by_user_and_ip_with_empty_username_allows_other_users_using_cac

# Test for true and false positives when blocking by user or IP together.
# With cache enabled. When AXES_LOCK_OUT_BY_USER_OR_IP = True
@override_settings(AXES_LOCK_OUT_BY_USER_OR_IP=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=["username", "ip_address"])
def test_lockout_by_user_or_ip_blocks_when_same_user_same_ip_using_cache(self):
# User 1 is locked out from IP 1.
self._lockout_user1_from_ip1()
Expand All @@ -552,7 +553,7 @@ def test_lockout_by_user_or_ip_blocks_when_same_user_same_ip_using_cache(self):
response = self._login(self.USER_1, self.VALID_PASSWORD, ip_addr=self.IP_1)
self.assertEqual(response.status_code, self.BLOCKED)

@override_settings(AXES_LOCK_OUT_BY_USER_OR_IP=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=["username", "ip_address"])
def test_lockout_by_user_or_ip_allows_when_same_user_diff_ip_using_cache(self):
# User 1 is locked out from IP 1.
self._lockout_user1_from_ip1()
Expand All @@ -561,7 +562,7 @@ def test_lockout_by_user_or_ip_allows_when_same_user_diff_ip_using_cache(self):
response = self._login(self.USER_1, self.VALID_PASSWORD, ip_addr=self.IP_2)
self.assertEqual(response.status_code, self.BLOCKED)

@override_settings(AXES_LOCK_OUT_BY_USER_OR_IP=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=["username", "ip_address"])
def test_lockout_by_user_or_ip_allows_when_diff_user_same_ip_using_cache(self):
# User 1 is locked out from IP 1.
self._lockout_user1_from_ip1()
Expand All @@ -570,7 +571,9 @@ def test_lockout_by_user_or_ip_allows_when_diff_user_same_ip_using_cache(self):
response = self._login(self.USER_2, self.VALID_PASSWORD, ip_addr=self.IP_1)
self.assertEqual(response.status_code, self.BLOCKED)

@override_settings(AXES_LOCK_OUT_BY_USER_OR_IP=True, AXES_FAILURE_LIMIT=3)
@override_settings(
AXES_LOCKOUT_PARAMETERS=["username", "ip_address"], AXES_FAILURE_LIMIT=3
)
def test_lockout_by_user_or_ip_allows_when_diff_user_same_ip_using_cache_multiple_attempts(
self,
):
Expand Down Expand Up @@ -600,7 +603,9 @@ def test_lockout_by_user_or_ip_allows_when_diff_user_same_ip_using_cache_multipl
response = self._login(self.USER_3, self.WRONG_PASSWORD, ip_addr=self.IP_1)
self.assertContains(response, self.LOCKED_MESSAGE, status_code=self.BLOCKED)

@override_settings(AXES_LOCK_OUT_BY_USER_OR_IP=True, AXES_FAILURE_LIMIT=3)
@override_settings(
AXES_LOCKOUT_PARAMETERS=["username", "ip_address"], AXES_FAILURE_LIMIT=3
)
def test_lockout_by_user_or_ip_allows_when_diff_user_same_ip_using_cache_multiple_failed_attempts(
self,
):
Expand All @@ -625,7 +630,7 @@ def test_lockout_by_user_or_ip_allows_when_diff_user_same_ip_using_cache_multipl
response = self._login(self.USER_2, self.VALID_PASSWORD, ip_addr=self.IP_2)
self.assertEqual(response.status_code, self.ALLOWED)

@override_settings(AXES_LOCK_OUT_BY_USER_OR_IP=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=["username", "ip_address"])
def test_lockout_by_user_or_ip_allows_when_diff_user_diff_ip_using_cache(self):
# User 1 is locked out from IP 1.
self._lockout_user1_from_ip1()
Expand All @@ -634,7 +639,7 @@ def test_lockout_by_user_or_ip_allows_when_diff_user_diff_ip_using_cache(self):
response = self._login(self.USER_2, self.VALID_PASSWORD, ip_addr=self.IP_2)
self.assertEqual(response.status_code, self.ALLOWED)

@override_settings(AXES_LOCK_OUT_BY_USER_OR_IP=True)
@override_settings(AXES_LOCKOUT_PARAMETERS=["username", "ip_address"])
def test_lockout_by_user_or_ip_with_empty_username_allows_other_users_using_cache(
self,
):
Expand Down

0 comments on commit 83a009b

Please sign in to comment.