test_caches.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import pathlib
  2. from django.core.checks import Warning
  3. from django.core.checks.caches import (
  4. E001, check_cache_location_not_exposed, check_default_cache_is_configured,
  5. )
  6. from django.test import SimpleTestCase
  7. from django.test.utils import override_settings
  8. class CheckCacheSettingsAppDirsTest(SimpleTestCase):
  9. VALID_CACHES_CONFIGURATION = {
  10. 'default': {
  11. 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
  12. },
  13. }
  14. INVALID_CACHES_CONFIGURATION = {
  15. 'other': {
  16. 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
  17. },
  18. }
  19. @override_settings(CACHES=VALID_CACHES_CONFIGURATION)
  20. def test_default_cache_included(self):
  21. """
  22. Don't error if 'default' is present in CACHES setting.
  23. """
  24. self.assertEqual(check_default_cache_is_configured(None), [])
  25. @override_settings(CACHES=INVALID_CACHES_CONFIGURATION)
  26. def test_default_cache_not_included(self):
  27. """
  28. Error if 'default' not present in CACHES setting.
  29. """
  30. self.assertEqual(check_default_cache_is_configured(None), [E001])
  31. class CheckCacheLocationTest(SimpleTestCase):
  32. warning_message = (
  33. "Your 'default' cache configuration might expose your cache or lead "
  34. "to corruption of your data because its LOCATION %s %s."
  35. )
  36. @staticmethod
  37. def get_settings(setting, cache_path, setting_path):
  38. return {
  39. 'CACHES': {
  40. 'default': {
  41. 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
  42. 'LOCATION': cache_path,
  43. },
  44. },
  45. setting: [setting_path] if setting == 'STATICFILES_DIRS' else setting_path,
  46. }
  47. def test_cache_path_matches_media_static_setting(self):
  48. root = pathlib.Path.cwd()
  49. for setting in ('MEDIA_ROOT', 'STATIC_ROOT', 'STATICFILES_DIRS'):
  50. settings = self.get_settings(setting, root, root)
  51. with self.subTest(setting=setting), self.settings(**settings):
  52. msg = self.warning_message % ('matches', setting)
  53. self.assertEqual(check_cache_location_not_exposed(None), [
  54. Warning(msg, id='caches.W002'),
  55. ])
  56. def test_cache_path_inside_media_static_setting(self):
  57. root = pathlib.Path.cwd()
  58. for setting in ('MEDIA_ROOT', 'STATIC_ROOT', 'STATICFILES_DIRS'):
  59. settings = self.get_settings(setting, root / 'cache', root)
  60. with self.subTest(setting=setting), self.settings(**settings):
  61. msg = self.warning_message % ('is inside', setting)
  62. self.assertEqual(check_cache_location_not_exposed(None), [
  63. Warning(msg, id='caches.W002'),
  64. ])
  65. def test_cache_path_contains_media_static_setting(self):
  66. root = pathlib.Path.cwd()
  67. for setting in ('MEDIA_ROOT', 'STATIC_ROOT', 'STATICFILES_DIRS'):
  68. settings = self.get_settings(setting, root, root / 'other')
  69. with self.subTest(setting=setting), self.settings(**settings):
  70. msg = self.warning_message % ('contains', setting)
  71. self.assertEqual(check_cache_location_not_exposed(None), [
  72. Warning(msg, id='caches.W002'),
  73. ])
  74. def test_cache_path_not_conflict(self):
  75. root = pathlib.Path.cwd()
  76. for setting in ('MEDIA_ROOT', 'STATIC_ROOT', 'STATICFILES_DIRS'):
  77. settings = self.get_settings(setting, root / 'cache', root / 'other')
  78. with self.subTest(setting=setting), self.settings(**settings):
  79. self.assertEqual(check_cache_location_not_exposed(None), [])