test_urls.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. from django.conf import settings
  2. from django.core.checks.messages import Error, Warning
  3. from django.core.checks.urls import (
  4. E006, check_url_config, check_url_namespaces_unique, check_url_settings,
  5. get_warning_for_invalid_pattern,
  6. )
  7. from django.test import SimpleTestCase
  8. from django.test.utils import override_settings
  9. class CheckUrlConfigTests(SimpleTestCase):
  10. @override_settings(ROOT_URLCONF='check_framework.urls.no_warnings')
  11. def test_no_warnings(self):
  12. result = check_url_config(None)
  13. self.assertEqual(result, [])
  14. @override_settings(ROOT_URLCONF='check_framework.urls.no_warnings_i18n')
  15. def test_no_warnings_i18n(self):
  16. self.assertEqual(check_url_config(None), [])
  17. @override_settings(ROOT_URLCONF='check_framework.urls.warning_in_include')
  18. def test_check_resolver_recursive(self):
  19. # The resolver is checked recursively (examining URL patterns in include()).
  20. result = check_url_config(None)
  21. self.assertEqual(len(result), 1)
  22. warning = result[0]
  23. self.assertEqual(warning.id, 'urls.W001')
  24. @override_settings(ROOT_URLCONF='check_framework.urls.include_with_dollar')
  25. def test_include_with_dollar(self):
  26. result = check_url_config(None)
  27. self.assertEqual(len(result), 1)
  28. warning = result[0]
  29. self.assertEqual(warning.id, 'urls.W001')
  30. self.assertEqual(warning.msg, (
  31. "Your URL pattern '^include-with-dollar$' uses include with a "
  32. "route ending with a '$'. Remove the dollar from the route to "
  33. "avoid problems including URLs."
  34. ))
  35. @override_settings(ROOT_URLCONF='check_framework.urls.contains_tuple')
  36. def test_contains_tuple_not_url_instance(self):
  37. result = check_url_config(None)
  38. warning = result[0]
  39. self.assertEqual(warning.id, 'urls.E004')
  40. self.assertRegex(warning.msg, (
  41. r"^Your URL pattern \('\^tuple/\$', <function <lambda> at 0x(\w+)>\) is "
  42. r"invalid. Ensure that urlpatterns is a list of path\(\) and/or re_path\(\) "
  43. r"instances\.$"
  44. ))
  45. @override_settings(ROOT_URLCONF='check_framework.urls.include_contains_tuple')
  46. def test_contains_included_tuple(self):
  47. result = check_url_config(None)
  48. warning = result[0]
  49. self.assertEqual(warning.id, 'urls.E004')
  50. self.assertRegex(warning.msg, (
  51. r"^Your URL pattern \('\^tuple/\$', <function <lambda> at 0x(\w+)>\) is "
  52. r"invalid. Ensure that urlpatterns is a list of path\(\) and/or re_path\(\) "
  53. r"instances\.$"
  54. ))
  55. @override_settings(ROOT_URLCONF='check_framework.urls.beginning_with_slash')
  56. def test_beginning_with_slash(self):
  57. msg = (
  58. "Your URL pattern '%s' has a route beginning with a '/'. Remove "
  59. "this slash as it is unnecessary. If this pattern is targeted in "
  60. "an include(), ensure the include() pattern has a trailing '/'."
  61. )
  62. warning1, warning2 = check_url_config(None)
  63. self.assertEqual(warning1.id, 'urls.W002')
  64. self.assertEqual(warning1.msg, msg % '/path-starting-with-slash/')
  65. self.assertEqual(warning2.id, 'urls.W002')
  66. self.assertEqual(warning2.msg, msg % '/url-starting-with-slash/$')
  67. @override_settings(
  68. ROOT_URLCONF='check_framework.urls.beginning_with_slash',
  69. APPEND_SLASH=False,
  70. )
  71. def test_beginning_with_slash_append_slash(self):
  72. # It can be useful to start a URL pattern with a slash when
  73. # APPEND_SLASH=False (#27238).
  74. result = check_url_config(None)
  75. self.assertEqual(result, [])
  76. @override_settings(ROOT_URLCONF='check_framework.urls.name_with_colon')
  77. def test_name_with_colon(self):
  78. result = check_url_config(None)
  79. self.assertEqual(len(result), 1)
  80. warning = result[0]
  81. self.assertEqual(warning.id, 'urls.W003')
  82. expected_msg = "Your URL pattern '^$' [name='name_with:colon'] has a name including a ':'."
  83. self.assertIn(expected_msg, warning.msg)
  84. @override_settings(ROOT_URLCONF=None)
  85. def test_no_root_urlconf_in_settings(self):
  86. delattr(settings, 'ROOT_URLCONF')
  87. result = check_url_config(None)
  88. self.assertEqual(result, [])
  89. def test_get_warning_for_invalid_pattern_string(self):
  90. warning = get_warning_for_invalid_pattern('')[0]
  91. self.assertEqual(
  92. warning.hint,
  93. "Try removing the string ''. The list of urlpatterns should "
  94. "not have a prefix string as the first element.",
  95. )
  96. def test_get_warning_for_invalid_pattern_tuple(self):
  97. warning = get_warning_for_invalid_pattern((r'^$', lambda x: x))[0]
  98. self.assertEqual(warning.hint, "Try using path() instead of a tuple.")
  99. def test_get_warning_for_invalid_pattern_other(self):
  100. warning = get_warning_for_invalid_pattern(object())[0]
  101. self.assertIsNone(warning.hint)
  102. @override_settings(ROOT_URLCONF='check_framework.urls.non_unique_namespaces')
  103. def test_check_non_unique_namespaces(self):
  104. result = check_url_namespaces_unique(None)
  105. self.assertEqual(len(result), 2)
  106. non_unique_namespaces = ['app-ns1', 'app-1']
  107. warning_messages = [
  108. "URL namespace '{}' isn't unique. You may not be able to reverse "
  109. "all URLs in this namespace".format(namespace)
  110. for namespace in non_unique_namespaces
  111. ]
  112. for warning in result:
  113. self.assertIsInstance(warning, Warning)
  114. self.assertEqual('urls.W005', warning.id)
  115. self.assertIn(warning.msg, warning_messages)
  116. @override_settings(ROOT_URLCONF='check_framework.urls.unique_namespaces')
  117. def test_check_unique_namespaces(self):
  118. result = check_url_namespaces_unique(None)
  119. self.assertEqual(result, [])
  120. @override_settings(ROOT_URLCONF='check_framework.urls.cbv_as_view')
  121. def test_check_view_not_class(self):
  122. self.assertEqual(check_url_config(None), [
  123. Error(
  124. "Your URL pattern 'missing_as_view' has an invalid view, pass "
  125. "EmptyCBV.as_view() instead of EmptyCBV.",
  126. id='urls.E009',
  127. ),
  128. ])
  129. class UpdatedToPathTests(SimpleTestCase):
  130. @override_settings(ROOT_URLCONF='check_framework.urls.path_compatibility.contains_re_named_group')
  131. def test_contains_re_named_group(self):
  132. result = check_url_config(None)
  133. self.assertEqual(len(result), 1)
  134. warning = result[0]
  135. self.assertEqual(warning.id, '2_0.W001')
  136. expected_msg = "Your URL pattern '(?P<named_group>\\d+)' has a route"
  137. self.assertIn(expected_msg, warning.msg)
  138. @override_settings(ROOT_URLCONF='check_framework.urls.path_compatibility.beginning_with_caret')
  139. def test_beginning_with_caret(self):
  140. result = check_url_config(None)
  141. self.assertEqual(len(result), 1)
  142. warning = result[0]
  143. self.assertEqual(warning.id, '2_0.W001')
  144. expected_msg = "Your URL pattern '^beginning-with-caret' has a route"
  145. self.assertIn(expected_msg, warning.msg)
  146. @override_settings(ROOT_URLCONF='check_framework.urls.path_compatibility.ending_with_dollar')
  147. def test_ending_with_dollar(self):
  148. result = check_url_config(None)
  149. self.assertEqual(len(result), 1)
  150. warning = result[0]
  151. self.assertEqual(warning.id, '2_0.W001')
  152. expected_msg = "Your URL pattern 'ending-with-dollar$' has a route"
  153. self.assertIn(expected_msg, warning.msg)
  154. class CheckCustomErrorHandlersTests(SimpleTestCase):
  155. @override_settings(
  156. ROOT_URLCONF='check_framework.urls.bad_function_based_error_handlers',
  157. )
  158. def test_bad_function_based_handlers(self):
  159. result = check_url_config(None)
  160. self.assertEqual(len(result), 4)
  161. for code, num_params, error in zip([400, 403, 404, 500], [2, 2, 2, 1], result):
  162. with self.subTest('handler{}'.format(code)):
  163. self.assertEqual(error, Error(
  164. "The custom handler{} view 'check_framework.urls."
  165. "bad_function_based_error_handlers.bad_handler' "
  166. "does not take the correct number of arguments (request{})."
  167. .format(code, ', exception' if num_params == 2 else ''),
  168. id='urls.E007',
  169. ))
  170. @override_settings(
  171. ROOT_URLCONF='check_framework.urls.bad_class_based_error_handlers',
  172. )
  173. def test_bad_class_based_handlers(self):
  174. result = check_url_config(None)
  175. self.assertEqual(len(result), 4)
  176. for code, num_params, error in zip([400, 403, 404, 500], [2, 2, 2, 1], result):
  177. with self.subTest('handler%s' % code):
  178. self.assertEqual(error, Error(
  179. "The custom handler%s view 'check_framework.urls."
  180. "bad_class_based_error_handlers.HandlerView.as_view."
  181. "<locals>.view' does not take the correct number of "
  182. "arguments (request%s)." % (
  183. code,
  184. ', exception' if num_params == 2 else '',
  185. ),
  186. id='urls.E007',
  187. ))
  188. @override_settings(ROOT_URLCONF='check_framework.urls.bad_error_handlers_invalid_path')
  189. def test_bad_handlers_invalid_path(self):
  190. result = check_url_config(None)
  191. paths = [
  192. 'django.views.bad_handler',
  193. 'django.invalid_module.bad_handler',
  194. 'invalid_module.bad_handler',
  195. 'django',
  196. ]
  197. hints = [
  198. "Could not import '{}'. View does not exist in module django.views.",
  199. "Could not import '{}'. Parent module django.invalid_module does not exist.",
  200. "No module named 'invalid_module'",
  201. "Could not import '{}'. The path must be fully qualified.",
  202. ]
  203. for code, path, hint, error in zip([400, 403, 404, 500], paths, hints, result):
  204. with self.subTest('handler{}'.format(code)):
  205. self.assertEqual(error, Error(
  206. "The custom handler{} view '{}' could not be imported.".format(code, path),
  207. hint=hint.format(path),
  208. id='urls.E008',
  209. ))
  210. @override_settings(
  211. ROOT_URLCONF='check_framework.urls.good_function_based_error_handlers',
  212. )
  213. def test_good_function_based_handlers(self):
  214. result = check_url_config(None)
  215. self.assertEqual(result, [])
  216. @override_settings(
  217. ROOT_URLCONF='check_framework.urls.good_class_based_error_handlers',
  218. )
  219. def test_good_class_based_handlers(self):
  220. result = check_url_config(None)
  221. self.assertEqual(result, [])
  222. class CheckURLSettingsTests(SimpleTestCase):
  223. @override_settings(STATIC_URL='a/', MEDIA_URL='b/')
  224. def test_slash_no_errors(self):
  225. self.assertEqual(check_url_settings(None), [])
  226. @override_settings(STATIC_URL='', MEDIA_URL='')
  227. def test_empty_string_no_errors(self):
  228. self.assertEqual(check_url_settings(None), [])
  229. @override_settings(STATIC_URL='noslash')
  230. def test_static_url_no_slash(self):
  231. self.assertEqual(check_url_settings(None), [E006('STATIC_URL')])
  232. @override_settings(STATIC_URL='slashes//')
  233. def test_static_url_double_slash_allowed(self):
  234. # The check allows for a double slash, presuming the user knows what
  235. # they are doing.
  236. self.assertEqual(check_url_settings(None), [])
  237. @override_settings(MEDIA_URL='noslash')
  238. def test_media_url_no_slash(self):
  239. self.assertEqual(check_url_settings(None), [E006('MEDIA_URL')])