comparison core/tests/test_html.py @ 955:71a671dab55d

First commit of whitelisting image hosts. This is behind a feature flag courtesy of waffle.
author Brian Neal <bgneal@gmail.com>
date Wed, 03 Jun 2015 21:13:08 -0500
parents
children 9b197dbba34b
comparison
equal deleted inserted replaced
954:e56455f4626b 955:71a671dab55d
1 """Tests for the core.html module."""
2 import unittest
3
4 from core.html import ImageCheckError
5 from core.html import image_check
6
7
8 TEST_HTML = """
9 <p>Posters and Facebook events are starting to come in...</p>
10 <p><img src="{src1}" alt="image"></p>
11 <p><img src="{src2}" alt="image"></p>
12 """
13
14
15 class ImageCheckTestCase(unittest.TestCase):
16 def setUp(self):
17 self.allowed_hosts = ['example.com']
18
19 def test_happy_path(self):
20 url1 = 'https://example.com/1.jpg'
21 url2 = 'https://example.com/2.jpg'
22 html = TEST_HTML.format(src1=url1, src2=url2)
23
24 result = image_check(html, self.allowed_hosts)
25 self.assertTrue(result)
26
27 def test_empty_image(self):
28 url1 = 'https://example.com/1.jpg'
29 url2 = ''
30 html = TEST_HTML.format(src1=url1, src2=url2)
31
32 self.assertRaises(ImageCheckError, image_check, html, self.allowed_hosts)
33
34 def test_relative_ok(self):
35 url1 = 'https://example.com/1.jpg'
36 url2 = '/some/path/2.jpg'
37 html = TEST_HTML.format(src1=url1, src2=url2)
38
39 result = image_check(html, self.allowed_hosts)
40 self.assertTrue(result)
41
42 def test_non_https(self):
43 url1 = 'http://example.com/1.jpg'
44 url2 = 'https://example.com/2.jpg'
45 html = TEST_HTML.format(src1=url1, src2=url2)
46
47 self.assertRaises(ImageCheckError, image_check, html, self.allowed_hosts)
48
49 def test_missing_hostname(self):
50 url1 = 'http:///1.jpg'
51 url2 = 'https://example.com/2.jpg'
52 html = TEST_HTML.format(src1=url1, src2=url2)
53
54 self.assertRaises(ImageCheckError, image_check, html, self.allowed_hosts)
55
56 def test_hostname_not_allowed1(self):
57 url1 = 'https://xxx.example.com/1.jpg'
58 url2 = 'https://example.com/2.jpg'
59 html = TEST_HTML.format(src1=url1, src2=url2)
60
61 self.assertRaises(ImageCheckError, image_check, html, self.allowed_hosts)
62
63 def test_hostname_not_allowed2(self):
64 url1 = 'https://xxx.example.com/1.jpg'
65 url2 = 'https://yyy.example.com/2.jpg'
66 html = TEST_HTML.format(src1=url1, src2=url2)
67
68 self.assertRaises(ImageCheckError, image_check, html, self.allowed_hosts)