annotate 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
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@889 4 from urlparse import urlparse
bgneal@868 5
bgneal@872 6 import mock
bgneal@872 7
bgneal@868 8 from core.management.commands.ssl_images import process_post
bgneal@889 9 import core.management.commands.ssl_images
bgneal@868 10
bgneal@868 11
bgneal@868 12 class ProcessPostTestCase(unittest.TestCase):
bgneal@868 13
bgneal@870 14 SG101_RE = re.compile(r'http://(?:www\.)?surfguitar101.com/', re.I)
bgneal@870 15
bgneal@889 16 def tearDown(self):
bgneal@889 17 core.management.commands.ssl_images.url_cache = {}
bgneal@889 18
bgneal@868 19 def test_empty_string(self):
bgneal@868 20 s = process_post('')
bgneal@868 21 self.assertEqual(s, '')
bgneal@870 22
bgneal@870 23 def test_no_matches(self):
bgneal@870 24 test_str = """Here is a post that doesn't contain any image links at
bgneal@870 25 all. It also spans lines.
bgneal@870 26 """
bgneal@870 27 result = process_post(test_str)
bgneal@870 28 self.assertEqual(test_str, result)
bgneal@870 29
bgneal@870 30 def test_sg101_images(self):
bgneal@870 31 test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg)
bgneal@870 32 And another: ![pic](HTTP://SURFGUITAR101.COM/foo/bar/img.png).
bgneal@870 33 More stuff here."""
bgneal@870 34 expected = self.SG101_RE.sub('/', test_str)
bgneal@870 35 result = process_post(test_str)
bgneal@870 36 self.assertNotEqual(test_str, expected)
bgneal@870 37 self.assertEqual(expected, result)
bgneal@870 38
bgneal@871 39 def test_sg101_with_newlines(self):
bgneal@871 40 test_str = """An image: ![image](
bgneal@871 41 http://surfguitar101.com/media/zzz.jpg
bgneal@871 42 )
bgneal@871 43 with trailing text."""
bgneal@871 44 expected = """An image: ![image](/media/zzz.jpg)
bgneal@871 45 with trailing text."""
bgneal@871 46 result = process_post(test_str)
bgneal@871 47 self.assertNotEqual(test_str, expected)
bgneal@871 48 self.assertEqual(expected, result)
bgneal@871 49
bgneal@870 50 def test_https_already(self):
bgneal@871 51 test_str = """An image that is already using https:
bgneal@871 52 ![flyer](https://example.com/zzz.png)
bgneal@871 53 It's cool.
bgneal@871 54 """
bgneal@871 55 result = process_post(test_str)
bgneal@871 56 self.assertEqual(test_str, result)
bgneal@871 57
bgneal@871 58 def test_https_sg101(self):
bgneal@871 59 test_str = """An image that is already using https:
bgneal@871 60 ![flyer](https://www.SURFGUITAR101.com/zzz.png)
bgneal@871 61 It's cool.
bgneal@871 62 """
bgneal@871 63 expected = """An image that is already using https:
bgneal@871 64 ![flyer](/zzz.png)
bgneal@871 65 It's cool.
bgneal@871 66 """
bgneal@871 67 result = process_post(test_str)
bgneal@871 68 self.assertEqual(expected, result)
bgneal@871 69
bgneal@871 70 def test_multiple_non_http(self):
bgneal@871 71 test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg)
bgneal@871 72 And another: ![pic](HTTPS://example.com/foo/bar/img.png).
bgneal@871 73 More stuff here."""
bgneal@871 74 expected = """An image: ![image](/img.jpg)
bgneal@871 75 And another: ![pic](HTTPS://example.com/foo/bar/img.png).
bgneal@871 76 More stuff here."""
bgneal@871 77 result = process_post(test_str)
bgneal@871 78 self.assertEqual(expected, result)
bgneal@871 79
bgneal@871 80 def test_https_already_with_title(self):
bgneal@871 81 test_str = """An image that is already using https:
bgneal@871 82 ![flyer](https://example.com/zzz.png "the title")
bgneal@871 83 It's cool.
bgneal@871 84 """
bgneal@871 85 result = process_post(test_str)
bgneal@871 86 self.assertEqual(test_str, result)
bgneal@871 87
bgneal@871 88 def test_sg101_with_title(self):
bgneal@871 89 test_str = """An image on SG101:
bgneal@871 90 ![flyer](http://surfguitar101.com/zzz.png "the title")
bgneal@871 91 It's cool.
bgneal@871 92 """
bgneal@871 93 expected = """An image on SG101:
bgneal@871 94 ![flyer](/zzz.png "the title")
bgneal@871 95 It's cool.
bgneal@871 96 """
bgneal@871 97 result = process_post(test_str)
bgneal@871 98 self.assertEqual(expected, result)
bgneal@871 99
bgneal@871 100 def test_https_sg101_brackets(self):
bgneal@871 101 test_str = """An image that is already using https:
bgneal@871 102 ![flyer](<https://www.SURFGUITAR101.com/zzz.png>)
bgneal@871 103 It's cool.
bgneal@871 104 """
bgneal@871 105 expected = """An image that is already using https:
bgneal@871 106 ![flyer](/zzz.png)
bgneal@871 107 It's cool.
bgneal@871 108 """
bgneal@871 109 result = process_post(test_str)
bgneal@871 110 self.assertEqual(expected, result)
bgneal@871 111
bgneal@871 112 def test_https_already_brackets(self):
bgneal@871 113 test_str = """An image that is already using https:
bgneal@871 114 ![flyer](<https://example.com/zzz.png>)
bgneal@871 115 It's cool.
bgneal@871 116 """
bgneal@871 117 expected = """An image that is already using https:
bgneal@871 118 ![flyer](https://example.com/zzz.png)
bgneal@871 119 It's cool.
bgneal@871 120 """
bgneal@871 121 result = process_post(test_str)
bgneal@871 122 self.assertEqual(expected, result)
bgneal@872 123
bgneal@872 124 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
bgneal@889 125 @mock.patch('core.management.commands.ssl_images.check_https_availability',
bgneal@889 126 new=lambda r: None)
bgneal@872 127 def test_simple_replacement(self, upload_mock):
bgneal@872 128 old_src = 'http://example.com/images/my_image.jpg'
bgneal@873 129 new_src = 'https://cloud.com/ABCDEF.jpg'
bgneal@872 130 test_str = """Here is a really cool http: based image:
bgneal@872 131 ![flyer]({})
bgneal@872 132 Cool, right?""".format(old_src)
bgneal@872 133 expected = """Here is a really cool http: based image:
bgneal@872 134 ![flyer]({})
bgneal@872 135 Cool, right?""".format(new_src)
bgneal@872 136
bgneal@872 137 upload_mock.return_value = new_src
bgneal@872 138 result = process_post(test_str)
bgneal@872 139 self.assertEqual(expected, result)
bgneal@872 140 upload_mock.assert_called_once_with(old_src)
bgneal@873 141
bgneal@873 142 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
bgneal@889 143 @mock.patch('core.management.commands.ssl_images.check_https_availability',
bgneal@889 144 new=lambda r: None)
bgneal@873 145 def test_multiple_replacement(self, upload_mock):
bgneal@873 146 old_src = [
bgneal@873 147 'http://example.com/images/my_image.jpg',
bgneal@873 148 'http://example.com/static/wow.gif',
bgneal@873 149 'http://example.com/media/a/b/c/pic.png',
bgneal@873 150 ]
bgneal@873 151 new_src = [
bgneal@873 152 'https://cloud.com/some/path/012345.jpg',
bgneal@873 153 'https://cloud.com/some/path/6789AB.gif',
bgneal@873 154 'https://cloud.com/some/path/CDEF01.png',
bgneal@873 155 ]
bgneal@873 156
bgneal@873 157 template = """Here is a really cool http: based image:
bgneal@873 158 ![flyer]({})
bgneal@873 159 Cool, right?
bgneal@873 160 Another one: ![pic]({})
bgneal@873 161 And finally
bgneal@873 162 ![an image]({})
bgneal@873 163 """
bgneal@873 164
bgneal@873 165 test_str = template.format(*old_src)
bgneal@873 166 expected = template.format(*new_src)
bgneal@873 167
bgneal@873 168 upload_mock.side_effect = new_src
bgneal@873 169 result = process_post(test_str)
bgneal@873 170 self.assertEqual(expected, result)
bgneal@873 171 expected_args = [mock.call(c) for c in old_src]
bgneal@873 172 self.assertEqual(upload_mock.call_args_list, expected_args)
bgneal@873 173
bgneal@873 174 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
bgneal@889 175 @mock.patch('core.management.commands.ssl_images.check_https_availability',
bgneal@889 176 new=lambda r: None)
bgneal@873 177 def test_multiple_replacement_2(self, upload_mock):
bgneal@873 178 old_src = [
bgneal@873 179 'http://example.com/images/my_image.jpg',
bgneal@873 180 'https://example.com/static/wow.gif',
bgneal@873 181 'http://www.surfguitar101.com/media/a/b/c/pic.png',
bgneal@873 182 'http://surfguitar101.com/media/a/b/c/pic2.png',
bgneal@873 183 ]
bgneal@873 184 new_src = [
bgneal@873 185 'https://cloud.com/some/path/012345.jpg',
bgneal@873 186 'https://example.com/static/wow.gif',
bgneal@873 187 '/media/a/b/c/pic.png',
bgneal@873 188 '/media/a/b/c/pic2.png',
bgneal@873 189 ]
bgneal@873 190
bgneal@873 191 template = """Here is a really cool http: based image:
bgneal@873 192 ![flyer]({})
bgneal@873 193 Cool, right?
bgneal@873 194 Another two: ![pic]({}) ![photo]({})
bgneal@873 195 And finally
bgneal@873 196 ![an image]({}).
bgneal@873 197 """
bgneal@873 198
bgneal@873 199 test_str = template.format(*old_src)
bgneal@873 200 expected = template.format(*new_src)
bgneal@873 201
bgneal@873 202 upload_mock.side_effect = new_src
bgneal@873 203 result = process_post(test_str)
bgneal@873 204 self.assertEqual(expected, result)
bgneal@873 205 upload_mock.assert_called_once_with(old_src[0])
bgneal@889 206
bgneal@889 207 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
bgneal@889 208 @mock.patch('core.management.commands.ssl_images.check_https_availability',
bgneal@889 209 new=lambda r: None)
bgneal@889 210 def test_caching(self, upload_mock):
bgneal@889 211 old_src = [
bgneal@889 212 'http://example.com/images/my_image.jpg',
bgneal@889 213 'http://example.com/static/wow.gif',
bgneal@889 214 'http://example.com/images/my_image.jpg',
bgneal@889 215 ]
bgneal@889 216 new_src = [
bgneal@889 217 'https://cloud.com/some/path/012345.jpg',
bgneal@889 218 'https://cloud.com/some/path/6789AB.gif',
bgneal@889 219 'https://cloud.com/some/path/012345.jpg',
bgneal@889 220 ]
bgneal@889 221
bgneal@889 222 template = """Here is a really cool http: based image:
bgneal@889 223 ![flyer]({})
bgneal@889 224 Cool, right?
bgneal@889 225 Another one: ![pic]({})
bgneal@889 226 And finally
bgneal@889 227 ![an image]({})
bgneal@889 228 """
bgneal@889 229
bgneal@889 230 test_str = template.format(*old_src)
bgneal@889 231 expected = template.format(*new_src)
bgneal@889 232
bgneal@889 233 upload_mock.side_effect = new_src
bgneal@889 234 result = process_post(test_str)
bgneal@889 235 self.assertEqual(expected, result)
bgneal@889 236 expected_args = [mock.call(c) for c in old_src[:2]]
bgneal@889 237 self.assertEqual(upload_mock.call_args_list, expected_args)
bgneal@889 238
bgneal@889 239 @mock.patch('core.management.commands.ssl_images.check_https_availability')
bgneal@889 240 def test_https_availability(self, check_https_mock):
bgneal@889 241 old_src = [
bgneal@889 242 'http://example.com/images/my_image.jpg',
bgneal@889 243 'http://example.com/static/wow.gif',
bgneal@889 244 'http://example.com/images/another_image.jpg',
bgneal@889 245 ]
bgneal@889 246 new_src = [
bgneal@889 247 'https://example.com/images/my_image.jpg',
bgneal@889 248 'https://example.com/static/wow.gif',
bgneal@889 249 'https://example.com/images/another_image.jpg',
bgneal@889 250 ]
bgneal@889 251
bgneal@889 252 template = """Here is a really cool http: based image:
bgneal@889 253 ![flyer]({})
bgneal@889 254 Cool, right?
bgneal@889 255 Another one: ![pic]({})
bgneal@889 256 And finally
bgneal@889 257 ![an image]({})
bgneal@889 258 """
bgneal@889 259
bgneal@889 260 test_str = template.format(*old_src)
bgneal@889 261 expected = template.format(*new_src)
bgneal@889 262
bgneal@889 263 check_https_mock.side_effect = new_src
bgneal@889 264 result = process_post(test_str)
bgneal@889 265 self.assertEqual(expected, result)
bgneal@889 266 expected_args = [mock.call(urlparse(c)) for c in old_src]
bgneal@889 267 self.assertEqual(check_https_mock.call_args_list, expected_args)