Debian Codegraph @

tests/check_framework/test_urls.py

raw
1 import unittest
2
3 from django.conf import settings
4 from django.core.checks.messages import Error, Warning
5 from django.core.checks.urls import (
6 E006,
7 check_custom_error_handlers,
8 check_url_config,
9 check_url_namespaces_unique,
10 check_url_settings,
11 get_warning_for_invalid_pattern,
12 )
13 from django.test import SimpleTestCase
14 from django.test.utils import override_settings
15 from django.utils.version import PY314
16
17
18 class CheckUrlConfigTests(SimpleTestCase):
19 @override_settings(ROOT_URLCONF="check_framework.urls.no_warnings")
20 def test_no_warnings(self):
21 result = check_url_config(None)
22 self.assertEqual(result, [])
23
24 @override_settings(ROOT_URLCONF="check_framework.urls.no_warnings_i18n")
25 def test_no_warnings_i18n(self):
26 self.assertEqual(check_url_config(None), [])
27
28 @override_settings(ROOT_URLCONF="check_framework.urls.warning_in_include")
29 def test_check_resolver_recursive(self):
30 # The resolver is checked recursively (examining URL patterns in include()).
31 result = check_url_config(None)
32 self.assertEqual(len(result), 1)
33 warning = result[0]
34 self.assertEqual(warning.id, "urls.W001")
35
36 @override_settings(ROOT_URLCONF="check_framework.urls.include_with_dollar")
37 def test_include_with_dollar(self):
38 result = check_url_config(None)
39 self.assertEqual(len(result), 1)
40 warning = result[0]
41 self.assertEqual(warning.id, "urls.W001")
42 self.assertEqual(
43 warning.msg,
44 (
45 "Your URL pattern '^include-with-dollar$' uses include with a "
46 "route ending with a '$'. Remove the dollar from the route to "
47 "avoid problems including URLs."
48 ),
49 )
50
51 @override_settings(ROOT_URLCONF="check_framework.urls.contains_tuple")
52 def test_contains_tuple_not_url_instance(self):
53 result = check_url_config(None)
54 warning = result[0]
55 self.assertEqual(warning.id, "urls.E004")
56 self.assertRegex(
57 warning.msg,
58 (
59 r"^Your URL pattern \('\^tuple/\$', <function <lambda> at 0x(\w+)>\) "
60 r"is invalid. Ensure that urlpatterns is a list of path\(\) and/or "
61 r"re_path\(\) instances\.$"
62 ),
63 )
64
65 @override_settings(ROOT_URLCONF="check_framework.urls.include_contains_tuple")
66 def test_contains_included_tuple(self):
67 result = check_url_config(None)
68 warning = result[0]
69 self.assertEqual(warning.id, "urls.E004")
70 self.assertRegex(
71 warning.msg,
72 (
73 r"^Your URL pattern \('\^tuple/\$', <function <lambda> at 0x(\w+)>\) "
74 r"is invalid. Ensure that urlpatterns is a list of path\(\) and/or "
75 r"re_path\(\) instances\.$"
76 ),
77 )
78
79 @override_settings(ROOT_URLCONF="check_framework.urls.beginning_with_slash")
80 def test_beginning_with_slash(self):
81 msg = (
82 "Your URL pattern '%s' has a route beginning with a '/'. Remove "
83 "this slash as it is unnecessary. If this pattern is targeted in "
84 "an include(), ensure the include() pattern has a trailing '/'."
85 )
86 warning1, warning2 = check_url_config(None)
87 self.assertEqual(warning1.id, "urls.W002")
88 self.assertEqual(warning1.msg, msg % "/path-starting-with-slash/")
89 self.assertEqual(warning2.id, "urls.W002")
90 self.assertEqual(warning2.msg, msg % "/url-starting-with-slash/$")
91
92 @override_settings(
93 ROOT_URLCONF="check_framework.urls.beginning_with_slash",
94 APPEND_SLASH=False,
95 )
96 def test_beginning_with_slash_append_slash(self):
97 # It can be useful to start a URL pattern with a slash when
98 # APPEND_SLASH=False (#27238).
99 result = check_url_config(None)
100 self.assertEqual(result, [])
101
102 @override_settings(ROOT_URLCONF="check_framework.urls.name_with_colon")
103 def test_name_with_colon(self):
104 result = check_url_config(None)
105 self.assertEqual(len(result), 1)
106 warning = result[0]
107 self.assertEqual(warning.id, "urls.W003")
108 expected_msg = (
109 "Your URL pattern '^$' [name='name_with:colon'] has a name including a ':'."
110 )
111 self.assertIn(expected_msg, warning.msg)
112
113 @override_settings(ROOT_URLCONF=None)
114 def test_no_root_urlconf_in_settings(self):
115 delattr(settings, "ROOT_URLCONF")
116 result = check_url_config(None)
117 self.assertEqual(result, [])
118
119 def test_get_warning_for_invalid_pattern_string(self):
120 warning = get_warning_for_invalid_pattern("")[0]
121 self.assertEqual(
122 warning.hint,
123 "Try removing the string ''. The list of urlpatterns should "
124 "not have a prefix string as the first element.",
125 )
126
127 def test_get_warning_for_invalid_pattern_tuple(self):
128 warning = get_warning_for_invalid_pattern((r"^$", lambda x: x))[0]
129 self.assertEqual(warning.hint, "Try using path() instead of a tuple.")
130
131 def test_get_warning_for_invalid_pattern_other(self):
132 warning = get_warning_for_invalid_pattern(object())[0]
133 self.assertIsNone(warning.hint)
134
135 @override_settings(ROOT_URLCONF="check_framework.urls.non_unique_namespaces")
136 def test_check_non_unique_namespaces(self):
137 result = check_url_namespaces_unique(None)
138 self.assertEqual(len(result), 2)
139 non_unique_namespaces = ["app-ns1", "app-1"]
140 warning_messages = [
141 "URL namespace '{}' isn't unique. You may not be able to reverse "
142 "all URLs in this namespace".format(namespace)
143 for namespace in non_unique_namespaces
144 ]
145 for warning in result:
146 self.assertIsInstance(warning, Warning)
147 self.assertEqual("urls.W005", warning.id)
148 self.assertIn(warning.msg, warning_messages)
149
150 @override_settings(ROOT_URLCONF="check_framework.urls.unique_namespaces")
151 def test_check_unique_namespaces(self):
152 result = check_url_namespaces_unique(None)
153 self.assertEqual(result, [])
154
155 @override_settings(ROOT_URLCONF="check_framework.urls.cbv_as_view")
156 def test_check_view_not_class(self):
157 self.assertEqual(
158 check_url_config(None),
159 [
160 Error(
161 "Your URL pattern 'missing_as_view' has an invalid view, pass "
162 "EmptyCBV.as_view() instead of EmptyCBV.",
163 id="urls.E009",
164 ),
165 ],
166 )
167
168 @override_settings(
169 ROOT_URLCONF="check_framework.urls.path_compatibility.matched_angle_brackets"
170 )
171 def test_no_warnings_matched_angle_brackets(self):
172 self.assertEqual(check_url_config(None), [])
173
174 @override_settings(
175 ROOT_URLCONF="check_framework.urls.path_compatibility.unmatched_angle_brackets"
176 )
177 def test_warning_unmatched_angle_brackets(self):
178 self.assertEqual(
179 check_url_config(None),
180 [
181 Warning(
182 "Your URL pattern 'beginning-with/<angle_bracket' has an unmatched "
183 "'<' bracket.",
184 id="urls.W010",
185 ),
186 Warning(
187 "Your URL pattern 'ending-with/angle_bracket>' has an unmatched "
188 "'>' bracket.",
189 id="urls.W010",
190 ),
191 Warning(
192 "Your URL pattern 'closed_angle>/x/<opened_angle' has an unmatched "
193 "'>' bracket.",
194 id="urls.W010",
195 ),
196 Warning(
197 "Your URL pattern 'closed_angle>/x/<opened_angle' has an unmatched "
198 "'<' bracket.",
199 id="urls.W010",
200 ),
201 Warning(
202 "Your URL pattern '<mixed>angle_bracket>' has an unmatched '>' "
203 "bracket.",
204 id="urls.W010",
205 ),
206 ],
207 )
208
209
210 class UpdatedToPathTests(SimpleTestCase):
211 @override_settings(
212 ROOT_URLCONF="check_framework.urls.path_compatibility.contains_re_named_group"
213 )
214 def test_contains_re_named_group(self):
215 result = check_url_config(None)
216 self.assertEqual(len(result), 1)
217 warning = result[0]
218 self.assertEqual(warning.id, "2_0.W001")
219 expected_msg = "Your URL pattern '(?P<named_group>\\d+)' has a route"
220 self.assertIn(expected_msg, warning.msg)
221
222 @override_settings(
223 ROOT_URLCONF="check_framework.urls.path_compatibility.beginning_with_caret"
224 )
225 def test_beginning_with_caret(self):
226 result = check_url_config(None)
227 self.assertEqual(len(result), 1)
228 warning = result[0]
229 self.assertEqual(warning.id, "2_0.W001")
230 expected_msg = "Your URL pattern '^beginning-with-caret' has a route"
231 self.assertIn(expected_msg, warning.msg)
232
233 @override_settings(
234 ROOT_URLCONF="check_framework.urls.path_compatibility.ending_with_dollar"
235 )
236 def test_ending_with_dollar(self):
237 result = check_url_config(None)
238 self.assertEqual(len(result), 1)
239 warning = result[0]
240 self.assertEqual(warning.id, "2_0.W001")
241 expected_msg = "Your URL pattern 'ending-with-dollar$' has a route"
242 self.assertIn(expected_msg, warning.msg)
243
244
245 class CheckCustomErrorHandlersTests(SimpleTestCase):
246 @override_settings(
247 ROOT_URLCONF="check_framework.urls.bad_function_based_error_handlers",
248 )
249 def test_bad_function_based_handlers(self):
250 result = check_custom_error_handlers(None)
251 self.assertEqual(len(result), 4)
252 for code, num_params, error in zip([400, 403, 404, 500], [2, 2, 2, 1], result):
253 with self.subTest("handler{}".format(code)):
254 self.assertEqual(
255 error,
256 Error(
257 "The custom handler{} view 'check_framework.urls."
258 "bad_function_based_error_handlers.bad_handler' "
259 "does not take the correct number of arguments "
260 "(request{}).".format(
261 code, ", exception" if num_params == 2 else ""
262 ),
263 id="urls.E007",
264 ),
265 )
266
267 @override_settings(
268 ROOT_URLCONF="check_framework.urls.bad_class_based_error_handlers",
269 )
270 def test_bad_class_based_handlers(self):
271 result = check_custom_error_handlers(None)
272 self.assertEqual(len(result), 4)
273 for code, num_params, error in zip([400, 403, 404, 500], [2, 2, 2, 1], result):
274 with self.subTest("handler%s" % code):
275 self.assertEqual(
276 error,
277 Error(
278 "The custom handler%s view 'check_framework.urls."
279 "bad_class_based_error_handlers.HandlerView.as_view."
280 "<locals>.view' does not take the correct number of "
281 "arguments (request%s)."
282 % (
283 code,
284 ", exception" if num_params == 2 else "",
285 ),
286 id="urls.E007",
287 ),
288 )
289
290 @override_settings(
291 ROOT_URLCONF="check_framework.urls.bad_error_handlers_invalid_path"
292 )
293 def test_bad_handlers_invalid_path(self):
294 result = check_custom_error_handlers(None)
295 paths = [
296 "django.views.bad_handler",
297 "django.invalid_module.bad_handler",
298 "invalid_module.bad_handler",
299 "django",
300 ]
301 hints = [
302 "Could not import '{}'. View does not exist in module django.views.",
303 "Could not import '{}'. Parent module django.invalid_module does not "
304 "exist.",
305 "No module named 'invalid_module'",
306 "Could not import '{}'. The path must be fully qualified.",
307 ]
308 for code, path, hint, error in zip([400, 403, 404, 500], paths, hints, result):
309 with self.subTest("handler{}".format(code)):
310 self.assertEqual(
311 error,
312 Error(
313 "The custom handler{} view '{}' could not be imported.".format(
314 code, path
315 ),
316 hint=hint.format(path),
317 id="urls.E008",
318 ),
319 )
320
321 @override_settings(
322 ROOT_URLCONF="check_framework.urls.good_function_based_error_handlers",
323 )
324 def test_good_function_based_handlers(self):
325 result = check_custom_error_handlers(None)
326 self.assertEqual(result, [])
327
328 @unittest.skipUnless(PY314, "Deferred annotations are Python 3.14+ only")
329 @override_settings(
330 ROOT_URLCONF="check_framework.urls.good_error_handler_deferred_annotations",
331 )
332 def test_good_function_based_handlers_deferred_annotations(self):
333 result = check_custom_error_handlers(None)
334 self.assertEqual(result, [])
335
336 @override_settings(
337 ROOT_URLCONF="check_framework.urls.good_class_based_error_handlers",
338 )
339 def test_good_class_based_handlers(self):
340 result = check_custom_error_handlers(None)
341 self.assertEqual(result, [])
342
343
344 class CheckURLSettingsTests(SimpleTestCase):
345 @override_settings(STATIC_URL="a/", MEDIA_URL="b/")
346 def test_slash_no_errors(self):
347 self.assertEqual(check_url_settings(None), [])
348
349 @override_settings(STATIC_URL="", MEDIA_URL="")
350 def test_empty_string_no_errors(self):
351 self.assertEqual(check_url_settings(None), [])
352
353 @override_settings(STATIC_URL="noslash")
354 def test_static_url_no_slash(self):
355 self.assertEqual(check_url_settings(None), [E006("STATIC_URL")])
356
357 @override_settings(STATIC_URL="slashes//")
358 def test_static_url_double_slash_allowed(self):
359 # The check allows for a double slash, presuming the user knows what
360 # they are doing.
361 self.assertEqual(check_url_settings(None), [])
362
363 @override_settings(MEDIA_URL="noslash")
364 def test_media_url_no_slash(self):
365 self.assertEqual(check_url_settings(None), [E006("MEDIA_URL")])