summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrancesco Panico <panico.francesco@gmail.com>2022-12-30 12:47:59 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-12-30 13:28:47 +0100
commitc179ad9fe7e82dcb80261aa016f2fe18c8fcc181 (patch)
treea8b2b0faa53ee7e84fe1d7aacd3bbd3dec147bb3
parent6e9e7ec472bdc870152e1f4f4130ecc07cdfc820 (diff)
Refs #34100 -- Made file upload tests use Storage.exists() where appropriate.
-rw-r--r--tests/file_uploads/tests.py3
-rw-r--r--tests/file_uploads/uploadhandler.py2
-rw-r--r--tests/file_uploads/views.py12
-rw-r--r--tests/model_fields/test_filefield.py9
4 files changed, 17 insertions, 9 deletions
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):