1. import pathlib
    
  2. 
    
  3. from django.core.checks import Warning
    
  4. from django.core.checks.caches import (
    
  5.     E001,
    
  6.     check_cache_location_not_exposed,
    
  7.     check_default_cache_is_configured,
    
  8.     check_file_based_cache_is_absolute,
    
  9. )
    
  10. from django.test import SimpleTestCase
    
  11. from django.test.utils import override_settings
    
  12. 
    
  13. 
    
  14. class CheckCacheSettingsAppDirsTest(SimpleTestCase):
    
  15.     VALID_CACHES_CONFIGURATION = {
    
  16.         "default": {
    
  17.             "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
    
  18.         },
    
  19.     }
    
  20.     INVALID_CACHES_CONFIGURATION = {
    
  21.         "other": {
    
  22.             "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
    
  23.         },
    
  24.     }
    
  25. 
    
  26.     @override_settings(CACHES=VALID_CACHES_CONFIGURATION)
    
  27.     def test_default_cache_included(self):
    
  28.         """
    
  29.         Don't error if 'default' is present in CACHES setting.
    
  30.         """
    
  31.         self.assertEqual(check_default_cache_is_configured(None), [])
    
  32. 
    
  33.     @override_settings(CACHES=INVALID_CACHES_CONFIGURATION)
    
  34.     def test_default_cache_not_included(self):
    
  35.         """
    
  36.         Error if 'default' not present in CACHES setting.
    
  37.         """
    
  38.         self.assertEqual(check_default_cache_is_configured(None), [E001])
    
  39. 
    
  40. 
    
  41. class CheckCacheLocationTest(SimpleTestCase):
    
  42.     warning_message = (
    
  43.         "Your 'default' cache configuration might expose your cache or lead "
    
  44.         "to corruption of your data because its LOCATION %s %s."
    
  45.     )
    
  46. 
    
  47.     @staticmethod
    
  48.     def get_settings(setting, cache_path, setting_path):
    
  49.         return {
    
  50.             "CACHES": {
    
  51.                 "default": {
    
  52.                     "BACKEND": "django.core.cache.backends.filebased.FileBasedCache",
    
  53.                     "LOCATION": cache_path,
    
  54.                 },
    
  55.             },
    
  56.             setting: [setting_path] if setting == "STATICFILES_DIRS" else setting_path,
    
  57.         }
    
  58. 
    
  59.     def test_cache_path_matches_media_static_setting(self):
    
  60.         root = pathlib.Path.cwd()
    
  61.         for setting in ("MEDIA_ROOT", "STATIC_ROOT", "STATICFILES_DIRS"):
    
  62.             settings = self.get_settings(setting, root, root)
    
  63.             with self.subTest(setting=setting), self.settings(**settings):
    
  64.                 msg = self.warning_message % ("matches", setting)
    
  65.                 self.assertEqual(
    
  66.                     check_cache_location_not_exposed(None),
    
  67.                     [
    
  68.                         Warning(msg, id="caches.W002"),
    
  69.                     ],
    
  70.                 )
    
  71. 
    
  72.     def test_cache_path_inside_media_static_setting(self):
    
  73.         root = pathlib.Path.cwd()
    
  74.         for setting in ("MEDIA_ROOT", "STATIC_ROOT", "STATICFILES_DIRS"):
    
  75.             settings = self.get_settings(setting, root / "cache", root)
    
  76.             with self.subTest(setting=setting), self.settings(**settings):
    
  77.                 msg = self.warning_message % ("is inside", setting)
    
  78.                 self.assertEqual(
    
  79.                     check_cache_location_not_exposed(None),
    
  80.                     [
    
  81.                         Warning(msg, id="caches.W002"),
    
  82.                     ],
    
  83.                 )
    
  84. 
    
  85.     def test_cache_path_contains_media_static_setting(self):
    
  86.         root = pathlib.Path.cwd()
    
  87.         for setting in ("MEDIA_ROOT", "STATIC_ROOT", "STATICFILES_DIRS"):
    
  88.             settings = self.get_settings(setting, root, root / "other")
    
  89.             with self.subTest(setting=setting), self.settings(**settings):
    
  90.                 msg = self.warning_message % ("contains", setting)
    
  91.                 self.assertEqual(
    
  92.                     check_cache_location_not_exposed(None),
    
  93.                     [
    
  94.                         Warning(msg, id="caches.W002"),
    
  95.                     ],
    
  96.                 )
    
  97. 
    
  98.     def test_cache_path_not_conflict(self):
    
  99.         root = pathlib.Path.cwd()
    
  100.         for setting in ("MEDIA_ROOT", "STATIC_ROOT", "STATICFILES_DIRS"):
    
  101.             settings = self.get_settings(setting, root / "cache", root / "other")
    
  102.             with self.subTest(setting=setting), self.settings(**settings):
    
  103.                 self.assertEqual(check_cache_location_not_exposed(None), [])
    
  104. 
    
  105.     def test_staticfiles_dirs_prefix(self):
    
  106.         root = pathlib.Path.cwd()
    
  107.         tests = [
    
  108.             (root, root, "matches"),
    
  109.             (root / "cache", root, "is inside"),
    
  110.             (root, root / "other", "contains"),
    
  111.         ]
    
  112.         for cache_path, setting_path, msg in tests:
    
  113.             settings = self.get_settings(
    
  114.                 "STATICFILES_DIRS",
    
  115.                 cache_path,
    
  116.                 ("prefix", setting_path),
    
  117.             )
    
  118.             with self.subTest(path=setting_path), self.settings(**settings):
    
  119.                 msg = self.warning_message % (msg, "STATICFILES_DIRS")
    
  120.                 self.assertEqual(
    
  121.                     check_cache_location_not_exposed(None),
    
  122.                     [
    
  123.                         Warning(msg, id="caches.W002"),
    
  124.                     ],
    
  125.                 )
    
  126. 
    
  127.     def test_staticfiles_dirs_prefix_not_conflict(self):
    
  128.         root = pathlib.Path.cwd()
    
  129.         settings = self.get_settings(
    
  130.             "STATICFILES_DIRS",
    
  131.             root / "cache",
    
  132.             ("prefix", root / "other"),
    
  133.         )
    
  134.         with self.settings(**settings):
    
  135.             self.assertEqual(check_cache_location_not_exposed(None), [])
    
  136. 
    
  137. 
    
  138. class CheckCacheAbsolutePath(SimpleTestCase):
    
  139.     def test_absolute_path(self):
    
  140.         with self.settings(
    
  141.             CACHES={
    
  142.                 "default": {
    
  143.                     "BACKEND": "django.core.cache.backends.filebased.FileBasedCache",
    
  144.                     "LOCATION": pathlib.Path.cwd() / "cache",
    
  145.                 },
    
  146.             }
    
  147.         ):
    
  148.             self.assertEqual(check_file_based_cache_is_absolute(None), [])
    
  149. 
    
  150.     def test_relative_path(self):
    
  151.         with self.settings(
    
  152.             CACHES={
    
  153.                 "default": {
    
  154.                     "BACKEND": "django.core.cache.backends.filebased.FileBasedCache",
    
  155.                     "LOCATION": "cache",
    
  156.                 },
    
  157.             }
    
  158.         ):
    
  159.             self.assertEqual(
    
  160.                 check_file_based_cache_is_absolute(None),
    
  161.                 [
    
  162.                     Warning(
    
  163.                         "Your 'default' cache LOCATION path is relative. Use an "
    
  164.                         "absolute path instead.",
    
  165.                         id="caches.W003",
    
  166.                     ),
    
  167.                 ],
    
  168.             )