Skip to content

Commit 21b127b

Browse files
aryanfelixxm
aryan
authored andcommitted
Refs #30422 -- Added test for removing temporary files in MultiPartParser when StopUpload is raised.
1 parent 5fcfe53 commit 21b127b

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

tests/file_uploads/tests.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,14 @@ def test_broken_custom_upload_handler(self):
435435
with self.assertRaisesMessage(AttributeError, msg):
436436
self.client.post('/quota/broken/', {'f': file})
437437

438+
def test_stop_upload_temporary_file_handler(self):
439+
with tempfile.NamedTemporaryFile() as temp_file:
440+
temp_file.write(b'a')
441+
temp_file.seek(0)
442+
response = self.client.post('/temp_file/stop_upload/', {'file': temp_file})
443+
temp_path = response.json()['temp_path']
444+
self.assertIs(os.path.exists(temp_path), False)
445+
438446
def test_fileupload_getlist(self):
439447
file = tempfile.NamedTemporaryFile
440448
with file() as file1, file() as file2, file() as file2a:

tests/file_uploads/uploadhandler.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
Upload handlers to test the upload API.
33
"""
44

5-
from django.core.files.uploadhandler import FileUploadHandler, StopUpload
5+
from django.core.files.uploadhandler import (
6+
FileUploadHandler, StopUpload, TemporaryFileUploadHandler,
7+
)
68

79

810
class QuotaUploadHandler(FileUploadHandler):
@@ -27,6 +29,12 @@ def file_complete(self, file_size):
2729
return None
2830

2931

32+
class StopUploadTemporaryFileHandler(TemporaryFileUploadHandler):
33+
"""A handler that raises a StopUpload exception."""
34+
def receive_data_chunk(self, raw_data, start):
35+
raise StopUpload()
36+
37+
3038
class CustomUploadError(Exception):
3139
pass
3240

tests/file_uploads/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
path('quota/broken/', views.file_upload_quota_broken),
1414
path('getlist_count/', views.file_upload_getlist_count),
1515
path('upload_errors/', views.file_upload_errors),
16+
path('temp_file/stop_upload/', views.file_stop_upload_temporary_file),
1617
path('filename_case/', views.file_upload_filename_case_view),
1718
re_path(r'^fd_closing/(?P<access>t|f)/$', views.file_upload_fd_closing),
1819
]

tests/file_uploads/views.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
from .models import FileModel
88
from .tests import UNICODE_FILENAME, UPLOAD_TO
9-
from .uploadhandler import ErroringUploadHandler, QuotaUploadHandler
9+
from .uploadhandler import (
10+
ErroringUploadHandler, QuotaUploadHandler, StopUploadTemporaryFileHandler,
11+
)
1012

1113

1214
def file_upload_view(request):
@@ -101,6 +103,15 @@ def file_upload_quota_broken(request):
101103
return response
102104

103105

106+
def file_stop_upload_temporary_file(request):
107+
request.upload_handlers.insert(0, StopUploadTemporaryFileHandler())
108+
request.upload_handlers.pop(2)
109+
request.FILES # Trigger file parsing.
110+
return JsonResponse(
111+
{'temp_path': request.upload_handlers[0].file.temporary_file_path()},
112+
)
113+
114+
104115
def file_upload_getlist_count(request):
105116
"""
106117
Check the .getlist() function to ensure we receive the correct number of files.

0 commit comments

Comments
 (0)