Ejemplo n.º 1
0
    def test_add_lazy_safe_text_and_safe_text(self):
        s = html.escape(lazystr('a'))
        s += mark_safe('&b')
        self.assertRenderEqual('{{ s }}', 'a&b', s=s)

        s = html.escapejs(lazystr('a'))
        s += mark_safe('&b')
        self.assertRenderEqual('{{ s }}', 'a&b', s=s)
Ejemplo n.º 2
0
    def test_add_lazy_safe_text_and_safe_text(self):
        s = html.escape(lazystr("a"))
        s += mark_safe("&b")
        self.assertRenderEqual("{{ s }}", "a&b", s=s)

        s = html.escapejs(lazystr("a"))
        s += mark_safe("&b")
        self.assertRenderEqual("{{ s }}", "a&b", s=s)

        s = text.slugify(lazystr("a"))
        s += mark_safe("&b")
        self.assertRenderEqual("{{ s }}", "a&b", s=s)
Ejemplo n.º 3
0
 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',
     )
Ejemplo n.º 4
0
    def test_mark_safe_lazy(self):
        s = lazystr("a&b")
        b = lazybytes(b"a&b")

        self.assertIsInstance(mark_safe(s), SafeData)
        self.assertIsInstance(mark_safe(b), SafeData)
        self.assertRenderEqual("{{ s }}", "a&b", s=mark_safe(s))
Ejemplo n.º 5
0
 def test_escapejs(self):
     items = (
         (
             "\"double quotes\" and 'single quotes'",
             "\\u0022double quotes\\u0022 and \\u0027single quotes\\u0027",
         ),
         (r"\ : backslashes, too", "\\u005C : backslashes, too"),
         (
             "and lots of whitespace: \r\n\t\v\f\b",
             "and lots of whitespace: \\u000D\\u000A\\u0009\\u000B\\u000C\\u0008",
         ),
         (
             r"<script>and this</script>",
             "\\u003Cscript\\u003Eand this\\u003C/script\\u003E",
         ),
         (
             "paragraph separator:\u2029and line separator:\u2028",
             "paragraph separator:\\u2029and line separator:\\u2028",
         ),
         ("`", "\\u0060"),
     )
     for value, output in items:
         with self.subTest(value=value, output=output):
             self.check_output(escapejs, value, output)
             self.check_output(escapejs, lazystr(value), output)
Ejemplo n.º 6
0
 def test_strip_spaces_between_tags(self):
     f = html.strip_spaces_between_tags
     # Strings that should come out untouched.
     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>'),
         ('<p>hello </p>\n<p> world</p>', '<p>hello </p><p> world</p>'),
         ('\n<p>\t</p>\n<p> </p>\n', '\n<p></p><p></p>\n'),
     )
     for value, output in items:
         self.check_output(f, value, output)
         self.check_output(f, lazystr(value), output)
Ejemplo n.º 7
0
    def test_mark_safe_lazy(self):
        s = lazystr('a&b')
        b = lazybytes(b'a&b')

        self.assertIsInstance(mark_safe(s), SafeData)
        self.assertIsInstance(mark_safe(b), SafeData)
        self.assertRenderEqual('{{ s }}', 'a&b', s=mark_safe(s))
Ejemplo n.º 8
0
    def test_mark_for_escaping_lazy(self):
        s = lazystr("a&b")
        b = lazybytes(b"a&b")

        self.assertIsInstance(mark_for_escaping(s), EscapeData)
        self.assertIsInstance(mark_for_escaping(b), EscapeData)
        self.assertRenderEqual("{% autoescape off %}{{ s }}{% endautoescape %}", "a&amp;b", s=mark_for_escaping(s))
Ejemplo n.º 9
0
 def test_strip_tags(self):
     items = (
         ('<p>See: &#39;&eacute; is an apostrophe followed by e acute</p>',
          'See: &#39;&eacute; is an apostrophe followed by e acute'),
         ('<adf>a', 'a'),
         ('</adf>a', 'a'),
         ('<asdf><asdf>e', 'e'),
         ('hi, <f x', 'hi, <f x'),
         ('234<235, right?', '234<235, right?'),
         ('a4<a5 right?', 'a4<a5 right?'),
         ('b7>b2!', 'b7>b2!'),
         ('</fe', '</fe'),
         ('<x>b<y>', 'b'),
         ('a<p onclick="alert(\'<test>\')">b</p>c', 'abc'),
         ('a<p a >b</p>c', 'abc'),
         ('d<a:b c:d>e</p>f', 'def'),
         ('<strong>foo</strong><a href="http://example.com">bar</a>',
          'foobar'),
         # caused infinite loop on Pythons not patched with
         # http://bugs.python.org/issue20288
         ('&gotcha&#;<>', '&gotcha&#;<>'),
         ('<sc<!-- -->ript>test<<!-- -->/script>', 'ript>test'),
         ('<script>alert()</script>&h', 'alert()h'),
     )
     for value, output in items:
         with self.subTest(value=value, output=output):
             self.check_output(strip_tags, value, output)
             self.check_output(strip_tags, lazystr(value), output)
