From 9b191bb932a3a7d6ebeb61be31ba666e8f8a9d9e Mon Sep 17 00:00:00 2001 From: mattack108 Date: Tue, 15 Jan 2013 15:48:39 +0000 Subject: Fix tests and add new ones for show_content_in_browser feature --- wkhtmltopdf/tests/templates/footer.html | 5 +- wkhtmltopdf/tests/tests.py | 415 +++++++++++++++++++------------- 2 files changed, 246 insertions(+), 174 deletions(-) (limited to 'wkhtmltopdf/tests') diff --git a/wkhtmltopdf/tests/templates/footer.html b/wkhtmltopdf/tests/templates/footer.html index 3ae09cb..cf98267 100644 --- a/wkhtmltopdf/tests/templates/footer.html +++ b/wkhtmltopdf/tests/templates/footer.html @@ -1,2 +1,3 @@ -MEDIA_URL = {{ MEDIA_URL }} -STATIC_URL = {{ STATIC_URL }} + + + diff --git a/wkhtmltopdf/tests/tests.py b/wkhtmltopdf/tests/tests.py index 16e89ba..6a405c7 100644 --- a/wkhtmltopdf/tests/tests.py +++ b/wkhtmltopdf/tests/tests.py @@ -5,11 +5,13 @@ from __future__ import absolute_import import os import sys +from django.conf import settings from django.test import TestCase from django.test.client import RequestFactory from wkhtmltopdf.subprocess import CalledProcessError -from wkhtmltopdf.utils import override_settings, _options_to_args, wkhtmltopdf +from wkhtmltopdf.utils import (_options_to_args, make_absolute_paths, + wkhtmltopdf) from wkhtmltopdf.views import PDFResponse, PDFTemplateView, PDFTemplateResponse @@ -98,6 +100,24 @@ class TestViews(TestCase): self.assertEqual(response['Content-Disposition'], 'attachment; filename="?.pdf"') + # Content as a direct output + response = PDFResponse(content=content, filename="nospace.pdf", + show_content_in_browser=True) + self.assertEqual(response['Content-Disposition'], + 'inline; filename="nospace.pdf"') + response = PDFResponse(content=content, filename="one space.pdf", + show_content_in_browser=True) + self.assertEqual(response['Content-Disposition'], + 'inline; filename="one space.pdf"') + response = PDFResponse(content=content, filename="4'5\".pdf", + show_content_in_browser=True) + self.assertEqual(response['Content-Disposition'], + 'inline; filename="4\'5.pdf"') + response = PDFResponse(content=content, filename=u"♥.pdf", + show_content_in_browser=True) + self.assertEqual(response['Content-Disposition'], + 'inline; filename="?.pdf"') + # Content-Type response = PDFResponse(content=content, content_type='application/x-pdf') @@ -106,185 +126,236 @@ class TestViews(TestCase): mimetype='application/x-pdf') self.assertEqual(response['Content-Type'], 'application/x-pdf') + def test_pdf_template_response_to_browser(self): + """Test PDFTemplateResponse.""" + # Setup sample.html + template = 'sample.html' + context = {'title': 'Heading'} + request = RequestFactory().get('/') + response = PDFTemplateResponse(request=request, + template=template, + context=context, + show_content_in_browser=True) + self.assertEqual(response._request, request) + self.assertEqual(response.template_name, template) + self.assertEqual(response.context_data, context) + self.assertEqual(response.filename, None) + self.assertEqual(response.header_template, None) + self.assertEqual(response.footer_template, None) + self.assertEqual(response.cmd_options, {}) + self.assertFalse(response.has_header('Content-Disposition')) + + # Render to temporary file + tempfile = response.render_to_temporary_file(template) + tempfile.seek(0) + html_content = tempfile.read() + self.assertTrue(html_content.startswith('')) + self.assertTrue('

{title}

