diff options
| author | Angus Holder <aholder97@gmail.com> | 2020-11-14 17:25:57 +0000 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-06-09 09:06:42 +0200 |
| commit | 3e73c65ffcf263d5ccd107589452a4615281a0e8 (patch) | |
| tree | 8dc22276119e1ef908452d4b1a6a37ad3932e54b /tests | |
| parent | 8f89454bbc873a117cc8614f2d1f1fbfd4e79ee4 (diff) | |
Fixed #32195 -- Added system check for invalid view in path() and improved error messages.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/check_framework/test_urls.py | 10 | ||||
| -rw-r--r-- | tests/check_framework/urls/cbv_as_view.py | 19 | ||||
| -rw-r--r-- | tests/urlpatterns/tests.py | 9 |
3 files changed, 38 insertions, 0 deletions
diff --git a/tests/check_framework/test_urls.py b/tests/check_framework/test_urls.py index 2ef3d5b07f..663c7a299c 100644 --- a/tests/check_framework/test_urls.py +++ b/tests/check_framework/test_urls.py @@ -134,6 +134,16 @@ class CheckUrlConfigTests(SimpleTestCase): result = check_url_namespaces_unique(None) self.assertEqual(result, []) + @override_settings(ROOT_URLCONF='check_framework.urls.cbv_as_view') + def test_check_view_not_class(self): + 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', + ), + ]) + class UpdatedToPathTests(SimpleTestCase): diff --git a/tests/check_framework/urls/cbv_as_view.py b/tests/check_framework/urls/cbv_as_view.py new file mode 100644 index 0000000000..932a2bfcc9 --- /dev/null +++ b/tests/check_framework/urls/cbv_as_view.py @@ -0,0 +1,19 @@ +from django.http import HttpResponse +from django.urls import path +from django.views import View + + +class EmptyCBV(View): + pass + + +class EmptyCallableView: + def __call__(self, request, *args, **kwargs): + return HttpResponse() + + +urlpatterns = [ + path('missing_as_view', EmptyCBV), + path('has_as_view', EmptyCBV.as_view()), + path('callable_class', EmptyCallableView()), +] diff --git a/tests/urlpatterns/tests.py b/tests/urlpatterns/tests.py index aa593294af..dca9f63086 100644 --- a/tests/urlpatterns/tests.py +++ b/tests/urlpatterns/tests.py @@ -5,6 +5,7 @@ from django.core.exceptions import ImproperlyConfigured from django.test import SimpleTestCase from django.test.utils import override_settings from django.urls import NoReverseMatch, Resolver404, path, resolve, reverse +from django.views import View from .converters import DynamicConverter from .views import empty_view @@ -146,6 +147,14 @@ class SimplifiedURLTests(SimpleTestCase): with self.assertRaisesMessage(TypeError, msg): path('articles/', 'invalid_view') + def test_invalid_view_instance(self): + class EmptyCBV(View): + pass + + msg = 'view must be a callable, pass EmptyCBV.as_view(), not EmptyCBV().' + with self.assertRaisesMessage(TypeError, msg): + path('foo', EmptyCBV()) + def test_whitespace_in_route(self): msg = ( "URL route 'space/<int:num>/extra/<str:%stest>' cannot contain " |
