bgneal@971
|
1 """Tests for the core.html module."""
|
bgneal@971
|
2 import unittest
|
bgneal@971
|
3
|
bgneal@1229
|
4 from mock import call, patch
|
bgneal@1229
|
5
|
bgneal@1229
|
6 from core.html import clean_html, _CLEAN_PROFILES
|
bgneal@971
|
7 from core.html import ImageCheckError
|
bgneal@971
|
8 from core.html import image_check
|
bgneal@971
|
9
|
bgneal@971
|
10
|
bgneal@971
|
11 TEST_HTML = """
|
bgneal@971
|
12 <p>Posters and Facebook events are starting to come in...</p>
|
bgneal@971
|
13 <p><img src="{src1}" alt="image"></p>
|
bgneal@971
|
14 <p><img src="{src2}" alt="image"></p>
|
bgneal@971
|
15 """
|
bgneal@971
|
16
|
bgneal@971
|
17
|
bgneal@971
|
18 class ImageCheckTestCase(unittest.TestCase):
|
bgneal@971
|
19 def setUp(self):
|
bgneal@971
|
20 self.allowed_hosts = ['example.com']
|
bgneal@971
|
21
|
bgneal@971
|
22 def test_happy_path(self):
|
bgneal@971
|
23 url1 = 'https://example.com/1.jpg'
|
bgneal@971
|
24 url2 = 'https://example.com/2.jpg'
|
bgneal@971
|
25 html = TEST_HTML.format(src1=url1, src2=url2)
|
bgneal@971
|
26
|
bgneal@971
|
27 result = image_check(html, self.allowed_hosts)
|
bgneal@971
|
28 self.assertTrue(result)
|
bgneal@971
|
29
|
bgneal@971
|
30 def test_empty_image(self):
|
bgneal@971
|
31 url1 = 'https://example.com/1.jpg'
|
bgneal@971
|
32 url2 = ''
|
bgneal@971
|
33 html = TEST_HTML.format(src1=url1, src2=url2)
|
bgneal@971
|
34
|
bgneal@971
|
35 self.assertRaises(ImageCheckError, image_check, html, self.allowed_hosts)
|
bgneal@971
|
36
|
bgneal@971
|
37 def test_relative_ok(self):
|
bgneal@971
|
38 url1 = 'https://example.com/1.jpg'
|
bgneal@971
|
39 url2 = '/some/path/2.jpg'
|
bgneal@971
|
40 html = TEST_HTML.format(src1=url1, src2=url2)
|
bgneal@971
|
41
|
bgneal@971
|
42 result = image_check(html, self.allowed_hosts)
|
bgneal@971
|
43 self.assertTrue(result)
|
bgneal@971
|
44
|
bgneal@971
|
45 def test_non_https(self):
|
bgneal@971
|
46 url1 = 'http://example.com/1.jpg'
|
bgneal@971
|
47 url2 = 'https://example.com/2.jpg'
|
bgneal@971
|
48 html = TEST_HTML.format(src1=url1, src2=url2)
|
bgneal@971
|
49
|
bgneal@971
|
50 self.assertRaises(ImageCheckError, image_check, html, self.allowed_hosts)
|
bgneal@971
|
51
|
bgneal@971
|
52 def test_missing_hostname(self):
|
bgneal@971
|
53 url1 = 'http:///1.jpg'
|
bgneal@971
|
54 url2 = 'https://example.com/2.jpg'
|
bgneal@971
|
55 html = TEST_HTML.format(src1=url1, src2=url2)
|
bgneal@971
|
56
|
bgneal@971
|
57 self.assertRaises(ImageCheckError, image_check, html, self.allowed_hosts)
|
bgneal@971
|
58
|
bgneal@971
|
59 def test_hostname_not_allowed1(self):
|
bgneal@971
|
60 url1 = 'https://xxx.example.com/1.jpg'
|
bgneal@971
|
61 url2 = 'https://example.com/2.jpg'
|
bgneal@971
|
62 html = TEST_HTML.format(src1=url1, src2=url2)
|
bgneal@971
|
63
|
bgneal@971
|
64 self.assertRaises(ImageCheckError, image_check, html, self.allowed_hosts)
|
bgneal@971
|
65
|
bgneal@971
|
66 def test_hostname_not_allowed2(self):
|
bgneal@971
|
67 url1 = 'https://xxx.example.com/1.jpg'
|
bgneal@971
|
68 url2 = 'https://yyy.example.com/2.jpg'
|
bgneal@971
|
69 html = TEST_HTML.format(src1=url1, src2=url2)
|
bgneal@971
|
70
|
bgneal@971
|
71 self.assertRaises(ImageCheckError, image_check, html, self.allowed_hosts)
|
bgneal@985
|
72
|
bgneal@985
|
73 def test_empty_string(self):
|
bgneal@985
|
74 result = image_check('')
|
bgneal@985
|
75 self.assertTrue(result)
|
bgneal@985
|
76
|
bgneal@985
|
77 def test_whitespace(self):
|
bgneal@985
|
78 result = image_check('\r\n\r\n')
|
bgneal@985
|
79 self.assertTrue(result)
|
bgneal@1229
|
80
|
bgneal@1229
|
81
|
bgneal@1229
|
82 class CleanHtmlTestCase(unittest.TestCase):
|
bgneal@1229
|
83 def test_all_whitespace(self):
|
bgneal@1229
|
84 text = ' \t \n '
|
bgneal@1229
|
85 result = clean_html(text)
|
bgneal@1229
|
86 self.assertEqual(result, '')
|
bgneal@1229
|
87
|
bgneal@1229
|
88 @patch('core.html.bleach.clean')
|
bgneal@1229
|
89 def test_comments(self, clean_mock):
|
bgneal@1229
|
90 text = ' Lorem ipsum '
|
bgneal@1229
|
91 result = clean_html(text)
|
bgneal@1229
|
92 self.assertEqual(clean_mock.mock_calls, [
|
bgneal@1229
|
93 call('Lorem ipsum', tags=_CLEAN_PROFILES['comments'][0],
|
bgneal@1229
|
94 attributes=_CLEAN_PROFILES['comments'][1],
|
bgneal@1229
|
95 styles=_CLEAN_PROFILES['comments'][2], strip=True,
|
bgneal@1229
|
96 strip_comments=True),
|
bgneal@1229
|
97 ])
|
bgneal@1229
|
98
|
bgneal@1229
|
99 @patch('core.html.bleach.clean')
|
bgneal@1229
|
100 def test_comments(self, clean_mock):
|
bgneal@1229
|
101 text = ' Lorem ipsum '
|
bgneal@1229
|
102 result = clean_html(text, profile='news')
|
bgneal@1229
|
103 self.assertEqual(clean_mock.mock_calls, [
|
bgneal@1229
|
104 call('Lorem ipsum', tags=_CLEAN_PROFILES['news'][0],
|
bgneal@1229
|
105 attributes=_CLEAN_PROFILES['news'][1],
|
bgneal@1229
|
106 styles=_CLEAN_PROFILES['news'][2], strip=True,
|
bgneal@1229
|
107 strip_comments=True),
|
bgneal@1229
|
108 ])
|