Ejemplo n.º 10
0
    def test_mark_for_escaping_lazy(self):
        s = lazystr('a&b')
        b = lazybytes(b'a&b')

        self.assertIsInstance(mark_for_escaping(s), EscapeData)
        self.assertIsInstance(mark_for_escaping(b), EscapeData)
        self.assertRenderEqual('{% autoescape off %}{{ s }}{% endautoescape %}', 'a&amp;b', s=mark_for_escaping(s))
    def test_mark_for_escaping_lazy(self):
        s = lazystr('a&b')
        b = lazybytes(b'a&b')

        self.assertIsInstance(mark_for_escaping(s), EscapeData)
        self.assertIsInstance(mark_for_escaping(b), EscapeData)
        self.assertRenderEqual('{% autoescape off %}{{ s }}{% endautoescape %}', 'a&amp;b', s=mark_for_escaping(s))
Ejemplo n.º 12
0
    def test_truncate_chars(self):
        truncator = text.Truncator(
            'The quick brown fox jumped over the lazy dog.'
        )
        self.assertEqual('The quick brown fox jumped over the lazy dog.', truncator.chars(100)),
        self.assertEqual('The quick brown fox ...', truncator.chars(23)),
        self.assertEqual('The quick brown fo.....', truncator.chars(23, '.....')),

        # Ensure that we normalize our unicode data first
        nfc = text.Truncator('o\xfco\xfco\xfco\xfc')
        nfd = text.Truncator('ou\u0308ou\u0308ou\u0308ou\u0308')
        self.assertEqual('oüoüoüoü', nfc.chars(8))
        self.assertEqual('oüoüoüoü', nfd.chars(8))
        self.assertEqual('oü...', nfc.chars(5))
        self.assertEqual('oü...', nfd.chars(5))

        # Ensure the final length is calculated correctly when there are
        # combining characters with no precomposed form, and that combining
        # characters are not split up.
        truncator = text.Truncator('-B\u030AB\u030A----8')
        self.assertEqual('-B\u030A...', truncator.chars(5))
        self.assertEqual('-B\u030AB\u030A-...', truncator.chars(7))
        self.assertEqual('-B\u030AB\u030A----8', truncator.chars(8))

        # Ensure the length of the end text is correctly calculated when it
        # contains combining characters with no precomposed form.
        truncator = text.Truncator('-----')
        self.assertEqual('---B\u030A', truncator.chars(4, 'B\u030A'))
        self.assertEqual('-----', truncator.chars(5, 'B\u030A'))

        # 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...')
Ejemplo n.º 13
0
    def test_truncate_chars(self):
        truncator = text.Truncator('The quick brown fox jumped over the lazy dog.')
        self.assertEqual('The quick brown fox jumped over the lazy dog.', truncator.chars(100)),
        self.assertEqual('The quick brown fox ...', truncator.chars(23)),
        self.assertEqual('The quick brown fo.....', truncator.chars(23, '.....')),

        nfc = text.Truncator('o\xfco\xfco\xfco\xfc')
        nfd = text.Truncator('ou\u0308ou\u0308ou\u0308ou\u0308')
        self.assertEqual('oüoüoüoü', nfc.chars(8))
        self.assertEqual('oüoüoüoü', nfd.chars(8))
        self.assertEqual('oü...', nfc.chars(5))
        self.assertEqual('oü...', nfd.chars(5))

        # Ensure the final length is calculated correctly when there are
        # combining characters with no precomposed form, and that combining
        # characters are not split up.
        truncator = text.Truncator('-B\u030AB\u030A----8')
        self.assertEqual('-B\u030A...', truncator.chars(5))
        self.assertEqual('-B\u030AB\u030A-...', truncator.chars(7))
        self.assertEqual('-B\u030AB\u030A----8', truncator.chars(8))

        # Ensure the length of the end text is correctly calculated when it
        # contains combining characters with no precomposed form.
        truncator = text.Truncator('-----')
        self.assertEqual('---B\u030A', truncator.chars(4, 'B\u030A'))
        self.assertEqual('-----', truncator.chars(5, 'B\u030A'))

        # 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))
        # lazy strings are handled correctly
        self.assertEqual(text.Truncator(lazystr('The quick brown fox')).chars(12), 'The quick...')
Ejemplo n.º 14
0
    def setup_main_menu(self, site, user_type, m):
        """
        Add a menu item for every journal.

        Menu items are grouped by journal group.

        If a journal group has a :attr:`menu_group`, then journals are added to
        the menu of that plugin, otherwise to the menu of the ledger plugin.

        """
        Journal = site.models.ledger.Journal
        JournalGroups = site.models.ledger.JournalGroups
        for grp in JournalGroups.get_list_items():
            mg = grp.menu_group
            if mg is None:
                lp = site.plugins.ledger
                lm = m.add_menu(lp.app_label, lp.verbose_name)
                subm = lm.add_menu(grp.name, grp.text)
            else:
                subm = m.add_menu(mg.app_label, mg.verbose_name)
            for jnl in Journal.objects.filter(
                    journal_group=grp).order_by('seqno'):
                subm.add_action(jnl.voucher_type.table_class,
                                label=lazystr(jnl),
                                params=dict(master_instance=jnl))
