1. from urllib.parse import urljoin
    
  2. 
    
  3. from django.conf import settings
    
  4. from django.template import TemplateSyntaxError
    
  5. from django.templatetags.static import StaticNode
    
  6. from django.test import SimpleTestCase, override_settings
    
  7. 
    
  8. from ..utils import setup
    
  9. 
    
  10. 
    
  11. @override_settings(INSTALLED_APPS=[], MEDIA_URL="media/", STATIC_URL="static/")
    
  12. class StaticTagTests(SimpleTestCase):
    
  13.     libraries = {"static": "django.templatetags.static"}
    
  14. 
    
  15.     @setup({"static-prefixtag01": "{% load static %}{% get_static_prefix %}"})
    
  16.     def test_static_prefixtag01(self):
    
  17.         output = self.engine.render_to_string("static-prefixtag01")
    
  18.         self.assertEqual(output, settings.STATIC_URL)
    
  19. 
    
  20.     @setup(
    
  21.         {
    
  22.             "static-prefixtag02": "{% load static %}"
    
  23.             "{% get_static_prefix as static_prefix %}{{ static_prefix }}"
    
  24.         }
    
  25.     )
    
  26.     def test_static_prefixtag02(self):
    
  27.         output = self.engine.render_to_string("static-prefixtag02")
    
  28.         self.assertEqual(output, settings.STATIC_URL)
    
  29. 
    
  30.     @setup({"static-prefixtag03": "{% load static %}{% get_media_prefix %}"})
    
  31.     def test_static_prefixtag03(self):
    
  32.         output = self.engine.render_to_string("static-prefixtag03")
    
  33.         self.assertEqual(output, settings.MEDIA_URL)
    
  34. 
    
  35.     @setup(
    
  36.         {
    
  37.             "static-prefixtag04": "{% load static %}"
    
  38.             "{% get_media_prefix as media_prefix %}{{ media_prefix }}"
    
  39.         }
    
  40.     )
    
  41.     def test_static_prefixtag04(self):
    
  42.         output = self.engine.render_to_string("static-prefixtag04")
    
  43.         self.assertEqual(output, settings.MEDIA_URL)
    
  44. 
    
  45.     @setup(
    
  46.         {
    
  47.             "t": (
    
  48.                 "{% load static %}{% get_media_prefix ad media_prefix %}"
    
  49.                 "{{ media_prefix }}"
    
  50.             )
    
  51.         }
    
  52.     )
    
  53.     def test_static_prefixtag_without_as(self):
    
  54.         msg = "First argument in 'get_media_prefix' must be 'as'"
    
  55.         with self.assertRaisesMessage(TemplateSyntaxError, msg):
    
  56.             self.engine.render_to_string("t")
    
  57. 
    
  58.     @setup({"static-statictag01": '{% load static %}{% static "admin/base.css" %}'})
    
  59.     def test_static_statictag01(self):
    
  60.         output = self.engine.render_to_string("static-statictag01")
    
  61.         self.assertEqual(output, urljoin(settings.STATIC_URL, "admin/base.css"))
    
  62. 
    
  63.     @setup({"static-statictag02": "{% load static %}{% static base_css %}"})
    
  64.     def test_static_statictag02(self):
    
  65.         output = self.engine.render_to_string(
    
  66.             "static-statictag02", {"base_css": "admin/base.css"}
    
  67.         )
    
  68.         self.assertEqual(output, urljoin(settings.STATIC_URL, "admin/base.css"))
    
  69. 
    
  70.     @setup(
    
  71.         {
    
  72.             "static-statictag03": (
    
  73.                 '{% load static %}{% static "admin/base.css" as foo %}{{ foo }}'
    
  74.             )
    
  75.         }
    
  76.     )
    
  77.     def test_static_statictag03(self):
    
  78.         output = self.engine.render_to_string("static-statictag03")
    
  79.         self.assertEqual(output, urljoin(settings.STATIC_URL, "admin/base.css"))
    
  80. 
    
  81.     @setup(
    
  82.         {"static-statictag04": "{% load static %}{% static base_css as foo %}{{ foo }}"}
    
  83.     )
    
  84.     def test_static_statictag04(self):
    
  85.         output = self.engine.render_to_string(
    
  86.             "static-statictag04", {"base_css": "admin/base.css"}
    
  87.         )
    
  88.         self.assertEqual(output, urljoin(settings.STATIC_URL, "admin/base.css"))
    
  89. 
    
  90.     @setup(
    
  91.         {
    
  92.             "static-statictag05": (
    
  93.                 '{% load static %}{% static "special?chars&quoted.html" %}'
    
  94.             )
    
  95.         }
    
  96.     )
    
  97.     def test_static_quotes_urls(self):
    
  98.         output = self.engine.render_to_string("static-statictag05")
    
  99.         self.assertEqual(
    
  100.             output,
    
  101.             urljoin(settings.STATIC_URL, "/static/special%3Fchars%26quoted.html"),
    
  102.         )
    
  103. 
    
  104.     @setup({"t": "{% load static %}{% static %}"})
    
  105.     def test_static_statictag_without_path(self):
    
  106.         msg = "'static' takes at least one argument (path to file)"
    
  107.         with self.assertRaisesMessage(TemplateSyntaxError, msg):
    
  108.             self.engine.render_to_string("t")
    
  109. 
    
  110. 
    
  111. class StaticNodeTests(SimpleTestCase):
    
  112.     def test_repr(self):
    
  113.         static_node = StaticNode(varname="named-var", path="named-path")
    
  114.         self.assertEqual(
    
  115.             repr(static_node),
    
  116.             "StaticNode(varname='named-var', path='named-path')",
    
  117.         )
    
  118.         static_node = StaticNode(path="named-path")
    
  119.         self.assertEqual(
    
  120.             repr(static_node),
    
  121.             "StaticNode(varname=None, path='named-path')",
    
  122.         )