1
|
|
|
from django.test import TestCase |
2
|
|
|
from django.utils.safestring import SafeText |
3
|
|
|
|
4
|
|
|
from simple_forums import markup_renderers |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class TestBaseRenderer(TestCase): |
8
|
|
|
""" Test base rendering class """ |
9
|
|
|
|
10
|
|
|
def setUp(self): |
11
|
|
|
""" Create renderer for each test """ |
12
|
|
|
self.renderer = markup_renderers.BaseRenderer() |
13
|
|
|
|
14
|
|
|
def test_render(self): |
15
|
|
|
""" Test the 'render' method. |
16
|
|
|
|
17
|
|
|
The 'render' method of the base class should raise a |
18
|
|
|
NotImplemented error. |
19
|
|
|
""" |
20
|
|
|
text = "Some sample text" |
21
|
|
|
|
22
|
|
|
with self.assertRaises(NotImplementedError): |
23
|
|
|
self.renderer.render(text) |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
class TestMarkdownRenderer(TestCase): |
27
|
|
|
""" Test class for rendering markdown """ |
28
|
|
|
|
29
|
|
|
def setUp(self): |
30
|
|
|
""" Create renderer for each test """ |
31
|
|
|
self.renderer = markup_renderers.MarkdownRenderer() |
32
|
|
|
|
33
|
|
|
def test_basic_render(self): |
34
|
|
|
""" Test basic behavior. |
35
|
|
|
|
36
|
|
|
The 'render' method should convert markdown to html. For a |
37
|
|
|
basic string, this should wrap text in <p> tags. |
38
|
|
|
""" |
39
|
|
|
text = "Some sample text" |
40
|
|
|
expected_html = "<p>Some sample text</p>" |
41
|
|
|
|
42
|
|
|
result = self.renderer.render(text) |
43
|
|
|
|
44
|
|
|
self.assertHTMLEqual(expected_html, result) |
45
|
|
|
|
46
|
|
|
def test_safe_string(self): |
47
|
|
|
""" Test that the output of the render method is marked safe. |
48
|
|
|
|
49
|
|
|
The output from MarkdownRenderer should be marked safe as it |
50
|
|
|
has been sanitized with the bleach library. |
51
|
|
|
""" |
52
|
|
|
text = 'test' |
53
|
|
|
result = self.renderer.render(text) |
54
|
|
|
|
55
|
|
|
self.assertTrue(type(result) is SafeText) |
56
|
|
|
|
57
|
|
|
def test_script_tag(self): |
58
|
|
|
""" Test passing in text with a script tag. |
59
|
|
|
|
60
|
|
|
If a <script> tag is received in the input, it should be |
61
|
|
|
escaped. |
62
|
|
|
""" |
63
|
|
|
text = "<script>alert('Hello, World!');</script>" |
64
|
|
|
|
65
|
|
|
expected_html = "<script>alert('Hello, World!');</script>" |
66
|
|
|
|
67
|
|
|
self.assertHTMLEqual(expected_html, self.renderer.render(text)) |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
class TestTextRenderer(TestCase): |
71
|
|
|
""" Test class for rendering plain text """ |
72
|
|
|
|
73
|
|
|
def setUp(self): |
74
|
|
|
""" Create renderer for each test """ |
75
|
|
|
self.renderer = markup_renderers.TextRenderer() |
76
|
|
|
|
77
|
|
|
def test_basic_render(self): |
78
|
|
|
""" Test the basic behavior of the text renderer. |
79
|
|
|
|
80
|
|
|
Rendered text should be the same as the input text. |
81
|
|
|
""" |
82
|
|
|
text = "Hello, World!" |
83
|
|
|
|
84
|
|
|
expected = "%s" % text |
85
|
|
|
|
86
|
|
|
self.assertEqual(expected, self.renderer.render(text)) |
87
|
|
|
|
88
|
|
|
def test_rendered_not_safe(self): |
89
|
|
|
""" Test if the rendered string is marked as safe. |
90
|
|
|
|
91
|
|
|
The plain text renderer should have its contents autoescaped, |
92
|
|
|
so it should return a plain string instance, rather than an |
93
|
|
|
instance of SafeString. |
94
|
|
|
""" |
95
|
|
|
text = 'test' |
96
|
|
|
rendered = self.renderer.render(text) |
97
|
|
|
|
98
|
|
|
self.assertTrue(type(rendered) is str) |
99
|
|
|
|