Ejemplo n.º 15
0
 def test_join_lazy(self):
     test_data = {
         'sep': (", ", lazystr(", ")),
         'items': (
             lambda: ["aa", "bb", "cc"],
             lambda: [lazystr("aa"), "bb", lazystr("cc")],
             lazy(lambda: ["aa", lazystr("bb"), "cc"], list),
         ),
     }
     for sep, i_sep, items, j_items in [(s, i, l, j)
                                        for i, s in enumerate(test_data['sep'], start=1)
                                        for j, l in enumerate(test_data['items'], start=1)]:
         with self.subTest(sep=i_sep, list=j_items):
             joined = join_lazy(sep, items())
             self.assertIn('_proxy____cast', dir(joined))
             self.assertEqual(str(joined), "aa, bb, cc")
Ejemplo n.º 16
0
 def test_normalize_newlines(self):
     self.assertEqual(text.normalize_newlines("abc\ndef\rghi\r\n"),
                      "abc\ndef\nghi\n")
     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")
Ejemplo n.º 17
0
    def test_mark_safe_lazy(self):
        s = lazystr('a&b')
        b = lazybytes(b'a&b')

        self.assertIsInstance(mark_safe(s), SafeData)
        self.assertIsInstance(mark_safe(b), SafeData)
        self.assertRenderEqual('{{ s }}', 'a&b', s=mark_safe(s))
Ejemplo n.º 18
0
 def test_strip_tags(self):
     items = (
         ('<p>See: &#39;&eacute; is an apostrophe followed by e acute</p>',
          'See: &#39;&eacute; is an apostrophe followed by e acute'),
         ('<adf>a', 'a'),
         ('</adf>a', 'a'),
         ('<asdf><asdf>e', 'e'),
         ('hi, <f x', 'hi, <f x'),
         ('234<235, right?', '234<235, right?'),
         ('a4<a5 right?', 'a4<a5 right?'),
         ('b7>b2!', 'b7>b2!'),
         ('</fe', '</fe'),
         ('<x>b<y>', 'b'),
         ('a<p onclick="alert(\'<test>\')">b</p>c', 'abc'),
         ('a<p a >b</p>c', 'abc'),
         ('d<a:b c:d>e</p>f', 'def'),
         ('<strong>foo</strong><a href="http://example.com">bar</a>', 'foobar'),
         # caused infinite loop on Pythons not patched with
         # https://bugs.python.org/issue20288
         ('&gotcha&#;<>', '&gotcha&#;<>'),
         ('<sc<!-- -->ript>test<<!-- -->/script>', 'ript>test'),
         ('<script>alert()</script>&h', 'alert()h'),
     )
     for value, output in items:
         with self.subTest(value=value, output=output):
             self.check_output(strip_tags, value, output)
             self.check_output(strip_tags, lazystr(value), output)
Ejemplo n.º 19
0
 def test_smart_split(self):
     testdata = [
         ('This is "a person" test.',
             ['This', 'is', '"a person"', 'test.']),
         ('This is "a person\'s" test.',
             ['This', 'is', '"a person\'s"', 'test.']),
         ('This is "a person\\"s" test.',
             ['This', 'is', '"a person\\"s"', 'test.']),
         ('"a \'one',
             ['"a', "'one"]),
         ('all friends\' tests',
             ['all', 'friends\'', 'tests']),
         ('url search_page words="something else"',
             ['url', 'search_page', 'words="something else"']),
         ("url search_page words='something else'",
             ['url', 'search_page', "words='something else'"]),
         ('url search_page words "something else"',
             ['url', 'search_page', 'words', '"something else"']),
         ('url search_page words-"something else"',
             ['url', 'search_page', 'words-"something else"']),
         ('url search_page words=hello',
             ['url', 'search_page', 'words=hello']),
         ('url search_page words="something else',
             ['url', 'search_page', 'words="something', 'else']),
         ("cut:','|cut:' '",
             ["cut:','|cut:' '"]),
         (lazystr("a b c d"),  # Test for #20231
             ['a', 'b', 'c', 'd']),
     ]
     for test, expected in testdata:
         self.assertEqual(list(text.smart_split(test)), expected)
Ejemplo n.º 20
0
    def test_truncate_chars(self):
        truncator = text.Truncator("The quick brown fox jumped over the lazy dog.")
        self.assertEqual("The quick brown fox jumped over the lazy dog.", truncator.chars(100)),
        self.assertEqual("The quick brown fox ...", truncator.chars(23)),
        self.assertEqual("The quick brown fo.....", truncator.chars(23, ".....")),

        nfc = text.Truncator("o\xfco\xfco\xfco\xfc")
        nfd = text.Truncator("ou\u0308ou\u0308ou\u0308ou\u0308")
        self.assertEqual("oüoüoüoü", nfc.chars(8))
        self.assertEqual("oüoüoüoü", nfd.chars(8))
        self.assertEqual("oü...", nfc.chars(5))
        self.assertEqual("oü...", nfd.chars(5))

        # Ensure the final length is calculated correctly when there are
        # combining characters with no precomposed form, and that combining
        # characters are not split up.
        truncator = text.Truncator("-B\u030AB\u030A----8")
        self.assertEqual("-B\u030A...", truncator.chars(5))
        self.assertEqual("-B\u030AB\u030A-...", truncator.chars(7))
        self.assertEqual("-B\u030AB\u030A----8", truncator.chars(8))

        # Ensure the length of the end text is correctly calculated when it
        # contains combining characters with no precomposed form.
        truncator = text.Truncator("-----")
        self.assertEqual("---B\u030A", truncator.chars(4, "B\u030A"))
        self.assertEqual("-----", truncator.chars(5, "B\u030A"))

        # 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))
        # lazy strings are handled correctly
        self.assertEqual(text.Truncator(lazystr("The quick brown fox")).chars(12), "The quick...")
