diff options
| author | Iacopo Spalletti <i.spalletti@nephila.it> | 2015-11-07 14:30:20 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-12-12 14:46:48 -0500 |
| commit | d693074d431c50e4801dd6bf52525ce1436358f0 (patch) | |
| tree | ad452646aad45bf9241478f3fc17803d05320cfc /tests | |
| parent | 93fc23b2d542105f5d129dff2dd2c8895e9abd5d (diff) | |
Fixed #20223 -- Added keep_lazy() as a replacement for allow_lazy().
Thanks to bmispelon and uruz for the initial patch.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/decorators/tests.py | 18 | ||||
| -rw-r--r-- | tests/httpwrappers/tests.py | 6 | ||||
| -rw-r--r-- | tests/template_tests/filter_tests/test_escape.py | 8 | ||||
| -rw-r--r-- | tests/template_tests/filter_tests/test_escapejs.py | 10 | ||||
| -rw-r--r-- | tests/template_tests/filter_tests/test_linebreaks.py | 9 | ||||
| -rw-r--r-- | tests/template_tests/filter_tests/test_slugify.py | 10 | ||||
| -rw-r--r-- | tests/template_tests/filter_tests/test_striptags.py | 7 | ||||
| -rw-r--r-- | tests/template_tests/filter_tests/test_urlize.py | 9 | ||||
| -rw-r--r-- | tests/template_tests/filter_tests/test_wordwrap.py | 9 | ||||
| -rw-r--r-- | tests/utils_tests/test_html.py | 7 | ||||
| -rw-r--r-- | tests/utils_tests/test_safestring.py | 5 | ||||
| -rw-r--r-- | tests/utils_tests/test_text.py | 31 |
12 files changed, 116 insertions, 13 deletions
diff --git a/tests/decorators/tests.py b/tests/decorators/tests.py index d23d700201..8fed78a67f 100644 --- a/tests/decorators/tests.py +++ b/tests/decorators/tests.py @@ -8,8 +8,12 @@ from django.contrib.auth.decorators import ( from django.http import HttpRequest, HttpResponse, HttpResponseNotAllowed from django.middleware.clickjacking import XFrameOptionsMiddleware from django.test import SimpleTestCase +from django.utils import six from django.utils.decorators import method_decorator -from django.utils.functional import allow_lazy, lazy +from django.utils.deprecation import RemovedInDjango20Warning +from django.utils.encoding import force_text +from django.utils.functional import allow_lazy, keep_lazy, keep_lazy_text, lazy +from django.utils.translation import ugettext_lazy from django.views.decorators.cache import ( cache_control, cache_page, never_cache, ) @@ -67,7 +71,8 @@ full_decorator = compose( staff_member_required, # django.utils.functional - allow_lazy, + keep_lazy(HttpResponse), + keep_lazy_text, lazy, ) @@ -149,6 +154,15 @@ class DecoratorsTest(TestCase): request.method = 'DELETE' self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed) + def test_deprecated_allow_lazy(self): + with self.assertRaises(RemovedInDjango20Warning): + def noop_text(text): + return force_text(text) + noop_text = allow_lazy(noop_text, six.text_type) + rendered = noop_text(ugettext_lazy("I am a text")) + self.assertEqual(type(rendered), six.text_type) + self.assertEqual(rendered, "I am a text") + # For testing method_decorator, a decorator that assumes a single argument. # We will get type arguments if there is a mismatch in the number of arguments. diff --git a/tests/httpwrappers/tests.py b/tests/httpwrappers/tests.py index 9edc7d12af..94d269c90b 100644 --- a/tests/httpwrappers/tests.py +++ b/tests/httpwrappers/tests.py @@ -21,10 +21,8 @@ from django.http import ( from django.test import SimpleTestCase from django.utils import six from django.utils._os import upath -from django.utils.encoding import force_text, smart_str -from django.utils.functional import lazy - -lazystr = lazy(force_text, six.text_type) +from django.utils.encoding import smart_str +from django.utils.functional import lazystr class QueryDictTests(unittest.TestCase): diff --git a/tests/template_tests/filter_tests/test_escape.py b/tests/template_tests/filter_tests/test_escape.py index 83bfb36444..7dba5e1637 100644 --- a/tests/template_tests/filter_tests/test_escape.py +++ b/tests/template_tests/filter_tests/test_escape.py @@ -1,5 +1,7 @@ from django.template.defaultfilters import escape from django.test import SimpleTestCase +from django.utils import six +from django.utils.functional import Promise, lazy from django.utils.safestring import mark_safe from ..utils import setup @@ -33,6 +35,12 @@ class EscapeTests(SimpleTestCase): output = self.engine.render_to_string('escape04', {"a": "x&y"}) self.assertEqual(output, "x&y") + def test_escape_lazy_string(self): + add_html = lazy(lambda string: string + 'special characters > here', six.text_type) + escaped = escape(add_html('<some html & ')) + self.assertIsInstance(escaped, Promise) + self.assertEqual(escaped, '<some html & special characters > here') + class FunctionTests(SimpleTestCase): diff --git a/tests/template_tests/filter_tests/test_escapejs.py b/tests/template_tests/filter_tests/test_escapejs.py index b913aeb1c0..ccb9cd3c0f 100644 --- a/tests/template_tests/filter_tests/test_escapejs.py +++ b/tests/template_tests/filter_tests/test_escapejs.py @@ -2,6 +2,8 @@ from __future__ import unicode_literals from django.template.defaultfilters import escapejs_filter from django.test import SimpleTestCase +from django.utils import six +from django.utils.functional import lazy from ..utils import setup @@ -51,3 +53,11 @@ class FunctionTests(SimpleTestCase): escapejs_filter('paragraph separator:\u2029and line separator:\u2028'), 'paragraph separator:\\u2029and line separator:\\u2028', ) + + def test_lazy_string(self): + append_script = lazy(lambda string: r'<script>this</script>' + string, six.text_type) + self.assertEqual( + escapejs_filter(append_script('whitespace: \r\n\t\v\f\b')), + '\\u003Cscript\\u003Ethis\\u003C/script\\u003E' + 'whitespace: \\u000D\\u000A\\u0009\\u000B\\u000C\\u0008' + ) diff --git a/tests/template_tests/filter_tests/test_linebreaks.py b/tests/template_tests/filter_tests/test_linebreaks.py index d895a067bb..144facf399 100644 --- a/tests/template_tests/filter_tests/test_linebreaks.py +++ b/tests/template_tests/filter_tests/test_linebreaks.py @@ -1,5 +1,7 @@ from django.template.defaultfilters import linebreaks_filter from django.test import SimpleTestCase +from django.utils import six +from django.utils.functional import lazy from django.utils.safestring import mark_safe from ..utils import setup @@ -51,3 +53,10 @@ class FunctionTests(SimpleTestCase): linebreaks_filter('foo\n<a>bar</a>\nbuz', autoescape=False), '<p>foo<br /><a>bar</a><br />buz</p>', ) + + def test_lazy_string_input(self): + add_header = lazy(lambda string: 'Header\n\n' + string, six.text_type) + self.assertEqual( + linebreaks_filter(add_header('line 1\r\nline2')), + '<p>Header</p>\n\n<p>line 1<br />line2</p>' + ) diff --git a/tests/template_tests/filter_tests/test_slugify.py b/tests/template_tests/filter_tests/test_slugify.py index 1ab5b947b4..5852417b2e 100644 --- a/tests/template_tests/filter_tests/test_slugify.py +++ b/tests/template_tests/filter_tests/test_slugify.py @@ -3,6 +3,9 @@ from __future__ import unicode_literals from django.template.defaultfilters import slugify from django.test import SimpleTestCase +from django.utils import six +from django.utils.encoding import force_text +from django.utils.functional import lazy from django.utils.safestring import mark_safe from ..utils import setup @@ -41,3 +44,10 @@ class FunctionTests(SimpleTestCase): def test_non_string_input(self): self.assertEqual(slugify(123), '123') + + def test_slugify_lazy_string(self): + lazy_str = lazy(lambda string: force_text(string), six.text_type) + self.assertEqual( + slugify(lazy_str(' Jack & Jill like numbers 1,2,3 and 4 and silly characters ?%.$!/')), + 'jack-jill-like-numbers-123-and-4-and-silly-characters', + ) diff --git a/tests/template_tests/filter_tests/test_striptags.py b/tests/template_tests/filter_tests/test_striptags.py index 27f5b393bb..8b946d1f83 100644 --- a/tests/template_tests/filter_tests/test_striptags.py +++ b/tests/template_tests/filter_tests/test_striptags.py @@ -1,5 +1,6 @@ from django.template.defaultfilters import striptags from django.test import SimpleTestCase +from django.utils.functional import lazystr from django.utils.safestring import mark_safe from ..utils import setup @@ -40,3 +41,9 @@ class FunctionTests(SimpleTestCase): def test_non_string_input(self): self.assertEqual(striptags(123), '123') + + def test_strip_lazy_string(self): + self.assertEqual( + striptags(lazystr('some <b>html</b> with <script>alert("Hello")</script> disallowed <img /> tags')), + 'some html with alert("Hello") disallowed tags', + ) diff --git a/tests/template_tests/filter_tests/test_urlize.py b/tests/template_tests/filter_tests/test_urlize.py index 81451f2d4e..9cf3f982a8 100644 --- a/tests/template_tests/filter_tests/test_urlize.py +++ b/tests/template_tests/filter_tests/test_urlize.py @@ -3,6 +3,8 @@ from __future__ import unicode_literals from django.template.defaultfilters import urlize from django.test import SimpleTestCase +from django.utils import six +from django.utils.functional import lazy from django.utils.safestring import mark_safe from ..utils import setup @@ -348,3 +350,10 @@ class FunctionTests(SimpleTestCase): urlize('foo<a href=" google.com ">bar</a>buz', autoescape=False), 'foo<a href=" <a href="http://google.com" rel="nofollow">google.com</a> ">bar</a>buz', ) + + def test_lazystring(self): + prepend_www = lazy(lambda url: 'www.' + url, six.text_type) + self.assertEqual( + urlize(prepend_www('google.com')), + '<a href="http://www.google.com" rel="nofollow">www.google.com</a>', + ) diff --git a/tests/template_tests/filter_tests/test_wordwrap.py b/tests/template_tests/filter_tests/test_wordwrap.py index dc8ab10868..b844ec5108 100644 --- a/tests/template_tests/filter_tests/test_wordwrap.py +++ b/tests/template_tests/filter_tests/test_wordwrap.py @@ -1,5 +1,6 @@ from django.template.defaultfilters import wordwrap from django.test import SimpleTestCase +from django.utils.functional import lazystr from django.utils.safestring import mark_safe from ..utils import setup @@ -41,3 +42,11 @@ class FunctionTests(SimpleTestCase): def test_non_string_input(self): self.assertEqual(wordwrap(123, 2), '123') + + def test_wrap_lazy_string(self): + self.assertEqual( + wordwrap(lazystr( + 'this is a long paragraph of text that really needs to be wrapped I\'m afraid' + ), 14), + 'this is a long\nparagraph of\ntext that\nreally needs\nto be wrapped\nI\'m afraid', + ) diff --git a/tests/utils_tests/test_html.py b/tests/utils_tests/test_html.py index ed026f0a28..20de85148e 100644 --- a/tests/utils_tests/test_html.py +++ b/tests/utils_tests/test_html.py @@ -8,6 +8,7 @@ from django.test import SimpleTestCase from django.utils import html, safestring, six from django.utils._os import upath from django.utils.encoding import force_text +from django.utils.functional import lazystr class TestUtilsHtml(SimpleTestCase): @@ -35,6 +36,7 @@ class TestUtilsHtml(SimpleTestCase): for value, output in items: for pattern in patterns: self.check_output(f, pattern % value, pattern % output) + self.check_output(f, lazystr(pattern % value), pattern % output) # Check repeated values. self.check_output(f, value * 2, output * 2) # Verify it doesn't double replace &. @@ -61,6 +63,7 @@ class TestUtilsHtml(SimpleTestCase): ) for value, output in items: self.check_output(f, value, output) + self.check_output(f, lazystr(value), output) def test_strip_tags(self): f = html.strip_tags @@ -86,6 +89,7 @@ class TestUtilsHtml(SimpleTestCase): ) for value, output in items: self.check_output(f, value, output) + self.check_output(f, lazystr(value), output) # Some convoluted syntax for which parsing may differ between python versions output = html.strip_tags('<sc<!-- -->ript>test<<!-- -->/script>') @@ -113,6 +117,7 @@ class TestUtilsHtml(SimpleTestCase): items = (' <adf>', '<adf> ', ' </adf> ', ' <f> x</f>') for value in items: self.check_output(f, value) + self.check_output(f, lazystr(value)) # Strings that have spaces to strip. items = ( ('<d> </d>', '<d></d>'), @@ -121,6 +126,7 @@ class TestUtilsHtml(SimpleTestCase): ) for value, output in items: self.check_output(f, value, output) + self.check_output(f, lazystr(value), output) def test_escapejs(self): f = html.escapejs @@ -139,6 +145,7 @@ class TestUtilsHtml(SimpleTestCase): ) for value, output in items: self.check_output(f, value, output) + self.check_output(f, lazystr(value), output) def test_smart_urlquote(self): quote = html.smart_urlquote diff --git a/tests/utils_tests/test_safestring.py b/tests/utils_tests/test_safestring.py index acd36053d6..7cc92a1370 100644 --- a/tests/utils_tests/test_safestring.py +++ b/tests/utils_tests/test_safestring.py @@ -3,13 +3,12 @@ from __future__ import unicode_literals from django.template import Context, Template from django.test import SimpleTestCase from django.utils import html, six, text -from django.utils.encoding import force_bytes, force_text -from django.utils.functional import lazy +from django.utils.encoding import force_bytes +from django.utils.functional import lazy, lazystr from django.utils.safestring import ( EscapeData, SafeData, mark_for_escaping, mark_safe, ) -lazystr = lazy(force_text, six.text_type) lazybytes = lazy(force_bytes, bytes) diff --git a/tests/utils_tests/test_text.py b/tests/utils_tests/test_text.py index e6f5c0f378..5d97844800 100644 --- a/tests/utils_tests/test_text.py +++ b/tests/utils_tests/test_text.py @@ -5,12 +5,9 @@ import json from django.test import SimpleTestCase from django.utils import six, text -from django.utils.encoding import force_text -from django.utils.functional import lazy +from django.utils.functional import lazystr from django.utils.translation import override -lazystr = lazy(force_text, six.text_type) - IS_WIDE_BUILD = (len('\U0001F4A9') == 1) @@ -93,6 +90,8 @@ class TestUtilsText(SimpleTestCase): # Make a best effort to shorten to the desired length, but requesting # a length shorter than the ellipsis shouldn't break self.assertEqual('...', text.Truncator('asdf').chars(1)) + # Ensure that lazy strings are handled correctly + self.assertEqual(text.Truncator(lazystr('The quick brown fox')).chars(12), 'The quick...') def test_truncate_words(self): truncator = text.Truncator('The quick brown fox jumped over the lazy ' @@ -102,6 +101,9 @@ class TestUtilsText(SimpleTestCase): self.assertEqual('The quick brown fox...', truncator.words(4)) self.assertEqual('The quick brown fox[snip]', truncator.words(4, '[snip]')) + # Ensure that lazy strings are handled correctly + truncator = text.Truncator(lazystr('The quick brown fox jumped over the lazy dog.')) + self.assertEqual('The quick brown fox...', truncator.words(4)) def test_truncate_html_words(self): truncator = text.Truncator('<p id="par"><strong><em>The quick brown fox' @@ -156,6 +158,7 @@ class TestUtilsText(SimpleTestCase): self.assertEqual(text.wrap(long_word, 20), long_word) self.assertEqual(text.wrap('a %s word' % long_word, 10), 'a\n%s\nword' % long_word) + self.assertEqual(text.wrap(lazystr(digits), 100), '1234 67 9') def test_normalize_newlines(self): self.assertEqual(text.normalize_newlines("abc\ndef\rghi\r\n"), @@ -163,6 +166,7 @@ class TestUtilsText(SimpleTestCase): self.assertEqual(text.normalize_newlines("\n\r\r\n\r"), "\n\n\n\n") self.assertEqual(text.normalize_newlines("abcdefghi"), "abcdefghi") self.assertEqual(text.normalize_newlines(""), "") + self.assertEqual(text.normalize_newlines(lazystr("abc\ndef\rghi\r\n")), "abc\ndef\nghi\n") def test_normalize_newlines_bytes(self): """normalize_newlines should be able to handle bytes too""" @@ -170,6 +174,12 @@ class TestUtilsText(SimpleTestCase): self.assertEqual(normalized, "abc\ndef\nghi\n") self.assertIsInstance(normalized, six.text_type) + def test_phone2numeric(self): + numeric = text.phone2numeric('0800 flowers') + self.assertEqual(numeric, '0800 3569377') + lazy_numeric = lazystr(text.phone2numeric('0800 flowers')) + self.assertEqual(lazy_numeric, '0800 3569377') + def test_slugify(self): items = ( # given - expected - unicode? @@ -195,10 +205,23 @@ class TestUtilsText(SimpleTestCase): ] for value, output in items: self.assertEqual(text.unescape_entities(value), output) + self.assertEqual(text.unescape_entities(lazystr(value)), output) + + def test_unescape_string_literal(self): + items = [ + ('"abc"', 'abc'), + ("'abc'", 'abc'), + ('"a \"bc\""', 'a "bc"'), + ("'\'ab\' c'", "'ab' c"), + ] + for value, output in items: + self.assertEqual(text.unescape_string_literal(value), output) + self.assertEqual(text.unescape_string_literal(lazystr(value)), output) def test_get_valid_filename(self): filename = "^&'@{}[],$=!-#()%+~_123.txt" self.assertEqual(text.get_valid_filename(filename), "-_123.txt") + self.assertEqual(text.get_valid_filename(lazystr(filename)), "-_123.txt") def test_compress_sequence(self): data = [{'key': i} for i in range(10)] |
