2017-01-07 11:11:46 +00:00
|
|
|
from urllib.parse import urljoin
|
|
|
|
|
2014-11-12 01:32:44 +00:00
|
|
|
from django.conf import settings
|
2018-03-25 13:02:07 +00:00
|
|
|
from django.template import TemplateSyntaxError
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2014-11-12 01:32:44 +00:00
|
|
|
|
2014-12-07 08:43:10 +00:00
|
|
|
from ..utils import setup
|
2014-11-12 01:32:44 +00:00
|
|
|
|
|
|
|
|
2021-02-05 17:41:49 +00:00
|
|
|
@override_settings(INSTALLED_APPS=[], MEDIA_URL='media/', STATIC_URL='static/')
|
2014-12-03 20:36:17 +00:00
|
|
|
class StaticTagTests(SimpleTestCase):
|
2015-05-08 20:10:36 +00:00
|
|
|
libraries = {'static': 'django.templatetags.static'}
|
2014-11-12 01:32:44 +00:00
|
|
|
|
|
|
|
@setup({'static-prefixtag01': '{% load static %}{% get_static_prefix %}'})
|
|
|
|
def test_static_prefixtag01(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('static-prefixtag01')
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, settings.STATIC_URL)
|
|
|
|
|
|
|
|
@setup({'static-prefixtag02': '{% load static %}'
|
|
|
|
'{% get_static_prefix as static_prefix %}{{ static_prefix }}'})
|
|
|
|
def test_static_prefixtag02(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('static-prefixtag02')
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, settings.STATIC_URL)
|
|
|
|
|
|
|
|
@setup({'static-prefixtag03': '{% load static %}{% get_media_prefix %}'})
|
|
|
|
def test_static_prefixtag03(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('static-prefixtag03')
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, settings.MEDIA_URL)
|
|
|
|
|
|
|
|
@setup({'static-prefixtag04': '{% load static %}'
|
|
|
|
'{% get_media_prefix as media_prefix %}{{ media_prefix }}'})
|
|
|
|
def test_static_prefixtag04(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('static-prefixtag04')
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, settings.MEDIA_URL)
|
|
|
|
|
2018-03-25 13:02:07 +00:00
|
|
|
@setup({'t': '{% load static %}{% get_media_prefix ad media_prefix %}{{ media_prefix }}'})
|
|
|
|
def test_static_prefixtag_without_as(self):
|
|
|
|
msg = "First argument in 'get_media_prefix' must be 'as'"
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
|
|
|
self.engine.render_to_string('t')
|
|
|
|
|
2014-11-12 01:32:44 +00:00
|
|
|
@setup({'static-statictag01': '{% load static %}{% static "admin/base.css" %}'})
|
|
|
|
def test_static_statictag01(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('static-statictag01')
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, urljoin(settings.STATIC_URL, 'admin/base.css'))
|
|
|
|
|
|
|
|
@setup({'static-statictag02': '{% load static %}{% static base_css %}'})
|
|
|
|
def test_static_statictag02(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('static-statictag02', {'base_css': 'admin/base.css'})
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, urljoin(settings.STATIC_URL, 'admin/base.css'))
|
|
|
|
|
|
|
|
@setup({'static-statictag03': '{% load static %}{% static "admin/base.css" as foo %}{{ foo }}'})
|
|
|
|
def test_static_statictag03(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('static-statictag03')
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, urljoin(settings.STATIC_URL, 'admin/base.css'))
|
|
|
|
|
|
|
|
@setup({'static-statictag04': '{% load static %}{% static base_css as foo %}{{ foo }}'})
|
|
|
|
def test_static_statictag04(self):
|
2014-12-07 08:43:10 +00:00
|
|
|
output = self.engine.render_to_string('static-statictag04', {'base_css': 'admin/base.css'})
|
2014-11-12 01:32:44 +00:00
|
|
|
self.assertEqual(output, urljoin(settings.STATIC_URL, 'admin/base.css'))
|
2016-12-17 15:03:44 +00:00
|
|
|
|
2016-12-19 18:44:02 +00:00
|
|
|
@setup({'static-statictag05': '{% load static %}{% static "special?chars"ed.html" %}'})
|
|
|
|
def test_static_quotes_urls(self):
|
2016-12-17 15:03:44 +00:00
|
|
|
output = self.engine.render_to_string('static-statictag05')
|
2016-12-19 18:44:02 +00:00
|
|
|
self.assertEqual(output, urljoin(settings.STATIC_URL, '/static/special%3Fchars%26quoted.html'))
|
2018-03-25 13:02:07 +00:00
|
|
|
|
|
|
|
@setup({'t': '{% load static %}{% static %}'})
|
|
|
|
def test_static_statictag_without_path(self):
|
|
|
|
msg = "'static' takes at least one argument (path to file)"
|
|
|
|
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
|
|
|
self.engine.render_to_string('t')
|