Ejemplo n.º 21
0
 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',
     )
Ejemplo n.º 22
0
 def test_strip_spaces_between_tags(self):
     f = html.strip_spaces_between_tags
     # Strings that should come out untouched.
     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>'),
         ('<p>hello </p>\n<p> world</p>', '<p>hello </p><p> world</p>'),
         ('\n<p>\t</p>\n<p> </p>\n', '\n<p></p><p></p>\n'),
     )
     for value, output in items:
         self.check_output(f, value, output)
         self.check_output(f, lazystr(value), output)
Ejemplo n.º 23
0
 def test_smart_split(self):
     testdata = [
         ('This is "a person" test.', ['This', 'is', '"a person"',
                                       'test.']),
         ('This is "a person\'s" test.',
          ['This', 'is', '"a person\'s"', 'test.']),
         ('This is "a person\\"s" test.',
          ['This', 'is', '"a person\\"s"', 'test.']),
         ('"a \'one', ['"a', "'one"]),
         ('all friends\' tests', ['all', 'friends\'', 'tests']),
         ('url search_page words="something else"',
          ['url', 'search_page', 'words="something else"']),
         ("url search_page words='something else'",
          ['url', 'search_page', "words='something else'"]),
         ('url search_page words "something else"',
          ['url', 'search_page', 'words', '"something else"']),
         ('url search_page words-"something else"',
          ['url', 'search_page', 'words-"something else"']),
         ('url search_page words=hello',
          ['url', 'search_page', 'words=hello']),
         ('url search_page words="something else',
          ['url', 'search_page', 'words="something', 'else']),
         ("cut:','|cut:' '", ["cut:','|cut:' '"]),
         (
             lazystr("a b c d"),  # Test for #20231
             ['a', 'b', 'c', 'd']),
     ]
     for test, expected in testdata:
         self.assertEqual(list(text.smart_split(test)), expected)
Ejemplo n.º 24
0
 def test_truncate_words(self):
     truncator = text.Truncator('The quick brown fox jumped over the lazy dog.')
     self.assertEqual('The quick brown fox jumped over the lazy dog.', truncator.words(10))
     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))
Ejemplo n.º 25
0
    def test_format_lazy(self):
        self.assertEqual("django/test", format_lazy("{}/{}", "django", lazystr("test")))
        self.assertEqual("django/test", format_lazy("{0}/{1}", *("django", "test")))
        self.assertEqual("django/test", format_lazy("{a}/{b}", **{"a": "django", "b": "test"}))
        self.assertEqual("django/test", format_lazy("{a[0]}/{a[1]}", a=("django", "test")))

        t = {}
        s = format_lazy("{0[a]}-{p[a]}", t, p=t)
        t["a"] = lazystr("django")
        self.assertEqual("django-django", s)
        t["a"] = "update"
        self.assertEqual("update-update", s)

        # The format string can be lazy. (string comes from contrib.admin)
        s = format_lazy(ugettext_lazy('Added {name} "{object}".'), name="article", object="My first try")
        with override("fr"):
            self.assertEqual("article «\xa0My first try\xa0» ajouté.", s)
Ejemplo n.º 26
0
 def test_truncate_words(self):
     truncator = text.Truncator("The quick brown fox jumped over the lazy dog.")
     self.assertEqual("The quick brown fox jumped over the lazy dog.", truncator.words(10))
     self.assertEqual("The quick brown fox...", truncator.words(4))
     self.assertEqual("The quick brown fox[snip]", truncator.words(4, "[snip]"))
     # 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))
Ejemplo n.º 27
0
 def test_truncate_words(self):
     truncator = text.Truncator('The quick brown fox jumped over the lazy dog.')
     self.assertEqual('The quick brown fox jumped over the lazy dog.', truncator.words(10))
     self.assertEqual('The quick brown fox…', truncator.words(4))
     self.assertEqual('The quick brown fox[snip]', truncator.words(4, '[snip]'))
     # 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))
Ejemplo n.º 28
0
 def test_template_name_lazy_string(self):
     """
     #26603 -- A template name specified as a lazy string should be forced
     to text before computing its cache key.
     """
     self.assertEqual(
         self.engine.template_loaders[0].cache_key(lazystr('template.html'),
                                                   []), 'template.html')
Ejemplo n.º 29
0
 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',
     )
Ejemplo n.º 30
0
 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)
