test_forms.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from urllib.parse import urljoin
  2. from django.contrib.staticfiles import storage
  3. from django.forms import Media
  4. from django.templatetags.static import static
  5. from django.test import SimpleTestCase, override_settings
  6. class StaticTestStorage(storage.StaticFilesStorage):
  7. def url(self, name):
  8. return urljoin('https://example.com/assets/', name)
  9. @override_settings(
  10. STATIC_URL='http://media.example.com/static/',
  11. INSTALLED_APPS=('django.contrib.staticfiles',),
  12. STATICFILES_STORAGE='staticfiles_tests.test_forms.StaticTestStorage',
  13. )
  14. class StaticFilesFormsMediaTestCase(SimpleTestCase):
  15. def test_absolute_url(self):
  16. m = Media(
  17. css={'all': ('path/to/css1', '/path/to/css2')},
  18. js=(
  19. '/path/to/js1',
  20. 'http://media.other.com/path/to/js2',
  21. 'https://secure.other.com/path/to/js3',
  22. static('relative/path/to/js4'),
  23. ),
  24. )
  25. self.assertEqual(
  26. str(m),
  27. """<link href="https://example.com/assets/path/to/css1" media="all" rel="stylesheet">
  28. <link href="/path/to/css2" media="all" rel="stylesheet">
  29. <script src="/path/to/js1"></script>
  30. <script src="http://media.other.com/path/to/js2"></script>
  31. <script src="https://secure.other.com/path/to/js3"></script>
  32. <script src="https://example.com/assets/relative/path/to/js4"></script>"""
  33. )