11"""isort:skip_file."""
22
33import sys
4+ import html
45
56import pytest
67
@@ -66,14 +67,24 @@ def test_pre_save_none_values(self):
6667 bleached_value = field .pre_save (model , False )
6768 self ._validate (value , bleached_value )
6869
70+ def test_pre_save_do_not_escape (self ):
71+ """Tests whether the :see:pre_save function works properly when field
72+ escape argument is set to False."""
73+
74+ value = self ._get_test_value ()
75+ model , field = self ._get_test_model (value , escape = False )
76+
77+ bleached_value = field .pre_save (model , False )
78+ self ._validate (value , bleached_value , False )
79+
6980 @staticmethod
70- def _get_test_model (value ):
71- """Gets a test model and a artifically constructed
81+ def _get_test_model (value , escape = True ):
82+ """Gets a test model and an artificially constructed
7283 :see:LocalizedBleachField instance to test with."""
7384
7485 model = ModelTest (value )
7586
76- field = LocalizedBleachField ()
87+ field = LocalizedBleachField (escape = escape )
7788 field .attname = "value"
7889 return model , field
7990
@@ -89,7 +100,7 @@ def _get_test_value():
89100 return value
90101
91102 @staticmethod
92- def _validate (non_bleached_value , bleached_value ):
103+ def _validate (non_bleached_value , bleached_value , escaped_value = True ):
93104 """Validates whether the specified non-bleached value ended up being
94105 correctly bleached.
95106
@@ -100,14 +111,20 @@ def _validate(non_bleached_value, bleached_value):
100111 bleached_value:
101112 The value after bleaching.
102113 """
103-
104114 for lang_code , _ in settings .LANGUAGES :
105115 if not non_bleached_value .get (lang_code ):
106116 assert not bleached_value .get (lang_code )
107117 continue
108118
109- expected_value = bleach .clean (
110- non_bleached_value .get (lang_code ), get_bleach_default_options ()
119+ cleaned_value = bleach .clean (
120+ non_bleached_value .get (lang_code )
121+ if escaped_value
122+ else html .unescape (non_bleached_value .get (lang_code )),
123+ get_bleach_default_options (),
124+ )
125+
126+ expected_value = (
127+ cleaned_value if escaped_value else html .unescape (cleaned_value )
111128 )
112129
113130 assert bleached_value .get (lang_code ) == expected_value
0 commit comments