summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorVajrasky Kok <sky.kok@speaklikeaking.com>2013-10-19 20:40:12 +0800
committerTim Graham <timograham@gmail.com>2013-10-24 17:40:01 -0400
commit9eecb9169566db263e243e4522b08ea1403ee95f (patch)
tree0da80e779e7702ff2ba05f9c9d6f0ff5b15edb03 /tests
parentc052699be3637c22e3a26383a4bdabc8c3cc0feb (diff)
Fixed #21219 -- Added a way to set different permission for static files.
Previously, when collecting static files, the files would receive permission from FILE_UPLOAD_PERMISSIONS. Now, there's an option to give different permission from uploaded files permission by subclassing any of the static files storage classes and setting the file_permissions_mode parameter. Thanks dblack at atlassian.com for the suggestion.
Diffstat (limited to 'tests')
-rw-r--r--tests/file_storage/tests.py5
-rw-r--r--tests/staticfiles_tests/tests.py44
2 files changed, 48 insertions, 1 deletions
diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py
index fa791c405b..00920a8618 100644
--- a/tests/file_storage/tests.py
+++ b/tests/file_storage/tests.py
@@ -442,7 +442,6 @@ class FileStoragePermissions(unittest.TestCase):
self.umask = 0o027
self.old_umask = os.umask(self.umask)
self.storage_dir = tempfile.mkdtemp()
- self.storage = FileSystemStorage(self.storage_dir)
def tearDown(self):
shutil.rmtree(self.storage_dir)
@@ -450,24 +449,28 @@ class FileStoragePermissions(unittest.TestCase):
@override_settings(FILE_UPLOAD_PERMISSIONS=0o654)
def test_file_upload_permissions(self):
+ self.storage = FileSystemStorage(self.storage_dir)
name = self.storage.save("the_file", ContentFile("data"))
actual_mode = os.stat(self.storage.path(name))[0] & 0o777
self.assertEqual(actual_mode, 0o654)
@override_settings(FILE_UPLOAD_PERMISSIONS=None)
def test_file_upload_default_permissions(self):
+ self.storage = FileSystemStorage(self.storage_dir)
fname = self.storage.save("some_file", ContentFile("data"))
mode = os.stat(self.storage.path(fname))[0] & 0o777
self.assertEqual(mode, 0o666 & ~self.umask)
@override_settings(FILE_UPLOAD_DIRECTORY_PERMISSIONS=0o765)
def test_file_upload_directory_permissions(self):
+ self.storage = FileSystemStorage(self.storage_dir)
name = self.storage.save("the_directory/the_file", ContentFile("data"))
dir_mode = os.stat(os.path.dirname(self.storage.path(name)))[0] & 0o777
self.assertEqual(dir_mode, 0o765)
@override_settings(FILE_UPLOAD_DIRECTORY_PERMISSIONS=None)
def test_file_upload_directory_default_permissions(self):
+ self.storage = FileSystemStorage(self.storage_dir)
name = self.storage.save("the_directory/the_file", ContentFile("data"))
dir_mode = os.stat(os.path.dirname(self.storage.path(name)))[0] & 0o777
self.assertEqual(dir_mode, 0o777 & ~self.umask)
diff --git a/tests/staticfiles_tests/tests.py b/tests/staticfiles_tests/tests.py
index 254e48d048..463ab47030 100644
--- a/tests/staticfiles_tests/tests.py
+++ b/tests/staticfiles_tests/tests.py
@@ -7,6 +7,7 @@ import posixpath
import shutil
import sys
import tempfile
+import unittest
from django.template import loader, Context
from django.conf import settings
@@ -21,6 +22,7 @@ from django.utils._os import rmtree_errorhandler, upath
from django.utils import six
from django.contrib.staticfiles import finders, storage
+from django.contrib.staticfiles.management.commands import collectstatic
TEST_ROOT = os.path.dirname(upath(__file__))
TEST_SETTINGS = {
@@ -804,3 +806,45 @@ class TestAppStaticStorage(TestCase):
st.path('bar')
finally:
sys.getfilesystemencoding = old_enc_func
+
+
+class CustomStaticFilesStorage(storage.StaticFilesStorage):
+ """
+ Used in TestStaticFilePermissions
+ """
+ def __init__(self, *args, **kwargs):
+ kwargs['file_permissions_mode'] = 0o640
+ super(CustomStaticFilesStorage, self).__init__(*args, **kwargs)
+
+
+@unittest.skipIf(sys.platform.startswith('win'),
+ "Windows only partially supports chmod.")
+class TestStaticFilePermissions(BaseCollectionTestCase, StaticFilesTestCase):
+
+ command_params = {'interactive': False,
+ 'post_process': True,
+ 'verbosity': '0',
+ 'ignore_patterns': ['*.ignoreme'],
+ 'use_default_ignore_patterns': True,
+ 'clear': False,
+ 'link': False,
+ 'dry_run': False}
+
+ # Don't run collectstatic command in this test class.
+ def run_collectstatic(self, **kwargs):
+ pass
+
+ @override_settings(FILE_UPLOAD_PERMISSIONS=0o655)
+ def test_collect_static_files_default_permissions(self):
+ collectstatic.Command().execute(**self.command_params)
+ test_file = os.path.join(settings.STATIC_ROOT, "test.txt")
+ file_mode = os.stat(test_file)[0] & 0o777
+ self.assertEqual(file_mode, 0o655)
+
+ @override_settings(FILE_UPLOAD_PERMISSIONS=0o655,
+ STATICFILES_STORAGE='staticfiles_tests.tests.CustomStaticFilesStorage')
+ def test_collect_static_files_subclass_of_static_storage(self):
+ collectstatic.Command().execute(**self.command_params)
+ test_file = os.path.join(settings.STATIC_ROOT, "test.txt")
+ file_mode = os.stat(test_file)[0] & 0o777
+ self.assertEqual(file_mode, 0o640)