Ejemplo n.º 31
0
    def test_strip_spaces_between_tags(self):
        # Strings that should come out untouched.
        items = (" <adf>", "<adf> ", " </adf> ", " <f> x</f>")
        for value in items:
            with self.subTest(value=value):
                self.check_output(strip_spaces_between_tags, value)
                self.check_output(strip_spaces_between_tags, lazystr(value))

        # Strings that have spaces to strip.
        items = (
            ("<d> </d>", "<d></d>"),
            ("<p>hello </p>\n<p> world</p>", "<p>hello </p><p> world</p>"),
            ("\n<p>\t</p>\n<p> </p>\n", "\n<p></p><p></p>\n"),
        )
        for value, output in items:
            with self.subTest(value=value, output=output):
                self.check_output(strip_spaces_between_tags, value, output)
                self.check_output(strip_spaces_between_tags, lazystr(value),
                                  output)
Ejemplo n.º 32
0
 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)
Ejemplo n.º 33
0
 def test_linebreaks(self):
     items = (
         ("para1\n\npara2\r\rpara3", "<p>para1</p>\n\n<p>para2</p>\n\n<p>para3</p>"),
         ("para1\nsub1\rsub2\n\npara2", "<p>para1<br>sub1<br>sub2</p>\n\n<p>para2</p>"),
         ("para1\r\n\r\npara2\rsub1\r\rpara4", "<p>para1</p>\n\n<p>para2<br>sub1</p>\n\n<p>para4</p>"),
         ("para1\tmore\n\npara2", "<p>para1\tmore</p>\n\n<p>para2</p>"),
     )
     for value, output in items:
         with self.subTest(value=value, output=output):
             self.check_output(linebreaks, value, output)
             self.check_output(linebreaks, lazystr(value), output)
Ejemplo n.º 34
0
 def test_linebreaks(self):
     f = html.linebreaks
     items = (
         ("para1\n\npara2\r\rpara3", "<p>para1</p>\n\n<p>para2</p>\n\n<p>para3</p>"),
         ("para1\nsub1\rsub2\n\npara2", "<p>para1<br />sub1<br />sub2</p>\n\n<p>para2</p>"),
         ("para1\r\n\r\npara2\rsub1\r\rpara4", "<p>para1</p>\n\n<p>para2<br />sub1</p>\n\n<p>para4</p>"),
         ("para1\tmore\n\npara2", "<p>para1\tmore</p>\n\n<p>para2</p>"),
     )
     for value, output in items:
         self.check_output(f, value, output)
         self.check_output(f, lazystr(value), output)
Ejemplo n.º 35
0
 def test_urlize(self):
     tests = (
         ('Search for google.com/?q=! and see.',
          'Search for <a href="http://google.com/?q=">google.com/?q=</a>! and see.'
          ),
         (lazystr('Search for google.com/?q=!'),
          'Search for <a href="http://google.com/?q=">google.com/?q=</a>!'),
     )
     for value, output in tests:
         with self.subTest(value=value):
             self.assertEqual(urlize(value), output)
Ejemplo n.º 36
0
 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\n"
         "I'm afraid",
     )
Ejemplo n.º 37
0
    def test_format_lazy(self):
        self.assertEqual('django/test', format_lazy('{}/{}', 'django', lazystr('test')))
        self.assertEqual('django/test', format_lazy('{0}/{1}', *('django', 'test')))
        self.assertEqual('django/test', format_lazy('{a}/{b}', **{'a': 'django', 'b': 'test'}))
        self.assertEqual('django/test', format_lazy('{a[0]}/{a[1]}', a=('django', 'test')))

        t = {}
        s = format_lazy('{0[a]}-{p[a]}', t, p=t)
        t['a'] = lazystr('django')
        self.assertEqual('django-django', s)
        t['a'] = 'update'
        self.assertEqual('update-update', s)

        # The format string can be lazy. (string comes from contrib.admin)
        s = format_lazy(
            ugettext_lazy("Added {name} \"{object}\"."),
            name='article', object='My first try',
        )
        with override('fr'):
            self.assertEqual('Ajout de article «\xa0My first try\xa0».', s)
Ejemplo n.º 38
0
    def test_format_lazy(self):
        self.assertEqual('django/test', format_lazy('{}/{}', 'django', lazystr('test')))
        self.assertEqual('django/test', format_lazy('{0}/{1}', *('django', 'test')))
        self.assertEqual('django/test', format_lazy('{a}/{b}', **{'a': 'django', 'b': 'test'}))
        self.assertEqual('django/test', format_lazy('{a[0]}/{a[1]}', a=('django', 'test')))

        t = {}
        s = format_lazy('{0[a]}-{p[a]}', t, p=t)
        t['a'] = lazystr('django')
        self.assertEqual('django-django', s)
        t['a'] = 'update'
        self.assertEqual('update-update', s)

        # The format string can be lazy. (string comes from contrib.admin)
        s = format_lazy(
            gettext_lazy('Added {name} “{object}”.'),
            name='article', object='My first try',
        )
        with override('fr'):
            self.assertEqual('Ajout de article «\xa0My first try\xa0».', s)
