2019-05-01 12:18:36 +00:00
|
|
|
from django.core.checks import run_checks, Warning # pylint: disable=redefined-builtin
|
2019-04-30 18:56:43 +00:00
|
|
|
from django.test import override_settings, modify_settings
|
2019-02-01 15:51:05 +00:00
|
|
|
|
2022-08-06 08:22:50 +00:00
|
|
|
from axes.backends import AxesStandaloneBackend
|
2019-02-01 15:51:05 +00:00
|
|
|
from axes.checks import Messages, Hints, Codes
|
2021-01-06 21:42:25 +00:00
|
|
|
from tests.base import AxesTestCase
|
2019-02-01 15:51:05 +00:00
|
|
|
|
|
|
|
|
|
2019-02-22 23:22:11 +00:00
|
|
|
class CacheCheckTestCase(AxesTestCase):
|
2019-02-26 10:45:37 +00:00
|
|
|
@override_settings(
|
2019-09-28 16:27:50 +00:00
|
|
|
AXES_HANDLER="axes.handlers.cache.AxesCacheHandler",
|
|
|
|
|
CACHES={
|
|
|
|
|
"default": {
|
|
|
|
|
"BACKEND": "django.core.cache.backends.db.DatabaseCache",
|
|
|
|
|
"LOCATION": "axes_cache",
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-02-26 10:45:37 +00:00
|
|
|
)
|
2019-02-25 19:05:18 +00:00
|
|
|
def test_cache_check(self):
|
2019-05-25 17:17:53 +00:00
|
|
|
warnings = run_checks()
|
|
|
|
|
self.assertEqual(warnings, [])
|
2019-02-25 19:05:18 +00:00
|
|
|
|
2019-02-26 10:45:37 +00:00
|
|
|
@override_settings(
|
2019-09-28 16:27:50 +00:00
|
|
|
AXES_HANDLER="axes.handlers.cache.AxesCacheHandler",
|
|
|
|
|
CACHES={
|
|
|
|
|
"default": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache"}
|
|
|
|
|
},
|
2019-02-26 10:45:37 +00:00
|
|
|
)
|
2019-05-25 17:17:53 +00:00
|
|
|
def test_cache_check_warnings(self):
|
|
|
|
|
warnings = run_checks()
|
|
|
|
|
warning = Warning(
|
2019-09-28 16:27:50 +00:00
|
|
|
msg=Messages.CACHE_INVALID, hint=Hints.CACHE_INVALID, id=Codes.CACHE_INVALID
|
2019-02-01 15:51:05 +00:00
|
|
|
)
|
|
|
|
|
|
2019-09-28 16:27:50 +00:00
|
|
|
self.assertEqual(warnings, [warning])
|
2019-02-26 10:45:37 +00:00
|
|
|
|
|
|
|
|
@override_settings(
|
2019-09-28 16:27:50 +00:00
|
|
|
AXES_HANDLER="axes.handlers.database.AxesDatabaseHandler",
|
|
|
|
|
CACHES={
|
|
|
|
|
"default": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache"}
|
|
|
|
|
},
|
2019-02-26 10:45:37 +00:00
|
|
|
)
|
2019-05-25 17:17:53 +00:00
|
|
|
def test_cache_check_does_not_produce_check_warnings_with_database_handler(self):
|
|
|
|
|
warnings = run_checks()
|
|
|
|
|
self.assertEqual(warnings, [])
|
2019-04-30 18:56:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class MiddlewareCheckTestCase(AxesTestCase):
|
2019-09-28 16:27:50 +00:00
|
|
|
@modify_settings(MIDDLEWARE={"remove": ["axes.middleware.AxesMiddleware"]})
|
2019-05-25 17:17:53 +00:00
|
|
|
def test_cache_check_warnings(self):
|
|
|
|
|
warnings = run_checks()
|
|
|
|
|
warning = Warning(
|
2019-04-30 18:56:43 +00:00
|
|
|
msg=Messages.MIDDLEWARE_INVALID,
|
|
|
|
|
hint=Hints.MIDDLEWARE_INVALID,
|
|
|
|
|
id=Codes.MIDDLEWARE_INVALID,
|
|
|
|
|
)
|
|
|
|
|
|
2019-09-28 16:27:50 +00:00
|
|
|
self.assertEqual(warnings, [warning])
|
2019-04-30 18:56:43 +00:00
|
|
|
|
|
|
|
|
|
2022-08-06 08:22:50 +00:00
|
|
|
class AxesSpecializedBackend(AxesStandaloneBackend):
|
2019-07-08 11:37:02 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2019-04-30 18:56:43 +00:00
|
|
|
class BackendCheckTestCase(AxesTestCase):
|
2022-08-06 08:22:50 +00:00
|
|
|
@modify_settings(
|
|
|
|
|
AUTHENTICATION_BACKENDS={"remove": ["axes.backends.AxesStandaloneBackend"]}
|
|
|
|
|
)
|
2019-07-08 11:37:02 +00:00
|
|
|
def test_backend_missing(self):
|
2019-05-25 17:17:53 +00:00
|
|
|
warnings = run_checks()
|
|
|
|
|
warning = Warning(
|
2019-04-30 18:56:43 +00:00
|
|
|
msg=Messages.BACKEND_INVALID,
|
|
|
|
|
hint=Hints.BACKEND_INVALID,
|
|
|
|
|
id=Codes.BACKEND_INVALID,
|
|
|
|
|
)
|
|
|
|
|
|
2019-09-28 16:27:50 +00:00
|
|
|
self.assertEqual(warnings, [warning])
|
2019-05-25 17:23:18 +00:00
|
|
|
|
2019-07-08 11:37:02 +00:00
|
|
|
@override_settings(
|
2021-01-06 21:42:25 +00:00
|
|
|
AUTHENTICATION_BACKENDS=["tests.test_checks.AxesSpecializedBackend"]
|
2019-07-08 11:37:02 +00:00
|
|
|
)
|
2019-07-09 10:54:49 +00:00
|
|
|
def test_specialized_backend(self):
|
2019-07-08 11:37:02 +00:00
|
|
|
warnings = run_checks()
|
|
|
|
|
self.assertEqual(warnings, [])
|
|
|
|
|
|
2019-07-09 10:54:49 +00:00
|
|
|
@override_settings(
|
2021-01-06 21:42:25 +00:00
|
|
|
AUTHENTICATION_BACKENDS=["tests.test_checks.AxesNotDefinedBackend"]
|
2019-07-09 10:54:49 +00:00
|
|
|
)
|
|
|
|
|
def test_import_error(self):
|
|
|
|
|
with self.assertRaises(ImportError):
|
|
|
|
|
run_checks()
|
|
|
|
|
|
2019-09-28 16:27:50 +00:00
|
|
|
@override_settings(AUTHENTICATION_BACKENDS=["module.not_defined"])
|
2019-07-09 10:54:49 +00:00
|
|
|
def test_module_not_found_error(self):
|
|
|
|
|
with self.assertRaises(ModuleNotFoundError):
|
|
|
|
|
run_checks()
|
|
|
|
|
|
2019-05-25 17:23:18 +00:00
|
|
|
|
|
|
|
|
class DeprecatedSettingsTestCase(AxesTestCase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.disable_success_access_log_warning = Warning(
|
2019-09-28 16:27:50 +00:00
|
|
|
msg=Messages.SETTING_DEPRECATED.format(
|
|
|
|
|
deprecated_setting="AXES_DISABLE_SUCCESS_ACCESS_LOG"
|
|
|
|
|
),
|
2019-05-25 17:23:18 +00:00
|
|
|
hint=Hints.SETTING_DEPRECATED,
|
|
|
|
|
id=Codes.SETTING_DEPRECATED,
|
|
|
|
|
)
|
|
|
|
|
|
2019-09-28 16:27:50 +00:00
|
|
|
@override_settings(AXES_DISABLE_SUCCESS_ACCESS_LOG=True)
|
2019-05-25 17:23:18 +00:00
|
|
|
def test_deprecated_success_access_log_flag(self):
|
|
|
|
|
warnings = run_checks()
|
2019-09-28 16:27:50 +00:00
|
|
|
self.assertEqual(warnings, [self.disable_success_access_log_warning])
|
2023-05-30 00:45:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class ConfCheckTestCase(AxesTestCase):
|
|
|
|
|
@override_settings(AXES_USERNAME_CALLABLE="module.not_defined")
|
|
|
|
|
def test_invalid_import_path(self):
|
|
|
|
|
warnings = run_checks()
|
|
|
|
|
warning = Warning(
|
|
|
|
|
msg=Messages.CALLABLE_INVALID.format(
|
|
|
|
|
callable_setting="AXES_USERNAME_CALLABLE"
|
|
|
|
|
),
|
|
|
|
|
hint=Hints.CALLABLE_INVALID,
|
|
|
|
|
id=Codes.CALLABLE_INVALID,
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(warnings, [warning])
|
|
|
|
|
|
|
|
|
|
@override_settings(AXES_COOLOFF_TIME=lambda: 1)
|
|
|
|
|
def test_valid_callable(self):
|
|
|
|
|
warnings = run_checks()
|
|
|
|
|
self.assertEqual(warnings, [])
|
2025-12-29 13:58:44 +00:00
|
|
|
|
|
|
|
|
def test_missing_settings_no_error(self):
|
|
|
|
|
warnings = run_checks()
|
|
|
|
|
self.assertEqual(warnings, [])
|
|
|
|
|
|
2025-12-25 10:41:05 +00:00
|
|
|
|
|
|
|
|
class LockoutParametersCheckTestCase(AxesTestCase):
|
|
|
|
|
@override_settings(AXES_LOCKOUT_PARAMETERS=["ip_address", "username"])
|
|
|
|
|
def test_valid_configuration(self):
|
|
|
|
|
warnings = run_checks()
|
|
|
|
|
self.assertEqual(warnings, [])
|
|
|
|
|
|
|
|
|
|
@override_settings(AXES_LOCKOUT_PARAMETERS=["username", "user_agent"])
|
|
|
|
|
def test_invalid_configuration(self):
|
|
|
|
|
warnings = run_checks()
|
|
|
|
|
warning = Warning(
|
|
|
|
|
msg=Messages.LOCKOUT_PARAMETERS_INVALID,
|
|
|
|
|
hint=Hints.LOCKOUT_PARAMETERS_INVALID,
|
|
|
|
|
id=Codes.LOCKOUT_PARAMETERS_INVALID,
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(warnings, [warning])
|