2013-02-05 00:55:35 +04:00
|
|
|
from __future__ import unicode_literals
|
2012-11-06 14:44:19 +04:00
|
|
|
from django.core.exceptions import PermissionDenied
|
|
|
|
from django.http import Http404
|
2012-10-05 15:13:44 +04:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.template import TemplateDoesNotExist, Template
|
|
|
|
import django.template.loader
|
2013-02-25 17:59:40 +04:00
|
|
|
from rest_framework import status
|
2012-12-20 03:12:27 +04:00
|
|
|
from rest_framework.compat import patterns, url
|
2012-10-05 15:13:44 +04:00
|
|
|
from rest_framework.decorators import api_view, renderer_classes
|
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
|
2013-01-03 14:41:07 +04:00
|
|
|
from rest_framework.compat import six
|
2012-10-05 15:13:44 +04:00
|
|
|
|
|
|
|
|
|
|
|
@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()
|
|
|
|
|
|
|
|
|
2012-10-05 15:13:44 +04:00
|
|
|
urlpatterns = patterns('',
|
|
|
|
url(r'^$', example),
|
2012-11-06 14:44:19 +04:00
|
|
|
url(r'^permission_denied$', permission_denied),
|
|
|
|
url(r'^not_found$', not_found),
|
2012-10-05 15:13:44 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2012-10-28 22:12:56 +04:00
|
|
|
class TemplateHTMLRendererTests(TestCase):
|
2012-10-05 15:13:44 +04:00
|
|
|
urls = 'rest_framework.tests.htmlrenderer'
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Monkeypatch get_template
|
|
|
|
"""
|
|
|
|
self.get_template = django.template.loader.get_template
|
|
|
|
|
|
|
|
def get_template(template_name):
|
|
|
|
if template_name == 'example.html':
|
|
|
|
return Template("example: {{ object }}")
|
|
|
|
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_simple_html_view(self):
|
|
|
|
response = self.client.get('/')
|
|
|
|
self.assertContains(response, "example: foobar")
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(response['Content-Type'], 'text/html')
|
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)
|
|
|
|
self.assertEqual(response.content, six.b("404 Not Found"))
|
|
|
|
self.assertEqual(response['Content-Type'], 'text/html')
|
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)
|
|
|
|
self.assertEqual(response.content, six.b("403 Forbidden"))
|
|
|
|
self.assertEqual(response['Content-Type'], 'text/html')
|
2012-11-06 14:44:19 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TemplateHTMLRendererExceptionTests(TestCase):
|
|
|
|
urls = 'rest_framework.tests.htmlrenderer'
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Monkeypatch get_template
|
|
|
|
"""
|
|
|
|
self.get_template = django.template.loader.get_template
|
|
|
|
|
|
|
|
def get_template(template_name):
|
|
|
|
if template_name == '404.html':
|
|
|
|
return Template("404: {{ detail }}")
|
|
|
|
if template_name == '403.html':
|
|
|
|
return Template("403: {{ detail }}")
|
|
|
|
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)
|
|
|
|
self.assertEqual(response.content, six.b("404: Not found"))
|
|
|
|
self.assertEqual(response['Content-Type'], 'text/html')
|
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)
|
|
|
|
self.assertEqual(response.content, six.b("403: Permission denied"))
|
|
|
|
self.assertEqual(response['Content-Type'], 'text/html')
|