bgneal@971: """Tests for the core.html module.""" bgneal@971: import unittest bgneal@971: bgneal@971: from core.html import ImageCheckError bgneal@971: from core.html import image_check bgneal@971: bgneal@971: bgneal@971: TEST_HTML = """ bgneal@971:
Posters and Facebook events are starting to come in...
bgneal@971: bgneal@971: bgneal@971: """ bgneal@971: bgneal@971: bgneal@971: class ImageCheckTestCase(unittest.TestCase): bgneal@971: def setUp(self): bgneal@971: self.allowed_hosts = ['example.com'] bgneal@971: bgneal@971: def test_happy_path(self): bgneal@971: url1 = 'https://example.com/1.jpg' bgneal@971: url2 = 'https://example.com/2.jpg' bgneal@971: html = TEST_HTML.format(src1=url1, src2=url2) bgneal@971: bgneal@971: result = image_check(html, self.allowed_hosts) bgneal@971: self.assertTrue(result) bgneal@971: bgneal@971: def test_empty_image(self): bgneal@971: url1 = 'https://example.com/1.jpg' bgneal@971: url2 = '' bgneal@971: html = TEST_HTML.format(src1=url1, src2=url2) bgneal@971: bgneal@971: self.assertRaises(ImageCheckError, image_check, html, self.allowed_hosts) bgneal@971: bgneal@971: def test_relative_ok(self): bgneal@971: url1 = 'https://example.com/1.jpg' bgneal@971: url2 = '/some/path/2.jpg' bgneal@971: html = TEST_HTML.format(src1=url1, src2=url2) bgneal@971: bgneal@971: result = image_check(html, self.allowed_hosts) bgneal@971: self.assertTrue(result) bgneal@971: bgneal@971: def test_non_https(self): bgneal@971: url1 = 'http://example.com/1.jpg' bgneal@971: url2 = 'https://example.com/2.jpg' bgneal@971: html = TEST_HTML.format(src1=url1, src2=url2) bgneal@971: bgneal@971: self.assertRaises(ImageCheckError, image_check, html, self.allowed_hosts) bgneal@971: bgneal@971: def test_missing_hostname(self): bgneal@971: url1 = 'http:///1.jpg' bgneal@971: url2 = 'https://example.com/2.jpg' bgneal@971: html = TEST_HTML.format(src1=url1, src2=url2) bgneal@971: bgneal@971: self.assertRaises(ImageCheckError, image_check, html, self.allowed_hosts) bgneal@971: bgneal@971: def test_hostname_not_allowed1(self): bgneal@971: url1 = 'https://xxx.example.com/1.jpg' bgneal@971: url2 = 'https://example.com/2.jpg' bgneal@971: html = TEST_HTML.format(src1=url1, src2=url2) bgneal@971: bgneal@971: self.assertRaises(ImageCheckError, image_check, html, self.allowed_hosts) bgneal@971: bgneal@971: def test_hostname_not_allowed2(self): bgneal@971: url1 = 'https://xxx.example.com/1.jpg' bgneal@971: url2 = 'https://yyy.example.com/2.jpg' bgneal@971: html = TEST_HTML.format(src1=url1, src2=url2) bgneal@971: bgneal@971: self.assertRaises(ImageCheckError, image_check, html, self.allowed_hosts) bgneal@985: bgneal@985: def test_empty_string(self): bgneal@985: result = image_check('') bgneal@985: self.assertTrue(result) bgneal@985: bgneal@985: def test_whitespace(self): bgneal@985: result = image_check('\r\n\r\n') bgneal@985: self.assertTrue(result)