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