test_storage.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. from __future__ import unicode_literals
  2. import os
  3. import shutil
  4. import sys
  5. import tempfile
  6. import unittest
  7. from django.conf import settings
  8. from django.contrib.staticfiles import finders, storage
  9. from django.contrib.staticfiles.management.commands.collectstatic import \
  10. Command as CollectstaticCommand
  11. from django.core.cache.backends.base import BaseCache
  12. from django.core.management import call_command
  13. from django.test import override_settings
  14. from django.utils import six
  15. from django.utils.encoding import force_text
  16. from .cases import CollectionTestCase
  17. from .settings import TEST_ROOT
  18. def hashed_file_path(test, path):
  19. fullpath = test.render_template(test.static_template_snippet(path))
  20. return fullpath.replace(settings.STATIC_URL, '')
  21. class TestHashedFiles(object):
  22. hashed_file_path = hashed_file_path
  23. def tearDown(self):
  24. # Clear hashed files to avoid side effects among tests.
  25. storage.staticfiles_storage.hashed_files.clear()
  26. def test_template_tag_return(self):
  27. """
  28. Test the CachedStaticFilesStorage backend.
  29. """
  30. self.assertStaticRaises(ValueError, "does/not/exist.png", "/static/does/not/exist.png")
  31. self.assertStaticRenders("test/file.txt", "/static/test/file.dad0999e4f8f.txt")
  32. self.assertStaticRenders("test/file.txt", "/static/test/file.dad0999e4f8f.txt", asvar=True)
  33. self.assertStaticRenders("cached/styles.css", "/static/cached/styles.bb84a0240107.css")
  34. self.assertStaticRenders("path/", "/static/path/")
  35. self.assertStaticRenders("path/?query", "/static/path/?query")
  36. def test_template_tag_simple_content(self):
  37. relpath = self.hashed_file_path("cached/styles.css")
  38. self.assertEqual(relpath, "cached/styles.bb84a0240107.css")
  39. with storage.staticfiles_storage.open(relpath) as relfile:
  40. content = relfile.read()
  41. self.assertNotIn(b"cached/other.css", content)
  42. self.assertIn(b"other.d41d8cd98f00.css", content)
  43. def test_path_ignored_completely(self):
  44. relpath = self.hashed_file_path("cached/css/ignored.css")
  45. self.assertEqual(relpath, "cached/css/ignored.6c77f2643390.css")
  46. with storage.staticfiles_storage.open(relpath) as relfile:
  47. content = relfile.read()
  48. self.assertIn(b'#foobar', content)
  49. self.assertIn(b'http:foobar', content)
  50. self.assertIn(b'https:foobar', content)
  51. self.assertIn(b'data:foobar', content)
  52. self.assertIn(b'//foobar', content)
  53. def test_path_with_querystring(self):
  54. relpath = self.hashed_file_path("cached/styles.css?spam=eggs")
  55. self.assertEqual(relpath, "cached/styles.bb84a0240107.css?spam=eggs")
  56. with storage.staticfiles_storage.open(
  57. "cached/styles.bb84a0240107.css") as relfile:
  58. content = relfile.read()
  59. self.assertNotIn(b"cached/other.css", content)
  60. self.assertIn(b"other.d41d8cd98f00.css", content)
  61. def test_path_with_fragment(self):
  62. relpath = self.hashed_file_path("cached/styles.css#eggs")
  63. self.assertEqual(relpath, "cached/styles.bb84a0240107.css#eggs")
  64. with storage.staticfiles_storage.open(
  65. "cached/styles.bb84a0240107.css") as relfile:
  66. content = relfile.read()
  67. self.assertNotIn(b"cached/other.css", content)
  68. self.assertIn(b"other.d41d8cd98f00.css", content)
  69. def test_path_with_querystring_and_fragment(self):
  70. relpath = self.hashed_file_path("cached/css/fragments.css")
  71. self.assertEqual(relpath, "cached/css/fragments.59dc2b188043.css")
  72. with storage.staticfiles_storage.open(relpath) as relfile:
  73. content = relfile.read()
  74. self.assertIn(b'fonts/font.a4b0478549d0.eot?#iefix', content)
  75. self.assertIn(b'fonts/font.b8d603e42714.svg#webfontIyfZbseF', content)
  76. self.assertIn(b'fonts/font.b8d603e42714.svg#path/to/../../fonts/font.svg', content)
  77. self.assertIn(b'data:font/woff;charset=utf-8;base64,d09GRgABAAAAADJoAA0AAAAAR2QAAQAAAAAAAAAAAAA', content)
  78. self.assertIn(b'#default#VML', content)
  79. def test_template_tag_absolute(self):
  80. relpath = self.hashed_file_path("cached/absolute.css")
  81. self.assertEqual(relpath, "cached/absolute.df312c6326e1.css")
  82. with storage.staticfiles_storage.open(relpath) as relfile:
  83. content = relfile.read()
  84. self.assertNotIn(b"/static/cached/styles.css", content)
  85. self.assertIn(b"/static/cached/styles.bb84a0240107.css", content)
  86. self.assertNotIn(b"/static/styles_root.css", content)
  87. self.assertIn(b"/static/styles_root.401f2509a628.css", content)
  88. self.assertIn(b'/static/cached/img/relative.acae32e4532b.png', content)
  89. def test_template_tag_absolute_root(self):
  90. """
  91. Like test_template_tag_absolute, but for a file in STATIC_ROOT (#26249).
  92. """
  93. relpath = self.hashed_file_path("absolute_root.css")
  94. self.assertEqual(relpath, "absolute_root.f864a4d7f083.css")
  95. with storage.staticfiles_storage.open(relpath) as relfile:
  96. content = relfile.read()
  97. self.assertNotIn(b"/static/styles_root.css", content)
  98. self.assertIn(b"/static/styles_root.401f2509a628.css", content)
  99. def test_template_tag_relative(self):
  100. relpath = self.hashed_file_path("cached/relative.css")
  101. self.assertEqual(relpath, "cached/relative.b0375bd89156.css")
  102. with storage.staticfiles_storage.open(relpath) as relfile:
  103. content = relfile.read()
  104. self.assertNotIn(b"../cached/styles.css", content)
  105. self.assertNotIn(b'@import "styles.css"', content)
  106. self.assertNotIn(b'url(img/relative.png)', content)
  107. self.assertIn(b'url("img/relative.acae32e4532b.png")', content)
  108. self.assertIn(b"../cached/styles.bb84a0240107.css", content)
  109. def test_import_replacement(self):
  110. "See #18050"
  111. relpath = self.hashed_file_path("cached/import.css")
  112. self.assertEqual(relpath, "cached/import.2b1d40b0bbd4.css")
  113. with storage.staticfiles_storage.open(relpath) as relfile:
  114. self.assertIn(b"""import url("styles.bb84a0240107.css")""", relfile.read())
  115. def test_template_tag_deep_relative(self):
  116. relpath = self.hashed_file_path("cached/css/window.css")
  117. self.assertEqual(relpath, "cached/css/window.3906afbb5a17.css")
  118. with storage.staticfiles_storage.open(relpath) as relfile:
  119. content = relfile.read()
  120. self.assertNotIn(b'url(img/window.png)', content)
  121. self.assertIn(b'url("img/window.acae32e4532b.png")', content)
  122. def test_template_tag_url(self):
  123. relpath = self.hashed_file_path("cached/url.css")
  124. self.assertEqual(relpath, "cached/url.902310b73412.css")
  125. with storage.staticfiles_storage.open(relpath) as relfile:
  126. self.assertIn(b"https://", relfile.read())
  127. def test_post_processing(self):
  128. """
  129. Test that post_processing behaves correctly.
  130. Files that are alterable should always be post-processed; files that
  131. aren't should be skipped.
  132. collectstatic has already been called once in setUp() for this testcase,
  133. therefore we check by verifying behavior on a second run.
  134. """
  135. collectstatic_args = {
  136. 'interactive': False,
  137. 'verbosity': 0,
  138. 'link': False,
  139. 'clear': False,
  140. 'dry_run': False,
  141. 'post_process': True,
  142. 'use_default_ignore_patterns': True,
  143. 'ignore_patterns': ['*.ignoreme'],
  144. }
  145. collectstatic_cmd = CollectstaticCommand()
  146. collectstatic_cmd.set_options(**collectstatic_args)
  147. stats = collectstatic_cmd.collect()
  148. self.assertIn(os.path.join('cached', 'css', 'window.css'), stats['post_processed'])
  149. self.assertIn(os.path.join('cached', 'css', 'img', 'window.png'), stats['unmodified'])
  150. self.assertIn(os.path.join('test', 'nonascii.css'), stats['post_processed'])
  151. def test_css_import_case_insensitive(self):
  152. relpath = self.hashed_file_path("cached/styles_insensitive.css")
  153. self.assertEqual(relpath, "cached/styles_insensitive.c609562b6d3c.css")
  154. with storage.staticfiles_storage.open(relpath) as relfile:
  155. content = relfile.read()
  156. self.assertNotIn(b"cached/other.css", content)
  157. self.assertIn(b"other.d41d8cd98f00.css", content)
  158. @override_settings(
  159. STATICFILES_DIRS=[os.path.join(TEST_ROOT, 'project', 'faulty')],
  160. STATICFILES_FINDERS=['django.contrib.staticfiles.finders.FileSystemFinder'],
  161. )
  162. def test_post_processing_failure(self):
  163. """
  164. Test that post_processing indicates the origin of the error when it
  165. fails. Regression test for #18986.
  166. """
  167. finders.get_finder.cache_clear()
  168. err = six.StringIO()
  169. with self.assertRaises(Exception):
  170. call_command('collectstatic', interactive=False, verbosity=0, stderr=err)
  171. self.assertEqual("Post-processing 'faulty.css' failed!\n\n", err.getvalue())
  172. @override_settings(
  173. STATICFILES_STORAGE='django.contrib.staticfiles.storage.CachedStaticFilesStorage',
  174. )
  175. class TestCollectionCachedStorage(TestHashedFiles, CollectionTestCase):
  176. """
  177. Tests for the Cache busting storage
  178. """
  179. def test_cache_invalidation(self):
  180. name = "cached/styles.css"
  181. hashed_name = "cached/styles.bb84a0240107.css"
  182. # check if the cache is filled correctly as expected
  183. cache_key = storage.staticfiles_storage.hash_key(name)
  184. cached_name = storage.staticfiles_storage.hashed_files.get(cache_key)
  185. self.assertEqual(self.hashed_file_path(name), cached_name)
  186. # clearing the cache to make sure we re-set it correctly in the url method
  187. storage.staticfiles_storage.hashed_files.clear()
  188. cached_name = storage.staticfiles_storage.hashed_files.get(cache_key)
  189. self.assertIsNone(cached_name)
  190. self.assertEqual(self.hashed_file_path(name), hashed_name)
  191. cached_name = storage.staticfiles_storage.hashed_files.get(cache_key)
  192. self.assertEqual(cached_name, hashed_name)
  193. def test_cache_key_memcache_validation(self):
  194. """
  195. Handle cache key creation correctly, see #17861.
  196. """
  197. name = (
  198. "/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff"
  199. "/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff"
  200. "/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff"
  201. "/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff"
  202. "/some crazy/long filename/ with spaces Here and ?#%#$/other/stuff"
  203. "/some crazy/\x16\xb4"
  204. )
  205. cache_key = storage.staticfiles_storage.hash_key(name)
  206. cache_validator = BaseCache({})
  207. cache_validator.validate_key(cache_key)
  208. self.assertEqual(cache_key, 'staticfiles:821ea71ef36f95b3922a77f7364670e7')
  209. @override_settings(
  210. STATICFILES_STORAGE='staticfiles_tests.storage.ExtraPatternsCachedStaticFilesStorage',
  211. )
  212. class TestExtraPatternsCachedStorage(CollectionTestCase):
  213. def setUp(self):
  214. storage.staticfiles_storage.hashed_files.clear() # avoid cache interference
  215. super(TestExtraPatternsCachedStorage, self).setUp()
  216. def cached_file_path(self, path):
  217. fullpath = self.render_template(self.static_template_snippet(path))
  218. return fullpath.replace(settings.STATIC_URL, '')
  219. def test_multi_extension_patterns(self):
  220. """
  221. With storage classes having several file extension patterns, only the
  222. files matching a specific file pattern should be affected by the
  223. substitution (#19670).
  224. """
  225. # CSS files shouldn't be touched by JS patterns.
  226. relpath = self.cached_file_path("cached/import.css")
  227. self.assertEqual(relpath, "cached/import.2b1d40b0bbd4.css")
  228. with storage.staticfiles_storage.open(relpath) as relfile:
  229. self.assertIn(b'import url("styles.bb84a0240107.css")', relfile.read())
  230. # Confirm JS patterns have been applied to JS files.
  231. relpath = self.cached_file_path("cached/test.js")
  232. self.assertEqual(relpath, "cached/test.62789ffcd280.js")
  233. with storage.staticfiles_storage.open(relpath) as relfile:
  234. self.assertIn(b'JS_URL("import.2b1d40b0bbd4.css")', relfile.read())
  235. @override_settings(
  236. STATICFILES_STORAGE='django.contrib.staticfiles.storage.ManifestStaticFilesStorage',
  237. )
  238. class TestCollectionManifestStorage(TestHashedFiles, CollectionTestCase):
  239. """
  240. Tests for the Cache busting storage
  241. """
  242. def setUp(self):
  243. super(TestCollectionManifestStorage, self).setUp()
  244. temp_dir = tempfile.mkdtemp()
  245. os.makedirs(os.path.join(temp_dir, 'test'))
  246. self._clear_filename = os.path.join(temp_dir, 'test', 'cleared.txt')
  247. with open(self._clear_filename, 'w') as f:
  248. f.write('to be deleted in one test')
  249. self.patched_settings = self.settings(
  250. STATICFILES_DIRS=settings.STATICFILES_DIRS + [temp_dir])
  251. self.patched_settings.enable()
  252. self.addCleanup(shutil.rmtree, six.text_type(temp_dir))
  253. def tearDown(self):
  254. self.patched_settings.disable()
  255. if os.path.exists(self._clear_filename):
  256. os.unlink(self._clear_filename)
  257. super(TestCollectionManifestStorage, self).tearDown()
  258. def test_manifest_exists(self):
  259. filename = storage.staticfiles_storage.manifest_name
  260. path = storage.staticfiles_storage.path(filename)
  261. self.assertTrue(os.path.exists(path))
  262. def test_loaded_cache(self):
  263. self.assertNotEqual(storage.staticfiles_storage.hashed_files, {})
  264. manifest_content = storage.staticfiles_storage.read_manifest()
  265. self.assertIn(
  266. '"version": "%s"' % storage.staticfiles_storage.manifest_version,
  267. force_text(manifest_content)
  268. )
  269. def test_parse_cache(self):
  270. hashed_files = storage.staticfiles_storage.hashed_files
  271. manifest = storage.staticfiles_storage.load_manifest()
  272. self.assertEqual(hashed_files, manifest)
  273. def test_clear_empties_manifest(self):
  274. cleared_file_name = os.path.join('test', 'cleared.txt')
  275. # collect the additional file
  276. self.run_collectstatic()
  277. hashed_files = storage.staticfiles_storage.hashed_files
  278. self.assertIn(cleared_file_name, hashed_files)
  279. manifest_content = storage.staticfiles_storage.load_manifest()
  280. self.assertIn(cleared_file_name, manifest_content)
  281. original_path = storage.staticfiles_storage.path(cleared_file_name)
  282. self.assertTrue(os.path.exists(original_path))
  283. # delete the original file form the app, collect with clear
  284. os.unlink(self._clear_filename)
  285. self.run_collectstatic(clear=True)
  286. self.assertFileNotFound(original_path)
  287. hashed_files = storage.staticfiles_storage.hashed_files
  288. self.assertNotIn(cleared_file_name, hashed_files)
  289. manifest_content = storage.staticfiles_storage.load_manifest()
  290. self.assertNotIn(cleared_file_name, manifest_content)
  291. @override_settings(
  292. STATICFILES_STORAGE='staticfiles_tests.storage.SimpleCachedStaticFilesStorage',
  293. )
  294. class TestCollectionSimpleCachedStorage(CollectionTestCase):
  295. """
  296. Tests for the Cache busting storage
  297. """
  298. hashed_file_path = hashed_file_path
  299. def setUp(self):
  300. storage.staticfiles_storage.hashed_files.clear() # avoid cache interference
  301. super(TestCollectionSimpleCachedStorage, self).setUp()
  302. def test_template_tag_return(self):
  303. """
  304. Test the CachedStaticFilesStorage backend.
  305. """
  306. self.assertStaticRaises(ValueError, "does/not/exist.png", "/static/does/not/exist.png")
  307. self.assertStaticRenders("test/file.txt", "/static/test/file.deploy12345.txt")
  308. self.assertStaticRenders("cached/styles.css", "/static/cached/styles.deploy12345.css")
  309. self.assertStaticRenders("path/", "/static/path/")
  310. self.assertStaticRenders("path/?query", "/static/path/?query")
  311. def test_template_tag_simple_content(self):
  312. relpath = self.hashed_file_path("cached/styles.css")
  313. self.assertEqual(relpath, "cached/styles.deploy12345.css")
  314. with storage.staticfiles_storage.open(relpath) as relfile:
  315. content = relfile.read()
  316. self.assertNotIn(b"cached/other.css", content)
  317. self.assertIn(b"other.deploy12345.css", content)
  318. class CustomStaticFilesStorage(storage.StaticFilesStorage):
  319. """
  320. Used in TestStaticFilePermissions
  321. """
  322. def __init__(self, *args, **kwargs):
  323. kwargs['file_permissions_mode'] = 0o640
  324. kwargs['directory_permissions_mode'] = 0o740
  325. super(CustomStaticFilesStorage, self).__init__(*args, **kwargs)
  326. @unittest.skipIf(sys.platform.startswith('win'), "Windows only partially supports chmod.")
  327. class TestStaticFilePermissions(CollectionTestCase):
  328. command_params = {
  329. 'interactive': False,
  330. 'verbosity': 0,
  331. 'ignore_patterns': ['*.ignoreme'],
  332. }
  333. def setUp(self):
  334. self.umask = 0o027
  335. self.old_umask = os.umask(self.umask)
  336. super(TestStaticFilePermissions, self).setUp()
  337. def tearDown(self):
  338. os.umask(self.old_umask)
  339. super(TestStaticFilePermissions, self).tearDown()
  340. # Don't run collectstatic command in this test class.
  341. def run_collectstatic(self, **kwargs):
  342. pass
  343. @override_settings(
  344. FILE_UPLOAD_PERMISSIONS=0o655,
  345. FILE_UPLOAD_DIRECTORY_PERMISSIONS=0o765,
  346. )
  347. def test_collect_static_files_permissions(self):
  348. call_command('collectstatic', **self.command_params)
  349. test_file = os.path.join(settings.STATIC_ROOT, "test.txt")
  350. test_dir = os.path.join(settings.STATIC_ROOT, "subdir")
  351. file_mode = os.stat(test_file)[0] & 0o777
  352. dir_mode = os.stat(test_dir)[0] & 0o777
  353. self.assertEqual(file_mode, 0o655)
  354. self.assertEqual(dir_mode, 0o765)
  355. @override_settings(
  356. FILE_UPLOAD_PERMISSIONS=None,
  357. FILE_UPLOAD_DIRECTORY_PERMISSIONS=None,
  358. )
  359. def test_collect_static_files_default_permissions(self):
  360. call_command('collectstatic', **self.command_params)
  361. test_file = os.path.join(settings.STATIC_ROOT, "test.txt")
  362. test_dir = os.path.join(settings.STATIC_ROOT, "subdir")
  363. file_mode = os.stat(test_file)[0] & 0o777
  364. dir_mode = os.stat(test_dir)[0] & 0o777
  365. self.assertEqual(file_mode, 0o666 & ~self.umask)
  366. self.assertEqual(dir_mode, 0o777 & ~self.umask)
  367. @override_settings(
  368. FILE_UPLOAD_PERMISSIONS=0o655,
  369. FILE_UPLOAD_DIRECTORY_PERMISSIONS=0o765,
  370. STATICFILES_STORAGE='staticfiles_tests.test_storage.CustomStaticFilesStorage',
  371. )
  372. def test_collect_static_files_subclass_of_static_storage(self):
  373. call_command('collectstatic', **self.command_params)
  374. test_file = os.path.join(settings.STATIC_ROOT, "test.txt")
  375. test_dir = os.path.join(settings.STATIC_ROOT, "subdir")
  376. file_mode = os.stat(test_file)[0] & 0o777
  377. dir_mode = os.stat(test_dir)[0] & 0o777
  378. self.assertEqual(file_mode, 0o640)
  379. self.assertEqual(dir_mode, 0o740)