Commit 14de8b7c authored by Diederik van der Boor's avatar Diederik van der Boor
Browse files

Added tests for the views

parent 9b1a73f2
import os
import shutil
from django.conf import settings
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase
from private_storage.tests.models import CustomerDossier, SimpleDossier, UploadToDossier
class PrivateFileTestCase(TestCase):
def tearDown(self):
"""
Empty the test folder after each test case.
"""
super(PrivateFileTestCase, self).tearDown()
shutil.rmtree(settings.PRIVATE_STORAGE_ROOT)
def assertExists(self, *parts):
"""
Extra assert, check whether a path exists.
"""
path = os.path.join(settings.PRIVATE_STORAGE_ROOT, *parts)
if not os.path.exists(path):
raise self.failureException("Path {} does not exist".format(path))
from private_storage.tests.utils import PrivateFileTestCase
class ModelTests(PrivateFileTestCase):
......
from django.contrib.auth.models import User
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import RequestFactory
from private_storage.tests.models import CustomerDossier
from private_storage.tests.utils import PrivateFileTestCase
from private_storage.views import PrivateStorageDetailView, PrivateStorageView
class ViewTests(PrivateFileTestCase):
def test_detail_view(self):
"""
Test the detail view that returns the object
"""
CustomerDossier.objects.create(
customer='cust1',
file=SimpleUploadedFile('test4.txt', b'test4')
)
request = RequestFactory().get('/cust1/file/')
request.user = User.objects.create_superuser('admin', 'admin@example.com', 'admin')
# Initialize locally, no need for urls.py etc..
# This behaves like a standard DetailView
view = PrivateStorageDetailView.as_view(
model=CustomerDossier,
slug_url_kwarg='customer',
slug_field='customer',
model_file_field='file'
)
response = view(
request,
customer='cust1',
)
self.assertEqual(list(response.streaming_content), [b'test4'])
self.assertEqual(response['Content-Type'], 'text/plain')
self.assertEqual(response['Content-Length'], '5')
self.assertIn('Last-Modified', response)
def test_private_file_view(self):
"""
Test the detail view that returns the object
"""
obj = CustomerDossier.objects.create(
customer='cust2',
file=SimpleUploadedFile('test5.txt', b'test5')
)
self.assertExists('dossier3', 'cust2', 'test5.txt')
request = RequestFactory().get('/cust1/file/')
request.user = User.objects.create_superuser('admin', 'admin@example.com', 'admin')
request.META['HTTP_USER_AGENT'] = 'Test'
# Initialize locally, no need for urls.py etc..
# This behaves like a standard DetailView
view = PrivateStorageView.as_view(
content_disposition='attachment',
)
response = view(
request,
path='dossier3/cust2/test5.txt'
)
self.assertEqual(list(response.streaming_content), [b'test5'])
self.assertEqual(response['Content-Type'], 'text/plain')
self.assertEqual(response['Content-Length'], '5')
self.assertEqual(response['Content-Disposition'], "attachment; filename*=UTF-8''test5.txt")
self.assertIn('Last-Modified', response)
import os
import shutil
from django.conf import settings
from django.test import TestCase
class PrivateFileTestCase(TestCase):
def tearDown(self):
"""
Empty the test folder after each test case.
"""
super(PrivateFileTestCase, self).tearDown()
shutil.rmtree(settings.PRIVATE_STORAGE_ROOT)
def assertExists(self, *parts):
"""
Extra assert, check whether a path exists.
"""
path = os.path.join(settings.PRIVATE_STORAGE_ROOT, *parts)
if not os.path.exists(path):
raise self.failureException("Path {} does not exist".format(path))
......@@ -19,8 +19,6 @@ except ImportError:
from urllib import quote # Python 2
class PrivateStorageView(View):
"""
Return the uploaded files
......@@ -102,11 +100,12 @@ class PrivateStorageView(View):
The filename, encoded to use in a ``Content-Disposition`` header.
"""
# Based on https://www.djangosnippets.org/snippets/1710/
if 'WebKit' in self.request.META['HTTP_USER_AGENT']:
user_agent = self.request.META.get('HTTP_USER_AGENT', None)
if 'WebKit' in user_agent:
# Support available for UTF-8 encoded strings.
utf8_filename = filename.encode("utf-8")
return 'filename={}'.format(utf8_filename)
elif 'MSIE' in self.request.META['HTTP_USER_AGENT']:
elif 'MSIE' in user_agent:
# IE does not support internationalized filename at all.
# It can only recognize internationalized URL, so we should perform a trick via URL names.
return ''
......
......@@ -16,6 +16,8 @@ if not settings.configured:
}
},
INSTALLED_APPS=(
'django.contrib.auth',
'django.contrib.contenttypes',
'private_storage',
),
TEST_RUNNER='django.test.runner.DiscoverRunner',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment