| 1 |
import django.template.loader |
| 2 |
import pytest |
| 3 |
from django.core.exceptions import ImproperlyConfigured, PermissionDenied |
| 4 |
from django.http import Http404 |
| 5 |
from django.template import TemplateDoesNotExist, engines |
| 6 |
from django.test import TestCase, override_settings |
| 7 |
from django.urls import path |
| 8 |
|
| 9 |
from rest_framework import status |
| 10 |
from rest_framework.decorators import api_view, renderer_classes |
| 11 |
from rest_framework.exceptions import ValidationError |
| 12 |
from rest_framework.renderers import TemplateHTMLRenderer |
| 13 |
from rest_framework.response import Response |
| 14 |
|
| 15 |
|
| 16 |
@api_view(('GET',)) |
| 17 |
@renderer_classes((TemplateHTMLRenderer,)) |
| 18 |
def example(request): |
| 19 |
""" |
| 20 |
A view that can returns an HTML representation. |
| 21 |
""" |
| 22 |
data = {'object': 'foobar'} |
| 23 |
return Response(data, template_name='example.html') |
| 24 |
|
| 25 |
|
| 26 |
@api_view(('GET',)) |
| 27 |
@renderer_classes((TemplateHTMLRenderer,)) |
| 28 |
def permission_denied(request): |
| 29 |
raise PermissionDenied() |
| 30 |
|
| 31 |
|
| 32 |
@api_view(('GET',)) |
| 33 |
@renderer_classes((TemplateHTMLRenderer,)) |
| 34 |
def not_found(request): |
| 35 |
raise Http404() |
| 36 |
|
| 37 |
|
| 38 |
@api_view(('GET',)) |
| 39 |
@renderer_classes((TemplateHTMLRenderer,)) |
| 40 |
def validation_error(request): |
| 41 |
raise ValidationError('error') |
| 42 |
|
| 43 |
|
| 44 |
urlpatterns = [ |
| 45 |
path('', example), |
| 46 |
path('permission_denied', permission_denied), |
| 47 |
path('not_found', not_found), |
| 48 |
path('validation_error', validation_error), |
| 49 |
] |
| 50 |
|
| 51 |
|
| 52 |
@override_settings(ROOT_URLCONF='tests.test_htmlrenderer') |
| 53 |
class TemplateHTMLRendererTests(TestCase): |
| 54 |
def setUp(self): |
| 55 |
class MockResponse: |
| 56 |
template_name = None |
| 57 |
self.mock_response = MockResponse() |
| 58 |
self._monkey_patch_get_template() |
| 59 |
|
| 60 |
def _monkey_patch_get_template(self): |
| 61 |
""" |
| 62 |
Monkeypatch get_template |
| 63 |
""" |
| 64 |
self.get_template = django.template.loader.get_template |
| 65 |
|
| 66 |
def get_template(template_name, dirs=None): |
| 67 |
if template_name == 'example.html': |
| 68 |
return engines['django'].from_string("example: {{ object }}") |
| 69 |
raise TemplateDoesNotExist(template_name) |
| 70 |
|
| 71 |
def select_template(template_name_list, dirs=None, using=None): |
| 72 |
if template_name_list == ['example.html']: |
| 73 |
return engines['django'].from_string("example: {{ object }}") |
| 74 |
raise TemplateDoesNotExist(template_name_list[0]) |
| 75 |
|
| 76 |
django.template.loader.get_template = get_template |
| 77 |
django.template.loader.select_template = select_template |
| 78 |
|
| 79 |
def tearDown(self): |
| 80 |
""" |
| 81 |
Revert monkeypatching |
| 82 |
""" |
| 83 |
django.template.loader.get_template = self.get_template |
| 84 |
|
| 85 |
def test_simple_html_view(self): |
| 86 |
response = self.client.get('/') |
| 87 |
self.assertContains(response, "example: foobar") |
| 88 |
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') |
| 89 |
|
| 90 |
def test_not_found_html_view(self): |
| 91 |
response = self.client.get('/not_found') |
| 92 |
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) |
| 93 |
self.assertEqual(response.content, b"404 Not Found") |
| 94 |
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') |
| 95 |
|
| 96 |
def test_permission_denied_html_view(self): |
| 97 |
response = self.client.get('/permission_denied') |
| 98 |
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) |
| 99 |
self.assertEqual(response.content, b"403 Forbidden") |
| 100 |
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') |
| 101 |
|
| 102 |
def test_validation_error_html_view(self): |
| 103 |
response = self.client.get('/validation_error') |
| 104 |
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
| 105 |
self.assertEqual(response.content, b"400 Bad Request") |
| 106 |
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') |
| 107 |
|
| 108 |
# 2 tests below are based on order of if statements in corresponding method |
| 109 |
# of TemplateHTMLRenderer |
| 110 |
def test_get_template_names_returns_own_template_name(self): |
| 111 |
renderer = TemplateHTMLRenderer() |
| 112 |
renderer.template_name = 'test_template' |
| 113 |
template_name = renderer.get_template_names(self.mock_response, view={}) |
| 114 |
assert template_name == ['test_template'] |
| 115 |
|
| 116 |
def test_get_template_names_returns_view_template_name(self): |
| 117 |
renderer = TemplateHTMLRenderer() |
| 118 |
|
| 119 |
class MockResponse: |
| 120 |
template_name = None |
| 121 |
|
| 122 |
class MockView: |
| 123 |
def get_template_names(self): |
| 124 |
return ['template from get_template_names method'] |
| 125 |
|
| 126 |
class MockView2: |
| 127 |
template_name = 'template from template_name attribute' |
| 128 |
|
| 129 |
template_name = renderer.get_template_names(self.mock_response, |
| 130 |
MockView()) |
| 131 |
assert template_name == ['template from get_template_names method'] |
| 132 |
|
| 133 |
template_name = renderer.get_template_names(self.mock_response, |
| 134 |
MockView2()) |
| 135 |
assert template_name == ['template from template_name attribute'] |
| 136 |
|
| 137 |
def test_get_template_names_raises_error_if_no_template_found(self): |
| 138 |
renderer = TemplateHTMLRenderer() |
| 139 |
with pytest.raises(ImproperlyConfigured): |
| 140 |
renderer.get_template_names(self.mock_response, view=object()) |
| 141 |
|
| 142 |
|
| 143 |
@override_settings(ROOT_URLCONF='tests.test_htmlrenderer') |
| 144 |
class TemplateHTMLRendererExceptionTests(TestCase): |
| 145 |
def setUp(self): |
| 146 |
""" |
| 147 |
Monkeypatch get_template |
| 148 |
""" |
| 149 |
self.get_template = django.template.loader.get_template |
| 150 |
|
| 151 |
def get_template(template_name): |
| 152 |
if template_name == '404.html': |
| 153 |
return engines['django'].from_string("404: {{ detail }}") |
| 154 |
if template_name == '403.html': |
| 155 |
return engines['django'].from_string("403: {{ detail }}") |
| 156 |
raise TemplateDoesNotExist(template_name) |
| 157 |
|
| 158 |
django.template.loader.get_template = get_template |
| 159 |
|
| 160 |
def tearDown(self): |
| 161 |
""" |
| 162 |
Revert monkeypatching |
| 163 |
""" |
| 164 |
django.template.loader.get_template = self.get_template |
| 165 |
|
| 166 |
def test_not_found_html_view_with_template(self): |
| 167 |
response = self.client.get('/not_found') |
| 168 |
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) |
| 169 |
self.assertTrue(response.content in ( |
| 170 |
b"404: Not found", b"404 Not Found")) |
| 171 |
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') |
| 172 |
|
| 173 |
def test_permission_denied_html_view_with_template(self): |
| 174 |
response = self.client.get('/permission_denied') |
| 175 |
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) |
| 176 |
self.assertTrue(response.content in (b"403: Permission denied", b"403 Forbidden")) |
| 177 |
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') |