comparison core/tests/test_ssl_images.py @ 889:ae146e30d588

Ensure the cache is populated correctly.
author Brian Neal <bgneal@gmail.com>
date Tue, 10 Feb 2015 20:36:34 -0600
parents 9676833dfdca
children 101728976f9c
comparison
equal deleted inserted replaced
888:deef1536a54a 889:ae146e30d588
1 """Unit tests for the ssl_images management command.""" 1 """Unit tests for the ssl_images management command."""
2 import re 2 import re
3 import unittest 3 import unittest
4 from urlparse import urlparse
4 5
5 import mock 6 import mock
6 7
7 from core.management.commands.ssl_images import process_post 8 from core.management.commands.ssl_images import process_post
9 import core.management.commands.ssl_images
8 10
9 11
10 class ProcessPostTestCase(unittest.TestCase): 12 class ProcessPostTestCase(unittest.TestCase):
11 13
12 SG101_RE = re.compile(r'http://(?:www\.)?surfguitar101.com/', re.I) 14 SG101_RE = re.compile(r'http://(?:www\.)?surfguitar101.com/', re.I)
15
16 def tearDown(self):
17 core.management.commands.ssl_images.url_cache = {}
13 18
14 def test_empty_string(self): 19 def test_empty_string(self):
15 s = process_post('') 20 s = process_post('')
16 self.assertEqual(s, '') 21 self.assertEqual(s, '')
17 22
115 """ 120 """
116 result = process_post(test_str) 121 result = process_post(test_str)
117 self.assertEqual(expected, result) 122 self.assertEqual(expected, result)
118 123
119 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud') 124 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
125 @mock.patch('core.management.commands.ssl_images.check_https_availability',
126 new=lambda r: None)
120 def test_simple_replacement(self, upload_mock): 127 def test_simple_replacement(self, upload_mock):
121 old_src = 'http://example.com/images/my_image.jpg' 128 old_src = 'http://example.com/images/my_image.jpg'
122 new_src = 'https://cloud.com/ABCDEF.jpg' 129 new_src = 'https://cloud.com/ABCDEF.jpg'
123 test_str = """Here is a really cool http: based image: 130 test_str = """Here is a really cool http: based image:
124 ![flyer]({}) 131 ![flyer]({})
131 result = process_post(test_str) 138 result = process_post(test_str)
132 self.assertEqual(expected, result) 139 self.assertEqual(expected, result)
133 upload_mock.assert_called_once_with(old_src) 140 upload_mock.assert_called_once_with(old_src)
134 141
135 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud') 142 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
143 @mock.patch('core.management.commands.ssl_images.check_https_availability',
144 new=lambda r: None)
136 def test_multiple_replacement(self, upload_mock): 145 def test_multiple_replacement(self, upload_mock):
137 old_src = [ 146 old_src = [
138 'http://example.com/images/my_image.jpg', 147 'http://example.com/images/my_image.jpg',
139 'http://example.com/static/wow.gif', 148 'http://example.com/static/wow.gif',
140 'http://example.com/media/a/b/c/pic.png', 149 'http://example.com/media/a/b/c/pic.png',
161 self.assertEqual(expected, result) 170 self.assertEqual(expected, result)
162 expected_args = [mock.call(c) for c in old_src] 171 expected_args = [mock.call(c) for c in old_src]
163 self.assertEqual(upload_mock.call_args_list, expected_args) 172 self.assertEqual(upload_mock.call_args_list, expected_args)
164 173
165 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud') 174 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
175 @mock.patch('core.management.commands.ssl_images.check_https_availability',
176 new=lambda r: None)
166 def test_multiple_replacement_2(self, upload_mock): 177 def test_multiple_replacement_2(self, upload_mock):
167 old_src = [ 178 old_src = [
168 'http://example.com/images/my_image.jpg', 179 'http://example.com/images/my_image.jpg',
169 'https://example.com/static/wow.gif', 180 'https://example.com/static/wow.gif',
170 'http://www.surfguitar101.com/media/a/b/c/pic.png', 181 'http://www.surfguitar101.com/media/a/b/c/pic.png',
190 201
191 upload_mock.side_effect = new_src 202 upload_mock.side_effect = new_src
192 result = process_post(test_str) 203 result = process_post(test_str)
193 self.assertEqual(expected, result) 204 self.assertEqual(expected, result)
194 upload_mock.assert_called_once_with(old_src[0]) 205 upload_mock.assert_called_once_with(old_src[0])
206
207 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
208 @mock.patch('core.management.commands.ssl_images.check_https_availability',
209 new=lambda r: None)
210 def test_caching(self, upload_mock):
211 old_src = [
212 'http://example.com/images/my_image.jpg',
213 'http://example.com/static/wow.gif',
214 'http://example.com/images/my_image.jpg',
215 ]
216 new_src = [
217 'https://cloud.com/some/path/012345.jpg',
218 'https://cloud.com/some/path/6789AB.gif',
219 'https://cloud.com/some/path/012345.jpg',
220 ]
221
222 template = """Here is a really cool http: based image:
223 ![flyer]({})
224 Cool, right?
225 Another one: ![pic]({})
226 And finally
227 ![an image]({})
228 """
229
230 test_str = template.format(*old_src)
231 expected = template.format(*new_src)
232
233 upload_mock.side_effect = new_src
234 result = process_post(test_str)
235 self.assertEqual(expected, result)
236 expected_args = [mock.call(c) for c in old_src[:2]]
237 self.assertEqual(upload_mock.call_args_list, expected_args)
238
239 @mock.patch('core.management.commands.ssl_images.check_https_availability')
240 def test_https_availability(self, check_https_mock):
241 old_src = [
242 'http://example.com/images/my_image.jpg',
243 'http://example.com/static/wow.gif',
244 'http://example.com/images/another_image.jpg',
245 ]
246 new_src = [
247 'https://example.com/images/my_image.jpg',
248 'https://example.com/static/wow.gif',
249 'https://example.com/images/another_image.jpg',
250 ]
251
252 template = """Here is a really cool http: based image:
253 ![flyer]({})
254 Cool, right?
255 Another one: ![pic]({})
256 And finally
257 ![an image]({})
258 """
259
260 test_str = template.format(*old_src)
261 expected = template.format(*new_src)
262
263 check_https_mock.side_effect = new_src
264 result = process_post(test_str)
265 self.assertEqual(expected, result)
266 expected_args = [mock.call(urlparse(c)) for c in old_src]
267 self.assertEqual(check_https_mock.call_args_list, expected_args)