Complete conversion of download views to CBV views using django-downloadview.
This also removes dependency on the filetransfers library.
This commit is contained in:
@@ -7,11 +7,11 @@ import time
|
||||
from json import loads
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.test import override_settings
|
||||
from django.utils.six import BytesIO
|
||||
|
||||
from django_downloadview import assert_download_response
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
@@ -21,7 +21,7 @@ from user_management.tests.literals import (
|
||||
|
||||
from .literals import (
|
||||
TEST_DOCUMENT_FILENAME, TEST_DOCUMENT_PATH, TEST_DOCUMENT_TYPE,
|
||||
TEST_SMALL_DOCUMENT_CHECKSUM, TEST_SMALL_DOCUMENT_PATH,
|
||||
TEST_SMALL_DOCUMENT_FILENAME, TEST_SMALL_DOCUMENT_PATH,
|
||||
)
|
||||
from ..models import Document, DocumentType, HASH_FUNCTION
|
||||
|
||||
@@ -263,14 +263,13 @@ class DocumentAPITestCase(APITestCase):
|
||||
'rest_api:document-download', args=(document.pk,)
|
||||
)
|
||||
)
|
||||
buf = BytesIO()
|
||||
buf.write(response.content)
|
||||
|
||||
self.assertEqual(
|
||||
HASH_FUNCTION(buf.getvalue()), TEST_SMALL_DOCUMENT_CHECKSUM
|
||||
)
|
||||
|
||||
del(buf)
|
||||
with document.open() as file_object:
|
||||
assert_download_response(
|
||||
self, response, content=file_object.read(),
|
||||
basename=TEST_SMALL_DOCUMENT_FILENAME,
|
||||
mime_type='{}; charset=utf-8'.format(document.file_mimetype)
|
||||
)
|
||||
|
||||
def test_document_version_download(self):
|
||||
with open(TEST_SMALL_DOCUMENT_PATH) as file_object:
|
||||
@@ -278,20 +277,22 @@ class DocumentAPITestCase(APITestCase):
|
||||
file_object=file_object,
|
||||
)
|
||||
|
||||
latest_version = document.latest_version
|
||||
response = self.client.get(
|
||||
reverse(
|
||||
'rest_api:documentversion-download',
|
||||
args=(document.latest_version.pk,)
|
||||
args=(latest_version.pk,)
|
||||
)
|
||||
)
|
||||
buf = BytesIO()
|
||||
buf.write(response.content)
|
||||
|
||||
self.assertEqual(
|
||||
HASH_FUNCTION(buf.getvalue()), TEST_SMALL_DOCUMENT_CHECKSUM
|
||||
)
|
||||
|
||||
del(buf)
|
||||
with latest_version.open() as file_object:
|
||||
assert_download_response(
|
||||
self, response, content=file_object.read(),
|
||||
basename='{} - {}'.format(
|
||||
TEST_SMALL_DOCUMENT_FILENAME,
|
||||
latest_version.timestamp
|
||||
), mime_type='application/octet-stream; charset=utf-8'
|
||||
)
|
||||
|
||||
# TODO: def test_document_set_document_type(self):
|
||||
# pass
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from actstream.models import Action
|
||||
from django_downloadview import assert_download_response
|
||||
|
||||
from common.tests import skip_file_descriptor_check
|
||||
from user_management.tests.literals import (
|
||||
TEST_USER_PASSWORD, TEST_USER_USERNAME
|
||||
)
|
||||
@@ -14,7 +14,6 @@ from ..permissions import (
|
||||
permission_document_download, permission_document_view
|
||||
)
|
||||
|
||||
|
||||
from .test_views import GenericDocumentViewTestCase
|
||||
|
||||
|
||||
@@ -32,17 +31,14 @@ class DocumentEventsTestCase(GenericDocumentViewTestCase):
|
||||
|
||||
Action.objects.all().delete()
|
||||
|
||||
response = self.post(
|
||||
response = self.get(
|
||||
'documents:document_download', args=(self.document.pk,)
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assertEqual(response.status_code, 403)
|
||||
self.assertEqual(list(Action.objects.any(obj=self.document)), [])
|
||||
|
||||
@skip_file_descriptor_check
|
||||
def test_document_download_event_with_permissions(self):
|
||||
# TODO: Skip this test's file descriptor check until it gets migrate
|
||||
# SingleObjectDownloadView CBV
|
||||
self.login(
|
||||
username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD
|
||||
)
|
||||
@@ -53,12 +49,19 @@ class DocumentEventsTestCase(GenericDocumentViewTestCase):
|
||||
permission_document_download.stored_permission
|
||||
)
|
||||
|
||||
self.expected_content_type = 'image/png'
|
||||
self.expected_content_type = 'image/png; charset=utf-8'
|
||||
|
||||
self.post(
|
||||
response = self.get(
|
||||
'documents:document_download', args=(self.document.pk,),
|
||||
)
|
||||
|
||||
# Download the file to close the file descriptor
|
||||
with self.document.open() as file_object:
|
||||
assert_download_response(
|
||||
self, response, content=file_object.read(),
|
||||
mime_type=self.document.file_mimetype
|
||||
)
|
||||
|
||||
event = Action.objects.any(obj=self.document).first()
|
||||
|
||||
self.assertEqual(event.verb, event_document_download.name)
|
||||
|
||||
@@ -94,7 +94,7 @@ class DocumentsLinksTestCase(GenericDocumentViewTestCase):
|
||||
self.assertEqual(
|
||||
resolved_link.url,
|
||||
reverse(
|
||||
'documents:document_version_download',
|
||||
'documents:document_version_download_form',
|
||||
args=(self.document.latest_version.pk,)
|
||||
)
|
||||
)
|
||||
|
||||
@@ -4,7 +4,7 @@ from datetime import timedelta
|
||||
import time
|
||||
|
||||
from common.tests import BaseTestCase
|
||||
from django.test import TestCase, override_settings
|
||||
from django.test import override_settings
|
||||
|
||||
from ..exceptions import NewDocumentVersionNotAllowed
|
||||
from ..literals import STUB_EXPIRATION_INTERVAL
|
||||
|
||||
@@ -4,9 +4,9 @@ from __future__ import unicode_literals
|
||||
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.test import override_settings
|
||||
from django.utils.six import BytesIO
|
||||
|
||||
from common.tests import skip_file_descriptor_check
|
||||
from django_downloadview import assert_download_response
|
||||
|
||||
from common.tests.test_views import GenericViewTestCase
|
||||
from converter.models import Transformation
|
||||
from converter.permissions import permission_transformation_delete
|
||||
@@ -15,9 +15,7 @@ from user_management.tests.literals import (
|
||||
)
|
||||
|
||||
from ..literals import DEFAULT_DELETE_PERIOD, DEFAULT_DELETE_TIME_UNIT
|
||||
from ..models import (
|
||||
DeletedDocument, Document, DocumentType, HASH_FUNCTION
|
||||
)
|
||||
from ..models import DeletedDocument, Document, DocumentType
|
||||
from ..permissions import (
|
||||
permission_document_create, permission_document_delete,
|
||||
permission_document_download, permission_document_properties_edit,
|
||||
@@ -30,7 +28,7 @@ from ..permissions import (
|
||||
|
||||
from .literals import (
|
||||
TEST_DOCUMENT_TYPE, TEST_DOCUMENT_TYPE_QUICK_LABEL,
|
||||
TEST_SMALL_DOCUMENT_CHECKSUM, TEST_SMALL_DOCUMENT_PATH
|
||||
TEST_SMALL_DOCUMENT_FILENAME, TEST_SMALL_DOCUMENT_PATH
|
||||
)
|
||||
|
||||
|
||||
@@ -226,113 +224,113 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase):
|
||||
Document.objects.first().document_type, document_type
|
||||
)
|
||||
|
||||
@skip_file_descriptor_check
|
||||
def test_document_download_user_view(self):
|
||||
# TODO: Skip this test's file descriptor check until it gets migrate
|
||||
# SingleObjectDownloadView CBV
|
||||
|
||||
def test_document_download_view_no_permission(self):
|
||||
self.login(
|
||||
username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD
|
||||
)
|
||||
|
||||
self.assertEqual(Document.objects.count(), 1)
|
||||
|
||||
response = self.post(
|
||||
response = self.get(
|
||||
'documents:document_download', args=(self.document.pk,)
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assertEqual(response.status_code, 403)
|
||||
|
||||
def test_document_download_view_with_permission(self):
|
||||
self.login(
|
||||
username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD
|
||||
)
|
||||
|
||||
self.role.permissions.add(
|
||||
permission_document_download.stored_permission
|
||||
)
|
||||
|
||||
# Set the expected_content_type for common.tests.mixins.ContentTypeCheckMixin
|
||||
self.expected_content_type = self.document.file_mimetype
|
||||
# Set the expected_content_type for
|
||||
# common.tests.mixins.ContentTypeCheckMixin
|
||||
self.expected_content_type = '{}; charset=utf-8'.format(
|
||||
self.document.file_mimetype
|
||||
)
|
||||
|
||||
response = self.post(
|
||||
response = self.get(
|
||||
'documents:document_download', args=(self.document.pk,)
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
buf = BytesIO()
|
||||
buf.write(response.content)
|
||||
|
||||
self.assertEqual(
|
||||
HASH_FUNCTION(buf.getvalue()), TEST_SMALL_DOCUMENT_CHECKSUM
|
||||
)
|
||||
|
||||
del(buf)
|
||||
|
||||
@skip_file_descriptor_check
|
||||
def test_document_multiple_download_user_view(self):
|
||||
# TODO: Skip this test's file descriptor check until it gets migrate
|
||||
# SingleObjectDownloadView CBV
|
||||
with self.document.open() as file_object:
|
||||
assert_download_response(
|
||||
self, response, content=file_object.read(),
|
||||
basename=TEST_SMALL_DOCUMENT_FILENAME,
|
||||
mime_type=self.document.file_mimetype
|
||||
)
|
||||
|
||||
def test_document_multiple_download_view_no_permission(self):
|
||||
self.login(
|
||||
username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD
|
||||
)
|
||||
|
||||
self.assertEqual(Document.objects.count(), 1)
|
||||
|
||||
response = self.post(
|
||||
response = self.get(
|
||||
'documents:document_multiple_download',
|
||||
data={'id_list': self.document.pk}
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assertEqual(response.status_code, 403)
|
||||
|
||||
def test_document_multiple_download_view_with_permission(self):
|
||||
self.login(
|
||||
username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD
|
||||
)
|
||||
|
||||
self.role.permissions.add(
|
||||
permission_document_download.stored_permission
|
||||
)
|
||||
|
||||
# Set the expected_content_type for common.tests.mixins.ContentTypeCheckMixin
|
||||
self.expected_content_type = self.document.file_mimetype
|
||||
# Set the expected_content_type for
|
||||
# common.tests.mixins.ContentTypeCheckMixin
|
||||
self.expected_content_type = '{}; charset=utf-8'.format(
|
||||
self.document.file_mimetype
|
||||
)
|
||||
|
||||
response = self.post(
|
||||
response = self.get(
|
||||
'documents:document_multiple_download',
|
||||
data={'id_list': self.document.pk}
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
buf = BytesIO()
|
||||
buf.write(response.content)
|
||||
|
||||
self.assertEqual(
|
||||
HASH_FUNCTION(buf.getvalue()), TEST_SMALL_DOCUMENT_CHECKSUM
|
||||
)
|
||||
|
||||
del(buf)
|
||||
|
||||
@skip_file_descriptor_check
|
||||
def test_document_version_download_user_view(self):
|
||||
# TODO: Skip this test's file descriptor check until it gets migrate
|
||||
# SingleObjectDownloadView CBV
|
||||
with self.document.open() as file_object:
|
||||
assert_download_response(
|
||||
self, response, content=file_object.read(),
|
||||
basename=TEST_SMALL_DOCUMENT_FILENAME,
|
||||
mime_type=self.document.file_mimetype
|
||||
)
|
||||
|
||||
def test_document_version_download_view_no_permission(self):
|
||||
self.login(
|
||||
username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD
|
||||
)
|
||||
|
||||
self.assertEqual(Document.objects.count(), 1)
|
||||
|
||||
response = self.post(
|
||||
response = self.get(
|
||||
'documents:document_version_download', args=(
|
||||
self.document.latest_version.pk,
|
||||
)
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assertEqual(response.status_code, 403)
|
||||
|
||||
def test_document_version_download_view_with_permission(self):
|
||||
self.login(
|
||||
username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD
|
||||
)
|
||||
|
||||
self.role.permissions.add(
|
||||
permission_document_download.stored_permission
|
||||
)
|
||||
|
||||
# Set the expected_content_type for common.tests.mixins.ContentTypeCheckMixin
|
||||
self.expected_content_type = self.document.file_mimetype
|
||||
# Set the expected_content_type for
|
||||
# common.tests.mixins.ContentTypeCheckMixin
|
||||
self.expected_content_type = 'application/octet-stream; charset=utf-8'
|
||||
|
||||
response = self.post(
|
||||
response = self.get(
|
||||
'documents:document_version_download', args=(
|
||||
self.document.latest_version.pk,
|
||||
)
|
||||
@@ -340,14 +338,14 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase):
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
buf = BytesIO()
|
||||
buf.write(response.content)
|
||||
|
||||
self.assertEqual(
|
||||
HASH_FUNCTION(buf.getvalue()), TEST_SMALL_DOCUMENT_CHECKSUM
|
||||
)
|
||||
|
||||
del(buf)
|
||||
with self.document.open() as file_object:
|
||||
assert_download_response(
|
||||
self, response, content=file_object.read(),
|
||||
basename='{} - {}'.format(
|
||||
TEST_SMALL_DOCUMENT_FILENAME,
|
||||
self.document.latest_version.timestamp
|
||||
), mime_type='application/octet-stream; charset=utf-8'
|
||||
)
|
||||
|
||||
def test_document_update_page_count_view_no_permission(self):
|
||||
self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD)
|
||||
@@ -380,7 +378,6 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase):
|
||||
self.assertContains(response, text='queued', status_code=200)
|
||||
self.assertEqual(self.document.pages.count(), page_count)
|
||||
|
||||
|
||||
def test_document_multiple_update_page_count_view_no_permission(self):
|
||||
self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user