77import json
88import os
99import shutil
10+ import tempfile as sys_tempfile
1011
1112from django .core .files import temp as tempfile
1213from django .core .files .uploadedfile import SimpleUploadedFile
1314from django .http .multipartparser import MultiPartParser
1415from django .test import TestCase , client
16+ from django .test .utils import override_settings
1517from django .utils .encoding import force_bytes
1618from django .utils .six import StringIO
1719from django .utils import unittest
1820
1921from . import uploadhandler
20- from .models import FileModel , temp_storage , UPLOAD_TO
22+ from .models import FileModel
2123
2224
2325UNICODE_FILENAME = 'test-0123456789_中文_Orléans.jpg'
26+ MEDIA_ROOT = sys_tempfile .mkdtemp ()
27+ UPLOAD_TO = os .path .join (MEDIA_ROOT , 'test_upload' )
2428
29+ @override_settings (MEDIA_ROOT = MEDIA_ROOT )
2530class FileUploadTests (TestCase ):
31+ @classmethod
32+ def setUpClass (cls ):
33+ if not os .path .isdir (MEDIA_ROOT ):
34+ os .makedirs (MEDIA_ROOT )
35+
36+ @classmethod
37+ def tearDownClass (cls ):
38+ shutil .rmtree (MEDIA_ROOT )
39+
2640 def test_simple_upload (self ):
2741 with open (__file__ , 'rb' ) as fp :
2842 post_data = {
@@ -83,7 +97,8 @@ def test_base64_upload(self):
8397 self .assertEqual (received ['file' ], test_string )
8498
8599 def test_unicode_file_name (self ):
86- tdir = tempfile .gettempdir ()
100+ tdir = sys_tempfile .mkdtemp ()
101+ self .addCleanup (shutil .rmtree , tdir , True )
87102
88103 # This file contains chinese symbols and an accented char in the name.
89104 with open (os .path .join (tdir , UNICODE_FILENAME ), 'w+b' ) as file1 :
@@ -96,11 +111,6 @@ def test_unicode_file_name(self):
96111
97112 response = self .client .post ('/file_uploads/unicode_name/' , post_data )
98113
99- try :
100- os .unlink (file1 .name )
101- except OSError :
102- pass
103-
104114 self .assertEqual (response .status_code , 200 )
105115
106116 def test_dangerous_file_names (self ):
@@ -347,26 +357,28 @@ def test_filename_case_preservation(self):
347357 # shouldn't differ.
348358 self .assertEqual (os .path .basename (obj .testfile .path ), 'MiXeD_cAsE.txt' )
349359
350- class DirectoryCreationTests (unittest .TestCase ):
360+ @override_settings (MEDIA_ROOT = MEDIA_ROOT )
361+ class DirectoryCreationTests (TestCase ):
351362 """
352363 Tests for error handling during directory creation
353364 via _save_FIELD_file (ticket #6450)
354365 """
366+ @classmethod
367+ def setUpClass (cls ):
368+ if not os .path .isdir (MEDIA_ROOT ):
369+ os .makedirs (MEDIA_ROOT )
370+
371+ @classmethod
372+ def tearDownClass (cls ):
373+ shutil .rmtree (MEDIA_ROOT )
374+
355375 def setUp (self ):
356376 self .obj = FileModel ()
357- if not os .path .isdir (temp_storage .location ):
358- os .makedirs (temp_storage .location )
359- if os .path .isdir (UPLOAD_TO ):
360- os .chmod (UPLOAD_TO , 0o700 )
361- shutil .rmtree (UPLOAD_TO )
362-
363- def tearDown (self ):
364- os .chmod (temp_storage .location , 0o700 )
365- shutil .rmtree (temp_storage .location )
366377
367378 def test_readonly_root (self ):
368379 """Permission errors are not swallowed"""
369- os .chmod (temp_storage .location , 0o500 )
380+ os .chmod (MEDIA_ROOT , 0o500 )
381+ self .addCleanup (os .chmod , MEDIA_ROOT , 0o700 )
370382 try :
371383 self .obj .testfile .save ('foo.txt' , SimpleUploadedFile ('foo.txt' , b'x' ))
372384 except OSError as err :
@@ -378,6 +390,7 @@ def test_not_a_directory(self):
378390 """The correct IOError is raised when the upload directory name exists but isn't a directory"""
379391 # Create a file with the upload directory name
380392 open (UPLOAD_TO , 'wb' ).close ()
393+ self .addCleanup (os .remove , UPLOAD_TO )
381394 with self .assertRaises (IOError ) as exc_info :
382395 self .obj .testfile .save ('foo.txt' , SimpleUploadedFile ('foo.txt' , b'x' ))
383396 # The test needs to be done on a specific string as IOError
0 commit comments