test_forms.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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" '
  28. 'rel="stylesheet">\n'
  29. '<link href="/path/to/css2" media="all" rel="stylesheet">\n'
  30. '<script src="/path/to/js1"></script>\n'
  31. '<script src="http://media.other.com/path/to/js2"></script>\n'
  32. '<script src="https://secure.other.com/path/to/js3"></script>\n'
  33. '<script src="https://example.com/assets/relative/path/to/js4"></script>',
  34. )