diff --git a/tests/file_uploads/tests.py b/tests/file_uploads/tests.py index d5377488ff..ecbee48160 100644 --- a/tests/file_uploads/tests.py +++ b/tests/file_uploads/tests.py @@ -25,7 +25,8 @@ from .models import FileModel UNICODE_FILENAME = "test-0123456789_中文_Orléans.jpg" MEDIA_ROOT = sys_tempfile.mkdtemp() -UPLOAD_TO = os.path.join(MEDIA_ROOT, "test_upload") +UPLOAD_FOLDER = "test_upload" +UPLOAD_TO = os.path.join(MEDIA_ROOT, UPLOAD_FOLDER) CANDIDATE_TRAVERSAL_FILE_NAMES = [ "/tmp/hax0rd.txt", # Absolute path, *nix-style. diff --git a/tests/file_uploads/uploadhandler.py b/tests/file_uploads/uploadhandler.py index eecbc6dc9b..a1e1a5af05 100644 --- a/tests/file_uploads/uploadhandler.py +++ b/tests/file_uploads/uploadhandler.py @@ -55,7 +55,7 @@ class TraversalUploadHandler(FileUploadHandler): """A handler with potential directory-traversal vulnerability.""" def __init__(self, request=None): - from .views import UPLOAD_TO + from .tests import UPLOAD_TO super().__init__(request) self.upload_dir = UPLOAD_TO diff --git a/tests/file_uploads/views.py b/tests/file_uploads/views.py index d5efbba3ce..c1d4ca5358 100644 --- a/tests/file_uploads/views.py +++ b/tests/file_uploads/views.py @@ -6,7 +6,7 @@ from django.core.files.uploadhandler import TemporaryFileUploadHandler from django.http import HttpResponse, HttpResponseServerError, JsonResponse from .models import FileModel -from .tests import UNICODE_FILENAME, UPLOAD_TO +from .tests import UNICODE_FILENAME, UPLOAD_FOLDER from .uploadhandler import ( ErroringUploadHandler, QuotaUploadHandler, @@ -68,9 +68,13 @@ def file_upload_unicode_name(request): # Check to make sure the exotic characters are preserved even # through file save. uni_named_file = request.FILES["file_unicode"] - FileModel.objects.create(testfile=uni_named_file) - full_name = "%s/%s" % (UPLOAD_TO, uni_named_file.name) - return HttpResponse() if os.path.exists(full_name) else HttpResponseServerError() + file_model = FileModel.objects.create(testfile=uni_named_file) + full_name = f"{UPLOAD_FOLDER}/{uni_named_file.name}" + return ( + HttpResponse() + if file_model.testfile.storage.exists(full_name) + else HttpResponseServerError() + ) def file_upload_echo(request): diff --git a/tests/model_fields/test_filefield.py b/tests/model_fields/test_filefield.py index e7c906112d..2259c1e480 100644 --- a/tests/model_fields/test_filefield.py +++ b/tests/model_fields/test_filefield.py @@ -120,9 +120,12 @@ class FileFieldTests(TestCase): with TemporaryUploadedFile( "foo.txt", "text/plain", 1, "utf-8" ) as tmp_file: - Document.objects.create(myfile=tmp_file) - self.assertTrue( - os.path.exists(os.path.join(tmp_dir, "unused", "foo.txt")) + document = Document.objects.create(myfile=tmp_file) + self.assertIs( + document.myfile.storage.exists( + os.path.join("unused", "foo.txt") + ), + True, ) def test_pickle(self):