bgneal@868: """Unit tests for the ssl_images management command.""" bgneal@870: import re bgneal@868: import unittest bgneal@868: bgneal@872: import mock bgneal@872: bgneal@868: from core.management.commands.ssl_images import process_post bgneal@868: bgneal@868: bgneal@868: class ProcessPostTestCase(unittest.TestCase): bgneal@868: bgneal@870: SG101_RE = re.compile(r'http://(?:www\.)?surfguitar101.com/', re.I) bgneal@870: bgneal@868: def test_empty_string(self): bgneal@868: s = process_post('') bgneal@868: self.assertEqual(s, '') bgneal@870: bgneal@870: def test_no_matches(self): bgneal@870: test_str = """Here is a post that doesn't contain any image links at bgneal@870: all. It also spans lines. bgneal@870: """ bgneal@870: result = process_post(test_str) bgneal@870: self.assertEqual(test_str, result) bgneal@870: bgneal@870: def test_sg101_images(self): bgneal@870: test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg) bgneal@870: And another: ![pic](HTTP://SURFGUITAR101.COM/foo/bar/img.png). bgneal@870: More stuff here.""" bgneal@870: expected = self.SG101_RE.sub('/', test_str) bgneal@870: result = process_post(test_str) bgneal@870: self.assertNotEqual(test_str, expected) bgneal@870: self.assertEqual(expected, result) bgneal@870: bgneal@871: def test_sg101_with_newlines(self): bgneal@871: test_str = """An image: ![image]( bgneal@871: http://surfguitar101.com/media/zzz.jpg bgneal@871: ) bgneal@871: with trailing text.""" bgneal@871: expected = """An image: ![image](/media/zzz.jpg) bgneal@871: with trailing text.""" bgneal@871: result = process_post(test_str) bgneal@871: self.assertNotEqual(test_str, expected) bgneal@871: self.assertEqual(expected, result) bgneal@871: bgneal@870: def test_https_already(self): bgneal@871: test_str = """An image that is already using https: bgneal@871: ![flyer](https://example.com/zzz.png) bgneal@871: It's cool. bgneal@871: """ bgneal@871: result = process_post(test_str) bgneal@871: self.assertEqual(test_str, result) bgneal@871: bgneal@871: def test_https_sg101(self): bgneal@871: test_str = """An image that is already using https: bgneal@871: ![flyer](https://www.SURFGUITAR101.com/zzz.png) bgneal@871: It's cool. bgneal@871: """ bgneal@871: expected = """An image that is already using https: bgneal@871: ![flyer](/zzz.png) bgneal@871: It's cool. bgneal@871: """ bgneal@871: result = process_post(test_str) bgneal@871: self.assertEqual(expected, result) bgneal@871: bgneal@871: def test_multiple_non_http(self): bgneal@871: test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg) bgneal@871: And another: ![pic](HTTPS://example.com/foo/bar/img.png). bgneal@871: More stuff here.""" bgneal@871: expected = """An image: ![image](/img.jpg) bgneal@871: And another: ![pic](HTTPS://example.com/foo/bar/img.png). bgneal@871: More stuff here.""" bgneal@871: result = process_post(test_str) bgneal@871: self.assertEqual(expected, result) bgneal@871: bgneal@871: def test_https_already_with_title(self): bgneal@871: test_str = """An image that is already using https: bgneal@871: ![flyer](https://example.com/zzz.png "the title") bgneal@871: It's cool. bgneal@871: """ bgneal@871: result = process_post(test_str) bgneal@871: self.assertEqual(test_str, result) bgneal@871: bgneal@871: def test_sg101_with_title(self): bgneal@871: test_str = """An image on SG101: bgneal@871: ![flyer](http://surfguitar101.com/zzz.png "the title") bgneal@871: It's cool. bgneal@871: """ bgneal@871: expected = """An image on SG101: bgneal@871: ![flyer](/zzz.png "the title") bgneal@871: It's cool. bgneal@871: """ bgneal@871: result = process_post(test_str) bgneal@871: self.assertEqual(expected, result) bgneal@871: bgneal@871: def test_https_sg101_brackets(self): bgneal@871: test_str = """An image that is already using https: bgneal@871: ![flyer]() bgneal@871: It's cool. bgneal@871: """ bgneal@871: expected = """An image that is already using https: bgneal@871: ![flyer](/zzz.png) bgneal@871: It's cool. bgneal@871: """ bgneal@871: result = process_post(test_str) bgneal@871: self.assertEqual(expected, result) bgneal@871: bgneal@871: def test_https_already_brackets(self): bgneal@871: test_str = """An image that is already using https: bgneal@871: ![flyer]() bgneal@871: It's cool. bgneal@871: """ bgneal@871: expected = """An image that is already using https: bgneal@871: ![flyer](https://example.com/zzz.png) bgneal@871: It's cool. bgneal@871: """ bgneal@871: result = process_post(test_str) bgneal@871: self.assertEqual(expected, result) bgneal@872: bgneal@872: @mock.patch('core.management.commands.ssl_images.save_image_to_cloud') bgneal@872: def test_simple_replacement(self, upload_mock): bgneal@872: old_src = 'http://example.com/images/my_image.jpg' bgneal@872: new_src = 'ttps://cloud.com/ABCDEF.jpg' bgneal@872: test_str = """Here is a really cool http: based image: bgneal@872: ![flyer]({}) bgneal@872: Cool, right?""".format(old_src) bgneal@872: expected = """Here is a really cool http: based image: bgneal@872: ![flyer]({}) bgneal@872: Cool, right?""".format(new_src) bgneal@872: bgneal@872: upload_mock.return_value = new_src bgneal@872: result = process_post(test_str) bgneal@872: self.assertEqual(expected, result) bgneal@872: upload_mock.assert_called_once_with(old_src)