Debian Codegraph @

tests/staticfiles_tests/test_checks.py

raw
1 from pathlib import Path
2 from unittest import mock
3
4 from django.conf import DEFAULT_STORAGE_ALIAS, STATICFILES_STORAGE_ALIAS, settings
5 from django.contrib.staticfiles.checks import E005, check_finders, check_storages
6 from django.contrib.staticfiles.finders import BaseFinder, get_finder
7 from django.core.checks import Error, Warning
8 from django.test import SimpleTestCase, override_settings
9
10 from .cases import CollectionTestCase
11 from .settings import TEST_ROOT
12
13
14 class FindersCheckTests(CollectionTestCase):
15 run_collectstatic_in_setUp = False
16
17 def test_base_finder_check_not_implemented(self):
18 finder = BaseFinder()
19 msg = (
20 "subclasses may provide a check() method to verify the finder is "
21 "configured correctly."
22 )
23 with self.assertRaisesMessage(NotImplementedError, msg):
24 finder.check()
25
26 def test_check_finders(self):
27 """check_finders() concatenates all errors."""
28 error1 = Error("1")
29 error2 = Error("2")
30 error3 = Error("3")
31
32 def get_finders():
33 class Finder1(BaseFinder):
34 def check(self, **kwargs):
35 return [error1]
36
37 class Finder2(BaseFinder):
38 def check(self, **kwargs):
39 return []
40
41 class Finder3(BaseFinder):
42 def check(self, **kwargs):
43 return [error2, error3]
44
45 class Finder4(BaseFinder):
46 pass
47
48 return [Finder1(), Finder2(), Finder3(), Finder4()]
49
50 with mock.patch("django.contrib.staticfiles.checks.get_finders", get_finders):
51 errors = check_finders(None)
52 self.assertEqual(errors, [error1, error2, error3])
53
54 def test_no_errors_with_test_settings(self):
55 self.assertEqual(check_finders(None), [])
56
57 @override_settings(STATICFILES_DIRS="a string")
58 def test_dirs_not_tuple_or_list(self):
59 self.assertEqual(
60 check_finders(None),
61 [
62 Error(
63 "The STATICFILES_DIRS setting is not a tuple or list.",
64 hint="Perhaps you forgot a trailing comma?",
65 id="staticfiles.E001",
66 )
67 ],
68 )
69
70 def test_dirs_contains_static_root(self):
71 with self.settings(STATICFILES_DIRS=[settings.STATIC_ROOT]):
72 self.assertEqual(
73 check_finders(None),
74 [
75 Error(
76 "The STATICFILES_DIRS setting should not contain the "
77 "STATIC_ROOT setting.",
78 id="staticfiles.E002",
79 )
80 ],
81 )
82
83 def test_dirs_contains_static_root_in_tuple(self):
84 with self.settings(STATICFILES_DIRS=[("prefix", settings.STATIC_ROOT)]):
85 self.assertEqual(
86 check_finders(None),
87 [
88 Error(
89 "The STATICFILES_DIRS setting should not contain the "
90 "STATIC_ROOT setting.",
91 id="staticfiles.E002",
92 )
93 ],
94 )
95
96 def test_prefix_contains_trailing_slash(self):
97 static_dir = Path(TEST_ROOT) / "project" / "documents"
98 with self.settings(STATICFILES_DIRS=[("prefix/", static_dir)]):
99 self.assertEqual(
100 check_finders(None),
101 [
102 Error(
103 "The prefix 'prefix/' in the STATICFILES_DIRS setting must "
104 "not end with a slash.",
105 id="staticfiles.E003",
106 ),
107 ],
108 )
109
110 def test_nonexistent_directories(self):
111 with self.settings(
112 STATICFILES_DIRS=[
113 "/fake/path",
114 ("prefix", "/fake/prefixed/path"),
115 ]
116 ):
117 self.assertEqual(
118 check_finders(None),
119 [
120 Warning(
121 "The directory '/fake/path' in the STATICFILES_DIRS "
122 "setting does not exist.",
123 id="staticfiles.W004",
124 ),
125 Warning(
126 "The directory '/fake/prefixed/path' in the "
127 "STATICFILES_DIRS setting does not exist.",
128 id="staticfiles.W004",
129 ),
130 ],
131 )
132 # Nonexistent directories are skipped.
133 finder = get_finder("django.contrib.staticfiles.finders.FileSystemFinder")
134 self.assertEqual(list(finder.list(None)), [])
135
136
137 class StoragesCheckTests(SimpleTestCase):
138 @override_settings(STORAGES={})
139 def test_error_empty_storages(self):
140 errors = check_storages(None)
141 self.assertEqual(errors, [E005])
142
143 @override_settings(
144 STORAGES={
145 DEFAULT_STORAGE_ALIAS: {
146 "BACKEND": "django.core.files.storage.FileSystemStorage",
147 },
148 "example": {
149 "BACKEND": "ignore.me",
150 },
151 }
152 )
153 def test_error_missing_staticfiles(self):
154 errors = check_storages(None)
155 self.assertEqual(errors, [E005])
156
157 @override_settings(
158 STORAGES={
159 STATICFILES_STORAGE_ALIAS: {
160 "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
161 },
162 }
163 )
164 def test_staticfiles_no_errors(self):
165 errors = check_storages(None)
166 self.assertEqual(errors, [])