2016-02-01 17:24:33 +00:00
|
|
|
from django.conf import settings
|
2018-09-07 07:33:19 +00:00
|
|
|
from django.core.checks.messages import Error, Warning
|
2016-04-02 10:49:12 +00:00
|
|
|
from django.core.checks.urls import (
|
2022-02-03 19:24:19 +00:00
|
|
|
E006,
|
|
|
|
check_url_config,
|
|
|
|
check_url_namespaces_unique,
|
|
|
|
check_url_settings,
|
2016-12-21 22:05:59 +00:00
|
|
|
get_warning_for_invalid_pattern,
|
2016-04-02 10:49:12 +00:00
|
|
|
)
|
2015-09-16 22:07:39 +00:00
|
|
|
from django.test import SimpleTestCase
|
|
|
|
from django.test.utils import override_settings
|
|
|
|
|
|
|
|
|
2016-12-23 15:55:00 +00:00
|
|
|
class CheckUrlConfigTests(SimpleTestCase):
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(ROOT_URLCONF="check_framework.urls.no_warnings")
|
2015-09-21 17:45:56 +00:00
|
|
|
def test_no_warnings(self):
|
2015-09-16 22:07:39 +00:00
|
|
|
result = check_url_config(None)
|
|
|
|
self.assertEqual(result, [])
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(ROOT_URLCONF="check_framework.urls.no_warnings_i18n")
|
2017-09-20 14:22:30 +00:00
|
|
|
def test_no_warnings_i18n(self):
|
|
|
|
self.assertEqual(check_url_config(None), [])
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(ROOT_URLCONF="check_framework.urls.warning_in_include")
|
2016-04-27 21:38:00 +00:00
|
|
|
def test_check_resolver_recursive(self):
|
2018-12-07 22:52:28 +00:00
|
|
|
# The resolver is checked recursively (examining URL patterns in include()).
|
2016-04-27 21:38:00 +00:00
|
|
|
result = check_url_config(None)
|
|
|
|
self.assertEqual(len(result), 1)
|
|
|
|
warning = result[0]
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(warning.id, "urls.W001")
|
2016-04-27 21:38:00 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(ROOT_URLCONF="check_framework.urls.include_with_dollar")
|
2015-09-16 22:07:39 +00:00
|
|
|
def test_include_with_dollar(self):
|
|
|
|
result = check_url_config(None)
|
|
|
|
self.assertEqual(len(result), 1)
|
|
|
|
warning = result[0]
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(warning.id, "urls.W001")
|
|
|
|
self.assertEqual(
|
|
|
|
warning.msg,
|
|
|
|
(
|
|
|
|
"Your URL pattern '^include-with-dollar$' uses include with a "
|
|
|
|
"route ending with a '$'. Remove the dollar from the route to "
|
|
|
|
"avoid problems including URLs."
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
@override_settings(ROOT_URLCONF="check_framework.urls.contains_tuple")
|
2016-04-02 10:49:12 +00:00
|
|
|
def test_contains_tuple_not_url_instance(self):
|
|
|
|
result = check_url_config(None)
|
|
|
|
warning = result[0]
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(warning.id, "urls.E004")
|
|
|
|
self.assertRegex(
|
|
|
|
warning.msg,
|
|
|
|
(
|
2022-02-04 07:08:27 +00:00
|
|
|
r"^Your URL pattern \('\^tuple/\$', <function <lambda> at 0x(\w+)>\) "
|
|
|
|
r"is invalid. Ensure that urlpatterns is a list of path\(\) and/or "
|
|
|
|
r"re_path\(\) instances\.$"
|
2022-02-03 19:24:19 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
@override_settings(ROOT_URLCONF="check_framework.urls.include_contains_tuple")
|
2016-10-20 17:29:04 +00:00
|
|
|
def test_contains_included_tuple(self):
|
2015-09-16 22:07:39 +00:00
|
|
|
result = check_url_config(None)
|
|
|
|
warning = result[0]
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(warning.id, "urls.E004")
|
|
|
|
self.assertRegex(
|
|
|
|
warning.msg,
|
|
|
|
(
|
2022-02-04 07:08:27 +00:00
|
|
|
r"^Your URL pattern \('\^tuple/\$', <function <lambda> at 0x(\w+)>\) "
|
|
|
|
r"is invalid. Ensure that urlpatterns is a list of path\(\) and/or "
|
|
|
|
r"re_path\(\) instances\.$"
|
2022-02-03 19:24:19 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
@override_settings(ROOT_URLCONF="check_framework.urls.beginning_with_slash")
|
2016-10-20 17:29:04 +00:00
|
|
|
def test_beginning_with_slash(self):
|
|
|
|
msg = (
|
|
|
|
"Your URL pattern '%s' has a route beginning with a '/'. Remove "
|
|
|
|
"this slash as it is unnecessary. If this pattern is targeted in "
|
|
|
|
"an include(), ensure the include() pattern has a trailing '/'."
|
|
|
|
)
|
|
|
|
warning1, warning2 = check_url_config(None)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(warning1.id, "urls.W002")
|
|
|
|
self.assertEqual(warning1.msg, msg % "/path-starting-with-slash/")
|
|
|
|
self.assertEqual(warning2.id, "urls.W002")
|
|
|
|
self.assertEqual(warning2.msg, msg % "/url-starting-with-slash/$")
|
2015-09-16 22:07:39 +00:00
|
|
|
|
2016-09-19 20:18:41 +00:00
|
|
|
@override_settings(
|
2022-02-03 19:24:19 +00:00
|
|
|
ROOT_URLCONF="check_framework.urls.beginning_with_slash",
|
2016-09-19 20:18:41 +00:00
|
|
|
APPEND_SLASH=False,
|
|
|
|
)
|
|
|
|
def test_beginning_with_slash_append_slash(self):
|
|
|
|
# It can be useful to start a URL pattern with a slash when
|
|
|
|
# APPEND_SLASH=False (#27238).
|
|
|
|
result = check_url_config(None)
|
|
|
|
self.assertEqual(result, [])
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(ROOT_URLCONF="check_framework.urls.name_with_colon")
|
2015-09-21 17:45:56 +00:00
|
|
|
def test_name_with_colon(self):
|
2015-09-16 22:07:39 +00:00
|
|
|
result = check_url_config(None)
|
|
|
|
self.assertEqual(len(result), 1)
|
|
|
|
warning = result[0]
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(warning.id, "urls.W003")
|
|
|
|
expected_msg = (
|
|
|
|
"Your URL pattern '^$' [name='name_with:colon'] has a name including a ':'."
|
|
|
|
)
|
2015-09-16 22:07:39 +00:00
|
|
|
self.assertIn(expected_msg, warning.msg)
|
2016-02-01 17:24:33 +00:00
|
|
|
|
|
|
|
@override_settings(ROOT_URLCONF=None)
|
|
|
|
def test_no_root_urlconf_in_settings(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
delattr(settings, "ROOT_URLCONF")
|
2016-02-01 17:24:33 +00:00
|
|
|
result = check_url_config(None)
|
|
|
|
self.assertEqual(result, [])
|
2016-04-02 10:49:12 +00:00
|
|
|
|
|
|
|
def test_get_warning_for_invalid_pattern_string(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
warning = get_warning_for_invalid_pattern("")[0]
|
2016-04-02 10:49:12 +00:00
|
|
|
self.assertEqual(
|
|
|
|
warning.hint,
|
|
|
|
"Try removing the string ''. The list of urlpatterns should "
|
|
|
|
"not have a prefix string as the first element.",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_get_warning_for_invalid_pattern_tuple(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
warning = get_warning_for_invalid_pattern((r"^$", lambda x: x))[0]
|
2016-10-20 17:29:04 +00:00
|
|
|
self.assertEqual(warning.hint, "Try using path() instead of a tuple.")
|
2016-04-02 10:49:12 +00:00
|
|
|
|
|
|
|
def test_get_warning_for_invalid_pattern_other(self):
|
|
|
|
warning = get_warning_for_invalid_pattern(object())[0]
|
|
|
|
self.assertIsNone(warning.hint)
|
2016-12-21 21:54:15 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(ROOT_URLCONF="check_framework.urls.non_unique_namespaces")
|
2016-12-21 21:54:15 +00:00
|
|
|
def test_check_non_unique_namespaces(self):
|
|
|
|
result = check_url_namespaces_unique(None)
|
|
|
|
self.assertEqual(len(result), 2)
|
2022-02-03 19:24:19 +00:00
|
|
|
non_unique_namespaces = ["app-ns1", "app-1"]
|
2016-12-21 21:54:15 +00:00
|
|
|
warning_messages = [
|
|
|
|
"URL namespace '{}' isn't unique. You may not be able to reverse "
|
|
|
|
"all URLs in this namespace".format(namespace)
|
|
|
|
for namespace in non_unique_namespaces
|
|
|
|
]
|
|
|
|
for warning in result:
|
|
|
|
self.assertIsInstance(warning, Warning)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual("urls.W005", warning.id)
|
2016-12-21 21:54:15 +00:00
|
|
|
self.assertIn(warning.msg, warning_messages)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(ROOT_URLCONF="check_framework.urls.unique_namespaces")
|
2016-12-21 21:54:15 +00:00
|
|
|
def test_check_unique_namespaces(self):
|
|
|
|
result = check_url_namespaces_unique(None)
|
|
|
|
self.assertEqual(result, [])
|
2016-12-23 15:55:00 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(ROOT_URLCONF="check_framework.urls.cbv_as_view")
|
2020-11-14 17:25:57 +00:00
|
|
|
def test_check_view_not_class(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
check_url_config(None),
|
|
|
|
[
|
|
|
|
Error(
|
|
|
|
"Your URL pattern 'missing_as_view' has an invalid view, pass "
|
|
|
|
"EmptyCBV.as_view() instead of EmptyCBV.",
|
|
|
|
id="urls.E009",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2020-11-14 17:25:57 +00:00
|
|
|
|
2016-12-23 15:55:00 +00:00
|
|
|
|
2017-11-07 16:39:59 +00:00
|
|
|
class UpdatedToPathTests(SimpleTestCase):
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(
|
|
|
|
ROOT_URLCONF="check_framework.urls.path_compatibility.contains_re_named_group"
|
|
|
|
)
|
2017-11-07 16:39:59 +00:00
|
|
|
def test_contains_re_named_group(self):
|
|
|
|
result = check_url_config(None)
|
|
|
|
self.assertEqual(len(result), 1)
|
|
|
|
warning = result[0]
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(warning.id, "2_0.W001")
|
2020-04-13 08:07:28 +00:00
|
|
|
expected_msg = "Your URL pattern '(?P<named_group>\\d+)' has a route"
|
2017-11-07 16:39:59 +00:00
|
|
|
self.assertIn(expected_msg, warning.msg)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(
|
|
|
|
ROOT_URLCONF="check_framework.urls.path_compatibility.beginning_with_caret"
|
|
|
|
)
|
2017-11-07 16:39:59 +00:00
|
|
|
def test_beginning_with_caret(self):
|
|
|
|
result = check_url_config(None)
|
|
|
|
self.assertEqual(len(result), 1)
|
|
|
|
warning = result[0]
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(warning.id, "2_0.W001")
|
2017-11-07 16:39:59 +00:00
|
|
|
expected_msg = "Your URL pattern '^beginning-with-caret' has a route"
|
|
|
|
self.assertIn(expected_msg, warning.msg)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(
|
|
|
|
ROOT_URLCONF="check_framework.urls.path_compatibility.ending_with_dollar"
|
|
|
|
)
|
2017-11-07 16:39:59 +00:00
|
|
|
def test_ending_with_dollar(self):
|
|
|
|
result = check_url_config(None)
|
|
|
|
self.assertEqual(len(result), 1)
|
|
|
|
warning = result[0]
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(warning.id, "2_0.W001")
|
2017-11-07 16:39:59 +00:00
|
|
|
expected_msg = "Your URL pattern 'ending-with-dollar$' has a route"
|
|
|
|
self.assertIn(expected_msg, warning.msg)
|
|
|
|
|
|
|
|
|
2018-09-07 07:33:19 +00:00
|
|
|
class CheckCustomErrorHandlersTests(SimpleTestCase):
|
2020-12-11 19:30:50 +00:00
|
|
|
@override_settings(
|
2022-02-03 19:24:19 +00:00
|
|
|
ROOT_URLCONF="check_framework.urls.bad_function_based_error_handlers",
|
2020-12-11 19:30:50 +00:00
|
|
|
)
|
|
|
|
def test_bad_function_based_handlers(self):
|
2018-09-07 07:33:19 +00:00
|
|
|
result = check_url_config(None)
|
|
|
|
self.assertEqual(len(result), 4)
|
|
|
|
for code, num_params, error in zip([400, 403, 404, 500], [2, 2, 2, 1], result):
|
2022-02-03 19:24:19 +00:00
|
|
|
with self.subTest("handler{}".format(code)):
|
|
|
|
self.assertEqual(
|
|
|
|
error,
|
|
|
|
Error(
|
|
|
|
"The custom handler{} view 'check_framework.urls."
|
|
|
|
"bad_function_based_error_handlers.bad_handler' "
|
2022-02-04 07:08:27 +00:00
|
|
|
"does not take the correct number of arguments "
|
|
|
|
"(request{}).".format(
|
2022-02-03 19:24:19 +00:00
|
|
|
code, ", exception" if num_params == 2 else ""
|
|
|
|
),
|
|
|
|
id="urls.E007",
|
|
|
|
),
|
|
|
|
)
|
2018-09-07 07:33:19 +00:00
|
|
|
|
2020-12-11 19:30:50 +00:00
|
|
|
@override_settings(
|
2022-02-03 19:24:19 +00:00
|
|
|
ROOT_URLCONF="check_framework.urls.bad_class_based_error_handlers",
|
2020-12-11 19:30:50 +00:00
|
|
|
)
|
|
|
|
def test_bad_class_based_handlers(self):
|
|
|
|
result = check_url_config(None)
|
|
|
|
self.assertEqual(len(result), 4)
|
|
|
|
for code, num_params, error in zip([400, 403, 404, 500], [2, 2, 2, 1], result):
|
2022-02-03 19:24:19 +00:00
|
|
|
with self.subTest("handler%s" % code):
|
|
|
|
self.assertEqual(
|
|
|
|
error,
|
|
|
|
Error(
|
|
|
|
"The custom handler%s view 'check_framework.urls."
|
|
|
|
"bad_class_based_error_handlers.HandlerView.as_view."
|
|
|
|
"<locals>.view' does not take the correct number of "
|
|
|
|
"arguments (request%s)."
|
|
|
|
% (
|
|
|
|
code,
|
|
|
|
", exception" if num_params == 2 else "",
|
|
|
|
),
|
|
|
|
id="urls.E007",
|
2020-12-11 19:30:50 +00:00
|
|
|
),
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2020-12-11 19:30:50 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(
|
|
|
|
ROOT_URLCONF="check_framework.urls.bad_error_handlers_invalid_path"
|
|
|
|
)
|
2019-04-03 23:17:25 +00:00
|
|
|
def test_bad_handlers_invalid_path(self):
|
|
|
|
result = check_url_config(None)
|
|
|
|
paths = [
|
2022-02-03 19:24:19 +00:00
|
|
|
"django.views.bad_handler",
|
|
|
|
"django.invalid_module.bad_handler",
|
|
|
|
"invalid_module.bad_handler",
|
|
|
|
"django",
|
2019-04-03 23:17:25 +00:00
|
|
|
]
|
|
|
|
hints = [
|
|
|
|
"Could not import '{}'. View does not exist in module django.views.",
|
2022-02-04 07:08:27 +00:00
|
|
|
"Could not import '{}'. Parent module django.invalid_module does not "
|
|
|
|
"exist.",
|
2019-04-03 23:17:25 +00:00
|
|
|
"No module named 'invalid_module'",
|
|
|
|
"Could not import '{}'. The path must be fully qualified.",
|
|
|
|
]
|
|
|
|
for code, path, hint, error in zip([400, 403, 404, 500], paths, hints, result):
|
2022-02-03 19:24:19 +00:00
|
|
|
with self.subTest("handler{}".format(code)):
|
|
|
|
self.assertEqual(
|
|
|
|
error,
|
|
|
|
Error(
|
|
|
|
"The custom handler{} view '{}' could not be imported.".format(
|
|
|
|
code, path
|
|
|
|
),
|
|
|
|
hint=hint.format(path),
|
|
|
|
id="urls.E008",
|
|
|
|
),
|
|
|
|
)
|
2019-04-03 23:17:25 +00:00
|
|
|
|
2020-12-11 19:30:50 +00:00
|
|
|
@override_settings(
|
2022-02-03 19:24:19 +00:00
|
|
|
ROOT_URLCONF="check_framework.urls.good_function_based_error_handlers",
|
2020-12-11 19:30:50 +00:00
|
|
|
)
|
|
|
|
def test_good_function_based_handlers(self):
|
|
|
|
result = check_url_config(None)
|
|
|
|
self.assertEqual(result, [])
|
|
|
|
|
|
|
|
@override_settings(
|
2022-02-03 19:24:19 +00:00
|
|
|
ROOT_URLCONF="check_framework.urls.good_class_based_error_handlers",
|
2020-12-11 19:30:50 +00:00
|
|
|
)
|
|
|
|
def test_good_class_based_handlers(self):
|
2018-09-07 07:33:19 +00:00
|
|
|
result = check_url_config(None)
|
|
|
|
self.assertEqual(result, [])
|
|
|
|
|
|
|
|
|
2016-12-23 15:55:00 +00:00
|
|
|
class CheckURLSettingsTests(SimpleTestCase):
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(STATIC_URL="a/", MEDIA_URL="b/")
|
2016-12-23 15:55:00 +00:00
|
|
|
def test_slash_no_errors(self):
|
|
|
|
self.assertEqual(check_url_settings(None), [])
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(STATIC_URL="", MEDIA_URL="")
|
2016-12-23 15:55:00 +00:00
|
|
|
def test_empty_string_no_errors(self):
|
|
|
|
self.assertEqual(check_url_settings(None), [])
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(STATIC_URL="noslash")
|
2016-12-23 15:55:00 +00:00
|
|
|
def test_static_url_no_slash(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(check_url_settings(None), [E006("STATIC_URL")])
|
2016-12-23 15:55:00 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(STATIC_URL="slashes//")
|
2016-12-23 15:55:00 +00:00
|
|
|
def test_static_url_double_slash_allowed(self):
|
|
|
|
# The check allows for a double slash, presuming the user knows what
|
|
|
|
# they are doing.
|
|
|
|
self.assertEqual(check_url_settings(None), [])
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(MEDIA_URL="noslash")
|
2016-12-23 15:55:00 +00:00
|
|
|
def test_media_url_no_slash(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(check_url_settings(None), [E006("MEDIA_URL")])
|