bgneal@971
|
1 """Tests for the core.html module."""
|
bgneal@971
|
2 import unittest
|
bgneal@971
|
3
|
bgneal@971
|
4 from core.html import ImageCheckError
|
bgneal@971
|
5 from core.html import image_check
|
bgneal@971
|
6
|
bgneal@971
|
7
|
bgneal@971
|
8 TEST_HTML = """
|
bgneal@971
|
9 <p>Posters and Facebook events are starting to come in...</p>
|
bgneal@971
|
10 <p><img src="{src1}" alt="image"></p>
|
bgneal@971
|
11 <p><img src="{src2}" alt="image"></p>
|
bgneal@971
|
12 """
|
bgneal@971
|
13
|
bgneal@971
|
14
|
bgneal@971
|
15 class ImageCheckTestCase(unittest.TestCase):
|
bgneal@971
|
16 def setUp(self):
|
bgneal@971
|
17 self.allowed_hosts = ['example.com']
|
bgneal@971
|
18
|
bgneal@971
|
19 def test_happy_path(self):
|
bgneal@971
|
20 url1 = 'https://example.com/1.jpg'
|
bgneal@971
|
21 url2 = 'https://example.com/2.jpg'
|
bgneal@971
|
22 html = TEST_HTML.format(src1=url1, src2=url2)
|
bgneal@971
|
23
|
bgneal@971
|
24 result = image_check(html, self.allowed_hosts)
|
bgneal@971
|
25 self.assertTrue(result)
|
bgneal@971
|
26
|
bgneal@971
|
27 def test_empty_image(self):
|
bgneal@971
|
28 url1 = 'https://example.com/1.jpg'
|
bgneal@971
|
29 url2 = ''
|
bgneal@971
|
30 html = TEST_HTML.format(src1=url1, src2=url2)
|
bgneal@971
|
31
|
bgneal@971
|
32 self.assertRaises(ImageCheckError, image_check, html, self.allowed_hosts)
|
bgneal@971
|
33
|
bgneal@971
|
34 def test_relative_ok(self):
|
bgneal@971
|
35 url1 = 'https://example.com/1.jpg'
|
bgneal@971
|
36 url2 = '/some/path/2.jpg'
|
bgneal@971
|
37 html = TEST_HTML.format(src1=url1, src2=url2)
|
bgneal@971
|
38
|
bgneal@971
|
39 result = image_check(html, self.allowed_hosts)
|
bgneal@971
|
40 self.assertTrue(result)
|
bgneal@971
|
41
|
bgneal@971
|
42 def test_non_https(self):
|
bgneal@971
|
43 url1 = 'http://example.com/1.jpg'
|
bgneal@971
|
44 url2 = 'https://example.com/2.jpg'
|
bgneal@971
|
45 html = TEST_HTML.format(src1=url1, src2=url2)
|
bgneal@971
|
46
|
bgneal@971
|
47 self.assertRaises(ImageCheckError, image_check, html, self.allowed_hosts)
|
bgneal@971
|
48
|
bgneal@971
|
49 def test_missing_hostname(self):
|
bgneal@971
|
50 url1 = 'http:///1.jpg'
|
bgneal@971
|
51 url2 = 'https://example.com/2.jpg'
|
bgneal@971
|
52 html = TEST_HTML.format(src1=url1, src2=url2)
|
bgneal@971
|
53
|
bgneal@971
|
54 self.assertRaises(ImageCheckError, image_check, html, self.allowed_hosts)
|
bgneal@971
|
55
|
bgneal@971
|
56 def test_hostname_not_allowed1(self):
|
bgneal@971
|
57 url1 = 'https://xxx.example.com/1.jpg'
|
bgneal@971
|
58 url2 = 'https://example.com/2.jpg'
|
bgneal@971
|
59 html = TEST_HTML.format(src1=url1, src2=url2)
|
bgneal@971
|
60
|
bgneal@971
|
61 self.assertRaises(ImageCheckError, image_check, html, self.allowed_hosts)
|
bgneal@971
|
62
|
bgneal@971
|
63 def test_hostname_not_allowed2(self):
|
bgneal@971
|
64 url1 = 'https://xxx.example.com/1.jpg'
|
bgneal@971
|
65 url2 = 'https://yyy.example.com/2.jpg'
|
bgneal@971
|
66 html = TEST_HTML.format(src1=url1, src2=url2)
|
bgneal@971
|
67
|
bgneal@971
|
68 self.assertRaises(ImageCheckError, image_check, html, self.allowed_hosts)
|