Skip to content

Commit

Permalink
Fix broken tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcass77 committed Aug 13, 2019
1 parent 09bc3f5 commit 4b03611
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
36 changes: 18 additions & 18 deletions django_tenants/files/storage.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
import urllib.parse

from django.conf import settings
from django.utils.functional import cached_property
from django.db import connection

from django.core.files.storage import FileSystemStorage

Expand Down Expand Up @@ -30,43 +30,43 @@ def relative_media_root(self):

@cached_property
def relative_media_url(self):
url = settings.MEDIA_URL

if not url.endswith('/'):
url += '/'

try:
multitenant_relative_url = settings.MULTITENANT_RELATIVE_MEDIA_ROOT
except AttributeError:
# MULTITENANT_RELATIVE_MEDIA_ROOT is an optional setting. Use the default of just appending
# the tenant schema_name to STATIC_ROOT if no configuration value is provided
multitenant_relative_url = "%s"

url = "{}{}".format(url, multitenant_relative_url)
return url
multitenant_relative_url = urllib.parse.urljoin(settings.MEDIA_URL, multitenant_relative_url)

if not multitenant_relative_url.endswith('/'):
multitenant_relative_url += '/'

return multitenant_relative_url

@property # Not cached like in parent class
def base_location(self):
return self._value_or_setting(
self._location,
utils.parse_tenant_config_path(self.relative_media_root)
)
relative_tenant_media_root = utils.parse_tenant_config_path(self.relative_media_root)

if self._location is None:
return relative_tenant_media_root

return os.path.join(self._location, relative_tenant_media_root)

@property # Not cached like in parent class
def location(self):
return os.path.abspath(self.base_location)

@property
def base_url(self):
if self._base_url is not None and not self._base_url.endswith('/'):
self._base_url += '/'
relative_tenant_media_url = utils.parse_tenant_config_path(self.relative_media_url)

relative_tenant_url = self.relative_media_url % connection.schema_name
if self._base_url is None:
return relative_tenant_media_url

if not relative_tenant_url.endswith('/'):
relative_tenant_url += '/'
relative_tenant_media_url = urllib.parse.urljoin(self._base_url, relative_tenant_media_url)

return self._value_or_setting(self._base_url, relative_tenant_url)
return relative_tenant_media_url

def listdir(self, path):
"""
Expand Down
18 changes: 9 additions & 9 deletions django_tenants/tests/files/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,39 +90,39 @@ def test_file_url(self):
# like encodeURIComponent() JavaScript function do
self.assertEqual(
self.storage.url(r"~!*()'@#$%^&*abc`+ =.file"),
f"/test_media_url/{connection.schema_name}/~!*()'%40%23%24%25%5E%26*abc%60%2B%20%3D.file",
"/test_media_url/{}/~!*()'%40%23%24%25%5E%26*abc%60%2B%20%3D.file".format(connection.schema_name),
)
self.assertEqual(
self.storage.url("ab\0c"),
f"/test_media_url/{connection.schema_name}/ab%00c",
"/test_media_url/{}/ab%00c".format(connection.schema_name),
)

# should translate os path separator(s) to the url path separator
self.assertEqual(
self.storage.url("""a/b\\c.file"""),
f"/test_media_url/{connection.schema_name}/a/b/c.file",
"/test_media_url/{}/a/b/c.file".format(connection.schema_name),
)

# remove leading slashes from file names to prevent unsafe url output
self.assertEqual(
self.storage.url("/evil.com"),
f"/test_media_url/{connection.schema_name}/evil.com",
"/test_media_url/{}/evil.com".format(connection.schema_name),
)
self.assertEqual(
self.storage.url(r"\evil.com"),
f"/test_media_url/{connection.schema_name}/evil.com",
"/test_media_url/{}/evil.com".format(connection.schema_name),
)
self.assertEqual(
self.storage.url("///evil.com"),
f"/test_media_url/{connection.schema_name}/evil.com",
"/test_media_url/{}/evil.com".format(connection.schema_name),
)
self.assertEqual(
self.storage.url(r"\\\evil.com"),
f"/test_media_url/{connection.schema_name}/evil.com",
"/test_media_url/{}/evil.com".format(connection.schema_name),
)

self.assertEqual(
self.storage.url(None), f"/test_media_url/{connection.schema_name}/"
self.storage.url(None), "/test_media_url/{}/".format(connection.schema_name)
)

def test_base_url(self):
Expand All @@ -132,7 +132,7 @@ def test_base_url(self):
self.storage._base_url = None
# with self.assertRaises(ValueError):
self.assertEqual(
self.storage.url("test.file"), f"{connection.schema_name}/test.file"
self.storage.url("test.file"), "{}/test.file".format(connection.schema_name)
)

# missing ending slash in base_url should be auto-corrected
Expand Down

0 comments on commit 4b03611

Please sign in to comment.