Ejemplo n.º 39
0
 def test_linebreaks(self):
     items = (
         ("para1\n\npara2\r\rpara3", "<p>para1</p>\n\n<p>para2</p>\n\n<p>para3</p>"),
         ("para1\nsub1\rsub2\n\npara2", "<p>para1<br>sub1<br>sub2</p>\n\n<p>para2</p>"),
         ("para1\r\n\r\npara2\rsub1\r\rpara4", "<p>para1</p>\n\n<p>para2<br>sub1</p>\n\n<p>para4</p>"),
         ("para1\tmore\n\npara2", "<p>para1\tmore</p>\n\n<p>para2</p>"),
     )
     for value, output in items:
         with self.subTest(value=value, output=output):
             self.check_output(linebreaks, value, output)
             self.check_output(linebreaks, lazystr(value), output)
Ejemplo n.º 40
0
 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")
     msg = "Could not derive file name from '???'"
     with self.assertRaisesMessage(SuspiciousFileOperation, msg):
         text.get_valid_filename('???')
     # After sanitizing this would yield '..'.
     msg = "Could not derive file name from '$.$.$'"
     with self.assertRaisesMessage(SuspiciousFileOperation, msg):
         text.get_valid_filename('$.$.$')
Ejemplo n.º 41
0
 def test_json_script(self):
     tests = (
         # "<", ">" and "&" are quoted inside JSON strings
         (('&<>', '<script id="test_id" type="application/json">"\\u0026\\u003C\\u003E"</script>')),
         # "<", ">" and "&" are quoted inside JSON objects
         (
             {'a': '<script>test&ing</script>'},
             '<script id="test_id" type="application/json">'
             '{"a": "\\u003Cscript\\u003Etest\\u0026ing\\u003C/script\\u003E"}</script>'
         ),
         # Lazy strings are quoted
         (lazystr('&<>'), '<script id="test_id" type="application/json">"\\u0026\\u003C\\u003E"</script>'),
         (
             {'a': lazystr('<script>test&ing</script>')},
             '<script id="test_id" type="application/json">'
             '{"a": "\\u003Cscript\\u003Etest\\u0026ing\\u003C/script\\u003E"}</script>'
         ),
     )
     for arg, expected in tests:
         with self.subTest(arg=arg):
             self.assertEqual(json_script(arg, 'test_id'), expected)
Ejemplo n.º 42
0
 def test_truncate_words(self):
     truncator = text.Truncator(
         "The quick brown fox jumped over the lazy dog.")
     self.assertEqual("The quick brown fox jumped over the lazy dog.",
                      truncator.words(10))
     self.assertEqual("The quick brown fox…", truncator.words(4))
     self.assertEqual("The quick brown fox[snip]",
                      truncator.words(4, "[snip]"))
     # 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))
Ejemplo n.º 43
0
 def test_json_script(self):
     tests = (
         # "<", ">" and "&" are quoted inside JSON strings
         (('&<>', '<script id="test_id" type="application/json">"\\u0026\\u003C\\u003E"</script>')),
         # "<", ">" and "&" are quoted inside JSON objects
         (
             {'a': '<script>test&ing</script>'},
             '<script id="test_id" type="application/json">'
             '{"a": "\\u003Cscript\\u003Etest\\u0026ing\\u003C/script\\u003E"}</script>'
         ),
         # Lazy strings are quoted
         (lazystr('&<>'), '<script id="test_id" type="application/json">"\\u0026\\u003C\\u003E"</script>'),
         (
             {'a': lazystr('<script>test&ing</script>')},
             '<script id="test_id" type="application/json">'
             '{"a": "\\u003Cscript\\u003Etest\\u0026ing\\u003C/script\\u003E"}</script>'
         ),
     )
     for arg, expected in tests:
         with self.subTest(arg=arg):
             self.assertEqual(json_script(arg, 'test_id'), expected)
Ejemplo n.º 44
0
 def test_unescape_string_literal(self):
     items = [
         ('"abc"', "abc"),
         ("'abc'", "abc"),
         ('"a "bc""', 'a "bc"'),
         ("''ab' c'", "'ab' c"),
     ]
     for value, output in items:
         with self.subTest(value=value):
             self.assertEqual(text.unescape_string_literal(value), output)
             self.assertEqual(text.unescape_string_literal(lazystr(value)),
                              output)
Ejemplo n.º 45
0
 def test_unescape_entities(self):
     items = [
         ('', ''),
         ('foo', 'foo'),
         ('&amp;', '&'),
         ('&#x26;', '&'),
         ('&#38;', '&'),
         ('foo &amp; bar', 'foo & bar'),
         ('foo & bar', 'foo & bar'),
     ]
     for value, output in items:
         self.assertEqual(text.unescape_entities(value), output)
         self.assertEqual(text.unescape_entities(lazystr(value)), output)
Ejemplo n.º 46
0
    def test_wrap(self):
        digits = '1234 67 9'
        self.assertEqual(text.wrap(digits, 100), '1234 67 9')
        self.assertEqual(text.wrap(digits, 9), '1234 67 9')
        self.assertEqual(text.wrap(digits, 8), '1234 67\n9')

        self.assertEqual(text.wrap('short\na long line', 7), 'short\na long\nline')
        self.assertEqual(text.wrap('do-not-break-long-words please? ok', 8), 'do-not-break-long-words\nplease?\nok')

        long_word = 'l%sng' % ('o' * 20)
        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')
