from urllib.parse import urljoin
from django.conf import STATICFILES_STORAGE_ALIAS
from django.contrib.staticfiles import storage
from django.forms import Media
from django.templatetags.static import static
from django.test import SimpleTestCase, override_settings
class StaticTestStorage(storage.StaticFilesStorage):
def url(self, name):
return urljoin("https://example.com/assets/", name)
@override_settings(
INSTALLED_APPS=("django.contrib.staticfiles",),
STORAGES={
STATICFILES_STORAGE_ALIAS: {
"BACKEND": "staticfiles_tests.test_forms.StaticTestStorage",
"OPTIONS": {"location": "http://media.example.com/static/"},
}
},
)
class StaticFilesFormsMediaTestCase(SimpleTestCase):
def test_absolute_url(self):
m = Media(
css={"all": ("path/to/css1", "/path/to/css2")},
js=(
"/path/to/js1",
"http://media.other.com/path/to/js2",
"https://secure.other.com/path/to/js3",
static("relative/path/to/js4"),
),
)
self.assertEqual(
str(m),
'\n'
'\n'
'\n'
'\n'
'\n'
'',
)