summaryrefslogtreecommitdiff
path: root/tests/regressiontests/staticfiles_tests
diff options
context:
space:
mode:
authorJannis Leidel <jannis@leidel.info>2010-10-20 01:33:24 +0000
committerJannis Leidel <jannis@leidel.info>2010-10-20 01:33:24 +0000
commitcfc19f84def07fb950ae8789ed0655eae4f66a92 (patch)
tree2c02cc5996a28f0ab900def5d804c961753c86d0 /tests/regressiontests/staticfiles_tests
parenta014ee02888d2fcea6880bef51f143632a60aab3 (diff)
Fixed #12323 and #11582 -- Extended the ability to handle static files. Thanks to all for helping with the original app, the patch, documentation and general support.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14293 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests/staticfiles_tests')
-rw-r--r--tests/regressiontests/staticfiles_tests/__init__.py0
-rw-r--r--tests/regressiontests/staticfiles_tests/apps/__init__.py0
-rw-r--r--tests/regressiontests/staticfiles_tests/apps/no_label/__init__.py0
-rw-r--r--tests/regressiontests/staticfiles_tests/apps/no_label/models.py0
-rw-r--r--tests/regressiontests/staticfiles_tests/apps/no_label/static/file2.txt1
-rw-r--r--tests/regressiontests/staticfiles_tests/apps/test/__init__.py0
-rw-r--r--tests/regressiontests/staticfiles_tests/apps/test/models.py0
-rw-r--r--tests/regressiontests/staticfiles_tests/apps/test/otherdir/odfile.txt1
-rw-r--r--tests/regressiontests/staticfiles_tests/apps/test/static/test/.hidden1
-rw-r--r--tests/regressiontests/staticfiles_tests/apps/test/static/test/CVS1
-rw-r--r--tests/regressiontests/staticfiles_tests/apps/test/static/test/backup~1
-rw-r--r--tests/regressiontests/staticfiles_tests/apps/test/static/test/file.txt1
-rw-r--r--tests/regressiontests/staticfiles_tests/apps/test/static/test/file1.txt1
-rw-r--r--tests/regressiontests/staticfiles_tests/apps/test/static/test/test.ignoreme1
-rw-r--r--tests/regressiontests/staticfiles_tests/models.py0
-rw-r--r--tests/regressiontests/staticfiles_tests/project/documents/subdir/test.txt1
-rw-r--r--tests/regressiontests/staticfiles_tests/project/documents/test.txt1
-rw-r--r--tests/regressiontests/staticfiles_tests/project/documents/test/file.txt2
-rw-r--r--tests/regressiontests/staticfiles_tests/project/site_media/media/media-file.txt1
-rw-r--r--tests/regressiontests/staticfiles_tests/project/site_media/static/test/storage.txt1
-rw-r--r--tests/regressiontests/staticfiles_tests/tests.py330
-rw-r--r--tests/regressiontests/staticfiles_tests/urls/__init__.py0
-rw-r--r--tests/regressiontests/staticfiles_tests/urls/default.py6
-rw-r--r--tests/regressiontests/staticfiles_tests/urls/helper.py3
24 files changed, 353 insertions, 0 deletions
diff --git a/tests/regressiontests/staticfiles_tests/__init__.py b/tests/regressiontests/staticfiles_tests/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/__init__.py
diff --git a/tests/regressiontests/staticfiles_tests/apps/__init__.py b/tests/regressiontests/staticfiles_tests/apps/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/apps/__init__.py
diff --git a/tests/regressiontests/staticfiles_tests/apps/no_label/__init__.py b/tests/regressiontests/staticfiles_tests/apps/no_label/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/apps/no_label/__init__.py
diff --git a/tests/regressiontests/staticfiles_tests/apps/no_label/models.py b/tests/regressiontests/staticfiles_tests/apps/no_label/models.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/apps/no_label/models.py
diff --git a/tests/regressiontests/staticfiles_tests/apps/no_label/static/file2.txt b/tests/regressiontests/staticfiles_tests/apps/no_label/static/file2.txt
new file mode 100644
index 0000000000..aa264cab9b
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/apps/no_label/static/file2.txt
@@ -0,0 +1 @@
+file2 in no_label_app
diff --git a/tests/regressiontests/staticfiles_tests/apps/test/__init__.py b/tests/regressiontests/staticfiles_tests/apps/test/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/apps/test/__init__.py
diff --git a/tests/regressiontests/staticfiles_tests/apps/test/models.py b/tests/regressiontests/staticfiles_tests/apps/test/models.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/apps/test/models.py
diff --git a/tests/regressiontests/staticfiles_tests/apps/test/otherdir/odfile.txt b/tests/regressiontests/staticfiles_tests/apps/test/otherdir/odfile.txt
new file mode 100644
index 0000000000..c62c93d162
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/apps/test/otherdir/odfile.txt
@@ -0,0 +1 @@
+File in otherdir.
diff --git a/tests/regressiontests/staticfiles_tests/apps/test/static/test/.hidden b/tests/regressiontests/staticfiles_tests/apps/test/static/test/.hidden
new file mode 100644
index 0000000000..cef6c23575
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/apps/test/static/test/.hidden
@@ -0,0 +1 @@
+This file should be ignored.
diff --git a/tests/regressiontests/staticfiles_tests/apps/test/static/test/CVS b/tests/regressiontests/staticfiles_tests/apps/test/static/test/CVS
new file mode 100644
index 0000000000..cef6c23575
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/apps/test/static/test/CVS
@@ -0,0 +1 @@
+This file should be ignored.
diff --git a/tests/regressiontests/staticfiles_tests/apps/test/static/test/backup~ b/tests/regressiontests/staticfiles_tests/apps/test/static/test/backup~
new file mode 100644
index 0000000000..cef6c23575
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/apps/test/static/test/backup~
@@ -0,0 +1 @@
+This file should be ignored.
diff --git a/tests/regressiontests/staticfiles_tests/apps/test/static/test/file.txt b/tests/regressiontests/staticfiles_tests/apps/test/static/test/file.txt
new file mode 100644
index 0000000000..169a206488
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/apps/test/static/test/file.txt
@@ -0,0 +1 @@
+In app media directory.
diff --git a/tests/regressiontests/staticfiles_tests/apps/test/static/test/file1.txt b/tests/regressiontests/staticfiles_tests/apps/test/static/test/file1.txt
new file mode 100644
index 0000000000..9f9a8d92ab
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/apps/test/static/test/file1.txt
@@ -0,0 +1 @@
+file1 in the app dir \ No newline at end of file
diff --git a/tests/regressiontests/staticfiles_tests/apps/test/static/test/test.ignoreme b/tests/regressiontests/staticfiles_tests/apps/test/static/test/test.ignoreme
new file mode 100644
index 0000000000..d7df09c191
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/apps/test/static/test/test.ignoreme
@@ -0,0 +1 @@
+This file should be ignored. \ No newline at end of file
diff --git a/tests/regressiontests/staticfiles_tests/models.py b/tests/regressiontests/staticfiles_tests/models.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/models.py
diff --git a/tests/regressiontests/staticfiles_tests/project/documents/subdir/test.txt b/tests/regressiontests/staticfiles_tests/project/documents/subdir/test.txt
new file mode 100644
index 0000000000..04326a212d
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/project/documents/subdir/test.txt
@@ -0,0 +1 @@
+Can we find this file?
diff --git a/tests/regressiontests/staticfiles_tests/project/documents/test.txt b/tests/regressiontests/staticfiles_tests/project/documents/test.txt
new file mode 100644
index 0000000000..04326a212d
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/project/documents/test.txt
@@ -0,0 +1 @@
+Can we find this file?
diff --git a/tests/regressiontests/staticfiles_tests/project/documents/test/file.txt b/tests/regressiontests/staticfiles_tests/project/documents/test/file.txt
new file mode 100644
index 0000000000..fdeaa23254
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/project/documents/test/file.txt
@@ -0,0 +1,2 @@
+In STATICFILES_DIRS directory.
+
diff --git a/tests/regressiontests/staticfiles_tests/project/site_media/media/media-file.txt b/tests/regressiontests/staticfiles_tests/project/site_media/media/media-file.txt
new file mode 100644
index 0000000000..466922d07a
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/project/site_media/media/media-file.txt
@@ -0,0 +1 @@
+Media file.
diff --git a/tests/regressiontests/staticfiles_tests/project/site_media/static/test/storage.txt b/tests/regressiontests/staticfiles_tests/project/site_media/static/test/storage.txt
new file mode 100644
index 0000000000..2eda9ce727
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/project/site_media/static/test/storage.txt
@@ -0,0 +1 @@
+Yeah! \ No newline at end of file
diff --git a/tests/regressiontests/staticfiles_tests/tests.py b/tests/regressiontests/staticfiles_tests/tests.py
new file mode 100644
index 0000000000..2f39354627
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/tests.py
@@ -0,0 +1,330 @@
+import tempfile
+import shutil
+import os
+import sys
+import posixpath
+
+from django.test import TestCase
+from django.conf import settings
+from django.core.exceptions import ImproperlyConfigured
+from django.core.management import call_command
+from django.db.models.loading import load_app
+from django.template import Template, Context
+
+from django.contrib.staticfiles import finders, storage
+
+TEST_ROOT = os.path.dirname(__file__)
+
+
+class StaticFilesTestCase(TestCase):
+ """
+ Test case with a couple utility assertions.
+ """
+ def setUp(self):
+ self.old_staticfiles_url = settings.STATICFILES_URL
+ self.old_staticfiles_root = settings.STATICFILES_ROOT
+ self.old_staticfiles_dirs = settings.STATICFILES_DIRS
+ self.old_staticfiles_finders = settings.STATICFILES_FINDERS
+ self.old_installed_apps = settings.INSTALLED_APPS
+ self.old_media_root = settings.MEDIA_ROOT
+ self.old_media_url = settings.MEDIA_URL
+ self.old_admin_media_prefix = settings.ADMIN_MEDIA_PREFIX
+
+ # We have to load these apps to test staticfiles.
+ load_app('regressiontests.staticfiles_tests.apps.test')
+ load_app('regressiontests.staticfiles_tests.apps.no_label')
+ site_media = os.path.join(TEST_ROOT, 'project', 'site_media')
+ settings.MEDIA_ROOT = os.path.join(site_media, 'media')
+ settings.MEDIA_URL = '/media/'
+ settings.STATICFILES_ROOT = os.path.join(site_media, 'static')
+ settings.STATICFILES_URL = '/static/'
+ settings.ADMIN_MEDIA_PREFIX = '/static/admin/'
+ settings.STATICFILES_DIRS = (
+ os.path.join(TEST_ROOT, 'project', 'documents'),
+ )
+ settings.STATICFILES_FINDERS = (
+ 'django.contrib.staticfiles.finders.FileSystemFinder',
+ 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
+ 'django.contrib.staticfiles.finders.DefaultStorageFinder',
+ )
+
+ def tearDown(self):
+ settings.MEDIA_ROOT = self.old_media_root
+ settings.MEDIA_URL = self.old_media_url
+ settings.ADMIN_MEDIA_PREFIX = self.old_admin_media_prefix
+ settings.STATICFILES_ROOT = self.old_staticfiles_root
+ settings.STATICFILES_URL = self.old_staticfiles_url
+ settings.STATICFILES_DIRS = self.old_staticfiles_dirs
+ settings.STATICFILES_FINDERS = self.old_staticfiles_finders
+ settings.INSTALLED_APPS = self.old_installed_apps
+
+ def assertFileContains(self, filepath, text):
+ self.failUnless(text in self._get_file(filepath),
+ "'%s' not in '%s'" % (text, filepath))
+
+ def assertFileNotFound(self, filepath):
+ self.assertRaises(IOError, self._get_file, filepath)
+
+
+class BuildStaticTestCase(StaticFilesTestCase):
+ """
+ Tests shared by all file-resolving features (collectstatic,
+ findstatic, and static serve view).
+
+ This relies on the asserts defined in UtilityAssertsTestCase, but
+ is separated because some test cases need those asserts without
+ all these tests.
+ """
+ def setUp(self):
+ super(BuildStaticTestCase, self).setUp()
+ self.old_staticfiles_storage = settings.STATICFILES_STORAGE
+ self.old_root = settings.STATICFILES_ROOT
+ settings.STATICFILES_ROOT = tempfile.mkdtemp()
+ self.run_collectstatic()
+
+ def tearDown(self):
+ shutil.rmtree(settings.STATICFILES_ROOT)
+ settings.STATICFILES_ROOT = self.old_root
+ super(BuildStaticTestCase, self).tearDown()
+
+ def run_collectstatic(self, **kwargs):
+ call_command('collectstatic', interactive=False, verbosity='0',
+ ignore_patterns=['*.ignoreme'], **kwargs)
+
+ def _get_file(self, filepath):
+ assert filepath, 'filepath is empty.'
+ filepath = os.path.join(settings.STATICFILES_ROOT, filepath)
+ return open(filepath).read()
+
+
+class TestDefaults(object):
+ """
+ A few standard test cases.
+ """
+ def test_staticfiles_dirs(self):
+ """
+ Can find a file in a STATICFILES_DIRS directory.
+
+ """
+ self.assertFileContains('test.txt', 'Can we find')
+
+ def test_staticfiles_dirs_subdir(self):
+ """
+ Can find a file in a subdirectory of a STATICFILES_DIRS
+ directory.
+
+ """
+ self.assertFileContains('subdir/test.txt', 'Can we find')
+
+ def test_staticfiles_dirs_priority(self):
+ """
+ File in STATICFILES_DIRS has priority over file in app.
+
+ """
+ self.assertFileContains('test/file.txt', 'STATICFILES_DIRS')
+
+ def test_app_files(self):
+ """
+ Can find a file in an app media/ directory.
+
+ """
+ self.assertFileContains('test/file1.txt', 'file1 in the app dir')
+
+
+class TestBuildStatic(BuildStaticTestCase, TestDefaults):
+ """
+ Test ``collectstatic`` management command.
+ """
+ def test_ignore(self):
+ """
+ Test that -i patterns are ignored.
+ """
+ self.assertFileNotFound('test/test.ignoreme')
+
+ def test_common_ignore_patterns(self):
+ """
+ Common ignore patterns (*~, .*, CVS) are ignored.
+ """
+ self.assertFileNotFound('test/.hidden')
+ self.assertFileNotFound('test/backup~')
+ self.assertFileNotFound('test/CVS')
+
+
+class TestBuildStaticExcludeNoDefaultIgnore(BuildStaticTestCase, TestDefaults):
+ """
+ Test ``--exclude-dirs`` and ``--no-default-ignore`` options for
+ ``collectstatic`` management command.
+ """
+ def run_collectstatic(self):
+ super(TestBuildStaticExcludeNoDefaultIgnore, self).run_collectstatic(
+ use_default_ignore_patterns=False)
+
+ def test_no_common_ignore_patterns(self):
+ """
+ With --no-default-ignore, common ignore patterns (*~, .*, CVS)
+ are not ignored.
+
+ """
+ self.assertFileContains('test/.hidden', 'should be ignored')
+ self.assertFileContains('test/backup~', 'should be ignored')
+ self.assertFileContains('test/CVS', 'should be ignored')
+
+
+class TestBuildStaticDryRun(BuildStaticTestCase):
+ """
+ Test ``--dry-run`` option for ``collectstatic`` management command.
+ """
+ def run_collectstatic(self):
+ super(TestBuildStaticDryRun, self).run_collectstatic(dry_run=True)
+
+ def test_no_files_created(self):
+ """
+ With --dry-run, no files created in destination dir.
+ """
+ self.assertEquals(os.listdir(settings.STATICFILES_ROOT), [])
+
+
+if sys.platform != 'win32':
+ class TestBuildStaticLinks(BuildStaticTestCase, TestDefaults):
+ """
+ Test ``--link`` option for ``collectstatic`` management command.
+
+ Note that by inheriting ``TestDefaults`` we repeat all
+ the standard file resolving tests here, to make sure using
+ ``--link`` does not change the file-selection semantics.
+ """
+ def run_collectstatic(self):
+ super(TestBuildStaticLinks, self).run_collectstatic(link=True)
+
+ def test_links_created(self):
+ """
+ With ``--link``, symbolic links are created.
+
+ """
+ self.failUnless(os.path.islink(os.path.join(settings.STATICFILES_ROOT, 'test.txt')))
+
+
+class TestServeStatic(StaticFilesTestCase):
+ """
+ Test static asset serving view.
+ """
+ def _response(self, filepath):
+ return self.client.get(
+ posixpath.join(settings.STATICFILES_URL, filepath))
+
+ def assertFileContains(self, filepath, text):
+ self.assertContains(self._response(filepath), text)
+
+ def assertFileNotFound(self, filepath):
+ self.assertEquals(self._response(filepath).status_code, 404)
+
+
+class TestServeStaticWithDefaultURL(TestServeStatic, TestDefaults):
+ """
+ Test static asset serving view with staticfiles_urlpatterns helper.
+ """
+ urls = "regressiontests.staticfiles_tests.urls.default"
+
+
+class TestServeStaticWithURLHelper(TestServeStatic, TestDefaults):
+ """
+ Test static asset serving view with staticfiles_urlpatterns helper.
+ """
+ urls = "regressiontests.staticfiles_tests.urls.helper"
+
+
+class TestServeAdminMedia(TestServeStatic):
+ """
+ Test serving media from django.contrib.admin.
+ """
+ def _response(self, filepath):
+ return self.client.get(
+ posixpath.join(settings.ADMIN_MEDIA_PREFIX, filepath))
+
+ def test_serve_admin_media(self):
+ self.assertFileContains('css/base.css', 'body')
+
+
+class FinderTestCase(object):
+ """
+ Base finder test mixin
+ """
+ def test_find_first(self):
+ src, dst = self.find_first
+ self.assertEquals(self.finder.find(src), dst)
+
+ def test_find_all(self):
+ src, dst = self.find_all
+ self.assertEquals(self.finder.find(src, all=True), dst)
+
+
+class TestFileSystemFinder(StaticFilesTestCase, FinderTestCase):
+ """
+ Test FileSystemFinder.
+ """
+ def setUp(self):
+ super(TestFileSystemFinder, self).setUp()
+ self.finder = finders.FileSystemFinder()
+ test_file_path = os.path.join(TEST_ROOT, 'project/documents/test/file.txt')
+ self.find_first = ("test/file.txt", test_file_path)
+ self.find_all = ("test/file.txt", [test_file_path])
+
+
+class TestAppDirectoriesFinder(StaticFilesTestCase, FinderTestCase):
+ """
+ Test AppDirectoriesFinder.
+ """
+ def setUp(self):
+ super(TestAppDirectoriesFinder, self).setUp()
+ self.finder = finders.AppDirectoriesFinder()
+ test_file_path = os.path.join(TEST_ROOT, 'apps/test/static/test/file1.txt')
+ self.find_first = ("test/file1.txt", test_file_path)
+ self.find_all = ("test/file1.txt", [test_file_path])
+
+
+class TestDefaultStorageFinder(StaticFilesTestCase, FinderTestCase):
+ """
+ Test DefaultStorageFinder.
+ """
+ def setUp(self):
+ super(TestDefaultStorageFinder, self).setUp()
+ self.finder = finders.DefaultStorageFinder(
+ storage=storage.StaticFilesStorage(location=settings.MEDIA_ROOT))
+ test_file_path = os.path.join(settings.MEDIA_ROOT, 'media-file.txt')
+ self.find_first = ("media-file.txt", test_file_path)
+ self.find_all = ("media-file.txt", [test_file_path])
+
+
+class TestMiscFinder(TestCase):
+ """
+ A few misc finder tests.
+ """
+ def test_get_finder(self):
+ self.assertTrue(isinstance(finders.get_finder(
+ "django.contrib.staticfiles.finders.FileSystemFinder"),
+ finders.FileSystemFinder))
+ self.assertRaises(ImproperlyConfigured,
+ finders.get_finder, "django.contrib.staticfiles.finders.FooBarFinder")
+ self.assertRaises(ImproperlyConfigured,
+ finders.get_finder, "foo.bar.FooBarFinder")
+
+
+class TemplateTagTest(TestCase):
+ def test_get_staticfiles_prefix(self):
+ """
+ Test the get_staticfiles_prefix helper return the STATICFILES_URL setting.
+ """
+ self.assertEquals(Template(
+ "{% load staticfiles %}"
+ "{% get_staticfiles_prefix %}"
+ ).render(Context()), settings.STATICFILES_URL)
+
+ def test_get_staticfiles_prefix_with_as(self):
+ """
+ Test the get_staticfiles_prefix helper return the STATICFILES_URL setting.
+ """
+ self.assertEquals(Template(
+ "{% load staticfiles %}"
+ "{% get_staticfiles_prefix as staticfiles_prefix %}"
+ "{{ staticfiles_prefix }}"
+ ).render(Context()), settings.STATICFILES_URL)
diff --git a/tests/regressiontests/staticfiles_tests/urls/__init__.py b/tests/regressiontests/staticfiles_tests/urls/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/urls/__init__.py
diff --git a/tests/regressiontests/staticfiles_tests/urls/default.py b/tests/regressiontests/staticfiles_tests/urls/default.py
new file mode 100644
index 0000000000..061ec6465e
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/urls/default.py
@@ -0,0 +1,6 @@
+from django.conf import settings
+from django.conf.urls.defaults import *
+
+urlpatterns = patterns('',
+ url(r'^static/(?P<path>.*)$', 'django.contrib.staticfiles.views.serve'),
+)
diff --git a/tests/regressiontests/staticfiles_tests/urls/helper.py b/tests/regressiontests/staticfiles_tests/urls/helper.py
new file mode 100644
index 0000000000..e4951d1afb
--- /dev/null
+++ b/tests/regressiontests/staticfiles_tests/urls/helper.py
@@ -0,0 +1,3 @@
+from django.contrib.staticfiles.urls import staticfiles_urlpatterns
+
+urlpatterns = staticfiles_urlpatterns()