comparison core/tests/test_ssl_images.py @ 963:4619290d171d

Whitelist hot-linked image sources.
author Brian Neal <bgneal@gmail.com>
date Tue, 01 Sep 2015 20:33:40 -0500
parents 37e75385e931
children 26de15fb5a80
comparison
equal deleted inserted replaced
962:10e7570a3aab 963:4619290d171d
2 import re 2 import re
3 import unittest 3 import unittest
4 from urlparse import urlparse 4 from urlparse import urlparse
5 5
6 import mock 6 import mock
7 from django.conf import settings
7 8
8 from core.management.commands.ssl_images import html_check 9 from core.management.commands.ssl_images import html_check
9 from core.management.commands.ssl_images import process_post 10 from core.management.commands.ssl_images import process_post
10 import core.management.commands.ssl_images 11 import core.management.commands.ssl_images
11 12
12 13
13 class ProcessPostTestCase(unittest.TestCase): 14 class ProcessPostTestCase(unittest.TestCase):
14 15
15 SG101_RE = re.compile(r'http://(?:www\.)?surfguitar101.com/', re.I) 16 SG101_RE = re.compile(r'http://(?:www\.)?surfguitar101.com/', re.I)
17
18 def setUp(self):
19 self.assertTrue(len(settings.USER_IMAGES_SOURCES) > 0)
20 self.safe_host = settings.USER_IMAGES_SOURCES[0]
16 21
17 def tearDown(self): 22 def tearDown(self):
18 core.management.commands.ssl_images.url_cache = {} 23 core.management.commands.ssl_images.url_cache = {}
19 24
20 def test_empty_string(self): 25 def test_empty_string(self):
48 self.assertNotEqual(test_str, expected) 53 self.assertNotEqual(test_str, expected)
49 self.assertEqual(expected, result) 54 self.assertEqual(expected, result)
50 55
51 def test_https_already(self): 56 def test_https_already(self):
52 test_str = """An image that is already using https: 57 test_str = """An image that is already using https:
53 ![flyer](https://example.com/zzz.png) 58 ![flyer](https://{}/zzz.png)
54 It's cool. 59 It's cool.
55 """ 60 """.format(self.safe_host)
56 result = process_post(test_str) 61 result = process_post(test_str)
57 self.assertEqual(test_str, result) 62 self.assertEqual(test_str, result)
58 63
59 def test_https_sg101(self): 64 def test_https_sg101(self):
60 test_str = """An image that is already using https: 65 test_str = """An image that is already using https:
68 result = process_post(test_str) 73 result = process_post(test_str)
69 self.assertEqual(expected, result) 74 self.assertEqual(expected, result)
70 75
71 def test_multiple_non_http(self): 76 def test_multiple_non_http(self):
72 test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg) 77 test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg)
73 And another: ![pic](HTTPS://example.com/foo/bar/img.png). 78 And another: ![pic](HTTPS://{}/foo/bar/img.png).
74 More stuff here.""" 79 More stuff here.""".format(self.safe_host)
75 expected = """An image: ![image](/img.jpg) 80 expected = """An image: ![image](/img.jpg)
76 And another: ![pic](HTTPS://example.com/foo/bar/img.png). 81 And another: ![pic](HTTPS://{}/foo/bar/img.png).
77 More stuff here.""" 82 More stuff here.""".format(self.safe_host)
78 result = process_post(test_str) 83 result = process_post(test_str)
79 self.assertEqual(expected, result) 84 self.assertEqual(expected, result)
80 85
81 def test_https_already_with_title(self): 86 def test_https_already_with_title(self):
82 test_str = """An image that is already using https: 87 test_str = """An image that is already using https:
83 ![flyer](https://example.com/zzz.png "the title") 88 ![flyer](https://{}/zzz.png "the title")
84 It's cool. 89 It's cool.
85 """ 90 """.format(self.safe_host)
86 result = process_post(test_str) 91 result = process_post(test_str)
87 self.assertEqual(test_str, result) 92 self.assertEqual(test_str, result)
88 93
89 def test_sg101_with_title(self): 94 def test_sg101_with_title(self):
90 test_str = """An image on SG101: 95 test_str = """An image on SG101:
110 result = process_post(test_str) 115 result = process_post(test_str)
111 self.assertEqual(expected, result) 116 self.assertEqual(expected, result)
112 117
113 def test_https_already_brackets(self): 118 def test_https_already_brackets(self):
114 test_str = """An image that is already using https: 119 test_str = """An image that is already using https:
115 ![flyer](<https://example.com/zzz.png>) 120 ![flyer](<https://{}/zzz.png>)
116 It's cool. 121 It's cool.
117 """ 122 """.format(self.safe_host)
118 expected = """An image that is already using https: 123 expected = """An image that is already using https:
119 ![flyer](https://example.com/zzz.png) 124 ![flyer](https://{}/zzz.png)
120 It's cool. 125 It's cool.
121 """ 126 """.format(self.safe_host)
122 result = process_post(test_str) 127 result = process_post(test_str)
123 self.assertEqual(expected, result) 128 self.assertEqual(expected, result)
124 129
125 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud') 130 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
126 def test_simple_replacement(self, upload_mock): 131 def test_simple_replacement(self, upload_mock):
170 175
171 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud') 176 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
172 def test_multiple_replacement_2(self, upload_mock): 177 def test_multiple_replacement_2(self, upload_mock):
173 old_src = [ 178 old_src = [
174 'http://example.com/images/my_image.jpg', 179 'http://example.com/images/my_image.jpg',
175 'https://example.com/static/wow.gif', 180 'https://{}/static/wow.gif'.format(self.safe_host),
176 'http://www.surfguitar101.com/media/a/b/c/pic.png', 181 'http://www.surfguitar101.com/media/a/b/c/pic.png',
177 'http://surfguitar101.com/media/a/b/c/pic2.png', 182 'http://surfguitar101.com/media/a/b/c/pic2.png',
178 ] 183 ]
179 new_src = [ 184 new_src = [
180 'https://cloud.com/some/path/012345.jpg', 185 'https://cloud.com/some/path/012345.jpg',
181 'https://example.com/static/wow.gif', 186 'https://{}/static/wow.gif'.format(self.safe_host),
182 '/media/a/b/c/pic.png', 187 '/media/a/b/c/pic.png',
183 '/media/a/b/c/pic2.png', 188 '/media/a/b/c/pic2.png',
184 ] 189 ]
185 190
186 template = """Here is a really cool http: based image: 191 template = """Here is a really cool http: based image: