# HG changeset patch # User Brian Neal # Date 1423622194 21600 # Node ID ae146e30d588a06301f9a2d5665797524daf28a3 # Parent deef1536a54a42687cc98b242e2fef770743f884 Ensure the cache is populated correctly. diff -r deef1536a54a -r ae146e30d588 core/management/commands/ssl_images.py --- a/core/management/commands/ssl_images.py Mon Feb 09 20:25:13 2015 -0600 +++ b/core/management/commands/ssl_images.py Tue Feb 10 20:36:34 2015 -0600 @@ -171,10 +171,14 @@ # Check to see if the image is available via https first. new_url = check_https_availability(parsed_url) if new_url: + url_cache[src] = new_url return new_url # If none of the above worked, try to download and upload to our S3 bucket - return save_image_to_cloud(src) + new_url = save_image_to_cloud(src) + if new_url: + url_cache[src] = new_url + return new_url def check_https_availability(parsed_url): @@ -215,10 +219,7 @@ fn = download_image(src) if fn: resize_image(fn) - new_url = upload_image(fn) - if new_url: - url_cache[src] = new_url - return new_url + return upload_image(fn) return None @@ -355,7 +356,7 @@ warn_if_image_refs(txt, model_name, model.pk) new_txt = process_post(txt) if txt != new_txt: - logger.info("Content changed on %s #%d (pk= %d)", + logger.info("Content changed on %s #%d (pk = %d)", model_name, n + i, model.pk) logger.debug("original: %s", txt) logger.debug("changed: %s", new_txt) diff -r deef1536a54a -r ae146e30d588 core/tests/test_ssl_images.py --- a/core/tests/test_ssl_images.py Mon Feb 09 20:25:13 2015 -0600 +++ b/core/tests/test_ssl_images.py Tue Feb 10 20:36:34 2015 -0600 @@ -1,16 +1,21 @@ """Unit tests for the ssl_images management command.""" import re import unittest +from urlparse import urlparse import mock from core.management.commands.ssl_images import process_post +import core.management.commands.ssl_images class ProcessPostTestCase(unittest.TestCase): SG101_RE = re.compile(r'http://(?:www\.)?surfguitar101.com/', re.I) + def tearDown(self): + core.management.commands.ssl_images.url_cache = {} + def test_empty_string(self): s = process_post('') self.assertEqual(s, '') @@ -117,6 +122,8 @@ self.assertEqual(expected, result) @mock.patch('core.management.commands.ssl_images.save_image_to_cloud') + @mock.patch('core.management.commands.ssl_images.check_https_availability', + new=lambda r: None) def test_simple_replacement(self, upload_mock): old_src = 'http://example.com/images/my_image.jpg' new_src = 'https://cloud.com/ABCDEF.jpg' @@ -133,6 +140,8 @@ upload_mock.assert_called_once_with(old_src) @mock.patch('core.management.commands.ssl_images.save_image_to_cloud') + @mock.patch('core.management.commands.ssl_images.check_https_availability', + new=lambda r: None) def test_multiple_replacement(self, upload_mock): old_src = [ 'http://example.com/images/my_image.jpg', @@ -163,6 +172,8 @@ self.assertEqual(upload_mock.call_args_list, expected_args) @mock.patch('core.management.commands.ssl_images.save_image_to_cloud') + @mock.patch('core.management.commands.ssl_images.check_https_availability', + new=lambda r: None) def test_multiple_replacement_2(self, upload_mock): old_src = [ 'http://example.com/images/my_image.jpg', @@ -192,3 +203,65 @@ result = process_post(test_str) self.assertEqual(expected, result) upload_mock.assert_called_once_with(old_src[0]) + + @mock.patch('core.management.commands.ssl_images.save_image_to_cloud') + @mock.patch('core.management.commands.ssl_images.check_https_availability', + new=lambda r: None) + def test_caching(self, upload_mock): + old_src = [ + 'http://example.com/images/my_image.jpg', + 'http://example.com/static/wow.gif', + 'http://example.com/images/my_image.jpg', + ] + new_src = [ + 'https://cloud.com/some/path/012345.jpg', + 'https://cloud.com/some/path/6789AB.gif', + 'https://cloud.com/some/path/012345.jpg', + ] + + template = """Here is a really cool http: based image: + ![flyer]({}) + Cool, right? + Another one: ![pic]({}) + And finally + ![an image]({}) + """ + + test_str = template.format(*old_src) + expected = template.format(*new_src) + + upload_mock.side_effect = new_src + result = process_post(test_str) + self.assertEqual(expected, result) + expected_args = [mock.call(c) for c in old_src[:2]] + self.assertEqual(upload_mock.call_args_list, expected_args) + + @mock.patch('core.management.commands.ssl_images.check_https_availability') + def test_https_availability(self, check_https_mock): + old_src = [ + 'http://example.com/images/my_image.jpg', + 'http://example.com/static/wow.gif', + 'http://example.com/images/another_image.jpg', + ] + new_src = [ + 'https://example.com/images/my_image.jpg', + 'https://example.com/static/wow.gif', + 'https://example.com/images/another_image.jpg', + ] + + template = """Here is a really cool http: based image: + ![flyer]({}) + Cool, right? + Another one: ![pic]({}) + And finally + ![an image]({}) + """ + + test_str = template.format(*old_src) + expected = template.format(*new_src) + + check_https_mock.side_effect = new_src + result = process_post(test_str) + self.assertEqual(expected, result) + expected_args = [mock.call(urlparse(c)) for c in old_src] + self.assertEqual(check_https_mock.call_args_list, expected_args)