-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
69 lines (56 loc) · 1.74 KB
/
tests.py
File metadata and controls
69 lines (56 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from bleached import UnsafeInput, check_html
import unittest
class TestBleached(unittest.TestCase):
def check(self, source):
check_html(
source,
tags=['p', 'br', 'code',
'a', 'img',
'b', 'u',
],
attributes={'a': ['href', 'title'], 'img': ['src']},
)
def bad(self, source, message, index, line, line_position):
with self.assertRaises(UnsafeInput) as e:
self.check(source)
self.assertEqual(e.exception.message, message)
self.assertEqual(e.exception.index, index)
self.assertEqual(e.exception.line, line)
self.assertEqual(e.exception.line_position, line_position)
def test_check(self):
self.check('<p>Hello <b>world</b></p>')
self.bad(
'<p style="color: red;">Hello</p>',
"Forbidden attribute 'style' in tag 'p'",
8, 1, 9,
)
self.check('<p><a href="somewhere" title="Click!">Hello</a></p>')
self.bad(
'<p>\n<a/>\n</p>',
"Self-closing tag for non-void element 'a'",
8, 2, 5,
)
self.check('<p><br/></p>')
self.check('<p><br></p>')
self.bad(
'<p>',
"Missing closing tag for element 'p'",
3, 1, 4,
)
self.bad(
'</p>',
"Closing tag for wrong element 'p'",
4, 1, 5,
)
self.bad(
'<p><a></p>',
"Closing tag for wrong element 'p'",
10, 1, 11,
)
self.bad(
'<a href>hello</a>',
"Unexpected character '>' in attribute",
8, 1, 9,
)
if __name__ == '__main__':
unittest.main()