2015-06-25 23:55:51 +03:00
|
|
|
import django.template.loader
|
2017-01-16 21:16:43 +03:00
|
|
|
import pytest
|
|
|
|
from django.core.exceptions import ImproperlyConfigured, PermissionDenied
|
2012-11-06 14:44:19 +04:00
|
|
|
from django.http import Http404
|
2017-10-05 21:41:38 +03:00
|
|
|
from django.template import TemplateDoesNotExist, engines
|
2016-06-01 17:31:00 +03:00
|
|
|
from django.test import TestCase, override_settings
|
2020-09-08 17:32:27 +03:00
|
|
|
from django.urls import path
|
2015-06-25 23:55:51 +03:00
|
|
|
|
2013-02-25 17:59:40 +04:00
|
|
|
from rest_framework import status
|
2012-10-05 15:13:44 +04:00
|
|
|
from rest_framework.decorators import api_view, renderer_classes
|
2024-07-15 19:03:40 +03:00
|
|
|
from rest_framework.exceptions import ValidationError
|
2012-10-28 22:12:56 +04:00
|
|
|
from rest_framework.renderers import TemplateHTMLRenderer
|
2012-10-05 15:13:44 +04:00
|
|
|
from rest_framework.response import Response
|
|
|
|
|
|
|
|
|
|
|
|
@api_view(('GET',))
|
2012-10-28 22:12:56 +04:00
|
|
|
@renderer_classes((TemplateHTMLRenderer,))
|
2012-10-05 15:13:44 +04:00
|
|
|
def example(request):
|
|
|
|
"""
|
|
|
|
A view that can returns an HTML representation.
|
|
|
|
"""
|
|
|
|
data = {'object': 'foobar'}
|
|
|
|
return Response(data, template_name='example.html')
|
|
|
|
|
|
|
|
|
2012-11-06 14:44:19 +04:00
|
|
|
@api_view(('GET',))
|
|
|
|
@renderer_classes((TemplateHTMLRenderer,))
|
|
|
|
def permission_denied(request):
|
|
|
|
raise PermissionDenied()
|
|
|
|
|
|
|
|
|
|
|
|
@api_view(('GET',))
|
|
|
|
@renderer_classes((TemplateHTMLRenderer,))
|
|
|
|
def not_found(request):
|
|
|
|
raise Http404()
|
|
|
|
|
|
|
|
|
2024-07-15 19:03:40 +03:00
|
|
|
@api_view(('GET',))
|
|
|
|
@renderer_classes((TemplateHTMLRenderer,))
|
|
|
|
def validation_error(request):
|
|
|
|
raise ValidationError('error')
|
|
|
|
|
|
|
|
|
2015-06-11 01:45:23 +03:00
|
|
|
urlpatterns = [
|
2020-09-08 17:32:27 +03:00
|
|
|
path('', example),
|
|
|
|
path('permission_denied', permission_denied),
|
|
|
|
path('not_found', not_found),
|
2024-07-15 19:03:40 +03:00
|
|
|
path('validation_error', validation_error),
|
2015-06-11 01:45:23 +03:00
|
|
|
]
|
2012-10-05 15:13:44 +04:00
|
|
|
|
|
|
|
|
2016-06-01 17:31:00 +03:00
|
|
|
@override_settings(ROOT_URLCONF='tests.test_htmlrenderer')
|
2012-10-28 22:12:56 +04:00
|
|
|
class TemplateHTMLRendererTests(TestCase):
|
2012-10-05 15:13:44 +04:00
|
|
|
def setUp(self):
|
2019-04-30 18:53:44 +03:00
|
|
|
class MockResponse:
|
2017-01-16 21:16:43 +03:00
|
|
|
template_name = None
|
|
|
|
self.mock_response = MockResponse()
|
|
|
|
self._monkey_patch_get_template()
|
|
|
|
|
|
|
|
def _monkey_patch_get_template(self):
|
2012-10-05 15:13:44 +04:00
|
|
|
"""
|
|
|
|
Monkeypatch get_template
|
|
|
|
"""
|
|
|
|
self.get_template = django.template.loader.get_template
|
|
|
|
|
2014-01-28 21:54:49 +04:00
|
|
|
def get_template(template_name, dirs=None):
|
2012-10-05 15:13:44 +04:00
|
|
|
if template_name == 'example.html':
|
2017-10-05 21:41:38 +03:00
|
|
|
return engines['django'].from_string("example: {{ object }}")
|
2012-10-05 15:13:44 +04:00
|
|
|
raise TemplateDoesNotExist(template_name)
|
|
|
|
|
2015-01-23 14:47:01 +03:00
|
|
|
def select_template(template_name_list, dirs=None, using=None):
|
|
|
|
if template_name_list == ['example.html']:
|
2017-10-05 21:41:38 +03:00
|
|
|
return engines['django'].from_string("example: {{ object }}")
|
2015-01-23 14:47:01 +03:00
|
|
|
raise TemplateDoesNotExist(template_name_list[0])
|
|
|
|
|
2012-10-05 15:13:44 +04:00
|
|
|
django.template.loader.get_template = get_template
|
2015-01-23 14:47:01 +03:00
|
|
|
django.template.loader.select_template = select_template
|
2012-10-05 15:13:44 +04:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""
|
|
|
|
Revert monkeypatching
|
|
|
|
"""
|
|
|
|
django.template.loader.get_template = self.get_template
|
|
|
|
|
|
|
|
def test_simple_html_view(self):
|
|
|
|
response = self.client.get('/')
|
|
|
|
self.assertContains(response, "example: foobar")
|
2013-05-18 20:21:43 +04:00
|
|
|
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
|
2012-11-06 14:44:19 +04:00
|
|
|
|
|
|
|
def test_not_found_html_view(self):
|
|
|
|
response = self.client.get('/not_found')
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
|
2019-04-30 18:53:44 +03:00
|
|
|
self.assertEqual(response.content, b"404 Not Found")
|
2013-05-18 20:21:43 +04:00
|
|
|
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
|
2012-11-06 14:44:19 +04:00
|
|
|
|
|
|
|
def test_permission_denied_html_view(self):
|
|
|
|
response = self.client.get('/permission_denied')
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
2019-04-30 18:53:44 +03:00
|
|
|
self.assertEqual(response.content, b"403 Forbidden")
|
2013-05-18 20:21:43 +04:00
|
|
|
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
|
2012-11-06 14:44:19 +04:00
|
|
|
|
2024-07-15 19:03:40 +03:00
|
|
|
def test_validation_error_html_view(self):
|
|
|
|
response = self.client.get('/validation_error')
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
|
|
|
self.assertEqual(response.content, b"400 Bad Request")
|
|
|
|
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
|
|
|
|
|
2017-01-16 21:16:43 +03:00
|
|
|
# 2 tests below are based on order of if statements in corresponding method
|
|
|
|
# of TemplateHTMLRenderer
|
|
|
|
def test_get_template_names_returns_own_template_name(self):
|
|
|
|
renderer = TemplateHTMLRenderer()
|
|
|
|
renderer.template_name = 'test_template'
|
|
|
|
template_name = renderer.get_template_names(self.mock_response, view={})
|
|
|
|
assert template_name == ['test_template']
|
|
|
|
|
|
|
|
def test_get_template_names_returns_view_template_name(self):
|
|
|
|
renderer = TemplateHTMLRenderer()
|
|
|
|
|
2019-04-30 18:53:44 +03:00
|
|
|
class MockResponse:
|
2017-01-16 21:16:43 +03:00
|
|
|
template_name = None
|
|
|
|
|
2019-04-30 18:53:44 +03:00
|
|
|
class MockView:
|
2017-01-16 21:16:43 +03:00
|
|
|
def get_template_names(self):
|
|
|
|
return ['template from get_template_names method']
|
|
|
|
|
2019-04-30 18:53:44 +03:00
|
|
|
class MockView2:
|
2017-01-16 21:16:43 +03:00
|
|
|
template_name = 'template from template_name attribute'
|
|
|
|
|
|
|
|
template_name = renderer.get_template_names(self.mock_response,
|
|
|
|
MockView())
|
|
|
|
assert template_name == ['template from get_template_names method']
|
|
|
|
|
|
|
|
template_name = renderer.get_template_names(self.mock_response,
|
|
|
|
MockView2())
|
|
|
|
assert template_name == ['template from template_name attribute']
|
|
|
|
|
|
|
|
def test_get_template_names_raises_error_if_no_template_found(self):
|
|
|
|
renderer = TemplateHTMLRenderer()
|
|
|
|
with pytest.raises(ImproperlyConfigured):
|
|
|
|
renderer.get_template_names(self.mock_response, view=object())
|
|
|
|
|
2012-11-06 14:44:19 +04:00
|
|
|
|
2016-06-01 17:31:00 +03:00
|
|
|
@override_settings(ROOT_URLCONF='tests.test_htmlrenderer')
|
2012-11-06 14:44:19 +04:00
|
|
|
class TemplateHTMLRendererExceptionTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Monkeypatch get_template
|
|
|
|
"""
|
|
|
|
self.get_template = django.template.loader.get_template
|
|
|
|
|
|
|
|
def get_template(template_name):
|
|
|
|
if template_name == '404.html':
|
2017-10-05 21:41:38 +03:00
|
|
|
return engines['django'].from_string("404: {{ detail }}")
|
2012-11-06 14:44:19 +04:00
|
|
|
if template_name == '403.html':
|
2017-10-05 21:41:38 +03:00
|
|
|
return engines['django'].from_string("403: {{ detail }}")
|
2012-11-06 14:44:19 +04:00
|
|
|
raise TemplateDoesNotExist(template_name)
|
|
|
|
|
|
|
|
django.template.loader.get_template = get_template
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""
|
|
|
|
Revert monkeypatching
|
|
|
|
"""
|
|
|
|
django.template.loader.get_template = self.get_template
|
|
|
|
|
|
|
|
def test_not_found_html_view_with_template(self):
|
|
|
|
response = self.client.get('/not_found')
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
|
2014-01-28 21:55:29 +04:00
|
|
|
self.assertTrue(response.content in (
|
2019-04-30 18:53:44 +03:00
|
|
|
b"404: Not found", b"404 Not Found"))
|
2013-05-18 20:21:43 +04:00
|
|
|
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
|
2012-11-06 14:44:19 +04:00
|
|
|
|
|
|
|
def test_permission_denied_html_view_with_template(self):
|
|
|
|
response = self.client.get('/permission_denied')
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
2019-04-30 18:53:44 +03:00
|
|
|
self.assertTrue(response.content in (b"403: Permission denied", b"403 Forbidden"))
|
2013-05-18 20:21:43 +04:00
|
|
|
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
|