Code

Ticket #15641: 15641.diff

File 15641.diff, 2.7 KB (added by Harm Geerts <hgeerts@…>, 3 years ago)
Line 
1Index: tests/regressiontests/file_storage/tests.py
2===================================================================
3--- tests/regressiontests/file_storage/tests.py (revision 15876)
4+++ tests/regressiontests/file_storage/tests.py (working copy)
5@@ -88,12 +88,13 @@
6 
7 class FileStorageTests(unittest.TestCase):
8     storage_class = FileSystemStorage
9+    base_url = '/test_media_url/'
10 
11     def setUp(self):
12         self.temp_dir = tempfile.mktemp()
13         os.makedirs(self.temp_dir)
14         self.storage = self.storage_class(location=self.temp_dir,
15-            base_url='/test_media_url/')
16+            base_url=self.base_url)
17 
18     def tearDown(self):
19         shutil.rmtree(self.temp_dir)
20@@ -207,11 +208,11 @@
21         # should encode special chars except ~!*()'
22         # like encodeURIComponent() JavaScript function do
23         self.assertEqual(self.storage.url(r"""~!*()'@#$%^&*abc`+=.file"""),
24-            """/test_media_url/~!*()'%40%23%24%25%5E%26*abc%60%2B%3D.file""")
25+            self.base_url + """~!*()'%40%23%24%25%5E%26*abc%60%2B%3D.file""")
26 
27         # should stanslate os path separator(s) to the url path separator
28         self.assertEqual(self.storage.url("""a/b\\c.file"""),
29-            """/test_media_url/a/b/c.file""")
30+            self.base_url + """a/b/c.file""")
31 
32         self.storage.base_url = None
33         self.assertRaises(ValueError, self.storage.url, 'test.file')
34@@ -265,6 +266,16 @@
35         self.assertRaises(SuspiciousOperation, self.storage.exists, '..')
36         self.assertRaises(SuspiciousOperation, self.storage.exists, '/etc/passwd')
37 
38+class NoUrlFileStorageTests(FileStorageTests):
39+    base_url = None
40+
41+    def test_file_url(self):
42+        """
43+        File storage raises ValueError if initialized with base_url=None
44+        """
45+        self.assertRaises(ValueError, self.storage.url, 'test.file')
46+
47+
48 class CustomStorage(FileSystemStorage):
49     def get_available_name(self, name):
50         """
51Index: django/core/files/storage.py
52===================================================================
53--- django/core/files/storage.py        (revision 15876)
54+++ django/core/files/storage.py        (working copy)
55@@ -142,15 +142,17 @@
56         """
57         raise NotImplementedError()
58 
59+UNUSABLE_BASE_URL = object()
60+
61 class FileSystemStorage(Storage):
62     """
63     Standard filesystem storage
64     """
65 
66-    def __init__(self, location=None, base_url=None):
67+    def __init__(self, location=None, base_url=UNUSABLE_BASE_URL):
68         if location is None:
69             location = settings.MEDIA_ROOT
70-        if base_url is None:
71+        if base_url is UNUSABLE_BASE_URL:
72             base_url = settings.MEDIA_URL
73         self.location = os.path.abspath(location)
74         self.base_url = base_url