| 1 |
import json |
| 2 |
import django |
| 3 |
from django.test import TestCase |
| 4 |
from django.core import serializers |
| 5 |
from django.utils.encoding import force_str |
| 6 |
from markupfield.markup import DEFAULT_MARKUP_TYPES |
| 7 |
from markupfield.fields import MarkupField, Markup, MarkupDescriptor |
| 8 |
from markupfield.widgets import MarkupTextarea, AdminMarkupTextareaWidget |
| 9 |
from markupfield.tests.models import ( |
| 10 |
Post, |
| 11 |
Article, |
| 12 |
Concrete, |
| 13 |
NullTestModel, |
| 14 |
DefaultTestModel, |
| 15 |
NullDefaultTestModel, |
| 16 |
) |
| 17 |
|
| 18 |
from django.forms.models import modelform_factory |
| 19 |
|
| 20 |
ArticleForm = modelform_factory( |
| 21 |
Article, |
| 22 |
fields=[ |
| 23 |
"normal_field", |
| 24 |
"normal_field_markup_type", |
| 25 |
"markup_choices_field", |
| 26 |
"markup_choices_field_markup_type", |
| 27 |
"default_field", |
| 28 |
"default_field_markup_type", |
| 29 |
"markdown_field", |
| 30 |
], |
| 31 |
) |
| 32 |
|
| 33 |
|
| 34 |
class MarkupFieldTestCase(TestCase): |
| 35 |
def setUp(self): |
| 36 |
self.xss_str = "<script>alert('xss');</script>" |
| 37 |
self.mp = Post( |
| 38 |
title="example markdown post", |
| 39 |
body="**markdown**", |
| 40 |
body_markup_type="markdown", |
| 41 |
) |
| 42 |
self.mp.save() |
| 43 |
self.rp = Post( |
| 44 |
title="example restructuredtext post", |
| 45 |
body="*ReST*", |
| 46 |
body_markup_type="ReST", |
| 47 |
) |
| 48 |
self.rp.save() |
| 49 |
self.xss_post = Post( |
| 50 |
title="example xss post", |
| 51 |
body=self.xss_str, |
| 52 |
body_markup_type="markdown", |
| 53 |
comment=self.xss_str, |
| 54 |
) |
| 55 |
self.xss_post.save() |
| 56 |
self.plain_str = ( |
| 57 |
'<span style="color: red">plain</span> post\n\n' "http://example.com" |
| 58 |
) |
| 59 |
self.pp = Post( |
| 60 |
title="example plain post", |
| 61 |
body=self.plain_str, |
| 62 |
body_markup_type="plain", |
| 63 |
comment=self.plain_str, |
| 64 |
comment_markup_type="plain", |
| 65 |
) |
| 66 |
self.pp.save() |
| 67 |
|
| 68 |
def test_verbose_name(self): |
| 69 |
self.assertEqual(self.mp._meta.get_field("body").verbose_name, "body of post") |
| 70 |
|
| 71 |
def test_markup_body(self): |
| 72 |
self.assertEqual(self.mp.body.raw, "**markdown**") |
| 73 |
self.assertEqual(self.mp.body.rendered, "<p><strong>markdown</strong></p>") |
| 74 |
self.assertEqual(self.mp.body.markup_type, "markdown") |
| 75 |
|
| 76 |
def test_markup_unicode(self): |
| 77 |
u = force_str(self.rp.body.rendered) |
| 78 |
self.assertEqual(u, "<p><em>ReST</em></p>\n") |
| 79 |
|
| 80 |
def test_markup_bool(self): |
| 81 |
|
| 82 |
p = Post( |
| 83 |
title="example empty", |
| 84 |
body="", |
| 85 |
body_markup_type="plain", |
| 86 |
comment=":)", |
| 87 |
comment_markup_type="markdown", |
| 88 |
) |
| 89 |
assert bool(p.body) is False |
| 90 |
assert bool(p.comment) is True |
| 91 |
|
| 92 |
def test_from_database(self): |
| 93 |
""" Test that data loads back from the database correctly and 'post' |
| 94 |
has the right type.""" |
| 95 |
p1 = Post.objects.get(pk=self.mp.pk) |
| 96 |
self.assertTrue(isinstance(p1.body, Markup)) |
| 97 |
self.assertEqual(force_str(p1.body), "<p><strong>markdown</strong></p>") |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
def test_body_assignment(self): |
| 102 |
self.rp.body = "**ReST**" |
| 103 |
self.rp.save() |
| 104 |
self.assertEqual(force_str(self.rp.body), "<p><strong>ReST</strong></p>\n") |
| 105 |
|
| 106 |
def test_raw_assignment(self): |
| 107 |
self.rp.body.raw = "*ReST*" |
| 108 |
self.rp.save() |
| 109 |
self.assertEqual(force_str(self.rp.body), "<p><em>ReST</em></p>\n") |
| 110 |
|
| 111 |
def test_rendered_assignment(self): |
| 112 |
def f(): |
| 113 |
self.rp.body.rendered = "this should fail" |
| 114 |
|
| 115 |
self.assertRaises(AttributeError, f) |
| 116 |
|
| 117 |
def test_body_type_assignment(self): |
| 118 |
self.rp.body.markup_type = "markdown" |
| 119 |
self.rp.save() |
| 120 |
self.assertEqual(self.rp.body.markup_type, "markdown") |
| 121 |
self.assertEqual(force_str(self.rp.body), "<p><em>ReST</em></p>") |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
def test_serialize_to_json(self): |
| 126 |
stream = serializers.serialize("json", Post.objects.all()[:3]) |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
actual = json.loads(stream) |
| 131 |
expected = [ |
| 132 |
{ |
| 133 |
"pk": 1, |
| 134 |
"model": "tests.post", |
| 135 |
"fields": { |
| 136 |
"body": "**markdown**", |
| 137 |
"comment": "", |
| 138 |
"_comment_rendered": "", |
| 139 |
"_body_rendered": "<p><strong>markdown</strong></p>", |
| 140 |
"title": "example markdown post", |
| 141 |
"comment_markup_type": "markdown", |
| 142 |
"body_markup_type": "markdown", |
| 143 |
}, |
| 144 |
}, |
| 145 |
{ |
| 146 |
"pk": 2, |
| 147 |
"model": "tests.post", |
| 148 |
"fields": { |
| 149 |
"body": "*ReST*", |
| 150 |
"comment": "", |
| 151 |
"_comment_rendered": "", |
| 152 |
"_body_rendered": "<p><em>ReST</em></p>\n", |
| 153 |
"title": "example restructuredtext post", |
| 154 |
"comment_markup_type": "markdown", |
| 155 |
"body_markup_type": "ReST", |
| 156 |
}, |
| 157 |
}, |
| 158 |
{ |
| 159 |
"pk": 3, |
| 160 |
"model": "tests.post", |
| 161 |
"fields": { |
| 162 |
"body": "<script>alert('xss');</script>", |
| 163 |
"comment": "<script>alert('xss');</script>", |
| 164 |
"_comment_rendered": ( |
| 165 |
"<p><script>alert(" "'xss');</script></p>" |
| 166 |
), |
| 167 |
"_body_rendered": "<script>alert('xss');</script>", |
| 168 |
"title": "example xss post", |
| 169 |
"comment_markup_type": "markdown", |
| 170 |
"body_markup_type": "markdown", |
| 171 |
}, |
| 172 |
}, |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
] |
| 190 |
self.assertEqual(len(expected), len(actual)) |
| 191 |
if django.VERSION[0] < 3: |
| 192 |
expected[2]["fields"]["_comment_rendered"] = expected[2]["fields"][ |
| 193 |
"_comment_rendered" |
| 194 |
].replace("x27", "39") |
| 195 |
for n, item in enumerate(expected): |
| 196 |
self.maxDiff = None |
| 197 |
self.assertEqual(item["fields"], actual[n]["fields"]) |
| 198 |
|
| 199 |
def test_deserialize_json(self): |
| 200 |
stream = serializers.serialize("json", Post.objects.all()) |
| 201 |
obj = list(serializers.deserialize("json", stream))[0] |
| 202 |
self.assertEqual(obj.object, self.mp) |
| 203 |
|
| 204 |
def test_value_to_string(self): |
| 205 |
""" |
| 206 |
Ensure field converts to string during _meta access |
| 207 |
|
| 208 |
Other libraries (Django REST framework, etc) go directly to the |
| 209 |
field layer to serialize, which can cause a "unicode object has no |
| 210 |
property called 'raw'" error. This tests the bugfix. |
| 211 |
""" |
| 212 |
obj = self.rp |
| 213 |
field = self.rp._meta.get_field("body") |
| 214 |
self.assertNotEqual(field.value_to_string(obj), u"") |
| 215 |
self.assertEqual(field.value_to_string(None), u"") |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
def test_escape_html(self): |
| 220 |
|
| 221 |
self.assertEqual(self.xss_post.comment.raw, self.xss_str) |
| 222 |
self.assertIn( |
| 223 |
force_str(self.xss_post.comment.rendered), |
| 224 |
( |
| 225 |
"<p><script>alert('xss');</script></p>", |
| 226 |
"<p><script>alert('xss');</script></p>", |
| 227 |
), |
| 228 |
) |
| 229 |
|
| 230 |
def test_escape_html_false(self): |
| 231 |
|
| 232 |
self.assertEqual(self.xss_post.body.raw, self.xss_str) |
| 233 |
self.assertEqual(force_str(self.xss_post.body.rendered), self.xss_str) |
| 234 |
|
| 235 |
def test_inheritance(self): |
| 236 |
|
| 237 |
concrete_fields = [f.name for f in Concrete._meta.fields] |
| 238 |
self.assertEqual( |
| 239 |
concrete_fields, |
| 240 |
["id", "content", "content_markup_type", "_content_rendered"], |
| 241 |
) |
| 242 |
|
| 243 |
def test_markup_type_validation(self): |
| 244 |
self.assertRaises( |
| 245 |
ValueError, MarkupField, "verbose name", "markup_field", "bad_markup_type" |
| 246 |
) |
| 247 |
|
| 248 |
def test_default_markup_types(self): |
| 249 |
for markup_type in DEFAULT_MARKUP_TYPES: |
| 250 |
rendered = markup_type[1]("test") |
| 251 |
self.assertTrue(hasattr(rendered, "__str__")) |
| 252 |
|
| 253 |
def test_plain_markup_urlize(self): |
| 254 |
for key, func, _ in DEFAULT_MARKUP_TYPES: |
| 255 |
if key != "plain": |
| 256 |
continue |
| 257 |
txt1 = "http://example.com some text" |
| 258 |
txt2 = "Some http://example.com text" |
| 259 |
txt3 = "Some text http://example.com" |
| 260 |
txt4 = "http://example.com. some text" |
| 261 |
txt5 = "Some http://example.com. text" |
| 262 |
txt6 = "Some text http://example.com." |
| 263 |
txt7 = ".http://example.com some text" |
| 264 |
txt8 = "Some .http://example.com text" |
| 265 |
txt9 = "Some text .http://example.com" |
| 266 |
self.assertEqual( |
| 267 |
func(txt1), |
| 268 |
'<p><a href="http://example.com">http://example.com</a> some text</p>', |
| 269 |
) |
| 270 |
self.assertEqual( |
| 271 |
func(txt2), |
| 272 |
'<p>Some <a href="http://example.com">http://example.com</a> text</p>', |
| 273 |
) |
| 274 |
self.assertEqual( |
| 275 |
func(txt3), |
| 276 |
'<p>Some text <a href="http://example.com">http://example.com</a></p>', |
| 277 |
) |
| 278 |
self.assertEqual( |
| 279 |
func(txt4), |
| 280 |
'<p><a href="http://example.com">http://example.com</a>. some text</p>', |
| 281 |
) |
| 282 |
self.assertEqual( |
| 283 |
func(txt5), |
| 284 |
'<p>Some <a href="http://example.com">http://example.com</a>. text</p>', |
| 285 |
) |
| 286 |
self.assertEqual( |
| 287 |
func(txt6), |
| 288 |
'<p>Some text <a href="http://example.com">http://example.com</a>.</p>', |
| 289 |
) |
| 290 |
self.assertEqual(func(txt7), "<p>.http://example.com some text</p>") |
| 291 |
self.assertEqual(func(txt8), "<p>Some .http://example.com text</p>") |
| 292 |
self.assertEqual(func(txt9), "<p>Some text .http://example.com</p>") |
| 293 |
break |
| 294 |
|
| 295 |
|
| 296 |
class MarkupWidgetTests(TestCase): |
| 297 |
def test_markuptextarea_used(self): |
| 298 |
self.assertTrue(isinstance(MarkupField().formfield().widget, MarkupTextarea)) |
| 299 |
self.assertTrue( |
| 300 |
isinstance(ArticleForm()["normal_field"].field.widget, MarkupTextarea) |
| 301 |
) |
| 302 |
|
| 303 |
def test_markuptextarea_render(self): |
| 304 |
expected = ( |
| 305 |
'<textarea id="id_normal_field" required rows="10" cols="40" ' |
| 306 |
'name="normal_field">**normal**</textarea>' |
| 307 |
) |
| 308 |
a = Article( |
| 309 |
normal_field="**normal**", |
| 310 |
normal_field_markup_type="markdown", |
| 311 |
default_field="**default**", |
| 312 |
markdown_field="**markdown**", |
| 313 |
markup_choices_field_markup_type="nomarkup", |
| 314 |
) |
| 315 |
a.save() |
| 316 |
af = ArticleForm(instance=a) |
| 317 |
self.assertHTMLEqual(force_str(af["normal_field"]), expected) |
| 318 |
|
| 319 |
def test_no_markup_type_field_if_set(self): |
| 320 |
"""ensure that a field with non-editable markup_type set does not |
| 321 |
have a _markup_type field""" |
| 322 |
self.assertTrue("markdown_field_markup_type" not in ArticleForm().fields.keys()) |
| 323 |
|
| 324 |
def test_markup_type_choices(self): |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
self.assertEqual( |
| 332 |
ArticleForm().fields["normal_field_markup_type"].choices, |
| 333 |
[ |
| 334 |
("", "--"), |
| 335 |
("markdown", "markdown"), |
| 336 |
("ReST", "ReST"), |
| 337 |
("plain", "plain"), |
| 338 |
], |
| 339 |
) |
| 340 |
self.assertEqual( |
| 341 |
ArticleForm().fields["markup_choices_field_markup_type"].choices, |
| 342 |
[ |
| 343 |
("", "--"), |
| 344 |
("pandamarkup", "pandamarkup"), |
| 345 |
("nomarkup", "nomarkup"), |
| 346 |
("fancy", "Some fancy Markup"), |
| 347 |
], |
| 348 |
) |
| 349 |
|
| 350 |
def test_default_markup_type(self): |
| 351 |
self.assertTrue( |
| 352 |
ArticleForm().fields["normal_field_markup_type"].initial is None |
| 353 |
) |
| 354 |
self.assertEqual( |
| 355 |
ArticleForm().fields["default_field_markup_type"].initial, "markdown" |
| 356 |
) |
| 357 |
|
| 358 |
def test_model_admin_field(self): |
| 359 |
|
| 360 |
from django.contrib import admin |
| 361 |
|
| 362 |
ma = admin.ModelAdmin(Post, admin.site) |
| 363 |
self.assertTrue( |
| 364 |
isinstance( |
| 365 |
ma.formfield_for_dbfield( |
| 366 |
Post._meta.get_field("body"), request=None |
| 367 |
).widget, |
| 368 |
AdminMarkupTextareaWidget, |
| 369 |
) |
| 370 |
) |
| 371 |
|
| 372 |
|
| 373 |
class MarkupFieldFormSaveTests(TestCase): |
| 374 |
def setUp(self): |
| 375 |
self.data = { |
| 376 |
"title": "example post", |
| 377 |
"body": "**markdown**", |
| 378 |
"body_markup_type": "markdown", |
| 379 |
} |
| 380 |
self.form_class = modelform_factory( |
| 381 |
Post, fields=["title", "body", "body_markup_type"] |
| 382 |
) |
| 383 |
|
| 384 |
def test_form_create(self): |
| 385 |
form = self.form_class(self.data) |
| 386 |
form.save() |
| 387 |
|
| 388 |
actual = Post.objects.get(title=self.data["title"]) |
| •389 |
self.assertEqual(actual.body.raw, self.data["body"]) |
| 390 |
|
| 391 |
def test_form_update(self): |
| 392 |
existing = Post.objects.create( |
| 393 |
title=self.data["title"], |
| 394 |
body=self.data["body"], |
| 395 |
body_markup_type="markdown", |
| 396 |
) |
| 397 |
|
| 398 |
update = { |
| 399 |
"title": "New title", |
| 400 |
"body": "**different markdown**", |
| 401 |
"body_markup_type": "markdown", |
| 402 |
} |
| 403 |
form = self.form_class(update, instance=existing) |
| 404 |
form.save() |
| 405 |
|
| 406 |
actual = Post.objects.get(title=update["title"]) |
| •407 |
self.assertEqual(actual.body.raw, update["body"]) |
| 408 |
|
| 409 |
|
| 410 |
class MarkupFieldLocalFileTestCase(TestCase): |
| 411 |
def test_no_raw(self): |
| 412 |
for markup_opt in DEFAULT_MARKUP_TYPES: |
| 413 |
if markup_opt[0] == "restructuredtext": |
| 414 |
render_rest = markup_opt[1] |
| 415 |
body = render_rest(".. raw:: html\n :file: AUTHORS.txt") |
| 416 |
|
| 417 |
self.assertNotIn("James Turk", body) |
| 418 |
|
| 419 |
|
| 420 |
class MarkupWidgetRenderTestCase(TestCase): |
| 421 |
def test_model_admin_render(self): |
| 422 |
from django.utils.translation import gettext_lazy as _ |
| 423 |
|
| 424 |
w = AdminMarkupTextareaWidget() |
| 425 |
assert w.render(_("body"), _("Body")) |
| 426 |
|
| 427 |
|
| 428 |
class NullTestCase(TestCase): |
| 429 |
def test_default_null_save(self): |
| 430 |
m = NullTestModel() |
| 431 |
m.save() |
| 432 |
self.assertEqual(force_str(m.text), "") |
| 433 |
self.assertIsNone(m.text.raw) |
| 434 |
self.assertIsNone(m.text.rendered) |
| 435 |
|
| 436 |
|
| 437 |
class DefaultTestCase(TestCase): |
| 438 |
def test_default_value_rendered(self): |
| 439 |
m = DefaultTestModel() |
| 440 |
m.save() |
| 441 |
|
| 442 |
self.assertEqual( |
| 443 |
m._meta.get_field("_text_rendered").default, |
| 444 |
m._meta.get_field("text").default, |
| 445 |
) |
| 446 |
|
| 447 |
self.assertEqual(m._text_rendered, "<p><strong>nice</strong></p>") |
| 448 |
|
| 449 |
def test_default_text_save(self): |
| 450 |
m = DefaultTestModel() |
| 451 |
m.save() |
| 452 |
self.assertEqual(force_str(m.text), "<p><strong>nice</strong></p>") |
| 453 |
|
| 454 |
def test_assign_none(self): |
| 455 |
m = DefaultTestModel() |
| 456 |
m.save() |
| 457 |
self.assertEqual(force_str(m.text), "<p><strong>nice</strong></p>") |
| 458 |
m.text.raw = None |
| 459 |
m.save() |
| 460 |
self.assertEqual(force_str(m.text), "") |
| 461 |
self.assertIsNone(m.text.raw) |
| 462 |
self.assertIsNone(m.text.rendered) |
| 463 |
|
| 464 |
|
| 465 |
class NullDefaultTestCase(TestCase): |
| 466 |
def test_default_value_rendered(self): |
| 467 |
m = NullDefaultTestModel() |
| 468 |
m.save() |
| 469 |
|
| 470 |
self.assertEqual( |
| 471 |
m._meta.get_field("_text_rendered").default, |
| 472 |
m._meta.get_field("text").default, |
| 473 |
) |
| 474 |
|
| 475 |
self.assertEqual(m._text_rendered, "<p><em>nice</em></p>") |
| 476 |
|
| 477 |
|
| 478 |
class MarkupDescriptorTestCase(TestCase): |
| 479 |
def test_class_access_returns_descriptor(self): |
| 480 |
""" |
| 481 |
Accessing a markup field through the class returns the descriptor instance for the field. |
| 482 |
|
| 483 |
(Regression test for issue #50.) |
| 484 |
""" |
| 485 |
self.assertIsInstance(Post.body, MarkupDescriptor) |
| 486 |
self.assertIs(Post._meta.get_field("body"), Post.body.field) |