Ejemplo n.º 47
0
    def test_wrap(self):
        digits = '1234 67 9'
        self.assertEqual(text.wrap(digits, 100), '1234 67 9')
        self.assertEqual(text.wrap(digits, 9), '1234 67 9')
        self.assertEqual(text.wrap(digits, 8), '1234 67\n9')

        self.assertEqual(text.wrap('short\na long line', 7), 'short\na long\nline')
        self.assertEqual(text.wrap('do-not-break-long-words please? ok', 8), 'do-not-break-long-words\nplease?\nok')

        long_word = 'l%sng' % ('o' * 20)
        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')
Ejemplo n.º 48
0
    def test_wrap(self):
        digits = "1234 67 9"
        self.assertEqual(text.wrap(digits, 100), "1234 67 9")
        self.assertEqual(text.wrap(digits, 9), "1234 67 9")
        self.assertEqual(text.wrap(digits, 8), "1234 67\n9")

        self.assertEqual(text.wrap("short\na long line", 7), "short\na long\nline")
        self.assertEqual(text.wrap("do-not-break-long-words please? ok", 8), "do-not-break-long-words\nplease?\nok")

        long_word = "l%sng" % ("o" * 20)
        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")
Ejemplo n.º 49
0
 def test_unescape_entities(self):
     items = [
         ("", ""),
         ("foo", "foo"),
         ("&amp;", "&"),
         ("&#x26;", "&"),
         ("&#38;", "&"),
         ("foo &amp; bar", "foo & bar"),
         ("foo & bar", "foo & bar"),
     ]
     for value, output in items:
         self.assertEqual(text.unescape_entities(value), output)
         self.assertEqual(text.unescape_entities(lazystr(value)), output)
Ejemplo n.º 50
0
 def test_unescape_entities(self):
     items = [
         ('', ''),
         ('foo', 'foo'),
         ('&amp;', '&'),
         ('&#x26;', '&'),
         ('&#38;', '&'),
         ('foo &amp; bar', 'foo & bar'),
         ('foo & bar', 'foo & bar'),
     ]
     for value, output in items:
         self.assertEqual(text.unescape_entities(value), output)
         self.assertEqual(text.unescape_entities(lazystr(value)), output)
Ejemplo n.º 51
0
 def test_urlize(self):
     tests = (
         (
             'Search for google.com/?q=! and see.',
             'Search for <a href="http://google.com/?q=">google.com/?q=</a>! and see.'
         ),
         (
             lazystr('Search for google.com/?q=!'),
             'Search for <a href="http://google.com/?q=">google.com/?q=</a>!'
         ),
     )
     for value, output in tests:
         with self.subTest(value=value):
             self.assertEqual(urlize(value), output)
Ejemplo n.º 52
0
    def setup_main_menu(self, site, user_type, m):
        if not self.intrusive_menu:
            mg = site.plugins.accounts
            m = m.add_menu(mg.app_label, mg.verbose_name)

        Journal = site.models.ledger.Journal
        JournalGroups = site.models.ledger.JournalGroups
        for grp in JournalGroups.objects():
            subm = m.add_menu(grp.name, grp.text)
            for jnl in Journal.objects.filter(
                    journal_group=grp).order_by('seqno'):
                subm.add_action(jnl.voucher_type.table_class,
                                label=lazystr(jnl),
                                params=dict(master_instance=jnl))
Ejemplo n.º 53
0
    def test_strip_tags(self):
        f = html.strip_tags
        items = (
            ('<p>See: &#39;&eacute; is an apostrophe followed by e acute</p>',
             'See: &#39;&eacute; is an apostrophe followed by e acute'),
            ('<adf>a', 'a'),
            ('</adf>a', 'a'),
            ('<asdf><asdf>e', 'e'),
            ('hi, <f x', 'hi, <f x'),
            ('234<235, right?', '234<235, right?'),
            ('a4<a5 right?', 'a4<a5 right?'),
            ('b7>b2!', 'b7>b2!'),
            ('</fe', '</fe'),
            ('<x>b<y>', 'b'),
            ('a<p onclick="alert(\'<test>\')">b</p>c', 'abc'),
            ('a<p a >b</p>c', 'abc'),
            ('d<a:b c:d>e</p>f', 'def'),
            ('<strong>foo</strong><a href="http://example.com">bar</a>',
             'foobar'),
            # caused infinite loop on Pythons not patched with
            # http://bugs.python.org/issue20288
            ('&gotcha&#;<>', '&gotcha&#;<>'),
            ('><!' + ('&' * 16000) + 'D', '><!' + ('&' * 16000) + 'D'),
            ('X<<<<br>br>br>br>X', 'XX'),
        )
        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>')
        self.assertNotIn('<script>', output)
        self.assertIn('test', output)
        output = html.strip_tags('<script>alert()</script>&h')
        self.assertNotIn('<script>', output)
        self.assertIn('alert()', output)

        # Test with more lengthy content (also catching performance regressions)
        for filename in ('strip_tags1.html', 'strip_tags2.txt'):
            path = os.path.join(os.path.dirname(upath(__file__)), 'files',
                                filename)
            with open(path, 'r') as fp:
                content = force_text(fp.read())
                start = datetime.now()
                stripped = html.strip_tags(content)
                elapsed = datetime.now() - start
            self.assertEqual(elapsed.seconds, 0)
            self.assertIn("Please try again.", stripped)
            self.assertNotIn('<', stripped)
