PageRenderTime 84ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/file_storage/tests.py

https://code.google.com/p/mango-py/
Python | 452 lines | 426 code | 7 blank | 19 comment | 10 complexity | 6994f1b2968c18a7f3ca0f741ee5aac6 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import shutil
  4. import sys
  5. import tempfile
  6. import time
  7. from datetime import datetime, timedelta
  8. try:
  9. from cStringIO import StringIO
  10. except ImportError:
  11. from StringIO import StringIO
  12. try:
  13. import threading
  14. except ImportError:
  15. import dummy_threading as threading
  16. from django.conf import settings
  17. from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured
  18. from django.core.files.base import ContentFile, File
  19. from django.core.files.images import get_image_dimensions
  20. from django.core.files.storage import FileSystemStorage, get_storage_class
  21. from django.core.files.uploadedfile import UploadedFile
  22. from django.utils import unittest
  23. # Try to import PIL in either of the two ways it can end up installed.
  24. # Checking for the existence of Image is enough for CPython, but
  25. # for PyPy, you need to check for the underlying modules
  26. try:
  27. from PIL import Image, _imaging
  28. except ImportError:
  29. try:
  30. import Image, _imaging
  31. except ImportError:
  32. Image = None
  33. class GetStorageClassTests(unittest.TestCase):
  34. def assertRaisesErrorWithMessage(self, error, message, callable,
  35. *args, **kwargs):
  36. self.assertRaises(error, callable, *args, **kwargs)
  37. try:
  38. callable(*args, **kwargs)
  39. except error, e:
  40. self.assertEqual(message, str(e))
  41. def test_get_filesystem_storage(self):
  42. """
  43. get_storage_class returns the class for a storage backend name/path.
  44. """
  45. self.assertEqual(
  46. get_storage_class('django.core.files.storage.FileSystemStorage'),
  47. FileSystemStorage)
  48. def test_get_invalid_storage_module(self):
  49. """
  50. get_storage_class raises an error if the requested import don't exist.
  51. """
  52. self.assertRaisesErrorWithMessage(
  53. ImproperlyConfigured,
  54. "NonExistingStorage isn't a storage module.",
  55. get_storage_class,
  56. 'NonExistingStorage')
  57. def test_get_nonexisting_storage_class(self):
  58. """
  59. get_storage_class raises an error if the requested class don't exist.
  60. """
  61. self.assertRaisesErrorWithMessage(
  62. ImproperlyConfigured,
  63. 'Storage module "django.core.files.storage" does not define a '\
  64. '"NonExistingStorage" class.',
  65. get_storage_class,
  66. 'django.core.files.storage.NonExistingStorage')
  67. def test_get_nonexisting_storage_module(self):
  68. """
  69. get_storage_class raises an error if the requested module don't exist.
  70. """
  71. # Error message may or may not be the fully qualified path.
  72. self.assertRaisesRegexp(
  73. ImproperlyConfigured,
  74. ('Error importing storage module django.core.files.non_existing_'
  75. 'storage: "No module named .*non_existing_storage"'),
  76. get_storage_class,
  77. 'django.core.files.non_existing_storage.NonExistingStorage'
  78. )
  79. class FileStorageTests(unittest.TestCase):
  80. storage_class = FileSystemStorage
  81. def setUp(self):
  82. self.temp_dir = tempfile.mktemp()
  83. os.makedirs(self.temp_dir)
  84. self.storage = self.storage_class(location=self.temp_dir,
  85. base_url='/test_media_url/')
  86. def tearDown(self):
  87. shutil.rmtree(self.temp_dir)
  88. def test_file_access_options(self):
  89. """
  90. Standard file access options are available, and work as expected.
  91. """
  92. self.assertFalse(self.storage.exists('storage_test'))
  93. f = self.storage.open('storage_test', 'w')
  94. f.write('storage contents')
  95. f.close()
  96. self.assertTrue(self.storage.exists('storage_test'))
  97. f = self.storage.open('storage_test', 'r')
  98. self.assertEqual(f.read(), 'storage contents')
  99. f.close()
  100. self.storage.delete('storage_test')
  101. self.assertFalse(self.storage.exists('storage_test'))
  102. def test_file_accessed_time(self):
  103. """
  104. File storage returns a Datetime object for the last accessed time of
  105. a file.
  106. """
  107. self.assertFalse(self.storage.exists('test.file'))
  108. f = ContentFile('custom contents')
  109. f_name = self.storage.save('test.file', f)
  110. atime = self.storage.accessed_time(f_name)
  111. self.assertEqual(atime, datetime.fromtimestamp(
  112. os.path.getatime(self.storage.path(f_name))))
  113. self.assertTrue(datetime.now() - self.storage.accessed_time(f_name) < timedelta(seconds=2))
  114. self.storage.delete(f_name)
  115. def test_file_created_time(self):
  116. """
  117. File storage returns a Datetime object for the creation time of
  118. a file.
  119. """
  120. self.assertFalse(self.storage.exists('test.file'))
  121. f = ContentFile('custom contents')
  122. f_name = self.storage.save('test.file', f)
  123. ctime = self.storage.created_time(f_name)
  124. self.assertEqual(ctime, datetime.fromtimestamp(
  125. os.path.getctime(self.storage.path(f_name))))
  126. self.assertTrue(datetime.now() - self.storage.created_time(f_name) < timedelta(seconds=2))
  127. self.storage.delete(f_name)
  128. def test_file_modified_time(self):
  129. """
  130. File storage returns a Datetime object for the last modified time of
  131. a file.
  132. """
  133. self.assertFalse(self.storage.exists('test.file'))
  134. f = ContentFile('custom contents')
  135. f_name = self.storage.save('test.file', f)
  136. mtime = self.storage.modified_time(f_name)
  137. self.assertEqual(mtime, datetime.fromtimestamp(
  138. os.path.getmtime(self.storage.path(f_name))))
  139. self.assertTrue(datetime.now() - self.storage.modified_time(f_name) < timedelta(seconds=2))
  140. self.storage.delete(f_name)
  141. def test_file_save_without_name(self):
  142. """
  143. File storage extracts the filename from the content object if no
  144. name is given explicitly.
  145. """
  146. self.assertFalse(self.storage.exists('test.file'))
  147. f = ContentFile('custom contents')
  148. f.name = 'test.file'
  149. storage_f_name = self.storage.save(None, f)
  150. self.assertEqual(storage_f_name, f.name)
  151. self.assertTrue(os.path.exists(os.path.join(self.temp_dir, f.name)))
  152. self.storage.delete(storage_f_name)
  153. def test_file_path(self):
  154. """
  155. File storage returns the full path of a file
  156. """
  157. self.assertFalse(self.storage.exists('test.file'))
  158. f = ContentFile('custom contents')
  159. f_name = self.storage.save('test.file', f)
  160. self.assertEqual(self.storage.path(f_name),
  161. os.path.join(self.temp_dir, f_name))
  162. self.storage.delete(f_name)
  163. def test_file_url(self):
  164. """
  165. File storage returns a url to access a given file from the Web.
  166. """
  167. self.assertEqual(self.storage.url('test.file'),
  168. '%s%s' % (self.storage.base_url, 'test.file'))
  169. # should encode special chars except ~!*()'
  170. # like encodeURIComponent() JavaScript function do
  171. self.assertEqual(self.storage.url(r"""~!*()'@#$%^&*abc`+=.file"""),
  172. """/test_media_url/~!*()'%40%23%24%25%5E%26*abc%60%2B%3D.file""")
  173. # should stanslate os path separator(s) to the url path separator
  174. self.assertEqual(self.storage.url("""a/b\\c.file"""),
  175. """/test_media_url/a/b/c.file""")
  176. self.storage.base_url = None
  177. self.assertRaises(ValueError, self.storage.url, 'test.file')
  178. def test_file_with_mixin(self):
  179. """
  180. File storage can get a mixin to extend the functionality of the
  181. returned file.
  182. """
  183. self.assertFalse(self.storage.exists('test.file'))
  184. class TestFileMixin(object):
  185. mixed_in = True
  186. f = ContentFile('custom contents')
  187. f_name = self.storage.save('test.file', f)
  188. self.assertTrue(isinstance(
  189. self.storage.open('test.file', mixin=TestFileMixin),
  190. TestFileMixin
  191. ))
  192. self.storage.delete('test.file')
  193. def test_listdir(self):
  194. """
  195. File storage returns a tuple containing directories and files.
  196. """
  197. self.assertFalse(self.storage.exists('storage_test_1'))
  198. self.assertFalse(self.storage.exists('storage_test_2'))
  199. self.assertFalse(self.storage.exists('storage_dir_1'))
  200. f = self.storage.save('storage_test_1', ContentFile('custom content'))
  201. f = self.storage.save('storage_test_2', ContentFile('custom content'))
  202. os.mkdir(os.path.join(self.temp_dir, 'storage_dir_1'))
  203. dirs, files = self.storage.listdir('')
  204. self.assertEqual(set(dirs), set([u'storage_dir_1']))
  205. self.assertEqual(set(files),
  206. set([u'storage_test_1', u'storage_test_2']))
  207. self.storage.delete('storage_test_1')
  208. self.storage.delete('storage_test_2')
  209. os.rmdir(os.path.join(self.temp_dir, 'storage_dir_1'))
  210. def test_file_storage_prevents_directory_traversal(self):
  211. """
  212. File storage prevents directory traversal (files can only be accessed if
  213. they're below the storage location).
  214. """
  215. self.assertRaises(SuspiciousOperation, self.storage.exists, '..')
  216. self.assertRaises(SuspiciousOperation, self.storage.exists, '/etc/passwd')
  217. class CustomStorage(FileSystemStorage):
  218. def get_available_name(self, name):
  219. """
  220. Append numbers to duplicate files rather than underscores, like Trac.
  221. """
  222. parts = name.split('.')
  223. basename, ext = parts[0], parts[1:]
  224. number = 2
  225. while self.exists(name):
  226. name = '.'.join([basename, str(number)] + ext)
  227. number += 1
  228. return name
  229. class CustomStorageTests(FileStorageTests):
  230. storage_class = CustomStorage
  231. def test_custom_get_available_name(self):
  232. first = self.storage.save('custom_storage', ContentFile('custom contents'))
  233. self.assertEqual(first, 'custom_storage')
  234. second = self.storage.save('custom_storage', ContentFile('more contents'))
  235. self.assertEqual(second, 'custom_storage.2')
  236. self.storage.delete(first)
  237. self.storage.delete(second)
  238. class UnicodeFileNameTests(unittest.TestCase):
  239. def test_unicode_file_names(self):
  240. """
  241. Regression test for #8156: files with unicode names I can't quite figure
  242. out the encoding situation between doctest and this file, but the actual
  243. repr doesn't matter; it just shouldn't return a unicode object.
  244. """
  245. uf = UploadedFile(name=u'?C?mo?',content_type='text')
  246. self.assertEqual(type(uf.__repr__()), str)
  247. # Tests for a race condition on file saving (#4948).
  248. # This is written in such a way that it'll always pass on platforms
  249. # without threading.
  250. class SlowFile(ContentFile):
  251. def chunks(self):
  252. time.sleep(1)
  253. return super(ContentFile, self).chunks()
  254. class FileSaveRaceConditionTest(unittest.TestCase):
  255. def setUp(self):
  256. self.storage_dir = tempfile.mkdtemp()
  257. self.storage = FileSystemStorage(self.storage_dir)
  258. self.thread = threading.Thread(target=self.save_file, args=['conflict'])
  259. def tearDown(self):
  260. shutil.rmtree(self.storage_dir)
  261. def save_file(self, name):
  262. name = self.storage.save(name, SlowFile("Data"))
  263. def test_race_condition(self):
  264. self.thread.start()
  265. name = self.save_file('conflict')
  266. self.thread.join()
  267. self.assertTrue(self.storage.exists('conflict'))
  268. self.assertTrue(self.storage.exists('conflict_1'))
  269. self.storage.delete('conflict')
  270. self.storage.delete('conflict_1')
  271. class FileStoragePermissions(unittest.TestCase):
  272. def setUp(self):
  273. self.old_perms = settings.FILE_UPLOAD_PERMISSIONS
  274. settings.FILE_UPLOAD_PERMISSIONS = 0666
  275. self.storage_dir = tempfile.mkdtemp()
  276. self.storage = FileSystemStorage(self.storage_dir)
  277. def tearDown(self):
  278. settings.FILE_UPLOAD_PERMISSIONS = self.old_perms
  279. shutil.rmtree(self.storage_dir)
  280. def test_file_upload_permissions(self):
  281. name = self.storage.save("the_file", ContentFile("data"))
  282. actual_mode = os.stat(self.storage.path(name))[0] & 0777
  283. self.assertEqual(actual_mode, 0666)
  284. class FileStoragePathParsing(unittest.TestCase):
  285. def setUp(self):
  286. self.storage_dir = tempfile.mkdtemp()
  287. self.storage = FileSystemStorage(self.storage_dir)
  288. def tearDown(self):
  289. shutil.rmtree(self.storage_dir)
  290. def test_directory_with_dot(self):
  291. """Regression test for #9610.
  292. If the directory name contains a dot and the file name doesn't, make
  293. sure we still mangle the file name instead of the directory name.
  294. """
  295. self.storage.save('dotted.path/test', ContentFile("1"))
  296. self.storage.save('dotted.path/test', ContentFile("2"))
  297. self.assertFalse(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path')))
  298. self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test')))
  299. self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test_1')))
  300. def test_first_character_dot(self):
  301. """
  302. File names with a dot as their first character don't have an extension,
  303. and the underscore should get added to the end.
  304. """
  305. self.storage.save('dotted.path/.test', ContentFile("1"))
  306. self.storage.save('dotted.path/.test', ContentFile("2"))
  307. self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test')))
  308. # Before 2.6, a leading dot was treated as an extension, and so
  309. # underscore gets added to beginning instead of end.
  310. if sys.version_info < (2, 6):
  311. self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/_1.test')))
  312. else:
  313. self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test_1')))
  314. class DimensionClosingBug(unittest.TestCase):
  315. """
  316. Test that get_image_dimensions() properly closes files (#8817)
  317. """
  318. @unittest.skipUnless(Image, "PIL not installed")
  319. def test_not_closing_of_files(self):
  320. """
  321. Open files passed into get_image_dimensions() should stay opened.
  322. """
  323. empty_io = StringIO()
  324. try:
  325. get_image_dimensions(empty_io)
  326. finally:
  327. self.assertTrue(not empty_io.closed)
  328. @unittest.skipUnless(Image, "PIL not installed")
  329. def test_closing_of_filenames(self):
  330. """
  331. get_image_dimensions() called with a filename should closed the file.
  332. """
  333. # We need to inject a modified open() builtin into the images module
  334. # that checks if the file was closed properly if the function is
  335. # called with a filename instead of an file object.
  336. # get_image_dimensions will call our catching_open instead of the
  337. # regular builtin one.
  338. class FileWrapper(object):
  339. _closed = []
  340. def __init__(self, f):
  341. self.f = f
  342. def __getattr__(self, name):
  343. return getattr(self.f, name)
  344. def close(self):
  345. self._closed.append(True)
  346. self.f.close()
  347. def catching_open(*args):
  348. return FileWrapper(open(*args))
  349. from django.core.files import images
  350. images.open = catching_open
  351. try:
  352. get_image_dimensions(os.path.join(os.path.dirname(__file__), "test1.png"))
  353. finally:
  354. del images.open
  355. self.assertTrue(FileWrapper._closed)
  356. class InconsistentGetImageDimensionsBug(unittest.TestCase):
  357. """
  358. Test that get_image_dimensions() works properly after various calls
  359. using a file handler (#11158)
  360. """
  361. @unittest.skipUnless(Image, "PIL not installed")
  362. def test_multiple_calls(self):
  363. """
  364. Multiple calls of get_image_dimensions() should return the same size.
  365. """
  366. from django.core.files.images import ImageFile
  367. img_path = os.path.join(os.path.dirname(__file__), "test.png")
  368. image = ImageFile(open(img_path, 'rb'))
  369. image_pil = Image.open(img_path)
  370. size_1, size_2 = get_image_dimensions(image), get_image_dimensions(image)
  371. self.assertEqual(image_pil.size, size_1)
  372. self.assertEqual(size_1, size_2)