Debian Codegraph @

tests/staticfiles_tests/cases.py

raw
1 import os
2 import shutil
3 import tempfile
4
5 from django.conf import settings
6 from django.core.management import call_command
7 from django.template import Context, Template
8 from django.test import SimpleTestCase, override_settings
9
10 from .settings import TEST_SETTINGS
11
12
13 class BaseStaticFilesMixin:
14 """
15 Test case with a couple utility assertions.
16 """
17
18 def assertFileContains(self, filepath, text):
19 self.assertIn(
20 text,
21 self._get_file(filepath),
22 "'%s' not in '%s'" % (text, filepath),
23 )
24
25 def assertFileNotFound(self, filepath):
26 with self.assertRaises(OSError):
27 self._get_file(filepath)
28
29 def render_template(self, template, **kwargs):
30 if isinstance(template, str):
31 template = Template(template)
32 return template.render(Context(**kwargs)).strip()
33
34 def static_template_snippet(self, path, asvar=False):
35 if asvar:
36 return (
37 "{%% load static from static %%}{%% static '%s' as var %%}{{ var }}"
38 % path
39 )
40 return "{%% load static from static %%}{%% static '%s' %%}" % path
41
42 def assertStaticRenders(self, path, result, asvar=False, **kwargs):
43 template = self.static_template_snippet(path, asvar)
44 self.assertEqual(self.render_template(template, **kwargs), result)
45
46 def assertStaticRaises(self, exc, path, result, asvar=False, **kwargs):
47 with self.assertRaises(exc):
48 self.assertStaticRenders(path, result, **kwargs)
49
50
51 @override_settings(**TEST_SETTINGS)
52 class StaticFilesTestCase(BaseStaticFilesMixin, SimpleTestCase):
53 pass
54
55
56 @override_settings(**TEST_SETTINGS)
57 class CollectionTestCase(BaseStaticFilesMixin, SimpleTestCase):
58 """
59 Tests shared by all file finding features (collectstatic,
60 findstatic, and static serve view).
61
62 This relies on the asserts defined in BaseStaticFilesTestCase, but
63 is separated because some test cases need those asserts without
64 all these tests.
65 """
66
67 run_collectstatic_in_setUp = True
68
69 def setUp(self):
70 super().setUp()
71 temp_dir = self.mkdtemp()
72 # Override the STATIC_ROOT for all tests from setUp to tearDown
73 # rather than as a context manager
74 patched_settings = self.settings(STATIC_ROOT=temp_dir)
75 patched_settings.enable()
76 if self.run_collectstatic_in_setUp:
77 self.run_collectstatic()
78 # Same comment as in runtests.teardown.
79 self.addCleanup(shutil.rmtree, temp_dir)
80 self.addCleanup(patched_settings.disable)
81
82 def mkdtemp(self):
83 return tempfile.mkdtemp()
84
85 def run_collectstatic(self, *, verbosity=0, **kwargs):
86 call_command(
87 "collectstatic",
88 interactive=False,
89 verbosity=verbosity,
90 ignore_patterns=["*.ignoreme"],
91 **kwargs,
92 )
93
94 def _get_file(self, filepath):
95 assert filepath, "filepath is empty."
96 filepath = os.path.join(settings.STATIC_ROOT, filepath)
97 with open(filepath, encoding="utf-8") as f:
98 return f.read()
99
100
101 class TestDefaults:
102 """
103 A few standard test cases.
104 """
105
106 def test_staticfiles_dirs(self):
107 """
108 Can find a file in a STATICFILES_DIRS directory.
109 """
110 self.assertFileContains("test.txt", "Can we find")
111 self.assertFileContains(os.path.join("prefix", "test.txt"), "Prefix")
112
113 def test_staticfiles_dirs_subdir(self):
114 """
115 Can find a file in a subdirectory of a STATICFILES_DIRS
116 directory.
117 """
118 self.assertFileContains("subdir/test.txt", "Can we find")
119
120 def test_staticfiles_dirs_priority(self):
121 """
122 File in STATICFILES_DIRS has priority over file in app.
123 """
124 self.assertFileContains("test/file.txt", "STATICFILES_DIRS")
125
126 def test_app_files(self):
127 """
128 Can find a file in an app static/ directory.
129 """
130 self.assertFileContains("test/file1.txt", "file1 in the app dir")
131
132 def test_nonascii_filenames(self):
133 """
134 Can find a file with non-ASCII character in an app static/ directory.
135 """
136 self.assertFileContains("test/⊗.txt", "⊗ in the app dir")
137
138 def test_camelcase_filenames(self):
139 """
140 Can find a file with capital letters.
141 """
142 self.assertFileContains("test/camelCase.txt", "camelCase")
143
144 def test_filename_with_percent_sign(self):
145 self.assertFileContains("test/%2F.txt", "%2F content")