summaryrefslogtreecommitdiff
path: root/tests/utils_tests
diff options
context:
space:
mode:
authorTheo Alexiou <theofilosalexiou@gmail.com>2022-02-18 20:27:05 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-02-21 10:11:26 +0100
commit659d2421c7adbbcd205604002d521d82d6b0b465 (patch)
tree05e8e6553c5463e3d01a1e62bd150ff139e1f68f /tests/utils_tests
parentb626c5a9798b045b655d085d59efdd60b5d7a0e3 (diff)
downloaddjango-659d2421c7adbbcd205604002d521d82d6b0b465.tar.gz
Fixed #20296 -- Prevented mark_safe() from evaluating lazy objects.
Diffstat (limited to 'tests/utils_tests')
-rw-r--r--tests/utils_tests/test_safestring.py18
1 files changed, 13 insertions, 5 deletions
diff --git a/tests/utils_tests/test_safestring.py b/tests/utils_tests/test_safestring.py
index d7805662f1..1a79afbf48 100644
--- a/tests/utils_tests/test_safestring.py
+++ b/tests/utils_tests/test_safestring.py
@@ -1,8 +1,9 @@
from django.template import Context, Template
from django.test import SimpleTestCase
-from django.utils import html
-from django.utils.functional import lazy, lazystr
+from django.utils import html, translation
+from django.utils.functional import Promise, lazy, lazystr
from django.utils.safestring import SafeData, SafeString, mark_safe
+from django.utils.translation import gettext_lazy
class customescape(str):
@@ -40,10 +41,17 @@ class SafeStringTest(SimpleTestCase):
self.assertRenderEqual("{{ s|force_escape }}", "&lt;a&amp;b&gt;", s=s)
def test_mark_safe_lazy(self):
- s = lazystr("a&b")
+ safe_s = mark_safe(lazystr("a&b"))
- self.assertIsInstance(mark_safe(s), SafeData)
- self.assertRenderEqual("{{ s }}", "a&b", s=mark_safe(s))
+ self.assertIsInstance(safe_s, Promise)
+ self.assertRenderEqual("{{ s }}", "a&b", s=safe_s)
+ self.assertIsInstance(str(safe_s), SafeData)
+
+ def test_mark_safe_lazy_i18n(self):
+ s = mark_safe(gettext_lazy("name"))
+ tpl = Template("{{ s }}")
+ with translation.override("fr"):
+ self.assertEqual(tpl.render(Context({"s": s})), "nom")
def test_mark_safe_object_implementing_dunder_str(self):
class Obj: