diff options
author | aryan <aryan@Aryans-MacBook-Pro.local> | 2020-02-20 00:23:48 +0530 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-09-30 10:30:43 +0200 |
commit | 11c4a4412b74bb1dfe52d706a58f230066821c33 (patch) | |
tree | e97fad6c1f924d372f791e6637a435f439a1f146 /tests/file_uploads | |
parent | 21b127bfbc8738705ecee97407161caae612d6b1 (diff) | |
download | django-11c4a4412b74bb1dfe52d706a58f230066821c33.tar.gz |
Fixed #30422 -- Made TemporaryFileUploadHandler handle interrupted uploads.
This patch allows upload handlers to handle interrupted uploads.
Co-Authored-By: Mariusz Felisiak <felisiak.mariusz@gmail.com>
Diffstat (limited to 'tests/file_uploads')
-rw-r--r-- | tests/file_uploads/tests.py | 27 | ||||
-rw-r--r-- | tests/file_uploads/urls.py | 1 | ||||
-rw-r--r-- | tests/file_uploads/views.py | 10 |
3 files changed, 37 insertions, 1 deletions
diff --git a/tests/file_uploads/tests.py b/tests/file_uploads/tests.py index d8cd8425a4..2a1b2998e5 100644 --- a/tests/file_uploads/tests.py +++ b/tests/file_uploads/tests.py @@ -6,12 +6,13 @@ import sys import tempfile as sys_tempfile import unittest from io import BytesIO, StringIO +from unittest import mock from urllib.parse import quote from django.core.files import temp as tempfile from django.core.files.uploadedfile import SimpleUploadedFile from django.http.multipartparser import ( - MultiPartParser, MultiPartParserError, parse_header, + FILE, MultiPartParser, MultiPartParserError, Parser, parse_header, ) from django.test import SimpleTestCase, TestCase, client, override_settings @@ -443,6 +444,30 @@ class FileUploadTests(TestCase): temp_path = response.json()['temp_path'] self.assertIs(os.path.exists(temp_path), False) + def test_upload_interrupted_temporary_file_handler(self): + # Simulate an interrupted upload by omitting the closing boundary. + class MockedParser(Parser): + def __iter__(self): + for item in super().__iter__(): + item_type, meta_data, field_stream = item + yield item_type, meta_data, field_stream + if item_type == FILE: + return + + with tempfile.NamedTemporaryFile() as temp_file: + temp_file.write(b'a') + temp_file.seek(0) + with mock.patch( + 'django.http.multipartparser.Parser', + MockedParser, + ): + response = self.client.post( + '/temp_file/upload_interrupted/', + {'file': temp_file}, + ) + temp_path = response.json()['temp_path'] + self.assertIs(os.path.exists(temp_path), False) + def test_fileupload_getlist(self): file = tempfile.NamedTemporaryFile with file() as file1, file() as file2, file() as file2a: diff --git a/tests/file_uploads/urls.py b/tests/file_uploads/urls.py index 84bb452fc4..d171be2c76 100644 --- a/tests/file_uploads/urls.py +++ b/tests/file_uploads/urls.py @@ -14,6 +14,7 @@ urlpatterns = [ path('getlist_count/', views.file_upload_getlist_count), path('upload_errors/', views.file_upload_errors), path('temp_file/stop_upload/', views.file_stop_upload_temporary_file), + path('temp_file/upload_interrupted/', views.file_upload_interrupted_temporary_file), path('filename_case/', views.file_upload_filename_case_view), re_path(r'^fd_closing/(?P<access>t|f)/$', views.file_upload_fd_closing), ] diff --git a/tests/file_uploads/views.py b/tests/file_uploads/views.py index 04bb25e012..d521f001fe 100644 --- a/tests/file_uploads/views.py +++ b/tests/file_uploads/views.py @@ -2,6 +2,7 @@ import hashlib import os from django.core.files.uploadedfile import UploadedFile +from django.core.files.uploadhandler import TemporaryFileUploadHandler from django.http import HttpResponse, HttpResponseServerError, JsonResponse from .models import FileModel @@ -112,6 +113,15 @@ def file_stop_upload_temporary_file(request): ) +def file_upload_interrupted_temporary_file(request): + request.upload_handlers.insert(0, TemporaryFileUploadHandler()) + request.upload_handlers.pop(2) + request.FILES # Trigger file parsing. + return JsonResponse( + {'temp_path': request.upload_handlers[0].file.temporary_file_path()}, + ) + + def file_upload_getlist_count(request): """ Check the .getlist() function to ensure we receive the correct number of files. |