'.format(**context) + in html_content) + + pdf_content = response.rendered_content + self.assertTrue(pdf_content.startswith('%PDF-')) + self.assertTrue(pdf_content.endswith('%%EOF\n')) + + # Footer + filename = 'output.pdf' + footer_template = 'footer.html' + cmd_options = {'title': 'Test PDF'} + response = PDFTemplateResponse(request=request, + template=template, + context=context, + filename=filename, + show_content_in_browser=True, + footer_template=footer_template, + cmd_options=cmd_options) + self.assertEqual(response.filename, filename) + self.assertEqual(response.header_template, None) + self.assertEqual(response.footer_template, footer_template) + self.assertEqual(response.cmd_options, cmd_options) + self.assertTrue(response.has_header('Content-Disposition')) + + tempfile = response.render_to_temporary_file(footer_template) + tempfile.seek(0) + footer_content = tempfile.read() + footer_content = make_absolute_paths(footer_content) + + media_url = 'file://{0}/'.format(settings.MEDIA_ROOT) + self.assertTrue(media_url in footer_content, True) + + static_url = 'file://{0}/'.format(settings.STATIC_ROOT) + self.assertTrue(static_url in footer_content, True) + + pdf_content = response.rendered_content + self.assertTrue('\0'.join('{title}'.format(**cmd_options)) + in pdf_content) + def test_pdf_template_response(self): """Test PDFTemplateResponse.""" - from django.conf import settings - - with override_settings( - MEDIA_URL='/media/', - MEDIA_ROOT='/tmp/media', - STATIC_URL='/static/', - STATIC_ROOT='/tmp/static', - TEMPLATE_CONTEXT_PROCESSORS=[ - 'django.core.context_processors.media', - 'django.core.context_processors.static', - ], - TEMPLATE_LOADERS=['django.template.loaders.filesystem.Loader'], - TEMPLATE_DIRS=[os.path.join(os.path.dirname(__file__), - '_testproject', 'templates')], - WKHTMLTOPDF_DEBUG=False, - ): - # Setup sample.html - template = 'sample.html' - context = {'title': 'Heading'} - request = RequestFactory().get('/') - response = PDFTemplateResponse(request=request, - template=template, - context=context) - self.assertEqual(response._request, request) - self.assertEqual(response.template_name, template) - self.assertEqual(response.context_data, context) - self.assertEqual(response.filename, None) - self.assertEqual(response.header_template, None) - self.assertEqual(response.footer_template, None) - self.assertEqual(response.cmd_options, {}) - self.assertFalse(response.has_header('Content-Disposition')) - - # Render to temporary file - tempfile = response.render_to_temporary_file(template) - tempfile.seek(0) - html_content = tempfile.read() - self.assertTrue(html_content.startswith('')) - self.assertTrue('

{title}

'.format(**context) - in html_content) - - pdf_content = response.rendered_content - self.assertTrue(pdf_content.startswith('%PDF-')) - self.assertTrue(pdf_content.endswith('%%EOF\n')) - - # Footer - filename = 'output.pdf' - footer_template = 'footer.html' - cmd_options = {'title': 'Test PDF'} - response = PDFTemplateResponse(request=request, - template=template, - context=context, - filename=filename, - footer_template=footer_template, - cmd_options=cmd_options) - self.assertEqual(response.filename, filename) - self.assertEqual(response.header_template, None) - self.assertEqual(response.footer_template, footer_template) - self.assertEqual(response.cmd_options, cmd_options) - self.assertTrue(response.has_header('Content-Disposition')) - - tempfile = response.render_to_temporary_file(footer_template) - tempfile.seek(0) - footer_content = tempfile.read() - - media_url = 'MEDIA_URL = file://{0}/'.format(settings.MEDIA_ROOT) - self.assertTrue( - media_url in footer_content, - "{0!r} not in {1!r}".format(media_url, footer_content) - ) - - static_url = 'STATIC_URL = file://{0}/'.format(settings.STATIC_ROOT) - self.assertTrue( - static_url in footer_content, - "{0!r} not in {1!r}".format(static_url, footer_content) - ) - - pdf_content = response.rendered_content - self.assertTrue('\0'.join('{title}'.format(**cmd_options)) - in pdf_content) - - # Override settings - response = PDFTemplateResponse(request=request, - template=template, - context=context, - filename=filename, - footer_template=footer_template, - cmd_options=cmd_options, - override_settings={ - 'STATIC_URL': 'file:///tmp/s/' - }) - tempfile = response.render_to_temporary_file(footer_template) - tempfile.seek(0) - footer_content = tempfile.read() - - static_url = 'STATIC_URL = {0}'.format('file:///tmp/s/') - self.assertTrue( - static_url in footer_content, - "{0!r} not in {1!r}".format(static_url, footer_content) - ) - self.assertEqual(settings.STATIC_URL, '/static/') + # Setup sample.html + template = 'sample.html' + context = {'title': 'Heading'} + request = RequestFactory().get('/') + response = PDFTemplateResponse(request=request, + template=template, + context=context) + self.assertEqual(response._request, request) + self.assertEqual(response.template_name, template) + self.assertEqual(response.context_data, context) + self.assertEqual(response.filename, None) + self.assertEqual(response.header_template, None) + self.assertEqual(response.footer_template, None) + self.assertEqual(response.cmd_options, {}) + self.assertFalse(response.has_header('Content-Disposition')) + + # Render to temporary file + tempfile = response.render_to_temporary_file(template) + tempfile.seek(0) + html_content = tempfile.read() + self.assertTrue(html_content.startswith('')) + self.assertTrue('

