tests.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import errno
  4. import os
  5. import shutil
  6. import sys
  7. import tempfile
  8. import time
  9. import unittest
  10. from datetime import datetime, timedelta
  11. try:
  12. import threading
  13. except ImportError:
  14. import dummy_threading as threading
  15. from django.core.cache import cache
  16. from django.core.exceptions import SuspiciousOperation
  17. from django.core.files.base import File, ContentFile
  18. from django.core.files.storage import FileSystemStorage, get_storage_class
  19. from django.core.files.uploadedfile import (InMemoryUploadedFile, SimpleUploadedFile,
  20. TemporaryUploadedFile)
  21. from django.test import LiveServerTestCase, SimpleTestCase
  22. from django.test import override_settings
  23. from django.utils import six
  24. from django.utils.six.moves.urllib.request import urlopen
  25. from django.utils._os import upath
  26. from .models import Storage, temp_storage, temp_storage_location
  27. FILE_SUFFIX_REGEX = '[A-Za-z0-9]{7}'
  28. class GetStorageClassTests(SimpleTestCase):
  29. def test_get_filesystem_storage(self):
  30. """
  31. get_storage_class returns the class for a storage backend name/path.
  32. """
  33. self.assertEqual(
  34. get_storage_class('django.core.files.storage.FileSystemStorage'),
  35. FileSystemStorage)
  36. def test_get_invalid_storage_module(self):
  37. """
  38. get_storage_class raises an error if the requested import don't exist.
  39. """
  40. with six.assertRaisesRegex(self, ImportError, "No module named '?storage'?"):
  41. get_storage_class('storage.NonExistingStorage')
  42. def test_get_nonexisting_storage_class(self):
  43. """
  44. get_storage_class raises an error if the requested class don't exist.
  45. """
  46. self.assertRaises(ImportError, get_storage_class,
  47. 'django.core.files.storage.NonExistingStorage')
  48. def test_get_nonexisting_storage_module(self):
  49. """
  50. get_storage_class raises an error if the requested module don't exist.
  51. """
  52. # Error message may or may not be the fully qualified path.
  53. with six.assertRaisesRegex(self, ImportError,
  54. "No module named '?(django.core.files.)?non_existing_storage'?"):
  55. get_storage_class(
  56. 'django.core.files.non_existing_storage.NonExistingStorage')
  57. class FileStorageDeconstructionTests(unittest.TestCase):
  58. def test_deconstruction(self):
  59. path, args, kwargs = temp_storage.deconstruct()
  60. self.assertEqual(path, "django.core.files.storage.FileSystemStorage")
  61. self.assertEqual(args, tuple())
  62. self.assertEqual(kwargs, {'location': temp_storage_location})
  63. kwargs_orig = {
  64. 'location': temp_storage_location,
  65. 'base_url': 'http://myfiles.example.com/'
  66. }
  67. storage = FileSystemStorage(**kwargs_orig)
  68. path, args, kwargs = storage.deconstruct()
  69. self.assertEqual(kwargs, kwargs_orig)
  70. class FileStorageTests(unittest.TestCase):
  71. storage_class = FileSystemStorage
  72. def setUp(self):
  73. self.temp_dir = tempfile.mkdtemp()
  74. self.storage = self.storage_class(location=self.temp_dir,
  75. base_url='/test_media_url/')
  76. # Set up a second temporary directory which is ensured to have a mixed
  77. # case name.
  78. self.temp_dir2 = tempfile.mkdtemp(suffix='aBc')
  79. def tearDown(self):
  80. shutil.rmtree(self.temp_dir)
  81. shutil.rmtree(self.temp_dir2)
  82. def test_empty_location(self):
  83. """
  84. Makes sure an exception is raised if the location is empty
  85. """
  86. storage = self.storage_class(location='')
  87. self.assertEqual(storage.base_location, '')
  88. self.assertEqual(storage.location, upath(os.getcwd()))
  89. def test_file_access_options(self):
  90. """
  91. Standard file access options are available, and work as expected.
  92. """
  93. self.assertFalse(self.storage.exists('storage_test'))
  94. f = self.storage.open('storage_test', 'w')
  95. f.write('storage contents')
  96. f.close()
  97. self.assertTrue(self.storage.exists('storage_test'))
  98. f = self.storage.open('storage_test', 'r')
  99. self.assertEqual(f.read(), 'storage contents')
  100. f.close()
  101. self.storage.delete('storage_test')
  102. self.assertFalse(self.storage.exists('storage_test'))
  103. def test_file_accessed_time(self):
  104. """
  105. File storage returns a Datetime object for the last accessed time of
  106. a file.
  107. """
  108. self.assertFalse(self.storage.exists('test.file'))
  109. f = ContentFile('custom contents')
  110. f_name = self.storage.save('test.file', f)
  111. atime = self.storage.accessed_time(f_name)
  112. self.assertEqual(atime, datetime.fromtimestamp(
  113. os.path.getatime(self.storage.path(f_name))))
  114. self.assertTrue(datetime.now() - self.storage.accessed_time(f_name) < timedelta(seconds=2))
  115. self.storage.delete(f_name)
  116. def test_file_created_time(self):
  117. """
  118. File storage returns a Datetime object for the creation time of
  119. a file.
  120. """
  121. self.assertFalse(self.storage.exists('test.file'))
  122. f = ContentFile('custom contents')
  123. f_name = self.storage.save('test.file', f)
  124. ctime = self.storage.created_time(f_name)
  125. self.assertEqual(ctime, datetime.fromtimestamp(
  126. os.path.getctime(self.storage.path(f_name))))
  127. self.assertTrue(datetime.now() - self.storage.created_time(f_name) < timedelta(seconds=2))
  128. self.storage.delete(f_name)
  129. def test_file_modified_time(self):
  130. """
  131. File storage returns a Datetime object for the last modified time of
  132. a file.
  133. """
  134. self.assertFalse(self.storage.exists('test.file'))
  135. f = ContentFile('custom contents')
  136. f_name = self.storage.save('test.file', f)
  137. mtime = self.storage.modified_time(f_name)
  138. self.assertEqual(mtime, datetime.fromtimestamp(
  139. os.path.getmtime(self.storage.path(f_name))))
  140. self.assertTrue(datetime.now() - self.storage.modified_time(f_name) < timedelta(seconds=2))
  141. self.storage.delete(f_name)
  142. def test_file_save_without_name(self):
  143. """
  144. File storage extracts the filename from the content object if no
  145. name is given explicitly.
  146. """
  147. self.assertFalse(self.storage.exists('test.file'))
  148. f = ContentFile('custom contents')
  149. f.name = 'test.file'
  150. storage_f_name = self.storage.save(None, f)
  151. self.assertEqual(storage_f_name, f.name)
  152. self.assertTrue(os.path.exists(os.path.join(self.temp_dir, f.name)))
  153. self.storage.delete(storage_f_name)
  154. def test_file_save_with_path(self):
  155. """
  156. Saving a pathname should create intermediate directories as necessary.
  157. """
  158. self.assertFalse(self.storage.exists('path/to'))
  159. self.storage.save('path/to/test.file',
  160. ContentFile('file saved with path'))
  161. self.assertTrue(self.storage.exists('path/to'))
  162. with self.storage.open('path/to/test.file') as f:
  163. self.assertEqual(f.read(), b'file saved with path')
  164. self.assertTrue(os.path.exists(
  165. os.path.join(self.temp_dir, 'path', 'to', 'test.file')))
  166. self.storage.delete('path/to/test.file')
  167. def test_save_doesnt_close(self):
  168. with TemporaryUploadedFile('test', 'text/plain', 1, 'utf8') as file:
  169. file.write(b'1')
  170. file.seek(0)
  171. self.assertFalse(file.closed)
  172. self.storage.save('path/to/test.file', file)
  173. self.assertFalse(file.closed)
  174. self.assertFalse(file.file.closed)
  175. file = InMemoryUploadedFile(six.StringIO('1'), '', 'test',
  176. 'text/plain', 1, 'utf8')
  177. with file:
  178. self.assertFalse(file.closed)
  179. self.storage.save('path/to/test.file', file)
  180. self.assertFalse(file.closed)
  181. self.assertFalse(file.file.closed)
  182. def test_file_path(self):
  183. """
  184. File storage returns the full path of a file
  185. """
  186. self.assertFalse(self.storage.exists('test.file'))
  187. f = ContentFile('custom contents')
  188. f_name = self.storage.save('test.file', f)
  189. self.assertEqual(self.storage.path(f_name),
  190. os.path.join(self.temp_dir, f_name))
  191. self.storage.delete(f_name)
  192. def test_file_url(self):
  193. """
  194. File storage returns a url to access a given file from the Web.
  195. """
  196. self.assertEqual(self.storage.url('test.file'),
  197. '%s%s' % (self.storage.base_url, 'test.file'))
  198. # should encode special chars except ~!*()'
  199. # like encodeURIComponent() JavaScript function do
  200. self.assertEqual(self.storage.url(r"""~!*()'@#$%^&*abc`+ =.file"""),
  201. """/test_media_url/~!*()'%40%23%24%25%5E%26*abc%60%2B%20%3D.file""")
  202. # should stanslate os path separator(s) to the url path separator
  203. self.assertEqual(self.storage.url("""a/b\\c.file"""),
  204. """/test_media_url/a/b/c.file""")
  205. self.storage.base_url = None
  206. self.assertRaises(ValueError, self.storage.url, 'test.file')
  207. # #22717: missing ending slash in base_url should be auto-corrected
  208. storage = self.storage_class(location=self.temp_dir,
  209. base_url='/no_ending_slash')
  210. self.assertEqual(
  211. storage.url('test.file'),
  212. '%s%s' % (storage.base_url, 'test.file')
  213. )
  214. def test_listdir(self):
  215. """
  216. File storage returns a tuple containing directories and files.
  217. """
  218. self.assertFalse(self.storage.exists('storage_test_1'))
  219. self.assertFalse(self.storage.exists('storage_test_2'))
  220. self.assertFalse(self.storage.exists('storage_dir_1'))
  221. self.storage.save('storage_test_1', ContentFile('custom content'))
  222. self.storage.save('storage_test_2', ContentFile('custom content'))
  223. os.mkdir(os.path.join(self.temp_dir, 'storage_dir_1'))
  224. dirs, files = self.storage.listdir('')
  225. self.assertEqual(set(dirs), {'storage_dir_1'})
  226. self.assertEqual(set(files),
  227. {'storage_test_1', 'storage_test_2'})
  228. self.storage.delete('storage_test_1')
  229. self.storage.delete('storage_test_2')
  230. os.rmdir(os.path.join(self.temp_dir, 'storage_dir_1'))
  231. def test_file_storage_prevents_directory_traversal(self):
  232. """
  233. File storage prevents directory traversal (files can only be accessed if
  234. they're below the storage location).
  235. """
  236. self.assertRaises(SuspiciousOperation, self.storage.exists, '..')
  237. self.assertRaises(SuspiciousOperation, self.storage.exists, '/etc/passwd')
  238. def test_file_storage_preserves_filename_case(self):
  239. """The storage backend should preserve case of filenames."""
  240. # Create a storage backend associated with the mixed case name
  241. # directory.
  242. other_temp_storage = self.storage_class(location=self.temp_dir2)
  243. # Ask that storage backend to store a file with a mixed case filename.
  244. mixed_case = 'CaSe_SeNsItIvE'
  245. file = other_temp_storage.open(mixed_case, 'w')
  246. file.write('storage contents')
  247. file.close()
  248. self.assertEqual(os.path.join(self.temp_dir2, mixed_case),
  249. other_temp_storage.path(mixed_case))
  250. other_temp_storage.delete(mixed_case)
  251. def test_makedirs_race_handling(self):
  252. """
  253. File storage should be robust against directory creation race conditions.
  254. """
  255. real_makedirs = os.makedirs
  256. # Monkey-patch os.makedirs, to simulate a normal call, a raced call,
  257. # and an error.
  258. def fake_makedirs(path):
  259. if path == os.path.join(self.temp_dir, 'normal'):
  260. real_makedirs(path)
  261. elif path == os.path.join(self.temp_dir, 'raced'):
  262. real_makedirs(path)
  263. raise OSError(errno.EEXIST, 'simulated EEXIST')
  264. elif path == os.path.join(self.temp_dir, 'error'):
  265. raise OSError(errno.EACCES, 'simulated EACCES')
  266. else:
  267. self.fail('unexpected argument %r' % path)
  268. try:
  269. os.makedirs = fake_makedirs
  270. self.storage.save('normal/test.file',
  271. ContentFile('saved normally'))
  272. with self.storage.open('normal/test.file') as f:
  273. self.assertEqual(f.read(), b'saved normally')
  274. self.storage.save('raced/test.file',
  275. ContentFile('saved with race'))
  276. with self.storage.open('raced/test.file') as f:
  277. self.assertEqual(f.read(), b'saved with race')
  278. # Check that OSErrors aside from EEXIST are still raised.
  279. self.assertRaises(OSError,
  280. self.storage.save, 'error/test.file', ContentFile('not saved'))
  281. finally:
  282. os.makedirs = real_makedirs
  283. def test_remove_race_handling(self):
  284. """
  285. File storage should be robust against file removal race conditions.
  286. """
  287. real_remove = os.remove
  288. # Monkey-patch os.remove, to simulate a normal call, a raced call,
  289. # and an error.
  290. def fake_remove(path):
  291. if path == os.path.join(self.temp_dir, 'normal.file'):
  292. real_remove(path)
  293. elif path == os.path.join(self.temp_dir, 'raced.file'):
  294. real_remove(path)
  295. raise OSError(errno.ENOENT, 'simulated ENOENT')
  296. elif path == os.path.join(self.temp_dir, 'error.file'):
  297. raise OSError(errno.EACCES, 'simulated EACCES')
  298. else:
  299. self.fail('unexpected argument %r' % path)
  300. try:
  301. os.remove = fake_remove
  302. self.storage.save('normal.file', ContentFile('delete normally'))
  303. self.storage.delete('normal.file')
  304. self.assertFalse(self.storage.exists('normal.file'))
  305. self.storage.save('raced.file', ContentFile('delete with race'))
  306. self.storage.delete('raced.file')
  307. self.assertFalse(self.storage.exists('normal.file'))
  308. # Check that OSErrors aside from ENOENT are still raised.
  309. self.storage.save('error.file', ContentFile('delete with error'))
  310. self.assertRaises(OSError, self.storage.delete, 'error.file')
  311. finally:
  312. os.remove = real_remove
  313. def test_file_chunks_error(self):
  314. """
  315. Test behavior when file.chunks() is raising an error
  316. """
  317. f1 = ContentFile('chunks fails')
  318. def failing_chunks():
  319. raise IOError
  320. f1.chunks = failing_chunks
  321. with self.assertRaises(IOError):
  322. self.storage.save('error.file', f1)
  323. def test_delete_no_name(self):
  324. """
  325. Calling delete with an empty name should not try to remove the base
  326. storage directory, but fail loudly (#20660).
  327. """
  328. with self.assertRaises(AssertionError):
  329. self.storage.delete('')
  330. class CustomStorage(FileSystemStorage):
  331. def get_available_name(self, name):
  332. """
  333. Append numbers to duplicate files rather than underscores, like Trac.
  334. """
  335. parts = name.split('.')
  336. basename, ext = parts[0], parts[1:]
  337. number = 2
  338. while self.exists(name):
  339. name = '.'.join([basename, str(number)] + ext)
  340. number += 1
  341. return name
  342. class CustomStorageTests(FileStorageTests):
  343. storage_class = CustomStorage
  344. def test_custom_get_available_name(self):
  345. first = self.storage.save('custom_storage', ContentFile('custom contents'))
  346. self.assertEqual(first, 'custom_storage')
  347. second = self.storage.save('custom_storage', ContentFile('more contents'))
  348. self.assertEqual(second, 'custom_storage.2')
  349. self.storage.delete(first)
  350. self.storage.delete(second)
  351. class FileFieldStorageTests(unittest.TestCase):
  352. def tearDown(self):
  353. shutil.rmtree(temp_storage_location)
  354. def test_files(self):
  355. # Attempting to access a FileField from the class raises a descriptive
  356. # error
  357. self.assertRaises(AttributeError, lambda: Storage.normal)
  358. # An object without a file has limited functionality.
  359. obj1 = Storage()
  360. self.assertEqual(obj1.normal.name, "")
  361. self.assertRaises(ValueError, lambda: obj1.normal.size)
  362. # Saving a file enables full functionality.
  363. obj1.normal.save("django_test.txt", ContentFile("content"))
  364. self.assertEqual(obj1.normal.name, "tests/django_test.txt")
  365. self.assertEqual(obj1.normal.size, 7)
  366. self.assertEqual(obj1.normal.read(), b"content")
  367. obj1.normal.close()
  368. # File objects can be assigned to FileField attributes, but shouldn't
  369. # get committed until the model it's attached to is saved.
  370. obj1.normal = SimpleUploadedFile("assignment.txt", b"content")
  371. dirs, files = temp_storage.listdir("tests")
  372. self.assertEqual(dirs, [])
  373. self.assertFalse("assignment.txt" in files)
  374. obj1.save()
  375. dirs, files = temp_storage.listdir("tests")
  376. self.assertEqual(sorted(files), ["assignment.txt", "django_test.txt"])
  377. # Save another file with the same name.
  378. obj2 = Storage()
  379. obj2.normal.save("django_test.txt", ContentFile("more content"))
  380. obj2_name = obj2.normal.name
  381. six.assertRegex(self, obj2_name, "tests/django_test_%s.txt" % FILE_SUFFIX_REGEX)
  382. self.assertEqual(obj2.normal.size, 12)
  383. obj2.normal.close()
  384. # Deleting an object does not delete the file it uses.
  385. obj2.delete()
  386. obj2.normal.save("django_test.txt", ContentFile("more content"))
  387. self.assertNotEqual(obj2_name, obj2.normal.name)
  388. six.assertRegex(self, obj2.normal.name, "tests/django_test_%s.txt" % FILE_SUFFIX_REGEX)
  389. obj2.normal.close()
  390. def test_filefield_read(self):
  391. # Files can be read in a little at a time, if necessary.
  392. obj = Storage.objects.create(
  393. normal=SimpleUploadedFile("assignment.txt", b"content"))
  394. obj.normal.open()
  395. self.assertEqual(obj.normal.read(3), b"con")
  396. self.assertEqual(obj.normal.read(), b"tent")
  397. self.assertEqual(list(obj.normal.chunks(chunk_size=2)), [b"co", b"nt", b"en", b"t"])
  398. obj.normal.close()
  399. def test_duplicate_filename(self):
  400. # Multiple files with the same name get _(7 random chars) appended to them.
  401. objs = [Storage() for i in range(2)]
  402. for o in objs:
  403. o.normal.save("multiple_files.txt", ContentFile("Same Content"))
  404. try:
  405. names = [o.normal.name for o in objs]
  406. self.assertEqual(names[0], "tests/multiple_files.txt")
  407. six.assertRegex(self, names[1], "tests/multiple_files_%s.txt" % FILE_SUFFIX_REGEX)
  408. finally:
  409. for o in objs:
  410. o.delete()
  411. def test_filefield_default(self):
  412. # Default values allow an object to access a single file.
  413. temp_storage.save('tests/default.txt', ContentFile('default content'))
  414. obj = Storage.objects.create()
  415. self.assertEqual(obj.default.name, "tests/default.txt")
  416. self.assertEqual(obj.default.read(), b"default content")
  417. obj.default.close()
  418. # But it shouldn't be deleted, even if there are no more objects using
  419. # it.
  420. obj.delete()
  421. obj = Storage()
  422. self.assertEqual(obj.default.read(), b"default content")
  423. obj.default.close()
  424. def test_empty_upload_to(self):
  425. # upload_to can be empty, meaning it does not use subdirectory.
  426. obj = Storage()
  427. obj.empty.save('django_test.txt', ContentFile('more content'))
  428. self.assertEqual(obj.empty.name, "./django_test.txt")
  429. self.assertEqual(obj.empty.read(), b"more content")
  430. obj.empty.close()
  431. def test_random_upload_to(self):
  432. # Verify the fix for #5655, making sure the directory is only
  433. # determined once.
  434. obj = Storage()
  435. obj.random.save("random_file", ContentFile("random content"))
  436. self.assertTrue(obj.random.name.endswith("/random_file"))
  437. obj.random.close()
  438. def test_filefield_pickling(self):
  439. # Push an object into the cache to make sure it pickles properly
  440. obj = Storage()
  441. obj.normal.save("django_test.txt", ContentFile("more content"))
  442. obj.normal.close()
  443. cache.set("obj", obj)
  444. self.assertEqual(cache.get("obj").normal.name, "tests/django_test.txt")
  445. def test_file_object(self):
  446. # Create sample file
  447. temp_storage.save('tests/example.txt', ContentFile('some content'))
  448. # Load it as python file object
  449. with open(temp_storage.path('tests/example.txt')) as file_obj:
  450. # Save it using storage and read its content
  451. temp_storage.save('tests/file_obj', file_obj)
  452. self.assertTrue(temp_storage.exists('tests/file_obj'))
  453. with temp_storage.open('tests/file_obj') as f:
  454. self.assertEqual(f.read(), b'some content')
  455. def test_stringio(self):
  456. # Test passing StringIO instance as content argument to save
  457. output = six.StringIO()
  458. output.write('content')
  459. output.seek(0)
  460. # Save it and read written file
  461. temp_storage.save('tests/stringio', output)
  462. self.assertTrue(temp_storage.exists('tests/stringio'))
  463. with temp_storage.open('tests/stringio') as f:
  464. self.assertEqual(f.read(), b'content')
  465. # Tests for a race condition on file saving (#4948).
  466. # This is written in such a way that it'll always pass on platforms
  467. # without threading.
  468. class SlowFile(ContentFile):
  469. def chunks(self):
  470. time.sleep(1)
  471. return super(ContentFile, self).chunks()
  472. class FileSaveRaceConditionTest(unittest.TestCase):
  473. def setUp(self):
  474. self.storage_dir = tempfile.mkdtemp()
  475. self.storage = FileSystemStorage(self.storage_dir)
  476. self.thread = threading.Thread(target=self.save_file, args=['conflict'])
  477. def tearDown(self):
  478. shutil.rmtree(self.storage_dir)
  479. def save_file(self, name):
  480. name = self.storage.save(name, SlowFile(b"Data"))
  481. def test_race_condition(self):
  482. self.thread.start()
  483. self.save_file('conflict')
  484. self.thread.join()
  485. files = sorted(os.listdir(self.storage_dir))
  486. self.assertEqual(files[0], 'conflict')
  487. six.assertRegex(self, files[1], 'conflict_%s' % FILE_SUFFIX_REGEX)
  488. @unittest.skipIf(sys.platform.startswith('win'), "Windows only partially supports umasks and chmod.")
  489. class FileStoragePermissions(unittest.TestCase):
  490. def setUp(self):
  491. self.umask = 0o027
  492. self.old_umask = os.umask(self.umask)
  493. self.storage_dir = tempfile.mkdtemp()
  494. def tearDown(self):
  495. shutil.rmtree(self.storage_dir)
  496. os.umask(self.old_umask)
  497. @override_settings(FILE_UPLOAD_PERMISSIONS=0o654)
  498. def test_file_upload_permissions(self):
  499. self.storage = FileSystemStorage(self.storage_dir)
  500. name = self.storage.save("the_file", ContentFile("data"))
  501. actual_mode = os.stat(self.storage.path(name))[0] & 0o777
  502. self.assertEqual(actual_mode, 0o654)
  503. @override_settings(FILE_UPLOAD_PERMISSIONS=None)
  504. def test_file_upload_default_permissions(self):
  505. self.storage = FileSystemStorage(self.storage_dir)
  506. fname = self.storage.save("some_file", ContentFile("data"))
  507. mode = os.stat(self.storage.path(fname))[0] & 0o777
  508. self.assertEqual(mode, 0o666 & ~self.umask)
  509. @override_settings(FILE_UPLOAD_DIRECTORY_PERMISSIONS=0o765)
  510. def test_file_upload_directory_permissions(self):
  511. self.storage = FileSystemStorage(self.storage_dir)
  512. name = self.storage.save("the_directory/the_file", ContentFile("data"))
  513. dir_mode = os.stat(os.path.dirname(self.storage.path(name)))[0] & 0o777
  514. self.assertEqual(dir_mode, 0o765)
  515. @override_settings(FILE_UPLOAD_DIRECTORY_PERMISSIONS=None)
  516. def test_file_upload_directory_default_permissions(self):
  517. self.storage = FileSystemStorage(self.storage_dir)
  518. name = self.storage.save("the_directory/the_file", ContentFile("data"))
  519. dir_mode = os.stat(os.path.dirname(self.storage.path(name)))[0] & 0o777
  520. self.assertEqual(dir_mode, 0o777 & ~self.umask)
  521. class FileStoragePathParsing(unittest.TestCase):
  522. def setUp(self):
  523. self.storage_dir = tempfile.mkdtemp()
  524. self.storage = FileSystemStorage(self.storage_dir)
  525. def tearDown(self):
  526. shutil.rmtree(self.storage_dir)
  527. def test_directory_with_dot(self):
  528. """Regression test for #9610.
  529. If the directory name contains a dot and the file name doesn't, make
  530. sure we still mangle the file name instead of the directory name.
  531. """
  532. self.storage.save('dotted.path/test', ContentFile("1"))
  533. self.storage.save('dotted.path/test', ContentFile("2"))
  534. files = sorted(os.listdir(os.path.join(self.storage_dir, 'dotted.path')))
  535. self.assertFalse(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path')))
  536. self.assertEqual(files[0], 'test')
  537. six.assertRegex(self, files[1], 'test_%s' % FILE_SUFFIX_REGEX)
  538. def test_first_character_dot(self):
  539. """
  540. File names with a dot as their first character don't have an extension,
  541. and the underscore should get added to the end.
  542. """
  543. self.storage.save('dotted.path/.test', ContentFile("1"))
  544. self.storage.save('dotted.path/.test', ContentFile("2"))
  545. files = sorted(os.listdir(os.path.join(self.storage_dir, 'dotted.path')))
  546. self.assertFalse(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path')))
  547. self.assertEqual(files[0], '.test')
  548. six.assertRegex(self, files[1], '.test_%s' % FILE_SUFFIX_REGEX)
  549. class ContentFileStorageTestCase(unittest.TestCase):
  550. def setUp(self):
  551. self.storage_dir = tempfile.mkdtemp()
  552. self.storage = FileSystemStorage(self.storage_dir)
  553. def tearDown(self):
  554. shutil.rmtree(self.storage_dir)
  555. def test_content_saving(self):
  556. """
  557. Test that ContentFile can be saved correctly with the filesystem storage,
  558. both if it was initialized with string or unicode content"""
  559. self.storage.save('bytes.txt', ContentFile(b"content"))
  560. self.storage.save('unicode.txt', ContentFile("español"))
  561. @override_settings(ROOT_URLCONF='file_storage.urls')
  562. class FileLikeObjectTestCase(LiveServerTestCase):
  563. """
  564. Test file-like objects (#15644).
  565. """
  566. available_apps = []
  567. def setUp(self):
  568. self.temp_dir = tempfile.mkdtemp()
  569. self.storage = FileSystemStorage(location=self.temp_dir)
  570. def tearDown(self):
  571. shutil.rmtree(self.temp_dir)
  572. def test_urllib2_urlopen(self):
  573. """
  574. Test the File storage API with a file like object coming from urllib2.urlopen()
  575. """
  576. file_like_object = urlopen(self.live_server_url + '/')
  577. f = File(file_like_object)
  578. stored_filename = self.storage.save("remote_file.html", f)
  579. remote_file = urlopen(self.live_server_url + '/')
  580. with self.storage.open(stored_filename) as stored_file:
  581. self.assertEqual(stored_file.read(), remote_file.read())