Ticket #15641: 15641-2.diff

File 15641-2.diff, 3.7 KB (added by Harm Geerts <hgeerts@…>, 14 years ago)

same patch with additional tests for MEDIA_URL as default

  • tests/regressiontests/file_storage/tests.py

     
    1919from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured
    2020from django.core.files.base import ContentFile, File
    2121from django.core.files.images import get_image_dimensions
    22 from django.core.files.storage import FileSystemStorage, get_storage_class
     22from django.core.files.storage import FileSystemStorage, get_storage_class, UNUSABLE_BASE_URL
    2323from django.core.files.uploadedfile import UploadedFile
    2424from django.utils import unittest
    2525
     
    8888
    8989class FileStorageTests(unittest.TestCase):
    9090    storage_class = FileSystemStorage
     91    init_base_url = '/test_media_url/'
     92    test_base_url = None
    9193
    9294    def setUp(self):
    9395        self.temp_dir = tempfile.mktemp()
    9496        os.makedirs(self.temp_dir)
    9597        self.storage = self.storage_class(location=self.temp_dir,
    96             base_url='/test_media_url/')
     98            base_url=self.init_base_url)
     99        if self.test_base_url is None:
     100            self.test_base_url = self.init_base_url
    97101
    98102    def tearDown(self):
    99103        shutil.rmtree(self.temp_dir)
     
    207211        # should encode special chars except ~!*()'
    208212        # like encodeURIComponent() JavaScript function do
    209213        self.assertEqual(self.storage.url(r"""~!*()'@#$%^&*abc`+=.file"""),
    210             """/test_media_url/~!*()'%40%23%24%25%5E%26*abc%60%2B%3D.file""")
     214            self.test_base_url + """~!*()'%40%23%24%25%5E%26*abc%60%2B%3D.file""")
    211215
    212216        # should stanslate os path separator(s) to the url path separator
    213217        self.assertEqual(self.storage.url("""a/b\\c.file"""),
    214             """/test_media_url/a/b/c.file""")
     218            self.test_base_url + """a/b/c.file""")
    215219
    216220        self.storage.base_url = None
    217221        self.assertRaises(ValueError, self.storage.url, 'test.file')
     
    265269        self.assertRaises(SuspiciousOperation, self.storage.exists, '..')
    266270        self.assertRaises(SuspiciousOperation, self.storage.exists, '/etc/passwd')
    267271
     272class NoUrlFileStorageTests(FileStorageTests):
     273    init_base_url = None
     274
     275    def test_file_url(self):
     276        """
     277        File storage raises ValueError if initialized with base_url=None
     278        """
     279        self.assertRaises(ValueError, self.storage.url, 'test.file')
     280
     281class DefaultUrlFileStorageTests(FileStorageTests):
     282    init_base_url = UNUSABLE_BASE_URL
     283    test_base_url = '/test_media_url/'
     284
     285    def setUp(self):
     286        self.old_media_url = settings.MEDIA_URL
     287        settings.MEDIA_URL = self.test_base_url
     288        super(DefaultUrlFileStorageTests, self).setUp()
     289
     290    def tearDown(self):
     291        super(DefaultUrlFileStorageTests, self).tearDown()
     292        settings.MEDIA_URL = self.old_media_url
     293
    268294class CustomStorage(FileSystemStorage):
    269295    def get_available_name(self, name):
    270296        """
  • django/core/files/storage.py

     
    142142        """
    143143        raise NotImplementedError()
    144144
     145UNUSABLE_BASE_URL = object()
     146
    145147class FileSystemStorage(Storage):
    146148    """
    147149    Standard filesystem storage
    148150    """
    149151
    150     def __init__(self, location=None, base_url=None):
     152    def __init__(self, location=None, base_url=UNUSABLE_BASE_URL):
    151153        if location is None:
    152154            location = settings.MEDIA_ROOT
    153         if base_url is None:
     155        if base_url is UNUSABLE_BASE_URL:
    154156            base_url = settings.MEDIA_URL
    155157        self.location = os.path.abspath(location)
    156158        self.base_url = base_url
Back to Top