|
3 | 3 | from django.template import Context, Origin, Template |
4 | 4 | from django.test import override_settings |
5 | 5 |
|
| 6 | +from ietf.utils.templatetags.textfilters import linkify |
6 | 7 | from ietf.utils.test_utils import TestCase |
7 | 8 | import debug # pyflakes: ignore |
8 | 9 |
|
@@ -39,3 +40,68 @@ def test_origin_outside_base_dir(self): |
39 | 40 | output = template.render(Context()) |
40 | 41 | self.assertNotIn(component, output, |
41 | 42 | 'Full path components should not be revealed in html') |
| 43 | + |
| 44 | + |
| 45 | +class TextfiltersTests(TestCase): |
| 46 | + def test_linkify(self): |
| 47 | + # Cases with autoescape = True (the default) |
| 48 | + self.assertEqual( |
| 49 | + linkify("plain string"), |
| 50 | + "plain string", |
| 51 | + ) |
| 52 | + self.assertEqual( |
| 53 | + linkify("https://www.ietf.org"), |
| 54 | + '<a href="https://www.ietf.org">https://www.ietf.org</a>', |
| 55 | + ) |
| 56 | + self.assertEqual( |
| 57 | + linkify('<a href="https://www.ietf.org">IETF</a>'), |
| 58 | + ( |
| 59 | + '<a href="<a href="https://www.ietf.org">https://www.ietf.org</a>">IETF</a>' |
| 60 | + ), |
| 61 | + ) |
| 62 | + self.assertEqual( |
| 63 | + linkify("somebody@example.com"), |
| 64 | + '<a href="mailto:somebody@example.com">somebody@example.com</a>', |
| 65 | + ) |
| 66 | + self.assertEqual( |
| 67 | + linkify("Some Body <somebody@example.com>"), |
| 68 | + ( |
| 69 | + 'Some Body <<a href="mailto:somebody@example.com">' |
| 70 | + 'somebody@example.com</a>>' |
| 71 | + ), |
| 72 | + ) |
| 73 | + self.assertEqual( |
| 74 | + linkify("<script>alert('h4x0r3d');</script>"), |
| 75 | + "<script>alert('h4x0r3d');</script>", |
| 76 | + ) |
| 77 | + |
| 78 | + # Cases with autoescape = False (these are dangerous and assume the caller |
| 79 | + # has sanitized already) |
| 80 | + self.assertEqual( |
| 81 | + linkify("plain string", autoescape=False), |
| 82 | + "plain string", |
| 83 | + ) |
| 84 | + self.assertEqual( |
| 85 | + linkify("https://www.ietf.org", autoescape=False), |
| 86 | + '<a href="https://www.ietf.org">https://www.ietf.org</a>', |
| 87 | + ) |
| 88 | + self.assertEqual( |
| 89 | + linkify('<a href="https://www.ietf.org">IETF</a>', autoescape=False), |
| 90 | + '<a href="https://www.ietf.org">IETF</a>', |
| 91 | + ) |
| 92 | + self.assertEqual( |
| 93 | + linkify("somebody@example.com", autoescape=False), |
| 94 | + '<a href="mailto:somebody@example.com">somebody@example.com</a>', |
| 95 | + ) |
| 96 | + # bleach.Linkifier translates the < -> < and > -> > on this one |
| 97 | + self.assertEqual( |
| 98 | + linkify("Some Body <somebody@example.com>", autoescape=False), |
| 99 | + ( |
| 100 | + 'Some Body <<a href="mailto:somebody@example.com">' |
| 101 | + 'somebody@example.com</a>>' |
| 102 | + ), |
| 103 | + ) |
| 104 | + self.assertEqual( |
| 105 | + linkify("<script>alert('friendly script');</script>", autoescape=False), |
| 106 | + "<script>alert('friendly script');</script>", |
| 107 | + ) |
0 commit comments