diff --git a/tests/file_uploads/tests.py b/tests/file_uploads/tests.py index df8f1b8031..d8cd8425a4 100644 --- a/tests/file_uploads/tests.py +++ b/tests/file_uploads/tests.py @@ -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: diff --git a/tests/file_uploads/uploadhandler.py b/tests/file_uploads/uploadhandler.py index 7c6199fd16..9ad335301f 100644 --- a/tests/file_uploads/uploadhandler.py +++ b/tests/file_uploads/uploadhandler.py @@ -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 diff --git a/tests/file_uploads/urls.py b/tests/file_uploads/urls.py index 3e7985d2f9..84bb452fc4 100644 --- a/tests/file_uploads/urls.py +++ b/tests/file_uploads/urls.py @@ -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/(?Pt|f)/$', views.file_upload_fd_closing), ] diff --git a/tests/file_uploads/views.py b/tests/file_uploads/views.py index ff381c0a05..04bb25e012 100644 --- a/tests/file_uploads/views.py +++ b/tests/file_uploads/views.py @@ -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.