summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJannis Leidel <jannis@leidel.info>2011-08-11 14:07:39 +0000
committerJannis Leidel <jannis@leidel.info>2011-08-11 14:07:39 +0000
commit1d32bdd3c9586ff10d0799264105850fa7e3f512 (patch)
treece0ed940aa5c725ddbc02bc2f32d3e15c0f1d0a8 /tests
parente9a909e30ab63cc4faa28e4d9296f522bbe3bb06 (diff)
Fixed #15252 -- Added static template tag and CachedStaticFilesStorage to staticfiles contrib app.
Many thanks to Florian Apolloner and Jacob Kaplan-Moss for reviewing and eagle eyeing. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16594 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
-rw-r--r--tests/regressiontests/staticfiles_tests/project/documents/cached/absolute.css1
-rw-r--r--tests/regressiontests/staticfiles_tests/project/documents/cached/denorm.css1
-rw-r--r--tests/regressiontests/staticfiles_tests/project/documents/cached/other.css0
-rw-r--r--tests/regressiontests/staticfiles_tests/project/documents/cached/relative.css2
-rw-r--r--tests/regressiontests/staticfiles_tests/project/documents/cached/styles.css1
-rw-r--r--tests/regressiontests/staticfiles_tests/project/documents/cached/url.css1
-rw-r--r--tests/regressiontests/staticfiles_tests/project/site_media/static/testfile.txt1
-rw-r--r--tests/regressiontests/staticfiles_tests/tests.py193
8 files changed, 153 insertions, 47 deletions
diff --git a/tests/regressiontests/staticfiles_tests/project/documents/cached/absolute.css b/tests/regressiontests/staticfiles_tests/project/documents/cached/absolute.css
new file mode 100644
index 0000000000..e64e7ccca7
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/project/documents/cached/absolute.css
@@ -0,0 +1 @@
+@import url("/static/cached/styles.css");
diff --git a/tests/regressiontests/staticfiles_tests/project/documents/cached/denorm.css b/tests/regressiontests/staticfiles_tests/project/documents/cached/denorm.css
new file mode 100644
index 0000000000..27b9a349b0
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/project/documents/cached/denorm.css
@@ -0,0 +1 @@
+@import url("..//cached///styles.css");
diff --git a/tests/regressiontests/staticfiles_tests/project/documents/cached/other.css b/tests/regressiontests/staticfiles_tests/project/documents/cached/other.css
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/project/documents/cached/other.css
diff --git a/tests/regressiontests/staticfiles_tests/project/documents/cached/relative.css b/tests/regressiontests/staticfiles_tests/project/documents/cached/relative.css
new file mode 100644
index 0000000000..40c4a256aa
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/project/documents/cached/relative.css
@@ -0,0 +1,2 @@
+@import url("../cached/styles.css");
+@import url("absolute.css"); \ No newline at end of file
diff --git a/tests/regressiontests/staticfiles_tests/project/documents/cached/styles.css b/tests/regressiontests/staticfiles_tests/project/documents/cached/styles.css
new file mode 100644
index 0000000000..84936d1dcb
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/project/documents/cached/styles.css
@@ -0,0 +1 @@
+@import url("cached/other.css"); \ No newline at end of file
diff --git a/tests/regressiontests/staticfiles_tests/project/documents/cached/url.css b/tests/regressiontests/staticfiles_tests/project/documents/cached/url.css
new file mode 100644
index 0000000000..184e254004
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/project/documents/cached/url.css
@@ -0,0 +1 @@
+@import url("https://www.djangoproject.com/m/css/base.css"); \ No newline at end of file
diff --git a/tests/regressiontests/staticfiles_tests/project/site_media/static/testfile.txt b/tests/regressiontests/staticfiles_tests/project/site_media/static/testfile.txt
new file mode 100644
index 0000000000..4d92dbe1ad
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/project/site_media/static/testfile.txt
@@ -0,0 +1 @@
+Test! \ No newline at end of file
diff --git a/tests/regressiontests/staticfiles_tests/tests.py b/tests/regressiontests/staticfiles_tests/tests.py
index 77b771d23d..40569484df 100644
--- a/tests/regressiontests/staticfiles_tests/tests.py
+++ b/tests/regressiontests/staticfiles_tests/tests.py
@@ -8,6 +8,7 @@ import sys
import tempfile
from StringIO import StringIO
+from django.template import loader, Context
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.files.storage import default_storage
@@ -21,9 +22,25 @@ from django.utils._os import rmtree_errorhandler
from django.contrib.staticfiles import finders, storage
TEST_ROOT = os.path.dirname(__file__)
+TEST_SETTINGS = {
+ 'DEBUG': True,
+ 'MEDIA_URL': '/media/',
+ 'STATIC_URL': '/static/',
+ 'MEDIA_ROOT': os.path.join(TEST_ROOT, 'project', 'site_media', 'media'),
+ 'STATIC_ROOT': os.path.join(TEST_ROOT, 'project', 'site_media', 'static'),
+ 'STATICFILES_DIRS': (
+ os.path.join(TEST_ROOT, 'project', 'documents'),
+ ('prefix', os.path.join(TEST_ROOT, 'project', 'prefixed')),
+ ),
+ 'STATICFILES_FINDERS': (
+ 'django.contrib.staticfiles.finders.FileSystemFinder',
+ 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
+ 'django.contrib.staticfiles.finders.DefaultStorageFinder',
+ ),
+}
-class StaticFilesTestCase(TestCase):
+class BaseStaticFilesTestCase(object):
"""
Test case with a couple utility assertions.
"""
@@ -32,6 +49,7 @@ class StaticFilesTestCase(TestCase):
# gets accessed (by some other test), it evaluates settings.MEDIA_ROOT,
# since we're planning on changing that we need to clear out the cache.
default_storage._wrapped = empty
+ storage.staticfiles_storage._wrapped = empty
# To make sure SVN doesn't hangs itself with the non-ASCII characters
# during checkout, we actually create one file dynamically.
@@ -48,27 +66,26 @@ class StaticFilesTestCase(TestCase):
def assertFileNotFound(self, filepath):
self.assertRaises(IOError, self._get_file, filepath)
-StaticFilesTestCase = override_settings(
- DEBUG = True,
- MEDIA_URL = '/media/',
- STATIC_URL = '/static/',
- MEDIA_ROOT = os.path.join(TEST_ROOT, 'project', 'site_media', 'media'),
- STATIC_ROOT = os.path.join(TEST_ROOT, 'project', 'site_media', 'static'),
- STATICFILES_DIRS = (
- os.path.join(TEST_ROOT, 'project', 'documents'),
- ('prefix', os.path.join(TEST_ROOT, 'project', 'prefixed')),
- ),
- STATICFILES_FINDERS = (
- 'django.contrib.staticfiles.finders.FileSystemFinder',
- 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
- 'django.contrib.staticfiles.finders.DefaultStorageFinder',
- ),
-)(StaticFilesTestCase)
+ def render_template(self, template, **kwargs):
+ if isinstance(template, basestring):
+ template = loader.get_template_from_string(template)
+ return template.render(Context(kwargs)).strip()
+
+ def assertTemplateRenders(self, template, result, **kwargs):
+ self.assertEqual(self.render_template(template, **kwargs), result)
+ def assertTemplateRaises(self, exc, template, result, **kwargs):
+ self.assertRaises(exc, self.assertTemplateRenders, template, result, **kwargs)
+
+
+class StaticFilesTestCase(BaseStaticFilesTestCase, TestCase):
+ pass
+StaticFilesTestCase = override_settings(**TEST_SETTINGS)(StaticFilesTestCase)
-class BuildStaticTestCase(StaticFilesTestCase):
+
+class BaseCollectionTestCase(BaseStaticFilesTestCase):
"""
- Tests shared by all file-resolving features (collectstatic,
+ Tests shared by all file finding features (collectstatic,
findstatic, and static serve view).
This relies on the asserts defined in UtilityAssertsTestCase, but
@@ -76,7 +93,7 @@ class BuildStaticTestCase(StaticFilesTestCase):
all these tests.
"""
def setUp(self):
- super(BuildStaticTestCase, self).setUp()
+ super(BaseCollectionTestCase, self).setUp()
self.old_root = settings.STATIC_ROOT
settings.STATIC_ROOT = tempfile.mkdtemp()
self.run_collectstatic()
@@ -86,7 +103,7 @@ class BuildStaticTestCase(StaticFilesTestCase):
def tearDown(self):
settings.STATIC_ROOT = self.old_root
- super(BuildStaticTestCase, self).tearDown()
+ super(BaseCollectionTestCase, self).tearDown()
def run_collectstatic(self, **kwargs):
call_command('collectstatic', interactive=False, verbosity='0',
@@ -99,6 +116,10 @@ class BuildStaticTestCase(StaticFilesTestCase):
return f.read()
+class CollectionTestCase(BaseCollectionTestCase, StaticFilesTestCase):
+ pass
+
+
class TestDefaults(object):
"""
A few standard test cases.
@@ -142,7 +163,7 @@ class TestDefaults(object):
self.assertFileContains(u'test/camelCase.txt', u'camelCase')
-class TestFindStatic(BuildStaticTestCase, TestDefaults):
+class TestFindStatic(CollectionTestCase, TestDefaults):
"""
Test ``findstatic`` management command.
"""
@@ -171,12 +192,12 @@ class TestFindStatic(BuildStaticTestCase, TestDefaults):
lines = [l.strip() for l in sys.stdout.readlines()]
finally:
sys.stdout = _stdout
- self.assertEqual(len(lines), 3) # three because there is also the "Found <file> here" line
+ self.assertEqual(len(lines), 3) # three because there is also the "Found <file> here" line
self.assertTrue('project' in lines[1])
self.assertTrue('apps' in lines[2])
-class TestBuildStatic(BuildStaticTestCase, TestDefaults):
+class TestCollection(CollectionTestCase, TestDefaults):
"""
Test ``collectstatic`` management command.
"""
@@ -195,7 +216,7 @@ class TestBuildStatic(BuildStaticTestCase, TestDefaults):
self.assertFileNotFound('test/CVS')
-class TestBuildStaticClear(BuildStaticTestCase):
+class TestCollectionClear(CollectionTestCase):
"""
Test the ``--clear`` option of the ``collectstatic`` managemenet command.
"""
@@ -203,19 +224,19 @@ class TestBuildStaticClear(BuildStaticTestCase):
clear_filepath = os.path.join(settings.STATIC_ROOT, 'cleared.txt')
with open(clear_filepath, 'w') as f:
f.write('should be cleared')
- super(TestBuildStaticClear, self).run_collectstatic(clear=True)
+ super(TestCollectionClear, self).run_collectstatic(clear=True)
def test_cleared_not_found(self):
self.assertFileNotFound('cleared.txt')
-class TestBuildStaticExcludeNoDefaultIgnore(BuildStaticTestCase, TestDefaults):
+class TestCollectionExcludeNoDefaultIgnore(CollectionTestCase, TestDefaults):
"""
Test ``--exclude-dirs`` and ``--no-default-ignore`` options of the
``collectstatic`` management command.
"""
def run_collectstatic(self):
- super(TestBuildStaticExcludeNoDefaultIgnore, self).run_collectstatic(
+ super(TestCollectionExcludeNoDefaultIgnore, self).run_collectstatic(
use_default_ignore_patterns=False)
def test_no_common_ignore_patterns(self):
@@ -238,27 +259,98 @@ class TestNoFilesCreated(object):
self.assertEqual(os.listdir(settings.STATIC_ROOT), [])
-class TestBuildStaticDryRun(BuildStaticTestCase, TestNoFilesCreated):
+class TestCollectionDryRun(CollectionTestCase, TestNoFilesCreated):
"""
Test ``--dry-run`` option for ``collectstatic`` management command.
"""
def run_collectstatic(self):
- super(TestBuildStaticDryRun, self).run_collectstatic(dry_run=True)
+ super(TestCollectionDryRun, self).run_collectstatic(dry_run=True)
-class TestBuildStaticNonLocalStorage(BuildStaticTestCase, TestNoFilesCreated):
+class TestCollectionNonLocalStorage(CollectionTestCase, TestNoFilesCreated):
"""
Tests for #15035
"""
pass
-TestBuildStaticNonLocalStorage = override_settings(
+TestCollectionNonLocalStorage = override_settings(
STATICFILES_STORAGE='regressiontests.staticfiles_tests.storage.DummyStorage',
-)(TestBuildStaticNonLocalStorage)
+)(TestCollectionNonLocalStorage)
+
+
+class TestCollectionCachedStorage(BaseCollectionTestCase,
+ BaseStaticFilesTestCase, TestCase):
+ """
+ Tests for the Cache busting storage
+ """
+ def cached_file_path(self, relpath):
+ template = "{%% load static from staticfiles %%}{%% static '%s' %%}"
+ fullpath = self.render_template(template % relpath)
+ return fullpath.replace(settings.STATIC_URL, '')
+
+ def test_template_tag_return(self):
+ """
+ Test the CachedStaticFilesStorage backend.
+ """
+ self.assertTemplateRaises(ValueError, """
+ {% load static from staticfiles %}{% static "does/not/exist.png" %}
+ """, "/static/does/not/exist.png")
+ self.assertTemplateRenders("""
+ {% load static from staticfiles %}{% static "test/file.txt" %}
+ """, "/static/test/file.dad0999e4f8f.txt")
+ self.assertTemplateRenders("""
+ {% load static from staticfiles %}{% static "cached/styles.css" %}
+ """, "/static/cached/styles.5653c259030b.css")
+
+ def test_template_tag_simple_content(self):
+ relpath = self.cached_file_path("cached/styles.css")
+ self.assertEqual(relpath, "cached/styles.5653c259030b.css")
+ with storage.staticfiles_storage.open(relpath) as relfile:
+ content = relfile.read()
+ self.assertFalse("cached/other.css" in content, content)
+ self.assertTrue("/static/cached/other.d41d8cd98f00.css" in content)
+
+ def test_template_tag_absolute(self):
+ relpath = self.cached_file_path("cached/absolute.css")
+ self.assertEqual(relpath, "cached/absolute.cc80cb5e2eb1.css")
+ with storage.staticfiles_storage.open(relpath) as relfile:
+ content = relfile.read()
+ self.assertFalse("/static/cached/styles.css" in content)
+ self.assertTrue("/static/cached/styles.5653c259030b.css" in content)
+
+ def test_template_tag_denorm(self):
+ relpath = self.cached_file_path("cached/denorm.css")
+ self.assertEqual(relpath, "cached/denorm.363de96e9b4b.css")
+ with storage.staticfiles_storage.open(relpath) as relfile:
+ content = relfile.read()
+ self.assertFalse("..//cached///styles.css" in content)
+ self.assertTrue("/static/cached/styles.5653c259030b.css" in content)
+
+ def test_template_tag_relative(self):
+ relpath = self.cached_file_path("cached/relative.css")
+ self.assertEqual(relpath, "cached/relative.298ff891a8d4.css")
+ with storage.staticfiles_storage.open(relpath) as relfile:
+ content = relfile.read()
+ self.assertFalse("../cached/styles.css" in content)
+ self.assertFalse('@import "styles.css"' in content)
+ self.assertTrue("/static/cached/styles.5653c259030b.css" in content)
+
+ def test_template_tag_url(self):
+ relpath = self.cached_file_path("cached/url.css")
+ self.assertEqual(relpath, "cached/url.615e21601e4b.css")
+ with storage.staticfiles_storage.open(relpath) as relfile:
+ self.assertTrue("https://" in relfile.read())
+
+# we set DEBUG to False here since the template tag wouldn't work otherwise
+TestCollectionCachedStorage = override_settings(**dict(TEST_SETTINGS,
+ STATICFILES_STORAGE='django.contrib.staticfiles.storage.CachedStaticFilesStorage',
+ DEBUG=False,
+))(TestCollectionCachedStorage)
if sys.platform != 'win32':
- class TestBuildStaticLinks(BuildStaticTestCase, TestDefaults):
+
+ class TestCollectionLinks(CollectionTestCase, TestDefaults):
"""
Test ``--link`` option for ``collectstatic`` management command.
@@ -267,7 +359,7 @@ if sys.platform != 'win32':
``--link`` does not change the file-selection semantics.
"""
def run_collectstatic(self):
- super(TestBuildStaticLinks, self).run_collectstatic(link=True)
+ super(TestCollectionLinks, self).run_collectstatic(link=True)
def test_links_created(self):
"""
@@ -312,6 +404,7 @@ class TestServeStaticWithDefaultURL(TestServeStatic, TestDefaults):
"""
pass
+
class TestServeStaticWithURLHelper(TestServeStatic, TestDefaults):
"""
Test static asset serving view with staticfiles_urlpatterns helper.
@@ -399,22 +492,28 @@ class TestMiscFinder(TestCase):
finders.FileSystemFinder))
def test_get_finder_bad_classname(self):
- self.assertRaises(ImproperlyConfigured,
- finders.get_finder, 'django.contrib.staticfiles.finders.FooBarFinder')
+ self.assertRaises(ImproperlyConfigured, finders.get_finder,
+ 'django.contrib.staticfiles.finders.FooBarFinder')
def test_get_finder_bad_module(self):
self.assertRaises(ImproperlyConfigured,
finders.get_finder, 'foo.bar.FooBarFinder')
-
-class TestStaticfilesDirsType(TestCase):
- """
- We can't determine if STATICFILES_DIRS is set correctly just by looking at
- the type, but we can determine if it's definitely wrong.
- """
def test_non_tuple_raises_exception(self):
- self.assertRaises(ImproperlyConfigured, finders.FileSystemFinder)
+ """
+ We can't determine if STATICFILES_DIRS is set correctly just by
+ looking at the type, but we can determine if it's definitely wrong.
+ """
+ with self.settings(STATICFILES_DIRS='a string'):
+ self.assertRaises(ImproperlyConfigured, finders.FileSystemFinder)
+
+
+class TestTemplateTag(StaticFilesTestCase):
-TestStaticfilesDirsType = override_settings(
- STATICFILES_DIRS = 'a string',
-)(TestStaticfilesDirsType)
+ def test_template_tag(self):
+ self.assertTemplateRenders("""
+ {% load static from staticfiles %}{% static "does/not/exist.png" %}
+ """, "/static/does/not/exist.png")
+ self.assertTemplateRenders("""
+ {% load static from staticfiles %}{% static "testfile.txt" %}
+ """, "/static/testfile.txt")