test_storage.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. import os
  2. import shutil
  3. import sys
  4. import tempfile
  5. import unittest
  6. from io import StringIO
  7. from pathlib import Path
  8. from unittest import mock
  9. from django.conf import settings
  10. from django.contrib.staticfiles import finders, storage
  11. from django.contrib.staticfiles.management.commands.collectstatic import (
  12. Command as CollectstaticCommand,
  13. )
  14. from django.core.management import call_command
  15. from django.test import override_settings
  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:
  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 assertPostCondition(self):
  27. """
  28. Assert post conditions for a test are met. Must be manually called at
  29. the end of each test.
  30. """
  31. pass
  32. def test_template_tag_return(self):
  33. self.assertStaticRaises(ValueError, "does/not/exist.png", "/static/does/not/exist.png")
  34. self.assertStaticRenders("test/file.txt", "/static/test/file.dad0999e4f8f.txt")
  35. self.assertStaticRenders("test/file.txt", "/static/test/file.dad0999e4f8f.txt", asvar=True)
  36. self.assertStaticRenders("cached/styles.css", "/static/cached/styles.5e0040571e1a.css")
  37. self.assertStaticRenders("path/", "/static/path/")
  38. self.assertStaticRenders("path/?query", "/static/path/?query")
  39. self.assertPostCondition()
  40. def test_template_tag_simple_content(self):
  41. relpath = self.hashed_file_path("cached/styles.css")
  42. self.assertEqual(relpath, "cached/styles.5e0040571e1a.css")
  43. with storage.staticfiles_storage.open(relpath) as relfile:
  44. content = relfile.read()
  45. self.assertNotIn(b"cached/other.css", content)
  46. self.assertIn(b"other.d41d8cd98f00.css", content)
  47. self.assertPostCondition()
  48. def test_path_ignored_completely(self):
  49. relpath = self.hashed_file_path("cached/css/ignored.css")
  50. self.assertEqual(relpath, "cached/css/ignored.554da52152af.css")
  51. with storage.staticfiles_storage.open(relpath) as relfile:
  52. content = relfile.read()
  53. self.assertIn(b'#foobar', content)
  54. self.assertIn(b'http:foobar', content)
  55. self.assertIn(b'https:foobar', content)
  56. self.assertIn(b'data:foobar', content)
  57. self.assertIn(b'chrome:foobar', content)
  58. self.assertIn(b'//foobar', content)
  59. self.assertPostCondition()
  60. def test_path_with_querystring(self):
  61. relpath = self.hashed_file_path("cached/styles.css?spam=eggs")
  62. self.assertEqual(relpath, "cached/styles.5e0040571e1a.css?spam=eggs")
  63. with storage.staticfiles_storage.open("cached/styles.5e0040571e1a.css") as relfile:
  64. content = relfile.read()
  65. self.assertNotIn(b"cached/other.css", content)
  66. self.assertIn(b"other.d41d8cd98f00.css", content)
  67. self.assertPostCondition()
  68. def test_path_with_fragment(self):
  69. relpath = self.hashed_file_path("cached/styles.css#eggs")
  70. self.assertEqual(relpath, "cached/styles.5e0040571e1a.css#eggs")
  71. with storage.staticfiles_storage.open("cached/styles.5e0040571e1a.css") as relfile:
  72. content = relfile.read()
  73. self.assertNotIn(b"cached/other.css", content)
  74. self.assertIn(b"other.d41d8cd98f00.css", content)
  75. self.assertPostCondition()
  76. def test_path_with_querystring_and_fragment(self):
  77. relpath = self.hashed_file_path("cached/css/fragments.css")
  78. self.assertEqual(relpath, "cached/css/fragments.a60c0e74834f.css")
  79. with storage.staticfiles_storage.open(relpath) as relfile:
  80. content = relfile.read()
  81. self.assertIn(b'fonts/font.b9b105392eb8.eot?#iefix', content)
  82. self.assertIn(b'fonts/font.b8d603e42714.svg#webfontIyfZbseF', content)
  83. self.assertIn(b'fonts/font.b8d603e42714.svg#path/to/../../fonts/font.svg', content)
  84. self.assertIn(b'data:font/woff;charset=utf-8;base64,d09GRgABAAAAADJoAA0AAAAAR2QAAQAAAAAAAAAAAAA', content)
  85. self.assertIn(b'#default#VML', content)
  86. self.assertPostCondition()
  87. def test_template_tag_absolute(self):
  88. relpath = self.hashed_file_path("cached/absolute.css")
  89. self.assertEqual(relpath, "cached/absolute.eb04def9f9a4.css")
  90. with storage.staticfiles_storage.open(relpath) as relfile:
  91. content = relfile.read()
  92. self.assertNotIn(b"/static/cached/styles.css", content)
  93. self.assertIn(b"/static/cached/styles.5e0040571e1a.css", content)
  94. self.assertNotIn(b"/static/styles_root.css", content)
  95. self.assertIn(b"/static/styles_root.401f2509a628.css", content)
  96. self.assertIn(b'/static/cached/img/relative.acae32e4532b.png', content)
  97. self.assertPostCondition()
  98. def test_template_tag_absolute_root(self):
  99. """
  100. Like test_template_tag_absolute, but for a file in STATIC_ROOT (#26249).
  101. """
  102. relpath = self.hashed_file_path("absolute_root.css")
  103. self.assertEqual(relpath, "absolute_root.f821df1b64f7.css")
  104. with storage.staticfiles_storage.open(relpath) as relfile:
  105. content = relfile.read()
  106. self.assertNotIn(b"/static/styles_root.css", content)
  107. self.assertIn(b"/static/styles_root.401f2509a628.css", content)
  108. self.assertPostCondition()
  109. def test_template_tag_relative(self):
  110. relpath = self.hashed_file_path("cached/relative.css")
  111. self.assertEqual(relpath, "cached/relative.c3e9e1ea6f2e.css")
  112. with storage.staticfiles_storage.open(relpath) as relfile:
  113. content = relfile.read()
  114. self.assertNotIn(b"../cached/styles.css", content)
  115. self.assertNotIn(b'@import "styles.css"', content)
  116. self.assertNotIn(b'url(img/relative.png)', content)
  117. self.assertIn(b'url("img/relative.acae32e4532b.png")', content)
  118. self.assertIn(b"../cached/styles.5e0040571e1a.css", content)
  119. self.assertPostCondition()
  120. def test_import_replacement(self):
  121. "See #18050"
  122. relpath = self.hashed_file_path("cached/import.css")
  123. self.assertEqual(relpath, "cached/import.f53576679e5a.css")
  124. with storage.staticfiles_storage.open(relpath) as relfile:
  125. self.assertIn(b"""import url("styles.5e0040571e1a.css")""", relfile.read())
  126. self.assertPostCondition()
  127. def test_template_tag_deep_relative(self):
  128. relpath = self.hashed_file_path("cached/css/window.css")
  129. self.assertEqual(relpath, "cached/css/window.5d5c10836967.css")
  130. with storage.staticfiles_storage.open(relpath) as relfile:
  131. content = relfile.read()
  132. self.assertNotIn(b'url(img/window.png)', content)
  133. self.assertIn(b'url("img/window.acae32e4532b.png")', content)
  134. self.assertPostCondition()
  135. def test_template_tag_url(self):
  136. relpath = self.hashed_file_path("cached/url.css")
  137. self.assertEqual(relpath, "cached/url.902310b73412.css")
  138. with storage.staticfiles_storage.open(relpath) as relfile:
  139. self.assertIn(b"https://", relfile.read())
  140. self.assertPostCondition()
  141. def test_module_import(self):
  142. relpath = self.hashed_file_path('cached/module.js')
  143. self.assertEqual(relpath, 'cached/module.91b9cf9935da.js')
  144. tests = [
  145. # Relative imports.
  146. b'import testConst from "./module_test.d489af3cf882.js";',
  147. b'import relativeModule from "../nested/js/nested.866475c46bb4.js";',
  148. b'import { firstConst, secondConst } from "./module_test.d489af3cf882.js";',
  149. # Absolute import.
  150. b'import rootConst from "/static/absolute_root.5586327fe78c.js";',
  151. # Dynamic import.
  152. b'const dynamicModule = import("./module_test.d489af3cf882.js");',
  153. # Creating a module object.
  154. b'import * as NewModule from "./module_test.d489af3cf882.js";',
  155. # Aliases.
  156. b'import { testConst as alias } from "./module_test.d489af3cf882.js";',
  157. b'import {\n'
  158. b' firstVar as firstVarAlias,\n'
  159. b' secondVar as secondVarAlias\n'
  160. b'} from "./module_test.d489af3cf882.js";',
  161. ]
  162. with storage.staticfiles_storage.open(relpath) as relfile:
  163. content = relfile.read()
  164. for module_import in tests:
  165. with self.subTest(module_import=module_import):
  166. self.assertIn(module_import, content)
  167. self.assertPostCondition()
  168. def test_aggregating_modules(self):
  169. relpath = self.hashed_file_path('cached/module.js')
  170. self.assertEqual(relpath, 'cached/module.91b9cf9935da.js')
  171. tests = [
  172. b'export * from "./module_test.d489af3cf882.js";',
  173. b'export { testConst } from "./module_test.d489af3cf882.js";',
  174. b'export {\n'
  175. b' firstVar as firstVarAlias,\n'
  176. b' secondVar as secondVarAlias\n'
  177. b'} from "./module_test.d489af3cf882.js";',
  178. ]
  179. with storage.staticfiles_storage.open(relpath) as relfile:
  180. content = relfile.read()
  181. for module_import in tests:
  182. with self.subTest(module_import=module_import):
  183. self.assertIn(module_import, content)
  184. self.assertPostCondition()
  185. @override_settings(
  186. STATICFILES_DIRS=[os.path.join(TEST_ROOT, 'project', 'loop')],
  187. STATICFILES_FINDERS=['django.contrib.staticfiles.finders.FileSystemFinder'],
  188. )
  189. def test_import_loop(self):
  190. finders.get_finder.cache_clear()
  191. err = StringIO()
  192. with self.assertRaisesMessage(RuntimeError, 'Max post-process passes exceeded'):
  193. call_command('collectstatic', interactive=False, verbosity=0, stderr=err)
  194. self.assertEqual("Post-processing 'All' failed!\n\n", err.getvalue())
  195. self.assertPostCondition()
  196. def test_post_processing(self):
  197. """
  198. post_processing behaves correctly.
  199. Files that are alterable should always be post-processed; files that
  200. aren't should be skipped.
  201. collectstatic has already been called once in setUp() for this testcase,
  202. therefore we check by verifying behavior on a second run.
  203. """
  204. collectstatic_args = {
  205. 'interactive': False,
  206. 'verbosity': 0,
  207. 'link': False,
  208. 'clear': False,
  209. 'dry_run': False,
  210. 'post_process': True,
  211. 'use_default_ignore_patterns': True,
  212. 'ignore_patterns': ['*.ignoreme'],
  213. }
  214. collectstatic_cmd = CollectstaticCommand()
  215. collectstatic_cmd.set_options(**collectstatic_args)
  216. stats = collectstatic_cmd.collect()
  217. self.assertIn(os.path.join('cached', 'css', 'window.css'), stats['post_processed'])
  218. self.assertIn(os.path.join('cached', 'css', 'img', 'window.png'), stats['unmodified'])
  219. self.assertIn(os.path.join('test', 'nonascii.css'), stats['post_processed'])
  220. # No file should be yielded twice.
  221. self.assertCountEqual(stats['post_processed'], set(stats['post_processed']))
  222. self.assertPostCondition()
  223. def test_css_import_case_insensitive(self):
  224. relpath = self.hashed_file_path("cached/styles_insensitive.css")
  225. self.assertEqual(relpath, "cached/styles_insensitive.3fa427592a53.css")
  226. with storage.staticfiles_storage.open(relpath) as relfile:
  227. content = relfile.read()
  228. self.assertNotIn(b"cached/other.css", content)
  229. self.assertIn(b"other.d41d8cd98f00.css", content)
  230. self.assertPostCondition()
  231. def test_js_source_map(self):
  232. relpath = self.hashed_file_path('cached/source_map.js')
  233. self.assertEqual(relpath, 'cached/source_map.9371cbb02a26.js')
  234. with storage.staticfiles_storage.open(relpath) as relfile:
  235. content = relfile.read()
  236. self.assertNotIn(b'//# sourceMappingURL=source_map.js.map', content)
  237. self.assertIn(
  238. b'//# sourceMappingURL=source_map.js.99914b932bd3.map',
  239. content,
  240. )
  241. self.assertPostCondition()
  242. def test_js_source_map_sensitive(self):
  243. relpath = self.hashed_file_path('cached/source_map_sensitive.js')
  244. self.assertEqual(relpath, 'cached/source_map_sensitive.5da96fdd3cb3.js')
  245. with storage.staticfiles_storage.open(relpath) as relfile:
  246. content = relfile.read()
  247. self.assertIn(b'//# sOuRcEMaPpInGURL=source_map.js.map', content)
  248. self.assertNotIn(
  249. b'//# sourceMappingURL=source_map.js.99914b932bd3.map',
  250. content,
  251. )
  252. self.assertPostCondition()
  253. @override_settings(
  254. STATICFILES_DIRS=[os.path.join(TEST_ROOT, 'project', 'faulty')],
  255. STATICFILES_FINDERS=['django.contrib.staticfiles.finders.FileSystemFinder'],
  256. )
  257. def test_post_processing_failure(self):
  258. """
  259. post_processing indicates the origin of the error when it fails.
  260. """
  261. finders.get_finder.cache_clear()
  262. err = StringIO()
  263. with self.assertRaises(Exception):
  264. call_command('collectstatic', interactive=False, verbosity=0, stderr=err)
  265. self.assertEqual("Post-processing 'faulty.css' failed!\n\n", err.getvalue())
  266. self.assertPostCondition()
  267. @override_settings(STATICFILES_STORAGE='staticfiles_tests.storage.ExtraPatternsStorage')
  268. class TestExtraPatternsStorage(CollectionTestCase):
  269. def setUp(self):
  270. storage.staticfiles_storage.hashed_files.clear() # avoid cache interference
  271. super().setUp()
  272. def cached_file_path(self, path):
  273. fullpath = self.render_template(self.static_template_snippet(path))
  274. return fullpath.replace(settings.STATIC_URL, '')
  275. def test_multi_extension_patterns(self):
  276. """
  277. With storage classes having several file extension patterns, only the
  278. files matching a specific file pattern should be affected by the
  279. substitution (#19670).
  280. """
  281. # CSS files shouldn't be touched by JS patterns.
  282. relpath = self.cached_file_path("cached/import.css")
  283. self.assertEqual(relpath, "cached/import.f53576679e5a.css")
  284. with storage.staticfiles_storage.open(relpath) as relfile:
  285. self.assertIn(b'import url("styles.5e0040571e1a.css")', relfile.read())
  286. # Confirm JS patterns have been applied to JS files.
  287. relpath = self.cached_file_path("cached/test.js")
  288. self.assertEqual(relpath, "cached/test.388d7a790d46.js")
  289. with storage.staticfiles_storage.open(relpath) as relfile:
  290. self.assertIn(b'JS_URL("import.f53576679e5a.css")', relfile.read())
  291. @override_settings(
  292. STATICFILES_STORAGE='django.contrib.staticfiles.storage.ManifestStaticFilesStorage',
  293. )
  294. class TestCollectionManifestStorage(TestHashedFiles, CollectionTestCase):
  295. """
  296. Tests for the Cache busting storage
  297. """
  298. def setUp(self):
  299. super().setUp()
  300. temp_dir = tempfile.mkdtemp()
  301. os.makedirs(os.path.join(temp_dir, 'test'))
  302. self._clear_filename = os.path.join(temp_dir, 'test', 'cleared.txt')
  303. with open(self._clear_filename, 'w') as f:
  304. f.write('to be deleted in one test')
  305. self.patched_settings = self.settings(
  306. STATICFILES_DIRS=settings.STATICFILES_DIRS + [temp_dir],
  307. )
  308. self.patched_settings.enable()
  309. self.addCleanup(shutil.rmtree, temp_dir)
  310. self._manifest_strict = storage.staticfiles_storage.manifest_strict
  311. def tearDown(self):
  312. self.patched_settings.disable()
  313. if os.path.exists(self._clear_filename):
  314. os.unlink(self._clear_filename)
  315. storage.staticfiles_storage.manifest_strict = self._manifest_strict
  316. super().tearDown()
  317. def assertPostCondition(self):
  318. hashed_files = storage.staticfiles_storage.hashed_files
  319. # The in-memory version of the manifest matches the one on disk
  320. # since a properly created manifest should cover all filenames.
  321. if hashed_files:
  322. manifest = storage.staticfiles_storage.load_manifest()
  323. self.assertEqual(hashed_files, manifest)
  324. def test_manifest_exists(self):
  325. filename = storage.staticfiles_storage.manifest_name
  326. path = storage.staticfiles_storage.path(filename)
  327. self.assertTrue(os.path.exists(path))
  328. def test_manifest_does_not_exist(self):
  329. storage.staticfiles_storage.manifest_name = 'does.not.exist.json'
  330. self.assertIsNone(storage.staticfiles_storage.read_manifest())
  331. def test_manifest_does_not_ignore_permission_error(self):
  332. with mock.patch('builtins.open', side_effect=PermissionError):
  333. with self.assertRaises(PermissionError):
  334. storage.staticfiles_storage.read_manifest()
  335. def test_loaded_cache(self):
  336. self.assertNotEqual(storage.staticfiles_storage.hashed_files, {})
  337. manifest_content = storage.staticfiles_storage.read_manifest()
  338. self.assertIn(
  339. '"version": "%s"' % storage.staticfiles_storage.manifest_version,
  340. manifest_content
  341. )
  342. def test_parse_cache(self):
  343. hashed_files = storage.staticfiles_storage.hashed_files
  344. manifest = storage.staticfiles_storage.load_manifest()
  345. self.assertEqual(hashed_files, manifest)
  346. def test_clear_empties_manifest(self):
  347. cleared_file_name = storage.staticfiles_storage.clean_name(os.path.join('test', 'cleared.txt'))
  348. # collect the additional file
  349. self.run_collectstatic()
  350. hashed_files = storage.staticfiles_storage.hashed_files
  351. self.assertIn(cleared_file_name, hashed_files)
  352. manifest_content = storage.staticfiles_storage.load_manifest()
  353. self.assertIn(cleared_file_name, manifest_content)
  354. original_path = storage.staticfiles_storage.path(cleared_file_name)
  355. self.assertTrue(os.path.exists(original_path))
  356. # delete the original file form the app, collect with clear
  357. os.unlink(self._clear_filename)
  358. self.run_collectstatic(clear=True)
  359. self.assertFileNotFound(original_path)
  360. hashed_files = storage.staticfiles_storage.hashed_files
  361. self.assertNotIn(cleared_file_name, hashed_files)
  362. manifest_content = storage.staticfiles_storage.load_manifest()
  363. self.assertNotIn(cleared_file_name, manifest_content)
  364. def test_missing_entry(self):
  365. missing_file_name = 'cached/missing.css'
  366. configured_storage = storage.staticfiles_storage
  367. self.assertNotIn(missing_file_name, configured_storage.hashed_files)
  368. # File name not found in manifest
  369. with self.assertRaisesMessage(ValueError, "Missing staticfiles manifest entry for '%s'" % missing_file_name):
  370. self.hashed_file_path(missing_file_name)
  371. configured_storage.manifest_strict = False
  372. # File doesn't exist on disk
  373. err_msg = "The file '%s' could not be found with %r." % (missing_file_name, configured_storage._wrapped)
  374. with self.assertRaisesMessage(ValueError, err_msg):
  375. self.hashed_file_path(missing_file_name)
  376. content = StringIO()
  377. content.write('Found')
  378. configured_storage.save(missing_file_name, content)
  379. # File exists on disk
  380. self.hashed_file_path(missing_file_name)
  381. def test_intermediate_files(self):
  382. cached_files = os.listdir(os.path.join(settings.STATIC_ROOT, 'cached'))
  383. # Intermediate files shouldn't be created for reference.
  384. self.assertEqual(
  385. len([
  386. cached_file
  387. for cached_file in cached_files
  388. if cached_file.startswith('relative.')
  389. ]),
  390. 2,
  391. )
  392. @override_settings(STATICFILES_STORAGE='staticfiles_tests.storage.NoneHashStorage')
  393. class TestCollectionNoneHashStorage(CollectionTestCase):
  394. hashed_file_path = hashed_file_path
  395. def test_hashed_name(self):
  396. relpath = self.hashed_file_path('cached/styles.css')
  397. self.assertEqual(relpath, 'cached/styles.css')
  398. @override_settings(STATICFILES_STORAGE='staticfiles_tests.storage.SimpleStorage')
  399. class TestCollectionSimpleStorage(CollectionTestCase):
  400. hashed_file_path = hashed_file_path
  401. def setUp(self):
  402. storage.staticfiles_storage.hashed_files.clear() # avoid cache interference
  403. super().setUp()
  404. def test_template_tag_return(self):
  405. self.assertStaticRaises(ValueError, "does/not/exist.png", "/static/does/not/exist.png")
  406. self.assertStaticRenders("test/file.txt", "/static/test/file.deploy12345.txt")
  407. self.assertStaticRenders("cached/styles.css", "/static/cached/styles.deploy12345.css")
  408. self.assertStaticRenders("path/", "/static/path/")
  409. self.assertStaticRenders("path/?query", "/static/path/?query")
  410. def test_template_tag_simple_content(self):
  411. relpath = self.hashed_file_path("cached/styles.css")
  412. self.assertEqual(relpath, "cached/styles.deploy12345.css")
  413. with storage.staticfiles_storage.open(relpath) as relfile:
  414. content = relfile.read()
  415. self.assertNotIn(b"cached/other.css", content)
  416. self.assertIn(b"other.deploy12345.css", content)
  417. class CustomStaticFilesStorage(storage.StaticFilesStorage):
  418. """
  419. Used in TestStaticFilePermissions
  420. """
  421. def __init__(self, *args, **kwargs):
  422. kwargs['file_permissions_mode'] = 0o640
  423. kwargs['directory_permissions_mode'] = 0o740
  424. super().__init__(*args, **kwargs)
  425. @unittest.skipIf(sys.platform == 'win32', "Windows only partially supports chmod.")
  426. class TestStaticFilePermissions(CollectionTestCase):
  427. command_params = {
  428. 'interactive': False,
  429. 'verbosity': 0,
  430. 'ignore_patterns': ['*.ignoreme'],
  431. }
  432. def setUp(self):
  433. self.umask = 0o027
  434. self.old_umask = os.umask(self.umask)
  435. super().setUp()
  436. def tearDown(self):
  437. os.umask(self.old_umask)
  438. super().tearDown()
  439. # Don't run collectstatic command in this test class.
  440. def run_collectstatic(self, **kwargs):
  441. pass
  442. @override_settings(
  443. FILE_UPLOAD_PERMISSIONS=0o655,
  444. FILE_UPLOAD_DIRECTORY_PERMISSIONS=0o765,
  445. )
  446. def test_collect_static_files_permissions(self):
  447. call_command('collectstatic', **self.command_params)
  448. static_root = Path(settings.STATIC_ROOT)
  449. test_file = static_root / 'test.txt'
  450. file_mode = test_file.stat().st_mode & 0o777
  451. self.assertEqual(file_mode, 0o655)
  452. tests = [
  453. static_root / 'subdir',
  454. static_root / 'nested',
  455. static_root / 'nested' / 'css',
  456. ]
  457. for directory in tests:
  458. with self.subTest(directory=directory):
  459. dir_mode = directory.stat().st_mode & 0o777
  460. self.assertEqual(dir_mode, 0o765)
  461. @override_settings(
  462. FILE_UPLOAD_PERMISSIONS=None,
  463. FILE_UPLOAD_DIRECTORY_PERMISSIONS=None,
  464. )
  465. def test_collect_static_files_default_permissions(self):
  466. call_command('collectstatic', **self.command_params)
  467. static_root = Path(settings.STATIC_ROOT)
  468. test_file = static_root / 'test.txt'
  469. file_mode = test_file.stat().st_mode & 0o777
  470. self.assertEqual(file_mode, 0o666 & ~self.umask)
  471. tests = [
  472. static_root / 'subdir',
  473. static_root / 'nested',
  474. static_root / 'nested' / 'css',
  475. ]
  476. for directory in tests:
  477. with self.subTest(directory=directory):
  478. dir_mode = directory.stat().st_mode & 0o777
  479. self.assertEqual(dir_mode, 0o777 & ~self.umask)
  480. @override_settings(
  481. FILE_UPLOAD_PERMISSIONS=0o655,
  482. FILE_UPLOAD_DIRECTORY_PERMISSIONS=0o765,
  483. STATICFILES_STORAGE='staticfiles_tests.test_storage.CustomStaticFilesStorage',
  484. )
  485. def test_collect_static_files_subclass_of_static_storage(self):
  486. call_command('collectstatic', **self.command_params)
  487. static_root = Path(settings.STATIC_ROOT)
  488. test_file = static_root / 'test.txt'
  489. file_mode = test_file.stat().st_mode & 0o777
  490. self.assertEqual(file_mode, 0o640)
  491. tests = [
  492. static_root / 'subdir',
  493. static_root / 'nested',
  494. static_root / 'nested' / 'css',
  495. ]
  496. for directory in tests:
  497. with self.subTest(directory=directory):
  498. dir_mode = directory.stat().st_mode & 0o777
  499. self.assertEqual(dir_mode, 0o740)
  500. @override_settings(
  501. STATICFILES_STORAGE='django.contrib.staticfiles.storage.ManifestStaticFilesStorage',
  502. )
  503. class TestCollectionHashedFilesCache(CollectionTestCase):
  504. """
  505. Files referenced from CSS use the correct final hashed name regardless of
  506. the order in which the files are post-processed.
  507. """
  508. hashed_file_path = hashed_file_path
  509. def setUp(self):
  510. super().setUp()
  511. self._temp_dir = temp_dir = tempfile.mkdtemp()
  512. os.makedirs(os.path.join(temp_dir, 'test'))
  513. self.addCleanup(shutil.rmtree, temp_dir)
  514. def _get_filename_path(self, filename):
  515. return os.path.join(self._temp_dir, 'test', filename)
  516. def test_file_change_after_collectstatic(self):
  517. # Create initial static files.
  518. file_contents = (
  519. ('foo.png', 'foo'),
  520. ('bar.css', 'url("foo.png")\nurl("xyz.png")'),
  521. ('xyz.png', 'xyz'),
  522. )
  523. for filename, content in file_contents:
  524. with open(self._get_filename_path(filename), 'w') as f:
  525. f.write(content)
  526. with self.modify_settings(STATICFILES_DIRS={'append': self._temp_dir}):
  527. finders.get_finder.cache_clear()
  528. err = StringIO()
  529. # First collectstatic run.
  530. call_command('collectstatic', interactive=False, verbosity=0, stderr=err)
  531. relpath = self.hashed_file_path('test/bar.css')
  532. with storage.staticfiles_storage.open(relpath) as relfile:
  533. content = relfile.read()
  534. self.assertIn(b'foo.acbd18db4cc2.png', content)
  535. self.assertIn(b'xyz.d16fb36f0911.png', content)
  536. # Change the contents of the png files.
  537. for filename in ('foo.png', 'xyz.png'):
  538. with open(self._get_filename_path(filename), 'w+b') as f:
  539. f.write(b"new content of file to change its hash")
  540. # The hashes of the png files in the CSS file are updated after
  541. # a second collectstatic.
  542. call_command('collectstatic', interactive=False, verbosity=0, stderr=err)
  543. relpath = self.hashed_file_path('test/bar.css')
  544. with storage.staticfiles_storage.open(relpath) as relfile:
  545. content = relfile.read()
  546. self.assertIn(b'foo.57a5cb9ba68d.png', content)
  547. self.assertIn(b'xyz.57a5cb9ba68d.png', content)