1
0
mirror of https://github.com/django/django.git synced 2024-12-22 09:05:43 +00:00

Refs #34100 -- Made file upload tests use Storage.exists() where appropriate.

This commit is contained in:
Francesco Panico 2022-12-30 12:47:59 +01:00 committed by Mariusz Felisiak
parent 6e9e7ec472
commit c179ad9fe7
4 changed files with 17 additions and 9 deletions

View File

@ -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.

View File

@ -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

View File

@ -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):

View File

@ -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):