2011-01-24 22:24:35 +08:00
|
|
|
import time
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
from django.http import HttpResponse
|
2020-08-24 15:00:12 +08:00
|
|
|
from django.test import RequestFactory, SimpleTestCase, override_settings
|
2015-01-27 04:57:10 +08:00
|
|
|
from django.test.utils import require_jinja2
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import resolve
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.views.generic import RedirectView, TemplateView, View
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2012-04-07 05:24:33 +08:00
|
|
|
from . import views
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
class SimpleView(View):
|
|
|
|
"""
|
|
|
|
A simple view with a docstring.
|
|
|
|
"""
|
|
|
|
def get(self, request):
|
|
|
|
return HttpResponse('This is a simple view')
|
|
|
|
|
|
|
|
|
|
|
|
class SimplePostView(SimpleView):
|
|
|
|
post = SimpleView.get
|
|
|
|
|
|
|
|
|
2012-02-18 17:50:03 +08:00
|
|
|
class PostOnlyView(View):
|
|
|
|
def post(self, request):
|
|
|
|
return HttpResponse('This view only accepts POST')
|
|
|
|
|
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
class CustomizableView(SimpleView):
|
|
|
|
parameter = {}
|
|
|
|
|
2012-02-18 17:50:03 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
def decorator(view):
|
|
|
|
view.is_decorated = True
|
|
|
|
return view
|
|
|
|
|
|
|
|
|
|
|
|
class DecoratedDispatchView(SimpleView):
|
|
|
|
|
|
|
|
@decorator
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().dispatch(request, *args, **kwargs)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
class AboutTemplateView(TemplateView):
|
|
|
|
def get(self, request):
|
|
|
|
return self.render_to_response({})
|
|
|
|
|
|
|
|
def get_template_names(self):
|
|
|
|
return ['generic_views/about.html']
|
|
|
|
|
|
|
|
|
|
|
|
class AboutTemplateAttributeView(TemplateView):
|
|
|
|
template_name = 'generic_views/about.html'
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
return self.render_to_response(context={})
|
|
|
|
|
|
|
|
|
|
|
|
class InstanceView(View):
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
2018-12-05 11:52:19 +08:00
|
|
|
class ViewTest(SimpleTestCase):
|
2010-10-18 21:34:47 +08:00
|
|
|
rf = RequestFactory()
|
|
|
|
|
|
|
|
def _assert_simple(self, response):
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2012-08-12 04:29:18 +08:00
|
|
|
self.assertEqual(response.content, b'This is a simple view')
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def test_no_init_kwargs(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
A view can't be accidentally instantiated before deployment
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
2018-12-05 11:52:19 +08:00
|
|
|
msg = 'This method is available only on the class, not on instances.'
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
2013-10-19 20:31:38 +08:00
|
|
|
SimpleView(key='value').as_view()
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def test_no_init_args(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
A view can't be accidentally instantiated before deployment
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
2018-12-05 11:52:19 +08:00
|
|
|
msg = 'as_view() takes 1 positional argument but 2 were given'
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
2013-10-19 20:31:38 +08:00
|
|
|
SimpleView.as_view('value')
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def test_pathological_http_method(self):
|
|
|
|
"""
|
2021-10-20 03:01:44 +08:00
|
|
|
The edge case of an HTTP request that spoofs an existing method name is
|
2021-10-19 00:06:00 +08:00
|
|
|
caught.
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
|
|
|
self.assertEqual(SimpleView.as_view()(
|
|
|
|
self.rf.get('/', REQUEST_METHOD='DISPATCH')
|
|
|
|
).status_code, 405)
|
|
|
|
|
|
|
|
def test_get_only(self):
|
|
|
|
"""
|
|
|
|
Test a view which only allows GET doesn't allow other methods.
|
|
|
|
"""
|
|
|
|
self._assert_simple(SimpleView.as_view()(self.rf.get('/')))
|
|
|
|
self.assertEqual(SimpleView.as_view()(self.rf.post('/')).status_code, 405)
|
|
|
|
self.assertEqual(SimpleView.as_view()(
|
|
|
|
self.rf.get('/', REQUEST_METHOD='FAKE')
|
|
|
|
).status_code, 405)
|
|
|
|
|
2011-04-23 23:10:51 +08:00
|
|
|
def test_get_and_head(self):
|
|
|
|
"""
|
2012-02-18 17:50:03 +08:00
|
|
|
Test a view which supplies a GET method also responds correctly to HEAD.
|
2011-04-23 23:10:51 +08:00
|
|
|
"""
|
|
|
|
self._assert_simple(SimpleView.as_view()(self.rf.get('/')))
|
|
|
|
response = SimpleView.as_view()(self.rf.head('/'))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2019-10-10 13:22:43 +08:00
|
|
|
|
|
|
|
def test_setup_get_and_head(self):
|
|
|
|
view_instance = SimpleView()
|
|
|
|
self.assertFalse(hasattr(view_instance, 'head'))
|
|
|
|
view_instance.setup(self.rf.get('/'))
|
|
|
|
self.assertTrue(hasattr(view_instance, 'head'))
|
|
|
|
self.assertEqual(view_instance.head, view_instance.get)
|
2011-04-23 23:10:51 +08:00
|
|
|
|
2012-02-18 17:50:03 +08:00
|
|
|
def test_head_no_get(self):
|
|
|
|
"""
|
|
|
|
Test a view which supplies no GET method responds to HEAD with HTTP 405.
|
|
|
|
"""
|
|
|
|
response = PostOnlyView.as_view()(self.rf.head('/'))
|
|
|
|
self.assertEqual(response.status_code, 405)
|
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
def test_get_and_post(self):
|
|
|
|
"""
|
|
|
|
Test a view which only allows both GET and POST.
|
|
|
|
"""
|
|
|
|
self._assert_simple(SimplePostView.as_view()(self.rf.get('/')))
|
|
|
|
self._assert_simple(SimplePostView.as_view()(self.rf.post('/')))
|
|
|
|
self.assertEqual(SimplePostView.as_view()(
|
|
|
|
self.rf.get('/', REQUEST_METHOD='FAKE')
|
|
|
|
).status_code, 405)
|
|
|
|
|
|
|
|
def test_invalid_keyword_argument(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
View arguments must be predefined on the class and can't
|
2021-10-19 00:06:00 +08:00
|
|
|
be named like an HTTP method.
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
2018-12-05 11:52:19 +08:00
|
|
|
msg = (
|
2020-01-05 18:04:51 +08:00
|
|
|
'The method name %s is not accepted as a keyword argument to '
|
|
|
|
'SimpleView().'
|
2018-12-05 11:52:19 +08:00
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
# Check each of the allowed method names
|
|
|
|
for method in SimpleView.http_method_names:
|
2018-12-05 11:52:19 +08:00
|
|
|
with self.assertRaisesMessage(TypeError, msg % method):
|
2017-06-02 07:08:59 +08:00
|
|
|
SimpleView.as_view(**{method: 'value'})
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
# Check the case view argument is ok if predefined on the class...
|
|
|
|
CustomizableView.as_view(parameter="value")
|
|
|
|
# ...but raises errors otherwise.
|
2018-12-05 11:52:19 +08:00
|
|
|
msg = (
|
|
|
|
"CustomizableView() received an invalid keyword 'foobar'. "
|
|
|
|
"as_view only accepts arguments that are already attributes of "
|
|
|
|
"the class."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
2016-01-17 19:26:39 +08:00
|
|
|
CustomizableView.as_view(foobar="value")
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def test_calling_more_than_once(self):
|
|
|
|
"""
|
|
|
|
Test a view can only be called once.
|
|
|
|
"""
|
|
|
|
request = self.rf.get('/')
|
|
|
|
view = InstanceView.as_view()
|
|
|
|
self.assertNotEqual(view(request), view(request))
|
|
|
|
|
|
|
|
def test_class_attributes(self):
|
|
|
|
"""
|
2020-12-12 03:30:50 +08:00
|
|
|
The callable returned from as_view() has proper special attributes.
|
|
|
|
"""
|
|
|
|
cls = SimpleView
|
|
|
|
view = cls.as_view()
|
|
|
|
self.assertEqual(view.__doc__, cls.__doc__)
|
|
|
|
self.assertEqual(view.__name__, 'view')
|
|
|
|
self.assertEqual(view.__module__, cls.__module__)
|
|
|
|
self.assertEqual(view.__qualname__, f'{cls.as_view.__qualname__}.<locals>.view')
|
|
|
|
self.assertEqual(view.__annotations__, cls.dispatch.__annotations__)
|
|
|
|
self.assertFalse(hasattr(view, '__wrapped__'))
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def test_dispatch_decoration(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Attributes set by decorators on the dispatch method
|
2010-10-18 21:34:47 +08:00
|
|
|
are also present on the closure.
|
|
|
|
"""
|
|
|
|
self.assertTrue(DecoratedDispatchView.as_view().is_decorated)
|
|
|
|
|
2012-05-17 19:54:51 +08:00
|
|
|
def test_options(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Views respond to HTTP OPTIONS requests with an Allow header
|
2012-05-17 19:54:51 +08:00
|
|
|
appropriate for the methods implemented by the view class.
|
|
|
|
"""
|
|
|
|
request = self.rf.options('/')
|
|
|
|
view = SimpleView.as_view()
|
|
|
|
response = view(request)
|
|
|
|
self.assertEqual(200, response.status_code)
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertTrue(response.headers['Allow'])
|
2012-05-17 19:54:51 +08:00
|
|
|
|
|
|
|
def test_options_for_get_view(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
A view implementing GET allows GET and HEAD.
|
2012-05-17 19:54:51 +08:00
|
|
|
"""
|
|
|
|
request = self.rf.options('/')
|
|
|
|
view = SimpleView.as_view()
|
|
|
|
response = view(request)
|
|
|
|
self._assert_allows(response, 'GET', 'HEAD')
|
|
|
|
|
|
|
|
def test_options_for_get_and_post_view(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
A view implementing GET and POST allows GET, HEAD, and POST.
|
2012-05-17 19:54:51 +08:00
|
|
|
"""
|
|
|
|
request = self.rf.options('/')
|
|
|
|
view = SimplePostView.as_view()
|
|
|
|
response = view(request)
|
|
|
|
self._assert_allows(response, 'GET', 'HEAD', 'POST')
|
|
|
|
|
|
|
|
def test_options_for_post_view(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
A view implementing POST allows POST.
|
2012-05-17 19:54:51 +08:00
|
|
|
"""
|
|
|
|
request = self.rf.options('/')
|
|
|
|
view = PostOnlyView.as_view()
|
|
|
|
response = view(request)
|
|
|
|
self._assert_allows(response, 'POST')
|
|
|
|
|
|
|
|
def _assert_allows(self, response, *expected_methods):
|
|
|
|
"Assert allowed HTTP methods reported in the Allow response header"
|
2020-07-14 19:32:24 +08:00
|
|
|
response_allows = set(response.headers['Allow'].split(', '))
|
2012-05-17 19:54:51 +08:00
|
|
|
self.assertEqual(set(expected_methods + ('OPTIONS',)), response_allows)
|
|
|
|
|
2012-11-23 03:10:01 +08:00
|
|
|
def test_args_kwargs_request_on_self(self):
|
|
|
|
"""
|
|
|
|
Test a view only has args, kwargs & request once `as_view`
|
|
|
|
has been called.
|
|
|
|
"""
|
|
|
|
bare_view = InstanceView()
|
|
|
|
view = InstanceView.as_view()(self.rf.get('/'))
|
|
|
|
for attribute in ('args', 'kwargs', 'request'):
|
|
|
|
self.assertNotIn(attribute, dir(bare_view))
|
|
|
|
self.assertIn(attribute, dir(view))
|
|
|
|
|
2018-09-19 16:53:05 +08:00
|
|
|
def test_overridden_setup(self):
|
|
|
|
class SetAttributeMixin:
|
|
|
|
def setup(self, request, *args, **kwargs):
|
|
|
|
self.attr = True
|
|
|
|
super().setup(request, *args, **kwargs)
|
|
|
|
|
|
|
|
class CheckSetupView(SetAttributeMixin, SimpleView):
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
assert hasattr(self, 'attr')
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
response = CheckSetupView.as_view()(self.rf.get('/'))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_not_calling_parent_setup_error(self):
|
|
|
|
class TestView(View):
|
|
|
|
def setup(self, request, *args, **kwargs):
|
2019-06-05 08:09:07 +08:00
|
|
|
pass # Not calling super().setup()
|
2018-09-19 16:53:05 +08:00
|
|
|
|
|
|
|
msg = (
|
|
|
|
"TestView instance has no 'request' attribute. Did you override "
|
|
|
|
"setup() and forget to call super()?"
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
|
|
|
TestView.as_view()(self.rf.get('/'))
|
|
|
|
|
2019-10-30 21:14:04 +08:00
|
|
|
def test_setup_adds_args_kwargs_request(self):
|
|
|
|
request = self.rf.get('/')
|
|
|
|
args = ('arg 1', 'arg 2')
|
|
|
|
kwargs = {'kwarg_1': 1, 'kwarg_2': 'year'}
|
|
|
|
|
|
|
|
view = View()
|
|
|
|
view.setup(request, *args, **kwargs)
|
|
|
|
self.assertEqual(request, view.request)
|
|
|
|
self.assertEqual(args, view.args)
|
|
|
|
self.assertEqual(kwargs, view.kwargs)
|
|
|
|
|
2013-12-16 23:45:18 +08:00
|
|
|
def test_direct_instantiation(self):
|
|
|
|
"""
|
|
|
|
It should be possible to use the view by directly instantiating it
|
|
|
|
without going through .as_view() (#21564).
|
|
|
|
"""
|
|
|
|
view = PostOnlyView()
|
|
|
|
response = view.dispatch(self.rf.head('/'))
|
|
|
|
self.assertEqual(response.status_code, 405)
|
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF='generic_views.urls')
|
2015-04-18 05:38:20 +08:00
|
|
|
class TemplateViewTest(SimpleTestCase):
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
rf = RequestFactory()
|
|
|
|
|
|
|
|
def _assert_about(self, response):
|
2010-12-07 21:57:01 +08:00
|
|
|
response.render()
|
2011-01-24 22:24:35 +08:00
|
|
|
self.assertContains(response, '<h1>About</h1>')
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def test_get(self):
|
|
|
|
"""
|
|
|
|
Test a view that simply renders a template on GET
|
|
|
|
"""
|
|
|
|
self._assert_about(AboutTemplateView.as_view()(self.rf.get('/about/')))
|
|
|
|
|
2011-04-23 23:10:51 +08:00
|
|
|
def test_head(self):
|
|
|
|
"""
|
|
|
|
Test a TemplateView responds correctly to HEAD
|
|
|
|
"""
|
|
|
|
response = AboutTemplateView.as_view()(self.rf.head('/about/'))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
def test_get_template_attribute(self):
|
|
|
|
"""
|
|
|
|
Test a view that renders a template on GET with the template name as
|
|
|
|
an attribute on the class.
|
|
|
|
"""
|
|
|
|
self._assert_about(AboutTemplateAttributeView.as_view()(self.rf.get('/about/')))
|
|
|
|
|
|
|
|
def test_get_generic_template(self):
|
|
|
|
"""
|
|
|
|
Test a completely generic view that renders a template on GET
|
|
|
|
with the template name as an argument at instantiation.
|
|
|
|
"""
|
|
|
|
self._assert_about(TemplateView.as_view(template_name='generic_views/about.html')(self.rf.get('/about/')))
|
|
|
|
|
2011-02-14 21:05:14 +08:00
|
|
|
def test_template_name_required(self):
|
|
|
|
"""
|
2015-01-27 04:57:10 +08:00
|
|
|
A template view must provide a template name.
|
2011-02-14 21:05:14 +08:00
|
|
|
"""
|
2017-05-29 03:37:21 +08:00
|
|
|
msg = (
|
|
|
|
"TemplateResponseMixin requires either a definition of "
|
|
|
|
"'template_name' or an implementation of 'get_template_names()'"
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
2016-01-17 19:26:39 +08:00
|
|
|
self.client.get('/template/no_template/')
|
2011-02-14 21:05:14 +08:00
|
|
|
|
2015-01-27 04:57:10 +08:00
|
|
|
@require_jinja2
|
|
|
|
def test_template_engine(self):
|
|
|
|
"""
|
|
|
|
A template view may provide a template engine.
|
|
|
|
"""
|
|
|
|
request = self.rf.get('/using/')
|
|
|
|
view = TemplateView.as_view(template_name='generic_views/using.html')
|
|
|
|
self.assertEqual(view(request).render().content, b'DTL\n')
|
|
|
|
view = TemplateView.as_view(template_name='generic_views/using.html', template_engine='django')
|
|
|
|
self.assertEqual(view(request).render().content, b'DTL\n')
|
|
|
|
view = TemplateView.as_view(template_name='generic_views/using.html', template_engine='jinja2')
|
|
|
|
self.assertEqual(view(request).render().content, b'Jinja2\n')
|
|
|
|
|
2020-08-24 15:00:12 +08:00
|
|
|
def test_template_params(self):
|
|
|
|
"""
|
|
|
|
A generic template view passes kwargs as context.
|
|
|
|
"""
|
|
|
|
response = self.client.get('/template/simple/bar/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.context['foo'], 'bar')
|
|
|
|
self.assertIsInstance(response.context['view'], View)
|
|
|
|
|
|
|
|
def test_extra_template_params(self):
|
|
|
|
"""
|
|
|
|
A template view can be customized to return extra context.
|
|
|
|
"""
|
|
|
|
response = self.client.get('/template/custom/bar/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.context['foo'], 'bar')
|
|
|
|
self.assertEqual(response.context['key'], 'value')
|
|
|
|
self.assertIsInstance(response.context['view'], View)
|
|
|
|
|
2011-01-24 22:24:35 +08:00
|
|
|
def test_cached_views(self):
|
|
|
|
"""
|
|
|
|
A template view can be cached
|
|
|
|
"""
|
|
|
|
response = self.client.get('/template/cached/bar/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
time.sleep(1.0)
|
|
|
|
|
|
|
|
response2 = self.client.get('/template/cached/bar/')
|
|
|
|
self.assertEqual(response2.status_code, 200)
|
|
|
|
|
|
|
|
self.assertEqual(response.content, response2.content)
|
|
|
|
|
|
|
|
time.sleep(2.0)
|
|
|
|
|
|
|
|
# Let the cache expire and test again
|
|
|
|
response2 = self.client.get('/template/cached/bar/')
|
|
|
|
self.assertEqual(response2.status_code, 200)
|
|
|
|
|
|
|
|
self.assertNotEqual(response.content, response2.content)
|
|
|
|
|
2013-01-31 04:26:17 +08:00
|
|
|
def test_content_type(self):
|
|
|
|
response = self.client.get('/template/content_type/')
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(response.headers['Content-Type'], 'text/plain')
|
2013-01-31 04:26:17 +08:00
|
|
|
|
2014-12-27 15:16:53 +08:00
|
|
|
def test_resolve_view(self):
|
|
|
|
match = resolve('/template/content_type/')
|
|
|
|
self.assertIs(match.func.view_class, TemplateView)
|
|
|
|
self.assertEqual(match.func.view_initkwargs['content_type'], 'text/plain')
|
|
|
|
|
|
|
|
def test_resolve_login_required_view(self):
|
|
|
|
match = resolve('/template/login_required/')
|
|
|
|
self.assertIs(match.func.view_class, TemplateView)
|
|
|
|
|
2017-07-06 22:34:54 +08:00
|
|
|
def test_extra_context(self):
|
|
|
|
response = self.client.get('/template/extra_context/')
|
|
|
|
self.assertEqual(response.context['title'], 'Title')
|
|
|
|
|
2013-01-31 04:26:17 +08:00
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF='generic_views.urls')
|
2015-04-18 05:38:20 +08:00
|
|
|
class RedirectViewTest(SimpleTestCase):
|
2013-06-14 18:59:26 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
rf = RequestFactory()
|
|
|
|
|
|
|
|
def test_no_url(self):
|
|
|
|
"Without any configuration, returns HTTP 410 GONE"
|
|
|
|
response = RedirectView.as_view()(self.rf.get('/foo/'))
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 410)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2015-01-19 05:43:57 +08:00
|
|
|
def test_default_redirect(self):
|
|
|
|
"Default is a temporary redirect"
|
2010-10-18 21:34:47 +08:00
|
|
|
response = RedirectView.as_view(url='/bar/')(self.rf.get('/foo/'))
|
2015-01-19 05:43:57 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.url, '/bar/')
|
|
|
|
|
|
|
|
def test_permanent_redirect(self):
|
|
|
|
"Permanent redirects are an option"
|
|
|
|
response = RedirectView.as_view(url='/bar/', permanent=True)(self.rf.get('/foo/'))
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 301)
|
2013-02-13 16:55:43 +08:00
|
|
|
self.assertEqual(response.url, '/bar/')
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def test_temporary_redirect(self):
|
2015-01-19 05:43:57 +08:00
|
|
|
"Temporary redirects are an option"
|
2010-10-18 21:34:47 +08:00
|
|
|
response = RedirectView.as_view(url='/bar/', permanent=False)(self.rf.get('/foo/'))
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2013-02-13 16:55:43 +08:00
|
|
|
self.assertEqual(response.url, '/bar/')
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def test_include_args(self):
|
|
|
|
"GET arguments can be included in the redirected URL"
|
|
|
|
response = RedirectView.as_view(url='/bar/')(self.rf.get('/foo/'))
|
2015-01-19 05:43:57 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2013-02-13 16:55:43 +08:00
|
|
|
self.assertEqual(response.url, '/bar/')
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
response = RedirectView.as_view(url='/bar/', query_string=True)(self.rf.get('/foo/?pork=spam'))
|
2015-01-19 05:43:57 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2013-02-13 16:55:43 +08:00
|
|
|
self.assertEqual(response.url, '/bar/?pork=spam')
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2012-03-03 00:55:56 +08:00
|
|
|
def test_include_urlencoded_args(self):
|
|
|
|
"GET arguments can be URL-encoded when included in the redirected URL"
|
|
|
|
response = RedirectView.as_view(url='/bar/', query_string=True)(
|
|
|
|
self.rf.get('/foo/?unicode=%E2%9C%93'))
|
2015-01-19 05:43:57 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2013-02-13 16:55:43 +08:00
|
|
|
self.assertEqual(response.url, '/bar/?unicode=%E2%9C%93')
|
2012-03-03 00:55:56 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
def test_parameter_substitution(self):
|
|
|
|
"Redirection URLs can be parameterized"
|
|
|
|
response = RedirectView.as_view(url='/bar/%(object_id)d/')(self.rf.get('/foo/42/'), object_id=42)
|
2015-01-19 05:43:57 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2013-02-13 16:55:43 +08:00
|
|
|
self.assertEqual(response.url, '/bar/42/')
|
2011-04-02 16:45:22 +08:00
|
|
|
|
2013-06-14 18:59:26 +08:00
|
|
|
def test_named_url_pattern(self):
|
|
|
|
"Named pattern parameter should reverse to the matching pattern"
|
|
|
|
response = RedirectView.as_view(pattern_name='artist_detail')(self.rf.get('/foo/'), pk=1)
|
2015-01-19 05:43:57 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(response.headers['Location'], '/detail/artist/1/')
|
2013-06-14 18:59:26 +08:00
|
|
|
|
|
|
|
def test_named_url_pattern_using_args(self):
|
|
|
|
response = RedirectView.as_view(pattern_name='artist_detail')(self.rf.get('/foo/'), 1)
|
2015-01-19 05:43:57 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(response.headers['Location'], '/detail/artist/1/')
|
2013-06-14 18:59:26 +08:00
|
|
|
|
2011-04-02 16:45:22 +08:00
|
|
|
def test_redirect_POST(self):
|
2015-01-19 05:43:57 +08:00
|
|
|
"Default is a temporary redirect"
|
2011-04-02 16:45:22 +08:00
|
|
|
response = RedirectView.as_view(url='/bar/')(self.rf.post('/foo/'))
|
2015-01-19 05:43:57 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2013-02-13 16:55:43 +08:00
|
|
|
self.assertEqual(response.url, '/bar/')
|
2011-04-02 16:45:22 +08:00
|
|
|
|
|
|
|
def test_redirect_HEAD(self):
|
2015-01-19 05:43:57 +08:00
|
|
|
"Default is a temporary redirect"
|
2011-04-02 16:45:22 +08:00
|
|
|
response = RedirectView.as_view(url='/bar/')(self.rf.head('/foo/'))
|
2015-01-19 05:43:57 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2013-02-13 16:55:43 +08:00
|
|
|
self.assertEqual(response.url, '/bar/')
|
2011-04-02 16:45:22 +08:00
|
|
|
|
|
|
|
def test_redirect_OPTIONS(self):
|
2015-01-19 05:43:57 +08:00
|
|
|
"Default is a temporary redirect"
|
2011-04-02 16:45:22 +08:00
|
|
|
response = RedirectView.as_view(url='/bar/')(self.rf.options('/foo/'))
|
2015-01-19 05:43:57 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2013-02-13 16:55:43 +08:00
|
|
|
self.assertEqual(response.url, '/bar/')
|
2011-04-02 16:45:22 +08:00
|
|
|
|
|
|
|
def test_redirect_PUT(self):
|
2015-01-19 05:43:57 +08:00
|
|
|
"Default is a temporary redirect"
|
2011-04-02 16:45:22 +08:00
|
|
|
response = RedirectView.as_view(url='/bar/')(self.rf.put('/foo/'))
|
2015-01-19 05:43:57 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2013-02-13 16:55:43 +08:00
|
|
|
self.assertEqual(response.url, '/bar/')
|
2011-04-02 16:45:22 +08:00
|
|
|
|
2013-05-22 00:01:29 +08:00
|
|
|
def test_redirect_PATCH(self):
|
2015-01-19 05:43:57 +08:00
|
|
|
"Default is a temporary redirect"
|
2013-05-22 00:01:29 +08:00
|
|
|
response = RedirectView.as_view(url='/bar/')(self.rf.patch('/foo/'))
|
2015-01-19 05:43:57 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2013-05-22 00:01:29 +08:00
|
|
|
self.assertEqual(response.url, '/bar/')
|
|
|
|
|
2011-04-02 16:45:22 +08:00
|
|
|
def test_redirect_DELETE(self):
|
2015-01-19 05:43:57 +08:00
|
|
|
"Default is a temporary redirect"
|
2011-04-02 16:45:22 +08:00
|
|
|
response = RedirectView.as_view(url='/bar/')(self.rf.delete('/foo/'))
|
2015-01-19 05:43:57 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2013-02-13 16:55:43 +08:00
|
|
|
self.assertEqual(response.url, '/bar/')
|
2011-10-07 04:39:15 +08:00
|
|
|
|
|
|
|
def test_redirect_when_meta_contains_no_query_string(self):
|
|
|
|
"regression for #16705"
|
|
|
|
# we can't use self.rf.get because it always sets QUERY_STRING
|
|
|
|
response = RedirectView.as_view(url='/bar/')(self.rf.request(PATH_INFO='/foo/'))
|
2015-01-19 05:43:57 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2012-04-07 05:24:33 +08:00
|
|
|
|
2013-12-16 23:45:18 +08:00
|
|
|
def test_direct_instantiation(self):
|
|
|
|
"""
|
|
|
|
It should be possible to use the view without going through .as_view()
|
|
|
|
(#21564).
|
|
|
|
"""
|
|
|
|
view = RedirectView()
|
|
|
|
response = view.dispatch(self.rf.head('/foo/'))
|
|
|
|
self.assertEqual(response.status_code, 410)
|
|
|
|
|
2012-04-07 05:24:33 +08:00
|
|
|
|
2018-12-05 11:52:19 +08:00
|
|
|
class GetContextDataTest(SimpleTestCase):
|
2012-04-07 05:24:33 +08:00
|
|
|
|
|
|
|
def test_get_context_data_super(self):
|
|
|
|
test_view = views.CustomContextView()
|
|
|
|
context = test_view.get_context_data(kwarg_test='kwarg_value')
|
|
|
|
|
|
|
|
# the test_name key is inserted by the test classes parent
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn('test_name', context)
|
2012-04-07 05:24:33 +08:00
|
|
|
self.assertEqual(context['kwarg_test'], 'kwarg_value')
|
|
|
|
self.assertEqual(context['custom_key'], 'custom_value')
|
|
|
|
|
|
|
|
# test that kwarg overrides values assigned higher up
|
|
|
|
context = test_view.get_context_data(test_name='test_value')
|
|
|
|
self.assertEqual(context['test_name'], 'test_value')
|
2013-04-10 18:27:28 +08:00
|
|
|
|
2013-05-18 18:42:18 +08:00
|
|
|
def test_object_at_custom_name_in_context_data(self):
|
|
|
|
# Checks 'pony' key presence in dict returned by get_context_date
|
|
|
|
test_view = views.CustomSingleObjectView()
|
|
|
|
test_view.context_object_name = 'pony'
|
|
|
|
context = test_view.get_context_data()
|
|
|
|
self.assertEqual(context['pony'], test_view.object)
|
|
|
|
|
|
|
|
def test_object_in_get_context_data(self):
|
|
|
|
# Checks 'object' key presence in dict returned by get_context_date #20234
|
|
|
|
test_view = views.CustomSingleObjectView()
|
|
|
|
context = test_view.get_context_data()
|
|
|
|
self.assertEqual(context['object'], test_view.object)
|
|
|
|
|
2013-04-10 18:27:28 +08:00
|
|
|
|
2018-12-05 11:52:19 +08:00
|
|
|
class UseMultipleObjectMixinTest(SimpleTestCase):
|
2013-04-10 18:27:28 +08:00
|
|
|
rf = RequestFactory()
|
|
|
|
|
|
|
|
def test_use_queryset_from_view(self):
|
|
|
|
test_view = views.CustomMultipleObjectMixinView()
|
|
|
|
test_view.get(self.rf.get('/'))
|
|
|
|
# Don't pass queryset as argument
|
|
|
|
context = test_view.get_context_data()
|
|
|
|
self.assertEqual(context['object_list'], test_view.queryset)
|
|
|
|
|
|
|
|
def test_overwrite_queryset(self):
|
|
|
|
test_view = views.CustomMultipleObjectMixinView()
|
|
|
|
test_view.get(self.rf.get('/'))
|
|
|
|
queryset = [{'name': 'Lennon'}, {'name': 'Ono'}]
|
|
|
|
self.assertNotEqual(test_view.queryset, queryset)
|
|
|
|
# Overwrite the view's queryset with queryset from kwarg
|
|
|
|
context = test_view.get_context_data(object_list=queryset)
|
|
|
|
self.assertEqual(context['object_list'], queryset)
|
2013-09-07 05:25:34 +08:00
|
|
|
|
|
|
|
|
2018-12-05 11:52:19 +08:00
|
|
|
class SingleObjectTemplateResponseMixinTest(SimpleTestCase):
|
2013-09-07 05:25:34 +08:00
|
|
|
|
2013-09-07 06:20:43 +08:00
|
|
|
def test_template_mixin_without_template(self):
|
2013-09-07 05:25:34 +08:00
|
|
|
"""
|
|
|
|
We want to makes sure that if you use a template mixin, but forget the
|
|
|
|
template, it still tells you it's ImproperlyConfigured instead of
|
|
|
|
TemplateDoesNotExist.
|
|
|
|
"""
|
|
|
|
view = views.TemplateResponseWithoutTemplate()
|
2018-12-05 11:52:19 +08:00
|
|
|
msg = (
|
|
|
|
"TemplateResponseMixin requires either a definition of "
|
|
|
|
"'template_name' or an implementation of 'get_template_names()'"
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
2016-01-17 19:26:39 +08:00
|
|
|
view.get_template_names()
|