Debian Codegraph @

tests/staticfiles_tests/test_storage.py

raw
1 import json
2 import os
3 import shutil
4 import sys
5 import tempfile
6 import unittest
7 from io import StringIO
8 from pathlib import Path
9 from unittest import mock
10
11 from django.conf import STATICFILES_STORAGE_ALIAS, settings
12 from django.contrib.staticfiles import finders, storage
13 from django.contrib.staticfiles.management.commands.collectstatic import (
14 Command as CollectstaticCommand,
15 )
16 from django.core.management import call_command
17 from django.test import SimpleTestCase, override_settings
18
19 from .cases import CollectionTestCase
20 from .settings import TEST_ROOT
21
22
23 def hashed_file_path(test, path):
24 fullpath = test.render_template(test.static_template_snippet(path))
25 return fullpath.removeprefix(settings.STATIC_URL)
26
27
28 class TestHashedFiles:
29 hashed_file_path = hashed_file_path
30
31 def tearDown(self):
32 # Clear hashed files to avoid side effects among tests.
33 storage.staticfiles_storage.hashed_files.clear()
34
35 def assertPostCondition(self):
36 """
37 Assert post conditions for a test are met. Must be manually called at
38 the end of each test.
39 """
40 pass
41
42 def test_template_tag_return(self):
43 self.assertStaticRaises(
44 ValueError, "does/not/exist.png", "/static/does/not/exist.png"
45 )
46 self.assertStaticRenders("test/file.txt", "/static/test/file.dad0999e4f8f.txt")
47 self.assertStaticRenders(
48 "test/file.txt", "/static/test/file.dad0999e4f8f.txt", asvar=True
49 )
50 self.assertStaticRenders(
51 "cached/styles.css", "/static/cached/styles.5e0040571e1a.css"
52 )
53 self.assertStaticRenders("path/", "/static/path/")
54 self.assertStaticRenders("path/?query", "/static/path/?query")
55 self.assertPostCondition()
56
57 def test_template_tag_simple_content(self):
58 relpath = self.hashed_file_path("cached/styles.css")
59 self.assertEqual(relpath, "cached/styles.5e0040571e1a.css")
60 with storage.staticfiles_storage.open(relpath) as relfile:
61 content = relfile.read()
62 self.assertNotIn(b"cached/other.css", content)
63 self.assertIn(b"other.d41d8cd98f00.css", content)
64 self.assertPostCondition()
65
66 def test_path_ignored_completely(self):
67 relpath = self.hashed_file_path("cached/css/ignored.css")
68 self.assertEqual(relpath, "cached/css/ignored.55e7c226dda1.css")
69 with storage.staticfiles_storage.open(relpath) as relfile:
70 content = relfile.read()
71 self.assertIn(b"#foobar", content)
72 self.assertIn(b"http:foobar", content)
73 self.assertIn(b"https:foobar", content)
74 self.assertIn(b"data:foobar", content)
75 self.assertIn(b"chrome:foobar", content)
76 self.assertIn(b"//foobar", content)
77 self.assertIn(b"url()", content)
78 self.assertPostCondition()
79
80 def test_path_with_querystring(self):
81 relpath = self.hashed_file_path("cached/styles.css?spam=eggs")
82 self.assertEqual(relpath, "cached/styles.5e0040571e1a.css?spam=eggs")
83 with storage.staticfiles_storage.open(
84 "cached/styles.5e0040571e1a.css"
85 ) as relfile:
86 content = relfile.read()
87 self.assertNotIn(b"cached/other.css", content)
88 self.assertIn(b"other.d41d8cd98f00.css", content)
89 self.assertPostCondition()
90
91 def test_path_with_fragment(self):
92 relpath = self.hashed_file_path("cached/styles.css#eggs")
93 self.assertEqual(relpath, "cached/styles.5e0040571e1a.css#eggs")
94 with storage.staticfiles_storage.open(
95 "cached/styles.5e0040571e1a.css"
96 ) as relfile:
97 content = relfile.read()
98 self.assertNotIn(b"cached/other.css", content)
99 self.assertIn(b"other.d41d8cd98f00.css", content)
100 self.assertPostCondition()
101
102 def test_path_with_querystring_and_fragment(self):
103 relpath = self.hashed_file_path("cached/css/fragments.css")
104 self.assertEqual(relpath, "cached/css/fragments.7fe344dee895.css")
105 with storage.staticfiles_storage.open(relpath) as relfile:
106 content = relfile.read()
107 self.assertIn(b"fonts/font.b9b105392eb8.eot?#iefix", content)
108 self.assertIn(b"fonts/font.b8d603e42714.svg#webfontIyfZbseF", content)
109 self.assertIn(
110 b"fonts/font.b8d603e42714.svg#path/to/../../fonts/font.svg", content
111 )
112 self.assertIn(
113 b"data:font/woff;charset=utf-8;"
114 b"base64,d09GRgABAAAAADJoAA0AAAAAR2QAAQAAAAAAAAAAAAA",
115 content,
116 )
117 self.assertIn(b"#default#VML", content)
118 self.assertPostCondition()
119
120 def test_template_tag_absolute(self):
121 relpath = self.hashed_file_path("cached/absolute.css")
122 self.assertEqual(relpath, "cached/absolute.eb04def9f9a4.css")
123 with storage.staticfiles_storage.open(relpath) as relfile:
124 content = relfile.read()
125 self.assertNotIn(b"/static/cached/styles.css", content)
126 self.assertIn(b"/static/cached/styles.5e0040571e1a.css", content)
127 self.assertNotIn(b"/static/styles_root.css", content)
128 self.assertIn(b"/static/styles_root.401f2509a628.css", content)
129 self.assertIn(b"/static/cached/img/relative.acae32e4532b.png", content)
130 self.assertPostCondition()
131
132 def test_template_tag_absolute_root(self):
133 """
134 Like test_template_tag_absolute, but for a file in STATIC_ROOT (#26249).
135 """
136 relpath = self.hashed_file_path("absolute_root.css")
137 self.assertEqual(relpath, "absolute_root.f821df1b64f7.css")
138 with storage.staticfiles_storage.open(relpath) as relfile:
139 content = relfile.read()
140 self.assertNotIn(b"/static/styles_root.css", content)
141 self.assertIn(b"/static/styles_root.401f2509a628.css", content)
142 self.assertPostCondition()
143
144 def test_template_tag_relative(self):
145 relpath = self.hashed_file_path("cached/relative.css")
146 self.assertEqual(relpath, "cached/relative.c3e9e1ea6f2e.css")
147 with storage.staticfiles_storage.open(relpath) as relfile:
148 content = relfile.read()
149 self.assertNotIn(b"../cached/styles.css", content)
150 self.assertNotIn(b'@import "styles.css"', content)
151 self.assertNotIn(b"url(img/relative.png)", content)
152 self.assertIn(b'url("img/relative.acae32e4532b.png")', content)
153 self.assertIn(b"../cached/styles.5e0040571e1a.css", content)
154 self.assertPostCondition()
155
156 def test_import_replacement(self):
157 "See #18050"
158 relpath = self.hashed_file_path("cached/import.css")
159 self.assertEqual(relpath, "cached/import.f53576679e5a.css")
160 with storage.staticfiles_storage.open(relpath) as relfile:
161 self.assertIn(b"""import url("styles.5e0040571e1a.css")""", relfile.read())
162 self.assertPostCondition()
163
164 def test_template_tag_deep_relative(self):
165 relpath = self.hashed_file_path("cached/css/window.css")
166 self.assertEqual(relpath, "cached/css/window.5d5c10836967.css")
167 with storage.staticfiles_storage.open(relpath) as relfile:
168 content = relfile.read()
169 self.assertNotIn(b"url(img/window.png)", content)
170 self.assertIn(b'url("img/window.acae32e4532b.png")', content)
171 self.assertPostCondition()
172
173 def test_template_tag_url(self):
174 relpath = self.hashed_file_path("cached/url.css")
175 self.assertEqual(relpath, "cached/url.902310b73412.css")
176 with storage.staticfiles_storage.open(relpath) as relfile:
177 self.assertIn(b"https://", relfile.read())
178 self.assertPostCondition()
179
180 @override_settings(
181 STATICFILES_DIRS=[os.path.join(TEST_ROOT, "project", "loop")],
182 STATICFILES_FINDERS=["django.contrib.staticfiles.finders.FileSystemFinder"],
183 )
184 def test_import_loop(self):
185 finders.get_finder.cache_clear()
186 err = StringIO()
187 with self.assertRaisesMessage(RuntimeError, "Max post-process passes exceeded"):
188 call_command("collectstatic", interactive=False, verbosity=0, stderr=err)
189 self.assertEqual(
190 "Post-processing 'bar.css, foo.css' failed!\n\n", err.getvalue()
191 )
192 self.assertPostCondition()
193
194 def test_post_processing(self):
195 """
196 post_processing behaves correctly.
197
198 Files that are alterable should always be post-processed; files that
199 aren't should be skipped.
200
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
215 collectstatic_cmd = CollectstaticCommand()
216 collectstatic_cmd.set_options(**collectstatic_args)
217 stats = collectstatic_cmd.collect()
218 self.assertIn(
219 os.path.join("cached", "css", "window.css"), stats["post_processed"]
220 )
221 self.assertIn(
222 os.path.join("cached", "css", "img", "window.png"), stats["unmodified"]
223 )
224 self.assertIn(os.path.join("test", "nonascii.css"), stats["post_processed"])
225 # No file should be yielded twice.
226 self.assertCountEqual(stats["post_processed"], set(stats["post_processed"]))
227 self.assertPostCondition()
228
229 def test_css_import_case_insensitive(self):
230 relpath = self.hashed_file_path("cached/styles_insensitive.css")
231 self.assertEqual(relpath, "cached/styles_insensitive.3fa427592a53.css")
232 with storage.staticfiles_storage.open(relpath) as relfile:
233 content = relfile.read()
234 self.assertNotIn(b"cached/other.css", content)
235 self.assertIn(b"other.d41d8cd98f00.css", content)
236 self.assertPostCondition()
237
238 def test_css_source_map(self):
239 relpath = self.hashed_file_path("cached/source_map.css")
240 self.assertEqual(relpath, "cached/source_map.b2fceaf426aa.css")
241 with storage.staticfiles_storage.open(relpath) as relfile:
242 content = relfile.read()
243 self.assertNotIn(b"/*# sourceMappingURL=source_map.css.map*/", content)
244 self.assertIn(
245 b"/*# sourceMappingURL=source_map.css.99914b932bd3.map */",
246 content,
247 )
248 self.assertPostCondition()
249
250 def test_css_source_map_tabs(self):
251 relpath = self.hashed_file_path("cached/source_map_tabs.css")
252 self.assertEqual(relpath, "cached/source_map_tabs.b2fceaf426aa.css")
253 with storage.staticfiles_storage.open(relpath) as relfile:
254 content = relfile.read()
255 self.assertNotIn(b"/*#\tsourceMappingURL=source_map.css.map\t*/", content)
256 self.assertIn(
257 b"/*# sourceMappingURL=source_map.css.99914b932bd3.map */",
258 content,
259 )
260 self.assertPostCondition()
261
262 def test_css_source_map_sensitive(self):
263 relpath = self.hashed_file_path("cached/source_map_sensitive.css")
264 self.assertEqual(relpath, "cached/source_map_sensitive.456683f2106f.css")
265 with storage.staticfiles_storage.open(relpath) as relfile:
266 content = relfile.read()
267 self.assertIn(b"/*# sOuRcEMaPpInGURL=source_map.css.map */", content)
268 self.assertNotIn(
269 b"/*# sourceMappingURL=source_map.css.99914b932bd3.map */",
270 content,
271 )
272 self.assertPostCondition()
273
274 def test_css_source_map_data_uri(self):
275 relpath = self.hashed_file_path("cached/source_map_data_uri.css")
276 self.assertEqual(relpath, "cached/source_map_data_uri.3166be10260d.css")
277 with storage.staticfiles_storage.open(relpath) as relfile:
278 content = relfile.read()
279 source_map_data_uri = (
280 b"/*# sourceMappingURL=data:application/json;charset=utf8;base64,"
281 b"eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIl9zcmMv*/"
282 )
283 self.assertIn(source_map_data_uri, content)
284 self.assertPostCondition()
285
286 def test_js_source_map(self):
287 relpath = self.hashed_file_path("cached/source_map.js")
288 self.assertEqual(relpath, "cached/source_map.cd45b8534a87.js")
289 with storage.staticfiles_storage.open(relpath) as relfile:
290 content = relfile.read()
291 self.assertNotIn(b"//# sourceMappingURL=source_map.js.map", content)
292 self.assertIn(
293 b"//# sourceMappingURL=source_map.js.99914b932bd3.map",
294 content,
295 )
296 self.assertPostCondition()
297
298 def test_js_source_map_trailing_whitespace(self):
299 relpath = self.hashed_file_path("cached/source_map_trailing_whitespace.js")
300 self.assertEqual(
301 relpath, "cached/source_map_trailing_whitespace.cd45b8534a87.js"
302 )
303 with storage.staticfiles_storage.open(relpath) as relfile:
304 content = relfile.read()
305 self.assertNotIn(b"//# sourceMappingURL=source_map.js.map\t ", content)
306 self.assertIn(
307 b"//# sourceMappingURL=source_map.js.99914b932bd3.map",
308 content,
309 )
310 self.assertPostCondition()
311
312 def test_js_source_map_sensitive(self):
313 relpath = self.hashed_file_path("cached/source_map_sensitive.js")
314 self.assertEqual(relpath, "cached/source_map_sensitive.5da96fdd3cb3.js")
315 with storage.staticfiles_storage.open(relpath) as relfile:
316 content = relfile.read()
317 self.assertIn(b"//# sOuRcEMaPpInGURL=source_map.js.map", content)
318 self.assertNotIn(
319 b"//# sourceMappingURL=source_map.js.99914b932bd3.map",
320 content,
321 )
322 self.assertPostCondition()
323
324 def test_js_source_map_data_uri(self):
325 relpath = self.hashed_file_path("cached/source_map_data_uri.js")
326 self.assertEqual(relpath, "cached/source_map_data_uri.a68d23cbf6dd.js")
327 with storage.staticfiles_storage.open(relpath) as relfile:
328 content = relfile.read()
329 source_map_data_uri = (
330 b"//# sourceMappingURL=data:application/json;charset=utf8;base64,"
331 b"eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIl9zcmMv"
332 )
333 self.assertIn(source_map_data_uri, content)
334 self.assertPostCondition()
335
336 @override_settings(
337 STATICFILES_DIRS=[os.path.join(TEST_ROOT, "project", "faulty")],
338 STATICFILES_FINDERS=["django.contrib.staticfiles.finders.FileSystemFinder"],
339 )
340 def test_post_processing_failure(self):
341 """
342 post_processing indicates the origin of the error when it fails.
343 """
344 finders.get_finder.cache_clear()
345 err = StringIO()
346 with self.assertRaises(Exception):
347 call_command("collectstatic", interactive=False, verbosity=0, stderr=err)
348 self.assertEqual("Post-processing 'faulty.css' failed!\n\n", err.getvalue())
349 self.assertPostCondition()
350
351 @override_settings(
352 STATICFILES_DIRS=[os.path.join(TEST_ROOT, "project", "nonutf8")],
353 STATICFILES_FINDERS=["django.contrib.staticfiles.finders.FileSystemFinder"],
354 )
355 def test_post_processing_nonutf8(self):
356 finders.get_finder.cache_clear()
357 err = StringIO()
358 with self.assertRaises(UnicodeDecodeError):
359 call_command("collectstatic", interactive=False, verbosity=0, stderr=err)
360 self.assertEqual("Post-processing 'nonutf8.css' failed!\n\n", err.getvalue())
361 self.assertPostCondition()
362
363
364 @override_settings(
365 STORAGES={
366 **settings.STORAGES,
367 STATICFILES_STORAGE_ALIAS: {
368 "BACKEND": "staticfiles_tests.storage.ExtraPatternsStorage",
369 },
370 }
371 )
372 class TestExtraPatternsStorage(CollectionTestCase):
373 def setUp(self):
374 storage.staticfiles_storage.hashed_files.clear() # avoid cache interference
375 super().setUp()
376
377 def cached_file_path(self, path):
378 fullpath = self.render_template(self.static_template_snippet(path))
379 return fullpath.replace(settings.STATIC_URL, "")
380
381 def test_multi_extension_patterns(self):
382 """
383 With storage classes having several file extension patterns, only the
384 files matching a specific file pattern should be affected by the
385 substitution (#19670).
386 """
387 # CSS files shouldn't be touched by JS patterns.
388 relpath = self.cached_file_path("cached/import.css")
389 self.assertEqual(relpath, "cached/import.f53576679e5a.css")
390 with storage.staticfiles_storage.open(relpath) as relfile:
391 self.assertIn(b'import url("styles.5e0040571e1a.css")', relfile.read())
392
393 # Confirm JS patterns have been applied to JS files.
394 relpath = self.cached_file_path("cached/test.js")
395 self.assertEqual(relpath, "cached/test.388d7a790d46.js")
396 with storage.staticfiles_storage.open(relpath) as relfile:
397 self.assertIn(b'JS_URL("import.f53576679e5a.css")', relfile.read())
398
399
400 @override_settings(
401 STORAGES={
402 **settings.STORAGES,
403 STATICFILES_STORAGE_ALIAS: {
404 "BACKEND": "django.contrib.staticfiles.storage.ManifestStaticFilesStorage",
405 },
406 }
407 )
408 class TestCollectionManifestStorage(TestHashedFiles, CollectionTestCase):
409 """
410 Tests for the Cache busting storage
411 """
412
413 def setUp(self):
414 super().setUp()
415
416 temp_dir = tempfile.mkdtemp()
417 os.makedirs(os.path.join(temp_dir, "test"))
418 self._clear_filename = os.path.join(temp_dir, "test", "cleared.txt")
419 with open(self._clear_filename, "w") as f:
420 f.write("to be deleted in one test")
421
422 patched_settings = self.settings(
423 STATICFILES_DIRS=settings.STATICFILES_DIRS + [temp_dir],
424 )
425 patched_settings.enable()
426 self.addCleanup(patched_settings.disable)
427 self.addCleanup(shutil.rmtree, temp_dir)
428 self._manifest_strict = storage.staticfiles_storage.manifest_strict
429
430 def tearDown(self):
431 if os.path.exists(self._clear_filename):
432 os.unlink(self._clear_filename)
433
434 storage.staticfiles_storage.manifest_strict = self._manifest_strict
435 super().tearDown()
436
437 def assertPostCondition(self):
438 hashed_files = storage.staticfiles_storage.hashed_files
439 # The in-memory version of the manifest matches the one on disk
440 # since a properly created manifest should cover all filenames.
441 if hashed_files:
442 manifest, _ = storage.staticfiles_storage.load_manifest()
443 self.assertEqual(hashed_files, manifest)
444
445 def test_manifest_exists(self):
446 filename = storage.staticfiles_storage.manifest_name
447 path = storage.staticfiles_storage.path(filename)
448 self.assertTrue(os.path.exists(path))
449
450 def test_manifest_does_not_exist(self):
451 storage.staticfiles_storage.manifest_name = "does.not.exist.json"
452 self.assertIsNone(storage.staticfiles_storage.read_manifest())
453
454 def test_manifest_does_not_ignore_permission_error(self):
455 with mock.patch("builtins.open", side_effect=PermissionError):
456 with self.assertRaises(PermissionError):
457 storage.staticfiles_storage.read_manifest()
458
459 def test_loaded_cache(self):
460 self.assertNotEqual(storage.staticfiles_storage.hashed_files, {})
461 manifest_content = storage.staticfiles_storage.read_manifest()
462 self.assertIn(
463 '"version": "%s"' % storage.staticfiles_storage.manifest_version,
464 manifest_content,
465 )
466
467 def test_parse_cache(self):
468 hashed_files = storage.staticfiles_storage.hashed_files
469 manifest, _ = storage.staticfiles_storage.load_manifest()
470 self.assertEqual(hashed_files, manifest)
471
472 def test_clear_empties_manifest(self):
473 cleared_file_name = storage.staticfiles_storage.clean_name(
474 os.path.join("test", "cleared.txt")
475 )
476 # collect the additional file
477 self.run_collectstatic()
478
479 hashed_files = storage.staticfiles_storage.hashed_files
480 self.assertIn(cleared_file_name, hashed_files)
481
482 manifest_content, _ = storage.staticfiles_storage.load_manifest()
483 self.assertIn(cleared_file_name, manifest_content)
484
485 original_path = storage.staticfiles_storage.path(cleared_file_name)
486 self.assertTrue(os.path.exists(original_path))
487
488 # delete the original file form the app, collect with clear
489 os.unlink(self._clear_filename)
490 self.run_collectstatic(clear=True)
491
492 self.assertFileNotFound(original_path)
493
494 hashed_files = storage.staticfiles_storage.hashed_files
495 self.assertNotIn(cleared_file_name, hashed_files)
496
497 manifest_content, _ = storage.staticfiles_storage.load_manifest()
498 self.assertNotIn(cleared_file_name, manifest_content)
499
500 def test_missing_entry(self):
501 missing_file_name = "cached/missing.css"
502 configured_storage = storage.staticfiles_storage
503 self.assertNotIn(missing_file_name, configured_storage.hashed_files)
504
505 # File name not found in manifest
506 with self.assertRaisesMessage(
507 ValueError,
508 "Missing staticfiles manifest entry for '%s'" % missing_file_name,
509 ):
510 self.hashed_file_path(missing_file_name)
511
512 configured_storage.manifest_strict = False
513 # File doesn't exist on disk
514 err_msg = "The file '%s' could not be found with %r." % (
515 missing_file_name,
516 configured_storage._wrapped,
517 )
518 with self.assertRaisesMessage(ValueError, err_msg):
519 self.hashed_file_path(missing_file_name)
520
521 content = StringIO()
522 content.write("Found")
523 configured_storage.save(missing_file_name, content)
524 # File exists on disk
525 self.hashed_file_path(missing_file_name)
526
527 def test_intermediate_files(self):
528 cached_files = os.listdir(os.path.join(settings.STATIC_ROOT, "cached"))
529 # Intermediate files shouldn't be created for reference.
530 self.assertEqual(
531 len(
532 [
533 cached_file
534 for cached_file in cached_files
535 if cached_file.startswith("relative.")
536 ]
537 ),
538 2,
539 )
540
541 def test_manifest_hash(self):
542 # Collect the additional file.
543 self.run_collectstatic()
544
545 _, manifest_hash_orig = storage.staticfiles_storage.load_manifest()
546 self.assertNotEqual(manifest_hash_orig, "")
547 self.assertEqual(storage.staticfiles_storage.manifest_hash, manifest_hash_orig)
548 # Saving doesn't change the hash.
549 storage.staticfiles_storage.save_manifest()
550 self.assertEqual(storage.staticfiles_storage.manifest_hash, manifest_hash_orig)
551 # Delete the original file from the app, collect with clear.
552 os.unlink(self._clear_filename)
553 self.run_collectstatic(clear=True)
554 # Hash is changed.
555 _, manifest_hash = storage.staticfiles_storage.load_manifest()
556 self.assertNotEqual(manifest_hash, manifest_hash_orig)
557
558 def test_manifest_hash_v1(self):
559 storage.staticfiles_storage.manifest_name = "staticfiles_v1.json"
560 manifest_content, manifest_hash = storage.staticfiles_storage.load_manifest()
561 self.assertEqual(manifest_hash, "")
562 self.assertEqual(manifest_content, {"dummy.txt": "dummy.txt"})
563
564
565 @override_settings(
566 STATIC_URL="/",
567 STORAGES={
568 **settings.STORAGES,
569 STATICFILES_STORAGE_ALIAS: {
570 "BACKEND": "django.contrib.staticfiles.storage.ManifestStaticFilesStorage",
571 },
572 },
573 )
574 class TestCollectionManifestStorageStaticUrlSlash(CollectionTestCase):
575 run_collectstatic_in_setUp = False
576 hashed_file_path = hashed_file_path
577
578 def test_protocol_relative_url_ignored(self):
579 with override_settings(
580 STATICFILES_DIRS=[os.path.join(TEST_ROOT, "project", "static_url_slash")],
581 STATICFILES_FINDERS=["django.contrib.staticfiles.finders.FileSystemFinder"],
582 ):
583 self.run_collectstatic()
584 relpath = self.hashed_file_path("ignored.css")
585 self.assertEqual(relpath, "ignored.61707f5f4942.css")
586 with storage.staticfiles_storage.open(relpath) as relfile:
587 content = relfile.read()
588 self.assertIn(b"//foobar", content)
589
590
591 @override_settings(
592 STORAGES={
593 **settings.STORAGES,
594 STATICFILES_STORAGE_ALIAS: {
595 "BACKEND": "staticfiles_tests.storage.NoneHashStorage",
596 },
597 }
598 )
599 class TestCollectionNoneHashStorage(CollectionTestCase):
600 hashed_file_path = hashed_file_path
601
602 def test_hashed_name(self):
603 relpath = self.hashed_file_path("cached/styles.css")
604 self.assertEqual(relpath, "cached/styles.css")
605
606
607 @override_settings(
608 STORAGES={
609 **settings.STORAGES,
610 STATICFILES_STORAGE_ALIAS: {
611 "BACKEND": "staticfiles_tests.storage.NoPostProcessReplacedPathStorage",
612 },
613 }
614 )
615 class TestCollectionNoPostProcessReplacedPaths(CollectionTestCase):
616 run_collectstatic_in_setUp = False
617
618 def test_collectstatistic_no_post_process_replaced_paths(self):
619 stdout = StringIO()
620 self.run_collectstatic(verbosity=1, stdout=stdout)
621 self.assertIn("post-processed", stdout.getvalue())
622
623
624 @override_settings(
625 STORAGES={
626 **settings.STORAGES,
627 STATICFILES_STORAGE_ALIAS: {
628 "BACKEND": "staticfiles_tests.storage.SimpleStorage",
629 },
630 }
631 )
632 class TestCollectionSimpleStorage(CollectionTestCase):
633 hashed_file_path = hashed_file_path
634
635 def setUp(self):
636 storage.staticfiles_storage.hashed_files.clear() # avoid cache interference
637 super().setUp()
638
639 def test_template_tag_return(self):
640 self.assertStaticRaises(
641 ValueError, "does/not/exist.png", "/static/does/not/exist.png"
642 )
643 self.assertStaticRenders("test/file.txt", "/static/test/file.deploy12345.txt")
644 self.assertStaticRenders(
645 "cached/styles.css", "/static/cached/styles.deploy12345.css"
646 )
647 self.assertStaticRenders("path/", "/static/path/")
648 self.assertStaticRenders("path/?query", "/static/path/?query")
649
650 def test_template_tag_simple_content(self):
651 relpath = self.hashed_file_path("cached/styles.css")
652 self.assertEqual(relpath, "cached/styles.deploy12345.css")
653 with storage.staticfiles_storage.open(relpath) as relfile:
654 content = relfile.read()
655 self.assertNotIn(b"cached/other.css", content)
656 self.assertIn(b"other.deploy12345.css", content)
657
658
659 class JSModuleImportAggregationManifestStorage(storage.ManifestStaticFilesStorage):
660 support_js_module_import_aggregation = True
661
662
663 @override_settings(
664 STORAGES={
665 **settings.STORAGES,
666 STATICFILES_STORAGE_ALIAS: {
667 "BACKEND": (
668 "staticfiles_tests.test_storage."
669 "JSModuleImportAggregationManifestStorage"
670 ),
671 },
672 }
673 )
674 class TestCollectionJSModuleImportAggregationManifestStorage(CollectionTestCase):
675 hashed_file_path = hashed_file_path
676
677 def test_module_import(self):
678 relpath = self.hashed_file_path("cached/module.js")
679 self.assertEqual(relpath, "cached/module.4326210cf0bd.js")
680 tests = [
681 # Relative imports.
682 b'import testConst from "./module_test.477bbebe77f0.js";',
683 b'import relativeModule from "../nested/js/nested.866475c46bb4.js";',
684 b'import { firstConst, secondConst } from "./module_test.477bbebe77f0.js";',
685 # Absolute import.
686 b'import rootConst from "/static/absolute_root.5586327fe78c.js";',
687 # Dynamic import.
688 b'const dynamicModule = import("./module_test.477bbebe77f0.js");',
689 # Creating a module object.
690 b'import * as NewModule from "./module_test.477bbebe77f0.js";',
691 # Creating a minified module object.
692 b'import*as m from "./module_test.477bbebe77f0.js";',
693 b'import* as m from "./module_test.477bbebe77f0.js";',
694 b'import *as m from "./module_test.477bbebe77f0.js";',
695 b'import* as m from "./module_test.477bbebe77f0.js";',
696 # Aliases.
697 b'import { testConst as alias } from "./module_test.477bbebe77f0.js";',
698 b"import {\n"
699 b" firstVar1 as firstVarAlias,\n"
700 b" $second_var_2 as secondVarAlias\n"
701 b'} from "./module_test.477bbebe77f0.js";',
702 ]
703 with storage.staticfiles_storage.open(relpath) as relfile:
704 content = relfile.read()
705 for module_import in tests:
706 with self.subTest(module_import=module_import):
707 self.assertIn(module_import, content)
708
709 def test_aggregating_modules(self):
710 relpath = self.hashed_file_path("cached/module.js")
711 self.assertEqual(relpath, "cached/module.4326210cf0bd.js")
712 tests = [
713 b'export * from "./module_test.477bbebe77f0.js";',
714 b'export { testConst } from "./module_test.477bbebe77f0.js";',
715 b"export {\n"
716 b" firstVar as firstVarAlias,\n"
717 b" secondVar as secondVarAlias\n"
718 b'} from "./module_test.477bbebe77f0.js";',
719 ]
720 with storage.staticfiles_storage.open(relpath) as relfile:
721 content = relfile.read()
722 for module_import in tests:
723 with self.subTest(module_import=module_import):
724 self.assertIn(module_import, content)
725
726
727 class CustomManifestStorage(storage.ManifestStaticFilesStorage):
728 def __init__(self, *args, manifest_storage=None, **kwargs):
729 manifest_storage = storage.StaticFilesStorage(
730 location=kwargs.pop("manifest_location"),
731 )
732 super().__init__(*args, manifest_storage=manifest_storage, **kwargs)
733
734
735 class TestCustomManifestStorage(SimpleTestCase):
736 def setUp(self):
737 manifest_path = Path(tempfile.mkdtemp())
738 self.addCleanup(shutil.rmtree, manifest_path)
739
740 self.staticfiles_storage = CustomManifestStorage(
741 manifest_location=manifest_path,
742 )
743 self.manifest_file = manifest_path / self.staticfiles_storage.manifest_name
744 # Manifest without paths.
745 self.manifest = {"version": self.staticfiles_storage.manifest_version}
746 with self.manifest_file.open("w") as manifest_file:
747 json.dump(self.manifest, manifest_file)
748
749 def test_read_manifest(self):
750 self.assertEqual(
751 self.staticfiles_storage.read_manifest(),
752 json.dumps(self.manifest),
753 )
754
755 def test_read_manifest_nonexistent(self):
756 os.remove(self.manifest_file)
757 self.assertIsNone(self.staticfiles_storage.read_manifest())
758
759 def test_save_manifest_override(self):
760 self.assertIs(self.manifest_file.exists(), True)
761 self.staticfiles_storage.save_manifest()
762 self.assertIs(self.manifest_file.exists(), True)
763 new_manifest = json.loads(self.staticfiles_storage.read_manifest())
764 self.assertIn("paths", new_manifest)
765 self.assertNotEqual(new_manifest, self.manifest)
766
767 def test_save_manifest_create(self):
768 os.remove(self.manifest_file)
769 self.staticfiles_storage.save_manifest()
770 self.assertIs(self.manifest_file.exists(), True)
771 new_manifest = json.loads(self.staticfiles_storage.read_manifest())
772 self.assertIn("paths", new_manifest)
773 self.assertNotEqual(new_manifest, self.manifest)
774
775
776 class CustomStaticFilesStorage(storage.StaticFilesStorage):
777 """
778 Used in TestStaticFilePermissions
779 """
780
781 def __init__(self, *args, **kwargs):
782 kwargs["file_permissions_mode"] = 0o640
783 kwargs["directory_permissions_mode"] = 0o740
784 super().__init__(*args, **kwargs)
785
786
787 @unittest.skipIf(sys.platform == "win32", "Windows only partially supports chmod.")
788 class TestStaticFilePermissions(CollectionTestCase):
789 command_params = {
790 "interactive": False,
791 "verbosity": 0,
792 "ignore_patterns": ["*.ignoreme"],
793 }
794
795 def setUp(self):
796 self.umask = 0o027
797 old_umask = os.umask(self.umask)
798 self.addCleanup(os.umask, old_umask)
799 super().setUp()
800
801 # Don't run collectstatic command in this test class.
802 def run_collectstatic(self, **kwargs):
803 pass
804
805 @override_settings(
806 FILE_UPLOAD_PERMISSIONS=0o655,
807 FILE_UPLOAD_DIRECTORY_PERMISSIONS=0o765,
808 )
809 def test_collect_static_files_permissions(self):
810 call_command("collectstatic", **self.command_params)
811 static_root = Path(settings.STATIC_ROOT)
812 test_file = static_root / "test.txt"
813 file_mode = test_file.stat().st_mode & 0o777
814 self.assertEqual(file_mode, 0o655)
815 tests = [
816 static_root / "subdir",
817 static_root / "nested",
818 static_root / "nested" / "css",
819 ]
820 for directory in tests:
821 with self.subTest(directory=directory):
822 dir_mode = directory.stat().st_mode & 0o777
823 self.assertEqual(dir_mode, 0o765)
824
825 @override_settings(
826 FILE_UPLOAD_PERMISSIONS=None,
827 FILE_UPLOAD_DIRECTORY_PERMISSIONS=None,
828 )
829 def test_collect_static_files_default_permissions(self):
830 call_command("collectstatic", **self.command_params)
831 static_root = Path(settings.STATIC_ROOT)
832 test_file = static_root / "test.txt"
833 file_mode = test_file.stat().st_mode & 0o777
834 self.assertEqual(file_mode, 0o666 & ~self.umask)
835 tests = [
836 static_root / "subdir",
837 static_root / "nested",
838 static_root / "nested" / "css",
839 ]
840 for directory in tests:
841 with self.subTest(directory=directory):
842 dir_mode = directory.stat().st_mode & 0o777
843 self.assertEqual(dir_mode, 0o777 & ~self.umask)
844
845 @override_settings(
846 FILE_UPLOAD_PERMISSIONS=0o655,
847 FILE_UPLOAD_DIRECTORY_PERMISSIONS=0o765,
848 STORAGES={
849 **settings.STORAGES,
850 STATICFILES_STORAGE_ALIAS: {
851 "BACKEND": "staticfiles_tests.test_storage.CustomStaticFilesStorage",
852 },
853 },
854 )
855 def test_collect_static_files_subclass_of_static_storage(self):
856 call_command("collectstatic", **self.command_params)
857 static_root = Path(settings.STATIC_ROOT)
858 test_file = static_root / "test.txt"
859 file_mode = test_file.stat().st_mode & 0o777
860 self.assertEqual(file_mode, 0o640)
861 tests = [
862 static_root / "subdir",
863 static_root / "nested",
864 static_root / "nested" / "css",
865 ]
866 for directory in tests:
867 with self.subTest(directory=directory):
868 dir_mode = directory.stat().st_mode & 0o777
869 self.assertEqual(dir_mode, 0o740)
870
871
872 @override_settings(
873 STORAGES={
874 **settings.STORAGES,
875 STATICFILES_STORAGE_ALIAS: {
876 "BACKEND": "django.contrib.staticfiles.storage.ManifestStaticFilesStorage",
877 },
878 }
879 )
880 class TestCollectionHashedFilesCache(CollectionTestCase):
881 """
882 Files referenced from CSS use the correct final hashed name regardless of
883 the order in which the files are post-processed.
884 """
885
886 hashed_file_path = hashed_file_path
887
888 def setUp(self):
889 super().setUp()
890 self._temp_dir = temp_dir = tempfile.mkdtemp()
891 os.makedirs(os.path.join(temp_dir, "test"))
892 self.addCleanup(shutil.rmtree, temp_dir)
893
894 def _get_filename_path(self, filename):
895 return os.path.join(self._temp_dir, "test", filename)
896
897 def test_file_change_after_collectstatic(self):
898 # Create initial static files.
899 file_contents = (
900 ("foo.png", "foo"),
901 ("bar.css", 'url("foo.png")\nurl("xyz.png")'),
902 ("xyz.png", "xyz"),
903 )
904 for filename, content in file_contents:
905 with open(self._get_filename_path(filename), "w") as f:
906 f.write(content)
907
908 with self.modify_settings(STATICFILES_DIRS={"append": self._temp_dir}):
909 finders.get_finder.cache_clear()
910 err = StringIO()
911 # First collectstatic run.
912 call_command("collectstatic", interactive=False, verbosity=0, stderr=err)
913 relpath = self.hashed_file_path("test/bar.css")
914 with storage.staticfiles_storage.open(relpath) as relfile:
915 content = relfile.read()
916 self.assertIn(b"foo.acbd18db4cc2.png", content)
917 self.assertIn(b"xyz.d16fb36f0911.png", content)
918
919 # Change the contents of the png files.
920 for filename in ("foo.png", "xyz.png"):
921 with open(self._get_filename_path(filename), "w+b") as f:
922 f.write(b"new content of file to change its hash")
923
924 # The hashes of the png files in the CSS file are updated after
925 # a second collectstatic.
926 call_command("collectstatic", interactive=False, verbosity=0, stderr=err)
927 relpath = self.hashed_file_path("test/bar.css")
928 with storage.staticfiles_storage.open(relpath) as relfile:
929 content = relfile.read()
930 self.assertIn(b"foo.57a5cb9ba68d.png", content)
931 self.assertIn(b"xyz.57a5cb9ba68d.png", content)