Refs #30422 -- Added test for removing temporary files in MultiPartParser when StopUpload is raised.

This commit is contained in:
aryan 2020-09-28 10:09:29 +02:00 committed by Mariusz Felisiak
parent 5fcfe5361e
commit 21b127bfbc
4 changed files with 30 additions and 2 deletions

View File

@ -435,6 +435,14 @@ class FileUploadTests(TestCase):
with self.assertRaisesMessage(AttributeError, msg):
self.client.post('/quota/broken/', {'f': file})
def test_stop_upload_temporary_file_handler(self):
with tempfile.NamedTemporaryFile() as temp_file:
temp_file.write(b'a')
temp_file.seek(0)
response = self.client.post('/temp_file/stop_upload/', {'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:

View File

@ -2,7 +2,9 @@
Upload handlers to test the upload API.
"""
from django.core.files.uploadhandler import FileUploadHandler, StopUpload
from django.core.files.uploadhandler import (
FileUploadHandler, StopUpload, TemporaryFileUploadHandler,
)
class QuotaUploadHandler(FileUploadHandler):
@ -27,6 +29,12 @@ class QuotaUploadHandler(FileUploadHandler):
return None
class StopUploadTemporaryFileHandler(TemporaryFileUploadHandler):
"""A handler that raises a StopUpload exception."""
def receive_data_chunk(self, raw_data, start):
raise StopUpload()
class CustomUploadError(Exception):
pass

View File

@ -13,6 +13,7 @@ urlpatterns = [
path('quota/broken/', views.file_upload_quota_broken),
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('filename_case/', views.file_upload_filename_case_view),
re_path(r'^fd_closing/(?P<access>t|f)/$', views.file_upload_fd_closing),
]

View File

@ -6,7 +6,9 @@ from django.http import HttpResponse, HttpResponseServerError, JsonResponse
from .models import FileModel
from .tests import UNICODE_FILENAME, UPLOAD_TO
from .uploadhandler import ErroringUploadHandler, QuotaUploadHandler
from .uploadhandler import (
ErroringUploadHandler, QuotaUploadHandler, StopUploadTemporaryFileHandler,
)
def file_upload_view(request):
@ -101,6 +103,15 @@ def file_upload_quota_broken(request):
return response
def file_stop_upload_temporary_file(request):
request.upload_handlers.insert(0, StopUploadTemporaryFileHandler())
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.