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