{title}

'.format(**context) + in html_content) + + pdf_content = response.rendered_content + self.assertTrue(pdf_content.startswith('%PDF-')) + self.assertTrue(pdf_content.endswith('%%EOF\n')) + + # Footer + filename = 'output.pdf' + footer_template = 'footer.html' + cmd_options = {'title': 'Test PDF'} + response = PDFTemplateResponse(request=request, + template=template, + context=context, + filename=filename, + footer_template=footer_template, + cmd_options=cmd_options) + self.assertEqual(response.filename, filename) + self.assertEqual(response.header_template, None) + self.assertEqual(response.footer_template, footer_template) + self.assertEqual(response.cmd_options, cmd_options) + self.assertTrue(response.has_header('Content-Disposition')) + + tempfile = response.render_to_temporary_file(footer_template) + tempfile.seek(0) + footer_content = tempfile.read() + footer_content = make_absolute_paths(footer_content) + + media_url = 'file://{0}/'.format(settings.MEDIA_ROOT) + self.assertTrue(media_url in footer_content, True) + + static_url = 'file://{0}/'.format(settings.STATIC_ROOT) + self.assertTrue(static_url in footer_content, True) + + pdf_content = response.rendered_content + self.assertTrue('\0'.join('{title}'.format(**cmd_options)) + in pdf_content) def test_pdf_template_view(self): """Test PDFTemplateView.""" - with override_settings( - MEDIA_URL='/media/', - STATIC_URL='/static/', - TEMPLATE_CONTEXT_PROCESSORS=[ - 'django.core.context_processors.media', - 'django.core.context_processors.static', - ], - TEMPLATE_LOADERS=['django.template.loaders.filesystem.Loader'], - TEMPLATE_DIRS=[os.path.join(os.path.dirname(__file__), - '_testproject', 'templates')], - WKHTMLTOPDF_DEBUG=False, - ): - # Setup sample.html - template = 'sample.html' - filename = 'output.pdf' - view = PDFTemplateView.as_view(filename=filename, - template_name=template, - footer_template='footer.html') - - # As PDF - request = RequestFactory().get('/') - response = view(request) - self.assertEqual(response.status_code, 200) - response.render() - self.assertEqual(response['Content-Disposition'], - 'attachment; filename="{0}"'.format(filename)) - self.assertTrue(response.content.startswith('%PDF-')) - self.assertTrue(response.content.endswith('%%EOF\n')) - - # As HTML - request = RequestFactory().get('/?as=html') - response = view(request) - self.assertEqual(response.status_code, 200) - response.render() - self.assertFalse(response.has_header('Content-Disposition')) - self.assertTrue(response.content.startswith('')) - - # POST - request = RequestFactory().post('/') - response = view(request) - self.assertEqual(response.status_code, 405) + + # Setup sample.html + template = 'sample.html' + filename = 'output.pdf' + view = PDFTemplateView.as_view(filename=filename, + template_name=template, + footer_template='footer.html') + + # As PDF + request = RequestFactory().get('/') + response = view(request) + self.assertEqual(response.status_code, 200) + response.render() + self.assertEqual(response['Content-Disposition'], + 'attachment; filename="{0}"'.format(filename)) + self.assertTrue(response.content.startswith('%PDF-')) + self.assertTrue(response.content.endswith('%%EOF\n')) + + # As HTML + request = RequestFactory().get('/?as=html') + response = view(request) + self.assertEqual(response.status_code, 200) + response.render() + self.assertFalse(response.has_header('Content-Disposition')) + self.assertTrue(response.content.startswith('')) + + # POST + request = RequestFactory().post('/') + response = view(request) + self.assertEqual(response.status_code, 405) + + def test_pdf_template_view_to_browser(self): + """Test PDFTemplateView as output to the browser.""" + + # Setup sample.html + template = 'sample.html' + filename = 'output.pdf' + view = PDFTemplateView.as_view(filename=filename, + show_content_in_browser=True, + template_name=template, + footer_template='footer.html') + + # As PDF + request = RequestFactory().get('/') + response = view(request) + self.assertEqual(response.status_code, 200) + response.render() + self.assertEqual(response['Content-Disposition'], + 'inline; filename="{0}"'.format(filename)) + self.assertTrue(response.content.startswith('%PDF-')) + self.assertTrue(response.content.endswith('%%EOF\n')) + + # As HTML + request = RequestFactory().get('/?as=html') + response = view(request) + self.assertEqual(response.status_code, 200) + response.render() + self.assertFalse(response.has_header('Content-Disposition')) + self.assertTrue(response.content.startswith('')) + + # POST + request = RequestFactory().post('/') + response = view(request) + self.assertEqual(response.status_code, 405) def test_pdf_template_view_unicode(self): """Test PDFTemplateView.""" - with override_settings( - MEDIA_URL='/media/', - STATIC_URL='/static/', - TEMPLATE_CONTEXT_PROCESSORS=[ - 'django.core.context_processors.media', - 'django.core.context_processors.static', - ], - TEMPLATE_LOADERS=['django.template.loaders.filesystem.Loader'], - TEMPLATE_DIRS=[os.path.join(os.path.dirname(__file__), - '_testproject', 'templates')], - WKHTMLTOPDF_DEBUG=False, - ): - # Setup sample.html - template = 'unicode.html' - filename = 'output.pdf' - view = PDFTemplateView.as_view(filename=filename, - template_name=template) - - # As PDF - request = RequestFactory().get('/') - response = view(request) - self.assertEqual(response.status_code, 200) - response.render() - self.assertEqual(response['Content-Disposition'], - 'attachment; filename="{0}"'.format(filename)) - # not sure how we can test this as the contents is all encoded... - # best we can do for the moment is check it's a pdf and it worked. - # self.assertTrue('☃' in response.content) - self.assertTrue(response.content.startswith('%PDF-')) - self.assertTrue(response.content.endswith('%%EOF\n')) + # Setup sample.html + template = 'unicode.html' + filename = 'output.pdf' + view = PDFTemplateView.as_view(filename=filename, + template_name=template) + + # As PDF + request = RequestFactory().get('/') + response = view(request) + self.assertEqual(response.status_code, 200) + response.render() + self.assertEqual(response['Content-Disposition'], + 'attachment; filename="{0}"'.format(filename)) + # not sure how we can test this as the contents is all encoded... + # best we can do for the moment is check it's a pdf and it worked. + # self.assertTrue('☃' in response.content) + self.assertTrue(response.content.startswith('%PDF-')) + self.assertTrue(response.content.endswith('%%EOF\n')) + + def test_pdf_template_view_unicode_to_browser(self): + """Test PDFTemplateView as output to the browser.""" + # Setup sample.html + template = 'unicode.html' + filename = 'output.pdf' + view = PDFTemplateView.as_view(filename=filename, + show_content_in_browser=True, + template_name=template) + + # As PDF + request = RequestFactory().get('/') + response = view(request) + self.assertEqual(response.status_code, 200) + response.render() + self.assertEqual(response['Content-Disposition'], + 'inline; filename="{0}"'.format(filename)) + self.assertTrue(response.content.startswith('%PDF-')) + self.assertTrue(response.content.endswith('%%EOF\n')) def test_get_cmd_options(self): # Default cmd_options -- cgit v1.2.3