annotate core/tests/test_ssl_images.py @ 872:1bd9dadcd4d9

Added mock-based test for ssl_images command. Also added .tags to .hgignore.
author Brian Neal <bgneal@gmail.com>
date Sun, 21 Dec 2014 21:37:30 -0600
parents 6900040df0f8
children 9676833dfdca
rev   line source
bgneal@868 1 """Unit tests for the ssl_images management command."""
bgneal@870 2 import re
bgneal@868 3 import unittest
bgneal@868 4
bgneal@872 5 import mock
bgneal@872 6
bgneal@868 7 from core.management.commands.ssl_images import process_post
bgneal@868 8
bgneal@868 9
bgneal@868 10 class ProcessPostTestCase(unittest.TestCase):
bgneal@868 11
bgneal@870 12 SG101_RE = re.compile(r'http://(?:www\.)?surfguitar101.com/', re.I)
bgneal@870 13
bgneal@868 14 def test_empty_string(self):
bgneal@868 15 s = process_post('')
bgneal@868 16 self.assertEqual(s, '')
bgneal@870 17
bgneal@870 18 def test_no_matches(self):
bgneal@870 19 test_str = """Here is a post that doesn't contain any image links at
bgneal@870 20 all. It also spans lines.
bgneal@870 21 """
bgneal@870 22 result = process_post(test_str)
bgneal@870 23 self.assertEqual(test_str, result)
bgneal@870 24
bgneal@870 25 def test_sg101_images(self):
bgneal@870 26 test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg)
bgneal@870 27 And another: ![pic](HTTP://SURFGUITAR101.COM/foo/bar/img.png).
bgneal@870 28 More stuff here."""
bgneal@870 29 expected = self.SG101_RE.sub('/', test_str)
bgneal@870 30 result = process_post(test_str)
bgneal@870 31 self.assertNotEqual(test_str, expected)
bgneal@870 32 self.assertEqual(expected, result)
bgneal@870 33
bgneal@871 34 def test_sg101_with_newlines(self):
bgneal@871 35 test_str = """An image: ![image](
bgneal@871 36 http://surfguitar101.com/media/zzz.jpg
bgneal@871 37 )
bgneal@871 38 with trailing text."""
bgneal@871 39 expected = """An image: ![image](/media/zzz.jpg)
bgneal@871 40 with trailing text."""
bgneal@871 41 result = process_post(test_str)
bgneal@871 42 self.assertNotEqual(test_str, expected)
bgneal@871 43 self.assertEqual(expected, result)
bgneal@871 44
bgneal@870 45 def test_https_already(self):
bgneal@871 46 test_str = """An image that is already using https:
bgneal@871 47 ![flyer](https://example.com/zzz.png)
bgneal@871 48 It's cool.
bgneal@871 49 """
bgneal@871 50 result = process_post(test_str)
bgneal@871 51 self.assertEqual(test_str, result)
bgneal@871 52
bgneal@871 53 def test_https_sg101(self):
bgneal@871 54 test_str = """An image that is already using https:
bgneal@871 55 ![flyer](https://www.SURFGUITAR101.com/zzz.png)
bgneal@871 56 It's cool.
bgneal@871 57 """
bgneal@871 58 expected = """An image that is already using https:
bgneal@871 59 ![flyer](/zzz.png)
bgneal@871 60 It's cool.
bgneal@871 61 """
bgneal@871 62 result = process_post(test_str)
bgneal@871 63 self.assertEqual(expected, result)
bgneal@871 64
bgneal@871 65 def test_multiple_non_http(self):
bgneal@871 66 test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg)
bgneal@871 67 And another: ![pic](HTTPS://example.com/foo/bar/img.png).
bgneal@871 68 More stuff here."""
bgneal@871 69 expected = """An image: ![image](/img.jpg)
bgneal@871 70 And another: ![pic](HTTPS://example.com/foo/bar/img.png).
bgneal@871 71 More stuff here."""
bgneal@871 72 result = process_post(test_str)
bgneal@871 73 self.assertEqual(expected, result)
bgneal@871 74
bgneal@871 75 def test_https_already_with_title(self):
bgneal@871 76 test_str = """An image that is already using https:
bgneal@871 77 ![flyer](https://example.com/zzz.png "the title")
bgneal@871 78 It's cool.
bgneal@871 79 """
bgneal@871 80 result = process_post(test_str)
bgneal@871 81 self.assertEqual(test_str, result)
bgneal@871 82
bgneal@871 83 def test_sg101_with_title(self):
bgneal@871 84 test_str = """An image on SG101:
bgneal@871 85 ![flyer](http://surfguitar101.com/zzz.png "the title")
bgneal@871 86 It's cool.
bgneal@871 87 """
bgneal@871 88 expected = """An image on SG101:
bgneal@871 89 ![flyer](/zzz.png "the title")
bgneal@871 90 It's cool.
bgneal@871 91 """
bgneal@871 92 result = process_post(test_str)
bgneal@871 93 self.assertEqual(expected, result)
bgneal@871 94
bgneal@871 95 def test_https_sg101_brackets(self):
bgneal@871 96 test_str = """An image that is already using https:
bgneal@871 97 ![flyer](<https://www.SURFGUITAR101.com/zzz.png>)
bgneal@871 98 It's cool.
bgneal@871 99 """
bgneal@871 100 expected = """An image that is already using https:
bgneal@871 101 ![flyer](/zzz.png)
bgneal@871 102 It's cool.
bgneal@871 103 """
bgneal@871 104 result = process_post(test_str)
bgneal@871 105 self.assertEqual(expected, result)
bgneal@871 106
bgneal@871 107 def test_https_already_brackets(self):
bgneal@871 108 test_str = """An image that is already using https:
bgneal@871 109 ![flyer](<https://example.com/zzz.png>)
bgneal@871 110 It's cool.
bgneal@871 111 """
bgneal@871 112 expected = """An image that is already using https:
bgneal@871 113 ![flyer](https://example.com/zzz.png)
bgneal@871 114 It's cool.
bgneal@871 115 """
bgneal@871 116 result = process_post(test_str)
bgneal@871 117 self.assertEqual(expected, result)
bgneal@872 118
bgneal@872 119 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
bgneal@872 120 def test_simple_replacement(self, upload_mock):
bgneal@872 121 old_src = 'http://example.com/images/my_image.jpg'
bgneal@872 122 new_src = 'ttps://cloud.com/ABCDEF.jpg'
bgneal@872 123 test_str = """Here is a really cool http: based image:
bgneal@872 124 ![flyer]({})
bgneal@872 125 Cool, right?""".format(old_src)
bgneal@872 126 expected = """Here is a really cool http: based image:
bgneal@872 127 ![flyer]({})
bgneal@872 128 Cool, right?""".format(new_src)
bgneal@872 129
bgneal@872 130 upload_mock.return_value = new_src
bgneal@872 131 result = process_post(test_str)
bgneal@872 132 self.assertEqual(expected, result)
bgneal@872 133 upload_mock.assert_called_once_with(old_src)