Ejemplo n.º 54
0
 def test_urlize(self):
     tests = (
         ('Search for google.com/?q=! and see.',
          'Search for <a href="http://google.com/?q=">google.com/?q=</a>! and see.'
          ),
         ('Search for google.com/?q=1&lt! and see.',
          'Search for <a href="http://google.com/?q=1%3C">google.com/?q=1&lt</a>! and see.'
          ),
         (lazystr('Search for google.com/?q=!'),
          'Search for <a href="http://google.com/?q=">google.com/?q=</a>!'),
         ('*****@*****.**',
          '<a href="mailto:[email protected]">[email protected]</a>'),
     )
     for value, output in tests:
         with self.subTest(value=value):
             self.assertEqual(urlize(value), output)
Ejemplo n.º 55
0
 def test_escapejs(self):
     items = (
         ('"double quotes" and \'single quotes\'',
          '\\u0022double quotes\\u0022 and \\u0027single quotes\\u0027'),
         (r'\ : backslashes, too', '\\u005C : backslashes, too'),
         ('and lots of whitespace: \r\n\t\v\f\b',
          'and lots of whitespace: \\u000D\\u000A\\u0009\\u000B\\u000C\\u0008'
          ),
         (r'<script>and this</script>',
          '\\u003Cscript\\u003Eand this\\u003C/script\\u003E'),
         ('paragraph separator:\u2029and line separator:\u2028',
          'paragraph separator:\\u2029and line separator:\\u2028'),
     )
     for value, output in items:
         with self.subTest(value=value, output=output):
             self.check_output(escapejs, value, output)
             self.check_output(escapejs, lazystr(value), output)
Ejemplo n.º 56
0
    def test_strip_tags(self):
        f = html.strip_tags
        items = (
            ('<p>See: &#39;&eacute; is an apostrophe followed by e acute</p>',
             'See: &#39;&eacute; is an apostrophe followed by e acute'),
            ('<adf>a', 'a'),
            ('</adf>a', 'a'),
            ('<asdf><asdf>e', 'e'),
            ('hi, <f x', 'hi, <f x'),
            ('234<235, right?', '234<235, right?'),
            ('a4<a5 right?', 'a4<a5 right?'),
            ('b7>b2!', 'b7>b2!'),
            ('</fe', '</fe'),
            ('<x>b<y>', 'b'),
            ('a<p onclick="alert(\'<test>\')">b</p>c', 'abc'),
            ('a<p a >b</p>c', 'abc'),
            ('d<a:b c:d>e</p>f', 'def'),
            ('<strong>foo</strong><a href="http://example.com">bar</a>', 'foobar'),
            # caused infinite loop on Pythons not patched with
            # http://bugs.python.org/issue20288
            ('&gotcha&#;<>', '&gotcha&#;<>'),
        )
        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>')
        self.assertNotIn('<script>', output)
        self.assertIn('test', output)
        output = html.strip_tags('<script>alert()</script>&h')
        self.assertNotIn('<script>', output)
        self.assertIn('alert()', output)

        # Test with more lengthy content (also catching performance regressions)
        for filename in ('strip_tags1.html', 'strip_tags2.txt'):
            path = os.path.join(os.path.dirname(upath(__file__)), 'files', filename)
            with open(path, 'r') as fp:
                content = force_text(fp.read())
                start = datetime.now()
                stripped = html.strip_tags(content)
                elapsed = datetime.now() - start
            self.assertEqual(elapsed.seconds, 0)
            self.assertIn("Please try again.", stripped)
            self.assertNotIn('<', stripped)
Ejemplo n.º 57
0
 def test_escapejs(self):
     f = html.escapejs
     items = (
         ('"double quotes" and \'single quotes\'', '\\u0022double quotes\\u0022 and \\u0027single quotes\\u0027'),
         (r'\ : backslashes, too', '\\u005C : backslashes, too'),
         (
             'and lots of whitespace: \r\n\t\v\f\b',
             'and lots of whitespace: \\u000D\\u000A\\u0009\\u000B\\u000C\\u0008'
         ),
         (r'<script>and this</script>', '\\u003Cscript\\u003Eand this\\u003C/script\\u003E'),
         (
             'paragraph separator:\u2029and line separator:\u2028',
             'paragraph separator:\\u2029and line separator:\\u2028'
         ),
     )
     for value, output in items:
         self.check_output(f, value, output)
         self.check_output(f, lazystr(value), output)
Ejemplo n.º 58
0
    def test_wrap(self):
        digits = "1234 67 9"
        self.assertEqual(text.wrap(digits, 100), "1234 67 9")
        self.assertEqual(text.wrap(digits, 9), "1234 67 9")
        self.assertEqual(text.wrap(digits, 8), "1234 67\n9")

        self.assertEqual(text.wrap("short\na long line", 7),
                         "short\na long\nline")
        self.assertEqual(
            text.wrap("do-not-break-long-words please? ok", 8),
            "do-not-break-long-words\nplease?\nok",
        )

        long_word = "l%sng" % ("o" * 20)
        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")