diff options
| author | Moritz Sichert <moritz.sichert@googlemail.com> | 2015-03-18 21:42:59 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-03-27 19:46:20 -0400 |
| commit | 1f2abf784a9fe550959de242d91963b2ad6f7e9c (patch) | |
| tree | bf56ca4f56279281a24793197fc6bed88781f727 /tests/utils_tests/test_html.py | |
| parent | dc5b01ad05e50ccde688c73c2ed3334a956076b0 (diff) | |
Fixed #24469 -- Refined escaping of Django's form elements in non-Django templates.
Diffstat (limited to 'tests/utils_tests/test_html.py')
| -rw-r--r-- | tests/utils_tests/test_html.py | 71 |
1 files changed, 67 insertions, 4 deletions
diff --git a/tests/utils_tests/test_html.py b/tests/utils_tests/test_html.py index 7456b67d50..58a4d48753 100644 --- a/tests/utils_tests/test_html.py +++ b/tests/utils_tests/test_html.py @@ -3,16 +3,15 @@ from __future__ import unicode_literals import os from datetime import datetime -from unittest import TestCase -from django.test import ignore_warnings -from django.utils import html, safestring +from django.test import SimpleTestCase, ignore_warnings +from django.utils import html, safestring, six from django.utils._os import upath from django.utils.deprecation import RemovedInDjango20Warning from django.utils.encoding import force_text -class TestUtilsHtml(TestCase): +class TestUtilsHtml(SimpleTestCase): def check_output(self, function, value, output=None): """ @@ -185,3 +184,67 @@ class TestUtilsHtml(TestCase): self.assertEqual(html.conditional_escape(s), '<h1>interop</h1>') self.assertEqual(html.conditional_escape(safestring.mark_safe(s)), s) + + def test_html_safe(self): + @html.html_safe + class HtmlClass(object): + if six.PY2: + def __unicode__(self): + return "<h1>I'm a html class!</h1>" + else: + def __str__(self): + return "<h1>I'm a html class!</h1>" + + html_obj = HtmlClass() + self.assertTrue(hasattr(HtmlClass, '__html__')) + self.assertTrue(hasattr(html_obj, '__html__')) + self.assertEqual(force_text(html_obj), html_obj.__html__()) + + def test_html_safe_subclass(self): + if six.PY2: + class BaseClass(object): + def __html__(self): + # defines __html__ on its own + return 'some html content' + + def __unicode__(self): + return 'some non html content' + + @html.html_safe + class Subclass(BaseClass): + def __unicode__(self): + # overrides __unicode__ and is marked as html_safe + return 'some html safe content' + else: + class BaseClass(object): + def __html__(self): + # defines __html__ on its own + return 'some html content' + + def __str__(self): + return 'some non html content' + + @html.html_safe + class Subclass(BaseClass): + def __str__(self): + # overrides __str__ and is marked as html_safe + return 'some html safe content' + + subclass_obj = Subclass() + self.assertEqual(force_text(subclass_obj), subclass_obj.__html__()) + + def test_html_safe_defines_html_error(self): + msg = "can't apply @html_safe to HtmlClass because it defines __html__()." + with self.assertRaisesMessage(ValueError, msg): + @html.html_safe + class HtmlClass(object): + def __html__(self): + return "<h1>I'm a html class!</h1>" + + def test_html_safe_doesnt_define_str(self): + method_name = '__unicode__()' if six.PY2 else '__str__()' + msg = "can't apply @html_safe to HtmlClass because it doesn't define %s." % method_name + with self.assertRaisesMessage(ValueError, msg): + @html.html_safe + class HtmlClass(object): + pass |
