2011-02-14 23:45:32 +00:00
# -*- encoding: utf-8 -*-
2012-06-07 16:08:47 +00:00
from __future__ import unicode_literals
2011-10-13 21:34:56 +00:00
2011-02-14 23:45:32 +00:00
import codecs
2010-10-20 01:33:24 +00:00
import os
import posixpath
2010-12-04 07:28:12 +00:00
import shutil
import sys
import tempfile
2010-10-20 01:33:24 +00:00
2011-08-11 14:07:39 +00:00
from django . template import loader , Context
2010-10-20 01:33:24 +00:00
from django . conf import settings
2012-05-16 11:21:46 +00:00
from django . core . cache . backends . base import BaseCache
2010-10-20 01:33:24 +00:00
from django . core . exceptions import ImproperlyConfigured
from django . core . management import call_command
2010-12-04 07:28:12 +00:00
from django . test import TestCase
2011-06-30 09:06:19 +00:00
from django . test . utils import override_settings
2012-07-21 08:00:10 +00:00
from django . utils . encoding import smart_text
2011-06-01 20:45:47 +00:00
from django . utils . functional import empty
2010-12-31 14:22:55 +00:00
from django . utils . _os import rmtree_errorhandler
2012-07-20 12:22:00 +00:00
from django . utils import six
2010-10-20 01:33:24 +00:00
2011-06-30 09:06:19 +00:00
from django . contrib . staticfiles import finders , storage
2010-10-20 01:33:24 +00:00
2011-05-27 10:55:07 +00:00
TEST_ROOT = os . path . dirname ( __file__ )
2011-08-11 14:07:39 +00:00
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 ' ,
) ,
}
2012-02-13 10:51:17 +00:00
from django . contrib . staticfiles . management . commands . collectstatic import Command as CollectstaticCommand
2010-12-31 14:22:32 +00:00
2010-10-20 01:33:24 +00:00
2011-08-11 14:07:39 +00:00
class BaseStaticFilesTestCase ( object ) :
2010-10-20 01:33:24 +00:00
"""
Test case with a couple utility assertions .
"""
def setUp ( self ) :
2012-10-27 20:38:15 +00:00
# Clear the cached staticfiles_storage out, this is because when it first
# gets accessed (by some other test), it evaluates settings.STATIC_ROOT,
2010-10-30 23:39:53 +00:00
# since we're planning on changing that we need to clear out the cache.
2011-08-11 14:07:39 +00:00
storage . staticfiles_storage . _wrapped = empty
2012-07-25 07:57:00 +00:00
# Clear the cached staticfile finders, so they are reinitialized every
# run and pick up changes in settings.STATICFILES_DIRS.
finders . _finders . clear ( )
2010-10-30 23:39:53 +00:00
2012-02-13 10:51:17 +00:00
testfiles_path = os . path . join ( TEST_ROOT , ' apps ' , ' test ' , ' static ' , ' test ' )
2011-02-15 18:15:38 +00:00
# To make sure SVN doesn't hangs itself with the non-ASCII characters
# during checkout, we actually create one file dynamically.
2012-06-07 16:08:47 +00:00
self . _nonascii_filepath = os . path . join ( testfiles_path , ' fi \u015f ier.txt ' )
2012-02-13 10:51:17 +00:00
with codecs . open ( self . _nonascii_filepath , ' w ' , ' utf-8 ' ) as f :
2012-06-07 16:08:47 +00:00
f . write ( " fi \u015f ier in the app dir " )
2012-02-13 10:51:17 +00:00
# And also create the stupid hidden file to dwarf the setup.py's
# package data handling.
self . _hidden_filepath = os . path . join ( testfiles_path , ' .hidden ' )
with codecs . open ( self . _hidden_filepath , ' w ' , ' utf-8 ' ) as f :
f . write ( " should be ignored " )
self . _backup_filepath = os . path . join (
TEST_ROOT , ' project ' , ' documents ' , ' test ' , ' backup~ ' )
with codecs . open ( self . _backup_filepath , ' w ' , ' utf-8 ' ) as f :
f . write ( " should be ignored " )
def tearDown ( self ) :
os . unlink ( self . _nonascii_filepath )
os . unlink ( self . _hidden_filepath )
os . unlink ( self . _backup_filepath )
2010-10-20 01:33:24 +00:00
def assertFileContains ( self , filepath , text ) :
2012-07-21 08:00:10 +00:00
self . assertIn ( text , self . _get_file ( smart_text ( filepath ) ) ,
2012-06-07 16:08:47 +00:00
" ' %s ' not in ' %s ' " % ( text , filepath ) )
2010-10-20 01:33:24 +00:00
def assertFileNotFound ( self , filepath ) :
self . assertRaises ( IOError , self . _get_file , filepath )
2011-08-11 14:07:39 +00:00
def render_template ( self , template , * * kwargs ) :
2012-07-20 12:22:00 +00:00
if isinstance ( template , six . string_types ) :
2011-08-11 14:07:39 +00:00
template = loader . get_template_from_string ( template )
return template . render ( Context ( kwargs ) ) . strip ( )
2012-07-07 13:30:25 +00:00
def static_template_snippet ( self , path , asvar = False ) :
if asvar :
return " { %% load static from staticfiles %% } { %% static ' %s ' as var %% } {{ var }} " % path
2011-11-03 10:51:02 +00:00
return " { %% load static from staticfiles %% } { %% static ' %s ' %% } " % path
2012-07-07 13:30:25 +00:00
def assertStaticRenders ( self , path , result , asvar = False , * * kwargs ) :
template = self . static_template_snippet ( path , asvar )
2011-08-11 14:07:39 +00:00
self . assertEqual ( self . render_template ( template , * * kwargs ) , result )
2011-06-30 09:06:19 +00:00
2012-07-07 13:30:25 +00:00
def assertStaticRaises ( self , exc , path , result , asvar = False , * * kwargs ) :
2011-11-03 10:51:02 +00:00
self . assertRaises ( exc , self . assertStaticRenders , path , result , * * kwargs )
2010-10-20 01:33:24 +00:00
2011-08-11 14:07:39 +00:00
2012-03-30 09:08:29 +00:00
@override_settings ( * * TEST_SETTINGS )
2011-08-11 14:07:39 +00:00
class StaticFilesTestCase ( BaseStaticFilesTestCase , TestCase ) :
pass
class BaseCollectionTestCase ( BaseStaticFilesTestCase ) :
2010-10-20 01:33:24 +00:00
"""
2011-08-11 14:07:39 +00:00
Tests shared by all file finding features ( collectstatic ,
2010-10-20 01:33:24 +00:00
findstatic , and static serve view ) .
2012-02-13 10:51:17 +00:00
This relies on the asserts defined in BaseStaticFilesTestCase , but
2010-10-20 01:33:24 +00:00
is separated because some test cases need those asserts without
all these tests .
"""
def setUp ( self ) :
2011-08-11 14:07:39 +00:00
super ( BaseCollectionTestCase , self ) . setUp ( )
2010-11-17 15:36:26 +00:00
self . old_root = settings . STATIC_ROOT
2011-11-13 19:05:02 +00:00
settings . STATIC_ROOT = tempfile . mkdtemp ( dir = os . environ [ ' DJANGO_TEST_TEMP_DIR ' ] )
2010-10-20 01:33:24 +00:00
self . run_collectstatic ( )
2011-06-30 09:06:19 +00:00
# Use our own error handler that can handle .svn dirs on Windows
2011-12-22 20:29:18 +00:00
self . addCleanup ( shutil . rmtree , settings . STATIC_ROOT ,
ignore_errors = True , onerror = rmtree_errorhandler )
2010-10-20 01:33:24 +00:00
def tearDown ( self ) :
2010-11-17 15:36:26 +00:00
settings . STATIC_ROOT = self . old_root
2011-08-11 14:07:39 +00:00
super ( BaseCollectionTestCase , self ) . tearDown ( )
2010-10-20 01:33:24 +00:00
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. '
2010-11-17 15:36:26 +00:00
filepath = os . path . join ( settings . STATIC_ROOT , filepath )
2011-07-04 21:34:29 +00:00
with codecs . open ( filepath , " r " , " utf-8 " ) as f :
2010-10-30 04:24:24 +00:00
return f . read ( )
2010-10-20 01:33:24 +00:00
2011-06-30 09:06:19 +00:00
2011-08-11 14:07:39 +00:00
class CollectionTestCase ( BaseCollectionTestCase , StaticFilesTestCase ) :
pass
2010-10-20 01:33:24 +00:00
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 ' )
2011-07-06 10:28:18 +00:00
self . assertFileContains ( os . path . join ( ' prefix ' , ' test.txt ' ) , ' Prefix ' )
2010-10-20 01:33:24 +00:00
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 ) :
"""
2011-02-14 23:45:32 +00:00
Can find a file in an app static / directory .
2010-10-20 01:33:24 +00:00
"""
self . assertFileContains ( ' test/file1.txt ' , ' file1 in the app dir ' )
2011-02-14 23:45:32 +00:00
def test_nonascii_filenames ( self ) :
"""
Can find a file with non - ASCII character in an app static / directory .
"""
2012-06-07 16:08:47 +00:00
self . assertFileContains ( ' test/fişier.txt ' , ' fişier in the app dir ' )
2011-02-14 23:45:32 +00:00
2011-03-18 18:47:08 +00:00
def test_camelcase_filenames ( self ) :
"""
Can find a file with capital letters .
"""
2012-06-07 16:08:47 +00:00
self . assertFileContains ( ' test/camelCase.txt ' , ' camelCase ' )
2011-03-18 18:47:08 +00:00
2010-10-20 01:33:24 +00:00
2011-08-11 14:07:39 +00:00
class TestFindStatic ( CollectionTestCase , TestDefaults ) :
2010-10-23 14:32:54 +00:00
"""
Test ` ` findstatic ` ` management command .
"""
def _get_file ( self , filepath ) :
2012-08-11 16:56:14 +00:00
out = six . StringIO ( )
2012-05-28 09:55:38 +00:00
call_command ( ' findstatic ' , filepath , all = False , verbosity = 0 , stdout = out )
out . seek ( 0 )
lines = [ l . strip ( ) for l in out . readlines ( ) ]
2012-08-15 08:25:01 +00:00
with codecs . open ( smart_text ( lines [ 1 ] . strip ( ) ) , " r " , " utf-8 " ) as f :
return f . read ( )
2010-10-23 14:32:54 +00:00
def test_all_files ( self ) :
"""
Test that findstatic returns all candidate files if run without - - first .
"""
2012-08-11 16:56:14 +00:00
out = six . StringIO ( )
2012-05-28 09:55:38 +00:00
call_command ( ' findstatic ' , ' test/file.txt ' , verbosity = 0 , stdout = out )
out . seek ( 0 )
lines = [ l . strip ( ) for l in out . readlines ( ) ]
2011-08-11 14:07:39 +00:00
self . assertEqual ( len ( lines ) , 3 ) # three because there is also the "Found <file> here" line
2011-11-03 10:51:02 +00:00
self . assertIn ( ' project ' , lines [ 1 ] )
self . assertIn ( ' apps ' , lines [ 2 ] )
2010-10-23 14:32:54 +00:00
2011-08-11 14:07:39 +00:00
class TestCollection ( CollectionTestCase , TestDefaults ) :
2010-10-20 01:33:24 +00:00
"""
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 ' )
2011-08-11 14:07:39 +00:00
class TestCollectionClear ( CollectionTestCase ) :
2011-07-04 21:34:29 +00:00
"""
Test the ` ` - - clear ` ` option of the ` ` collectstatic ` ` managemenet command .
"""
def run_collectstatic ( self , * * kwargs ) :
clear_filepath = os . path . join ( settings . STATIC_ROOT , ' cleared.txt ' )
with open ( clear_filepath , ' w ' ) as f :
f . write ( ' should be cleared ' )
2011-08-11 14:07:39 +00:00
super ( TestCollectionClear , self ) . run_collectstatic ( clear = True )
2011-07-04 21:34:29 +00:00
def test_cleared_not_found ( self ) :
self . assertFileNotFound ( ' cleared.txt ' )
2011-08-11 14:07:39 +00:00
class TestCollectionExcludeNoDefaultIgnore ( CollectionTestCase , TestDefaults ) :
2010-10-20 01:33:24 +00:00
"""
2011-07-04 21:34:29 +00:00
Test ` ` - - exclude - dirs ` ` and ` ` - - no - default - ignore ` ` options of the
2010-10-20 01:33:24 +00:00
` ` collectstatic ` ` management command .
"""
def run_collectstatic ( self ) :
2011-08-11 14:07:39 +00:00
super ( TestCollectionExcludeNoDefaultIgnore , self ) . run_collectstatic (
2010-10-20 01:33:24 +00:00
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 ' )
2011-01-08 10:03:27 +00:00
class TestNoFilesCreated ( object ) :
def test_no_files_created ( self ) :
"""
Make sure no files were create in the destination directory .
"""
2011-03-03 15:04:39 +00:00
self . assertEqual ( os . listdir ( settings . STATIC_ROOT ) , [ ] )
2011-01-08 10:03:27 +00:00
2011-08-11 14:07:39 +00:00
class TestCollectionDryRun ( CollectionTestCase , TestNoFilesCreated ) :
2010-10-20 01:33:24 +00:00
"""
Test ` ` - - dry - run ` ` option for ` ` collectstatic ` ` management command .
"""
def run_collectstatic ( self ) :
2011-08-11 14:07:39 +00:00
super ( TestCollectionDryRun , self ) . run_collectstatic ( dry_run = True )
2010-10-20 01:33:24 +00:00
2011-01-08 10:03:27 +00:00
2012-03-01 23:02:38 +00:00
class TestCollectionFilesOverride ( CollectionTestCase ) :
"""
Test overriding duplicated files by ` ` collectstatic ` ` management command .
Check for proper handling of apps order in INSTALLED_APPS even if file modification
dates are in different order :
' regressiontests.staticfiles_tests.apps.test ' ,
' regressiontests.staticfiles_tests.apps.no_label ' ,
"""
def setUp ( self ) :
self . orig_path = os . path . join ( TEST_ROOT , ' apps ' , ' no_label ' , ' static ' , ' file2.txt ' )
# get modification and access times for no_label/static/file2.txt
self . orig_mtime = os . path . getmtime ( self . orig_path )
self . orig_atime = os . path . getatime ( self . orig_path )
# prepare duplicate of file2.txt from no_label app
# this file will have modification time older than no_label/static/file2.txt
# anyway it should be taken to STATIC_ROOT because 'test' app is before
# 'no_label' app in INSTALLED_APPS
self . testfile_path = os . path . join ( TEST_ROOT , ' apps ' , ' test ' , ' static ' , ' file2.txt ' )
with open ( self . testfile_path , ' w+ ' ) as f :
f . write ( ' duplicate of file2.txt ' )
os . utime ( self . testfile_path , ( self . orig_atime - 1 , self . orig_mtime - 1 ) )
super ( TestCollectionFilesOverride , self ) . setUp ( )
def tearDown ( self ) :
if os . path . exists ( self . testfile_path ) :
os . unlink ( self . testfile_path )
# set back original modification time
os . utime ( self . orig_path , ( self . orig_atime , self . orig_mtime ) )
2012-03-16 00:29:11 +00:00
super ( TestCollectionFilesOverride , self ) . tearDown ( )
2012-03-01 23:02:38 +00:00
def test_ordering_override ( self ) :
"""
Test if collectstatic takes files in proper order
"""
self . assertFileContains ( ' file2.txt ' , ' duplicate of file2.txt ' )
# run collectstatic again
self . run_collectstatic ( )
self . assertFileContains ( ' file2.txt ' , ' duplicate of file2.txt ' )
# and now change modification time of no_label/static/file2.txt
# test app is first in INSTALLED_APPS so file2.txt should remain unmodified
mtime = os . path . getmtime ( self . testfile_path )
atime = os . path . getatime ( self . testfile_path )
os . utime ( self . orig_path , ( mtime + 1 , atime + 1 ) )
# run collectstatic again
self . run_collectstatic ( )
self . assertFileContains ( ' file2.txt ' , ' duplicate of file2.txt ' )
2012-03-30 09:08:29 +00:00
@override_settings (
STATICFILES_STORAGE = ' regressiontests.staticfiles_tests.storage.DummyStorage ' ,
)
2011-08-11 14:07:39 +00:00
class TestCollectionNonLocalStorage ( CollectionTestCase , TestNoFilesCreated ) :
2011-01-08 10:03:27 +00:00
"""
Tests for #15035
"""
2011-06-30 09:06:19 +00:00
pass
2011-01-08 10:03:27 +00:00
2011-08-11 14:07:39 +00:00
2012-03-30 09:08:29 +00:00
# we set DEBUG to False here since the template tag wouldn't work otherwise
@override_settings ( * * dict ( TEST_SETTINGS ,
STATICFILES_STORAGE = ' django.contrib.staticfiles.storage.CachedStaticFilesStorage ' ,
DEBUG = False ,
) )
2011-08-11 14:07:39 +00:00
class TestCollectionCachedStorage ( BaseCollectionTestCase ,
BaseStaticFilesTestCase , TestCase ) :
"""
Tests for the Cache busting storage
"""
2011-11-03 10:51:02 +00:00
def cached_file_path ( self , path ) :
fullpath = self . render_template ( self . static_template_snippet ( path ) )
2011-08-11 14:07:39 +00:00
return fullpath . replace ( settings . STATIC_URL , ' ' )
def test_template_tag_return ( self ) :
"""
Test the CachedStaticFilesStorage backend .
"""
2011-11-03 10:51:02 +00:00
self . assertStaticRaises ( ValueError ,
" does/not/exist.png " ,
" /static/does/not/exist.png " )
self . assertStaticRenders ( " test/file.txt " ,
2012-03-01 23:02:38 +00:00
" /static/test/file.dad0999e4f8f.txt " )
2012-07-07 13:30:25 +00:00
self . assertStaticRenders ( " test/file.txt " ,
" /static/test/file.dad0999e4f8f.txt " , asvar = True )
2011-11-03 10:51:02 +00:00
self . assertStaticRenders ( " cached/styles.css " ,
" /static/cached/styles.93b1147e8552.css " )
2012-02-16 14:40:32 +00:00
self . assertStaticRenders ( " path/ " ,
" /static/path/ " )
self . assertStaticRenders ( " path/?query " ,
" /static/path/?query " )
2011-08-11 14:07:39 +00:00
def test_template_tag_simple_content ( self ) :
relpath = self . cached_file_path ( " cached/styles.css " )
2011-09-21 15:58:21 +00:00
self . assertEqual ( relpath , " cached/styles.93b1147e8552.css " )
2011-08-11 14:07:39 +00:00
with storage . staticfiles_storage . open ( relpath ) as relfile :
content = relfile . read ( )
2012-05-19 15:43:34 +00:00
self . assertNotIn ( b " cached/other.css " , content )
self . assertIn ( b " other.d41d8cd98f00.css " , content )
2011-11-03 10:51:02 +00:00
2012-07-08 16:25:12 +00:00
def test_path_ignored_completely ( self ) :
relpath = self . cached_file_path ( " cached/css/ignored.css " )
self . assertEqual ( relpath , " cached/css/ignored.6c77f2643390.css " )
with storage . staticfiles_storage . open ( relpath ) as relfile :
content = relfile . read ( )
self . assertIn ( b ' #foobar ' , content )
self . assertIn ( b ' http:foobar ' , content )
self . assertIn ( b ' https:foobar ' , content )
self . assertIn ( b ' data:foobar ' , content )
self . assertIn ( b ' //foobar ' , content )
2011-11-03 10:51:02 +00:00
def test_path_with_querystring ( self ) :
relpath = self . cached_file_path ( " cached/styles.css?spam=eggs " )
self . assertEqual ( relpath ,
" cached/styles.93b1147e8552.css?spam=eggs " )
with storage . staticfiles_storage . open (
" cached/styles.93b1147e8552.css " ) as relfile :
content = relfile . read ( )
2012-05-19 15:43:34 +00:00
self . assertNotIn ( b " cached/other.css " , content )
self . assertIn ( b " other.d41d8cd98f00.css " , content )
2011-11-03 10:51:02 +00:00
def test_path_with_fragment ( self ) :
relpath = self . cached_file_path ( " cached/styles.css#eggs " )
self . assertEqual ( relpath , " cached/styles.93b1147e8552.css#eggs " )
with storage . staticfiles_storage . open (
" cached/styles.93b1147e8552.css " ) as relfile :
content = relfile . read ( )
2012-05-19 15:43:34 +00:00
self . assertNotIn ( b " cached/other.css " , content )
self . assertIn ( b " other.d41d8cd98f00.css " , content )
2011-08-11 14:07:39 +00:00
2011-12-27 22:49:24 +00:00
def test_path_with_querystring_and_fragment ( self ) :
relpath = self . cached_file_path ( " cached/css/fragments.css " )
self . assertEqual ( relpath , " cached/css/fragments.75433540b096.css " )
with storage . staticfiles_storage . open ( relpath ) as relfile :
content = relfile . read ( )
2012-05-19 15:43:34 +00:00
self . assertIn ( b ' fonts/font.a4b0478549d0.eot?#iefix ' , content )
self . assertIn ( b ' fonts/font.b8d603e42714.svg#webfontIyfZbseF ' , content )
self . assertIn ( b ' data:font/woff;charset=utf-8;base64,d09GRgABAAAAADJoAA0AAAAAR2QAAQAAAAAAAAAAAAA ' , content )
self . assertIn ( b ' #default#VML ' , content )
2011-12-27 22:49:24 +00:00
2011-08-11 14:07:39 +00:00
def test_template_tag_absolute ( self ) :
relpath = self . cached_file_path ( " cached/absolute.css " )
2012-03-02 11:29:14 +00:00
self . assertEqual ( relpath , " cached/absolute.23f087ad823a.css " )
2011-08-11 14:07:39 +00:00
with storage . staticfiles_storage . open ( relpath ) as relfile :
content = relfile . read ( )
2012-05-19 15:43:34 +00:00
self . assertNotIn ( b " /static/cached/styles.css " , content )
self . assertIn ( b " /static/cached/styles.93b1147e8552.css " , content )
self . assertIn ( b ' /static/cached/img/relative.acae32e4532b.png ' , content )
2011-08-11 14:07:39 +00:00
def test_template_tag_denorm ( self ) :
relpath = self . cached_file_path ( " cached/denorm.css " )
2012-03-13 03:48:03 +00:00
self . assertEqual ( relpath , " cached/denorm.c5bd139ad821.css " )
2011-08-11 14:07:39 +00:00
with storage . staticfiles_storage . open ( relpath ) as relfile :
content = relfile . read ( )
2012-05-19 15:43:34 +00:00
self . assertNotIn ( b " ..//cached///styles.css " , content )
self . assertIn ( b " ../cached/styles.93b1147e8552.css " , content )
self . assertNotIn ( b " url(img/relative.png ) " , content )
self . assertIn ( b ' url( " img/relative.acae32e4532b.png ' , content )
2011-08-11 14:07:39 +00:00
def test_template_tag_relative ( self ) :
relpath = self . cached_file_path ( " cached/relative.css " )
2011-11-03 10:51:02 +00:00
self . assertEqual ( relpath , " cached/relative.2217ea7273c2.css " )
2011-08-11 14:07:39 +00:00
with storage . staticfiles_storage . open ( relpath ) as relfile :
content = relfile . read ( )
2012-05-19 15:43:34 +00:00
self . assertNotIn ( b " ../cached/styles.css " , content )
self . assertNotIn ( b ' @import " styles.css " ' , content )
self . assertNotIn ( b ' url(img/relative.png) ' , content )
self . assertIn ( b ' url( " img/relative.acae32e4532b.png " ) ' , content )
self . assertIn ( b " ../cached/styles.93b1147e8552.css " , content )
2011-09-21 15:58:21 +00:00
2012-07-08 16:17:53 +00:00
def test_import_replacement ( self ) :
" See #18050 "
relpath = self . cached_file_path ( " cached/import.css " )
self . assertEqual ( relpath , " cached/import.2b1d40b0bbd4.css " )
with storage . staticfiles_storage . open ( relpath ) as relfile :
self . assertIn ( b """ import url( " styles.93b1147e8552.css " ) """ , relfile . read ( ) )
2011-09-21 15:58:21 +00:00
def test_template_tag_deep_relative ( self ) :
relpath = self . cached_file_path ( " cached/css/window.css " )
self . assertEqual ( relpath , " cached/css/window.9db38d5169f3.css " )
with storage . staticfiles_storage . open ( relpath ) as relfile :
content = relfile . read ( )
2012-05-19 15:43:34 +00:00
self . assertNotIn ( b ' url(img/window.png) ' , content )
self . assertIn ( b ' url( " img/window.acae32e4532b.png " ) ' , content )
2011-08-11 14:07:39 +00:00
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 :
2012-05-19 15:43:34 +00:00
self . assertIn ( b " https:// " , relfile . read ( ) )
2011-08-11 14:07:39 +00:00
2011-11-02 17:45:32 +00:00
def test_cache_invalidation ( self ) :
name = " cached/styles.css "
hashed_name = " cached/styles.93b1147e8552.css "
# check if the cache is filled correctly as expected
cache_key = storage . staticfiles_storage . cache_key ( name )
cached_name = storage . staticfiles_storage . cache . get ( cache_key )
self . assertEqual ( self . cached_file_path ( name ) , cached_name )
# clearing the cache to make sure we re-set it correctly in the url method
storage . staticfiles_storage . cache . clear ( )
cached_name = storage . staticfiles_storage . cache . get ( cache_key )
self . assertEqual ( cached_name , None )
self . assertEqual ( self . cached_file_path ( name ) , hashed_name )
cached_name = storage . staticfiles_storage . cache . get ( cache_key )
self . assertEqual ( cached_name , hashed_name )
2012-02-13 10:51:17 +00:00
def test_post_processing ( self ) :
""" Test that post_processing behaves correctly.
Files that are alterable should always be post - processed ; files that
aren ' t should be skipped.
collectstatic has already been called once in setUp ( ) for this testcase ,
therefore we check by verifying behavior on a second run .
"""
collectstatic_args = {
' interactive ' : False ,
' verbosity ' : ' 0 ' ,
' link ' : False ,
' clear ' : False ,
' dry_run ' : False ,
' post_process ' : True ,
' use_default_ignore_patterns ' : True ,
' ignore_patterns ' : [ ' *.ignoreme ' ] ,
}
collectstatic_cmd = CollectstaticCommand ( )
collectstatic_cmd . set_options ( * * collectstatic_args )
stats = collectstatic_cmd . collect ( )
2012-07-08 10:56:49 +00:00
self . assertIn ( os . path . join ( ' cached ' , ' css ' , ' window.css ' ) , stats [ ' post_processed ' ] )
self . assertIn ( os . path . join ( ' cached ' , ' css ' , ' img ' , ' window.png ' ) , stats [ ' unmodified ' ] )
self . assertIn ( os . path . join ( ' test ' , ' nonascii.css ' ) , stats [ ' post_processed ' ] )
2012-02-13 10:51:17 +00:00
2012-03-12 20:28:09 +00:00
def test_cache_key_memcache_validation ( self ) :
"""
Handle cache key creation correctly , see #17861.
"""
2012-08-14 15:24:31 +00:00
name = " /some crazy/long filename/ with spaces Here and ?# % #$/other/stuff/some crazy/long filename/ with spaces Here and ?# % #$/other/stuff/some crazy/long filename/ with spaces Here and ?# % #$/other/stuff/some crazy/long filename/ with spaces Here and ?# % #$/other/stuff/some crazy/long filename/ with spaces Here and ?# % #$/other/stuff/some crazy/ " + " \x16 " + " \xb4 "
2012-03-12 20:28:09 +00:00
cache_key = storage . staticfiles_storage . cache_key ( name )
cache_validator = BaseCache ( { } )
cache_validator . validate_key ( cache_key )
2012-08-14 15:24:31 +00:00
self . assertEqual ( cache_key , ' staticfiles:821ea71ef36f95b3922a77f7364670e7 ' )
2012-03-12 20:28:09 +00:00
2012-05-16 11:21:46 +00:00
# we set DEBUG to False here since the template tag wouldn't work otherwise
@override_settings ( * * dict ( TEST_SETTINGS ,
STATICFILES_STORAGE = ' regressiontests.staticfiles_tests.storage.SimpleCachedStaticFilesStorage ' ,
DEBUG = False ,
) )
class TestCollectionSimpleCachedStorage ( BaseCollectionTestCase ,
BaseStaticFilesTestCase , TestCase ) :
"""
Tests for the Cache busting storage
"""
def cached_file_path ( self , path ) :
fullpath = self . render_template ( self . static_template_snippet ( path ) )
return fullpath . replace ( settings . STATIC_URL , ' ' )
def test_template_tag_return ( self ) :
"""
Test the CachedStaticFilesStorage backend .
"""
self . assertStaticRaises ( ValueError ,
" does/not/exist.png " ,
" /static/does/not/exist.png " )
self . assertStaticRenders ( " test/file.txt " ,
" /static/test/file.deploy12345.txt " )
self . assertStaticRenders ( " cached/styles.css " ,
" /static/cached/styles.deploy12345.css " )
self . assertStaticRenders ( " path/ " ,
" /static/path/ " )
self . assertStaticRenders ( " path/?query " ,
" /static/path/?query " )
def test_template_tag_simple_content ( self ) :
relpath = self . cached_file_path ( " cached/styles.css " )
self . assertEqual ( relpath , " cached/styles.deploy12345.css " )
with storage . staticfiles_storage . open ( relpath ) as relfile :
content = relfile . read ( )
2012-08-14 15:24:31 +00:00
self . assertNotIn ( b " cached/other.css " , content )
self . assertIn ( b " other.deploy12345.css " , content )
2012-05-16 11:21:46 +00:00
2010-10-20 01:33:24 +00:00
if sys . platform != ' win32 ' :
2011-08-11 14:07:39 +00:00
class TestCollectionLinks ( CollectionTestCase , TestDefaults ) :
2010-10-20 01:33:24 +00:00
"""
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 ) :
2011-08-11 14:07:39 +00:00
super ( TestCollectionLinks , self ) . run_collectstatic ( link = True )
2010-10-20 01:33:24 +00:00
def test_links_created ( self ) :
"""
With ` ` - - link ` ` , symbolic links are created .
"""
2010-12-04 07:28:12 +00:00
self . assertTrue ( os . path . islink ( os . path . join ( settings . STATIC_ROOT , ' test.txt ' ) ) )
2010-10-20 01:33:24 +00:00
class TestServeStatic ( StaticFilesTestCase ) :
"""
Test static asset serving view .
"""
2010-12-31 14:22:32 +00:00
urls = ' regressiontests.staticfiles_tests.urls.default '
2010-10-21 03:16:41 +00:00
2010-10-20 01:33:24 +00:00
def _response ( self , filepath ) :
return self . client . get (
2010-11-17 15:36:26 +00:00
posixpath . join ( settings . STATIC_URL , filepath ) )
2010-10-20 01:33:24 +00:00
def assertFileContains ( self , filepath , text ) :
self . assertContains ( self . _response ( filepath ) , text )
def assertFileNotFound ( self , filepath ) :
2011-03-03 15:04:39 +00:00
self . assertEqual ( self . _response ( filepath ) . status_code , 404 )
2010-10-20 01:33:24 +00:00
2010-10-21 03:16:41 +00:00
class TestServeDisabled ( TestServeStatic ) :
"""
2011-02-01 20:39:40 +00:00
Test serving static files disabled when DEBUG is False .
2010-10-21 03:16:41 +00:00
"""
def setUp ( self ) :
super ( TestServeDisabled , self ) . setUp ( )
settings . DEBUG = False
def test_disabled_serving ( self ) :
2012-09-07 17:17:09 +00:00
six . assertRaisesRegex ( self , ImproperlyConfigured , ' The staticfiles view '
2011-02-14 01:42:26 +00:00
' can only be used in debug mode ' , self . _response , ' test.txt ' )
2010-10-21 03:16:41 +00:00
2010-10-20 01:33:24 +00:00
class TestServeStaticWithDefaultURL ( TestServeStatic , TestDefaults ) :
"""
2011-02-01 20:39:40 +00:00
Test static asset serving view with manually configured URLconf .
2010-10-20 01:33:24 +00:00
"""
2010-10-21 03:16:41 +00:00
pass
2010-10-20 01:33:24 +00:00
2011-08-11 14:07:39 +00:00
2010-10-20 01:33:24 +00:00
class TestServeStaticWithURLHelper ( TestServeStatic , TestDefaults ) :
"""
Test static asset serving view with staticfiles_urlpatterns helper .
"""
2010-12-31 14:22:32 +00:00
urls = ' regressiontests.staticfiles_tests.urls.helper '
2010-10-20 01:33:24 +00:00
class TestServeAdminMedia ( TestServeStatic ) :
"""
Test serving media from django . contrib . admin .
"""
def _response ( self , filepath ) :
return self . client . get (
2011-06-30 09:06:19 +00:00
posixpath . join ( settings . STATIC_URL , ' admin/ ' , filepath ) )
2010-10-20 01:33:24 +00:00
def test_serve_admin_media ( self ) :
self . assertFileContains ( ' css/base.css ' , ' body ' )
class FinderTestCase ( object ) :
"""
2011-05-27 10:55:07 +00:00
Base finder test mixin .
On Windows , sometimes the case of the path we ask the finders for and the
path ( s ) they find can differ . Compare them using os . path . normcase ( ) to
avoid false negatives .
2010-10-20 01:33:24 +00:00
"""
def test_find_first ( self ) :
src , dst = self . find_first
2011-05-27 10:55:07 +00:00
found = self . finder . find ( src )
self . assertEqual ( os . path . normcase ( found ) , os . path . normcase ( dst ) )
2010-10-20 01:33:24 +00:00
def test_find_all ( self ) :
src , dst = self . find_all
2011-05-27 10:55:07 +00:00
found = self . finder . find ( src , all = True )
found = [ os . path . normcase ( f ) for f in found ]
dst = [ os . path . normcase ( d ) for d in dst ]
self . assertEqual ( found , dst )
2010-10-20 01:33:24 +00:00
class TestFileSystemFinder ( StaticFilesTestCase , FinderTestCase ) :
"""
Test FileSystemFinder .
"""
def setUp ( self ) :
super ( TestFileSystemFinder , self ) . setUp ( )
self . finder = finders . FileSystemFinder ( )
2010-12-31 14:22:32 +00:00
test_file_path = os . path . join ( TEST_ROOT , ' project ' , ' documents ' , ' test ' , ' file.txt ' )
self . find_first = ( os . path . join ( ' test ' , ' file.txt ' ) , test_file_path )
self . find_all = ( os . path . join ( ' test ' , ' file.txt ' ) , [ test_file_path ] )
2010-10-20 01:33:24 +00:00
class TestAppDirectoriesFinder ( StaticFilesTestCase , FinderTestCase ) :
"""
Test AppDirectoriesFinder .
"""
def setUp ( self ) :
super ( TestAppDirectoriesFinder , self ) . setUp ( )
self . finder = finders . AppDirectoriesFinder ( )
2010-12-31 14:22:32 +00:00
test_file_path = os . path . join ( TEST_ROOT , ' apps ' , ' test ' , ' static ' , ' test ' , ' file1.txt ' )
self . find_first = ( os . path . join ( ' test ' , ' file1.txt ' ) , test_file_path )
self . find_all = ( os . path . join ( ' test ' , ' file1.txt ' ) , [ test_file_path ] )
2010-10-20 01:33:24 +00:00
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 ' )
2010-12-31 14:22:32 +00:00
self . find_first = ( ' media-file.txt ' , test_file_path )
self . find_all = ( ' media-file.txt ' , [ test_file_path ] )
2010-10-20 01:33:24 +00:00
class TestMiscFinder ( TestCase ) :
"""
A few misc finder tests .
"""
def test_get_finder ( self ) :
2011-11-03 10:51:02 +00:00
self . assertIsInstance ( finders . get_finder (
2010-12-31 14:22:32 +00:00
' django.contrib.staticfiles.finders.FileSystemFinder ' ) ,
2011-11-03 10:51:02 +00:00
finders . FileSystemFinder )
2011-02-01 14:57:10 +00:00
def test_get_finder_bad_classname ( self ) :
2011-08-11 14:07:39 +00:00
self . assertRaises ( ImproperlyConfigured , finders . get_finder ,
' django.contrib.staticfiles.finders.FooBarFinder ' )
2011-02-01 14:57:10 +00:00
def test_get_finder_bad_module ( self ) :
2010-10-20 01:33:24 +00:00
self . assertRaises ( ImproperlyConfigured ,
2010-12-31 14:22:32 +00:00
finders . get_finder , ' foo.bar.FooBarFinder ' )
2011-02-01 14:57:10 +00:00
2011-09-21 15:58:32 +00:00
@override_settings ( STATICFILES_DIRS = ' a string ' )
2011-02-01 14:57:10 +00:00
def test_non_tuple_raises_exception ( self ) :
2011-08-11 14:07:39 +00:00
"""
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.
"""
2011-09-21 15:58:32 +00:00
self . assertRaises ( ImproperlyConfigured , finders . FileSystemFinder )
@override_settings ( MEDIA_ROOT = ' ' )
def test_location_empty ( self ) :
self . assertRaises ( ImproperlyConfigured , finders . DefaultStorageFinder )
2011-08-11 14:07:39 +00:00
class TestTemplateTag ( StaticFilesTestCase ) :
2011-06-30 09:06:19 +00:00
2011-08-11 14:07:39 +00:00
def test_template_tag ( self ) :
2011-11-03 10:51:02 +00:00
self . assertStaticRenders ( " does/not/exist.png " ,
" /static/does/not/exist.png " )
self . assertStaticRenders ( " testfile.txt " , " /static/testfile.txt " )