annotate core/tests/test_ssl_images.py @ 917:0365fdbb4d78

Fix app conflict with messages. Django's messages app label conflicts with our messages app. We can't easily rename our label as that will make us rename database tables. Since our app came first we'll just customize Django messages label. For Django 1.7.7 upgrade.
author Brian Neal <bgneal@gmail.com>
date Mon, 06 Apr 2015 20:02:25 -0500
parents 101728976f9c
children 37e75385e931
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@894 8 from core.management.commands.ssl_images import html_check
bgneal@868 9 from core.management.commands.ssl_images import process_post
bgneal@889 10 import core.management.commands.ssl_images
bgneal@868 11
bgneal@868 12
bgneal@868 13 class ProcessPostTestCase(unittest.TestCase):
bgneal@868 14
bgneal@870 15 SG101_RE = re.compile(r'http://(?:www\.)?surfguitar101.com/', re.I)
bgneal@870 16
bgneal@889 17 def tearDown(self):
bgneal@889 18 core.management.commands.ssl_images.url_cache = {}
bgneal@889 19
bgneal@868 20 def test_empty_string(self):
bgneal@868 21 s = process_post('')
bgneal@868 22 self.assertEqual(s, '')
bgneal@870 23
bgneal@870 24 def test_no_matches(self):
bgneal@870 25 test_str = """Here is a post that doesn't contain any image links at
bgneal@870 26 all. It also spans lines.
bgneal@870 27 """
bgneal@870 28 result = process_post(test_str)
bgneal@870 29 self.assertEqual(test_str, result)
bgneal@870 30
bgneal@870 31 def test_sg101_images(self):
bgneal@870 32 test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg)
bgneal@870 33 And another: ![pic](HTTP://SURFGUITAR101.COM/foo/bar/img.png).
bgneal@870 34 More stuff here."""
bgneal@870 35 expected = self.SG101_RE.sub('/', test_str)
bgneal@870 36 result = process_post(test_str)
bgneal@870 37 self.assertNotEqual(test_str, expected)
bgneal@870 38 self.assertEqual(expected, result)
bgneal@870 39
bgneal@871 40 def test_sg101_with_newlines(self):
bgneal@871 41 test_str = """An image: ![image](
bgneal@871 42 http://surfguitar101.com/media/zzz.jpg
bgneal@871 43 )
bgneal@871 44 with trailing text."""
bgneal@871 45 expected = """An image: ![image](/media/zzz.jpg)
bgneal@871 46 with trailing text."""
bgneal@871 47 result = process_post(test_str)
bgneal@871 48 self.assertNotEqual(test_str, expected)
bgneal@871 49 self.assertEqual(expected, result)
bgneal@871 50
bgneal@870 51 def test_https_already(self):
bgneal@871 52 test_str = """An image that is already using https:
bgneal@871 53 ![flyer](https://example.com/zzz.png)
bgneal@871 54 It's cool.
bgneal@871 55 """
bgneal@871 56 result = process_post(test_str)
bgneal@871 57 self.assertEqual(test_str, result)
bgneal@871 58
bgneal@871 59 def test_https_sg101(self):
bgneal@871 60 test_str = """An image that is already using https:
bgneal@871 61 ![flyer](https://www.SURFGUITAR101.com/zzz.png)
bgneal@871 62 It's cool.
bgneal@871 63 """
bgneal@871 64 expected = """An image that is already using https:
bgneal@871 65 ![flyer](/zzz.png)
bgneal@871 66 It's cool.
bgneal@871 67 """
bgneal@871 68 result = process_post(test_str)
bgneal@871 69 self.assertEqual(expected, result)
bgneal@871 70
bgneal@871 71 def test_multiple_non_http(self):
bgneal@871 72 test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg)
bgneal@871 73 And another: ![pic](HTTPS://example.com/foo/bar/img.png).
bgneal@871 74 More stuff here."""
bgneal@871 75 expected = """An image: ![image](/img.jpg)
bgneal@871 76 And another: ![pic](HTTPS://example.com/foo/bar/img.png).
bgneal@871 77 More stuff here."""
bgneal@871 78 result = process_post(test_str)
bgneal@871 79 self.assertEqual(expected, result)
bgneal@871 80
bgneal@871 81 def test_https_already_with_title(self):
bgneal@871 82 test_str = """An image that is already using https:
bgneal@871 83 ![flyer](https://example.com/zzz.png "the title")
bgneal@871 84 It's cool.
bgneal@871 85 """
bgneal@871 86 result = process_post(test_str)
bgneal@871 87 self.assertEqual(test_str, result)
bgneal@871 88
bgneal@871 89 def test_sg101_with_title(self):
bgneal@871 90 test_str = """An image on SG101:
bgneal@871 91 ![flyer](http://surfguitar101.com/zzz.png "the title")
bgneal@871 92 It's cool.
bgneal@871 93 """
bgneal@871 94 expected = """An image on SG101:
bgneal@871 95 ![flyer](/zzz.png "the title")
bgneal@871 96 It's cool.
bgneal@871 97 """
bgneal@871 98 result = process_post(test_str)
bgneal@871 99 self.assertEqual(expected, result)
bgneal@871 100
bgneal@871 101 def test_https_sg101_brackets(self):
bgneal@871 102 test_str = """An image that is already using https:
bgneal@871 103 ![flyer](<https://www.SURFGUITAR101.com/zzz.png>)
bgneal@871 104 It's cool.
bgneal@871 105 """
bgneal@871 106 expected = """An image that is already using https:
bgneal@871 107 ![flyer](/zzz.png)
bgneal@871 108 It's cool.
bgneal@871 109 """
bgneal@871 110 result = process_post(test_str)
bgneal@871 111 self.assertEqual(expected, result)
bgneal@871 112
bgneal@871 113 def test_https_already_brackets(self):
bgneal@871 114 test_str = """An image that is already using https:
bgneal@871 115 ![flyer](<https://example.com/zzz.png>)
bgneal@871 116 It's cool.
bgneal@871 117 """
bgneal@871 118 expected = """An image that is already using https:
bgneal@871 119 ![flyer](https://example.com/zzz.png)
bgneal@871 120 It's cool.
bgneal@871 121 """
bgneal@871 122 result = process_post(test_str)
bgneal@871 123 self.assertEqual(expected, result)
bgneal@872 124
bgneal@872 125 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
bgneal@889 126 @mock.patch('core.management.commands.ssl_images.check_https_availability',
bgneal@889 127 new=lambda r: None)
bgneal@872 128 def test_simple_replacement(self, upload_mock):
bgneal@872 129 old_src = 'http://example.com/images/my_image.jpg'
bgneal@873 130 new_src = 'https://cloud.com/ABCDEF.jpg'
bgneal@872 131 test_str = """Here is a really cool http: based image:
bgneal@872 132 ![flyer]({})
bgneal@872 133 Cool, right?""".format(old_src)
bgneal@872 134 expected = """Here is a really cool http: based image:
bgneal@872 135 ![flyer]({})
bgneal@872 136 Cool, right?""".format(new_src)
bgneal@872 137
bgneal@872 138 upload_mock.return_value = new_src
bgneal@872 139 result = process_post(test_str)
bgneal@872 140 self.assertEqual(expected, result)
bgneal@872 141 upload_mock.assert_called_once_with(old_src)
bgneal@873 142
bgneal@873 143 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
bgneal@889 144 @mock.patch('core.management.commands.ssl_images.check_https_availability',
bgneal@889 145 new=lambda r: None)
bgneal@873 146 def test_multiple_replacement(self, upload_mock):
bgneal@873 147 old_src = [
bgneal@873 148 'http://example.com/images/my_image.jpg',
bgneal@873 149 'http://example.com/static/wow.gif',
bgneal@873 150 'http://example.com/media/a/b/c/pic.png',
bgneal@873 151 ]
bgneal@873 152 new_src = [
bgneal@873 153 'https://cloud.com/some/path/012345.jpg',
bgneal@873 154 'https://cloud.com/some/path/6789AB.gif',
bgneal@873 155 'https://cloud.com/some/path/CDEF01.png',
bgneal@873 156 ]
bgneal@873 157
bgneal@873 158 template = """Here is a really cool http: based image:
bgneal@873 159 ![flyer]({})
bgneal@873 160 Cool, right?
bgneal@873 161 Another one: ![pic]({})
bgneal@873 162 And finally
bgneal@873 163 ![an image]({})
bgneal@873 164 """
bgneal@873 165
bgneal@873 166 test_str = template.format(*old_src)
bgneal@873 167 expected = template.format(*new_src)
bgneal@873 168
bgneal@873 169 upload_mock.side_effect = new_src
bgneal@873 170 result = process_post(test_str)
bgneal@873 171 self.assertEqual(expected, result)
bgneal@873 172 expected_args = [mock.call(c) for c in old_src]
bgneal@873 173 self.assertEqual(upload_mock.call_args_list, expected_args)
bgneal@873 174
bgneal@873 175 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
bgneal@889 176 @mock.patch('core.management.commands.ssl_images.check_https_availability',
bgneal@889 177 new=lambda r: None)
bgneal@873 178 def test_multiple_replacement_2(self, upload_mock):
bgneal@873 179 old_src = [
bgneal@873 180 'http://example.com/images/my_image.jpg',
bgneal@873 181 'https://example.com/static/wow.gif',
bgneal@873 182 'http://www.surfguitar101.com/media/a/b/c/pic.png',
bgneal@873 183 'http://surfguitar101.com/media/a/b/c/pic2.png',
bgneal@873 184 ]
bgneal@873 185 new_src = [
bgneal@873 186 'https://cloud.com/some/path/012345.jpg',
bgneal@873 187 'https://example.com/static/wow.gif',
bgneal@873 188 '/media/a/b/c/pic.png',
bgneal@873 189 '/media/a/b/c/pic2.png',
bgneal@873 190 ]
bgneal@873 191
bgneal@873 192 template = """Here is a really cool http: based image:
bgneal@873 193 ![flyer]({})
bgneal@873 194 Cool, right?
bgneal@873 195 Another two: ![pic]({}) ![photo]({})
bgneal@873 196 And finally
bgneal@873 197 ![an image]({}).
bgneal@873 198 """
bgneal@873 199
bgneal@873 200 test_str = template.format(*old_src)
bgneal@873 201 expected = template.format(*new_src)
bgneal@873 202
bgneal@873 203 upload_mock.side_effect = new_src
bgneal@873 204 result = process_post(test_str)
bgneal@873 205 self.assertEqual(expected, result)
bgneal@873 206 upload_mock.assert_called_once_with(old_src[0])
bgneal@889 207
bgneal@889 208 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
bgneal@889 209 @mock.patch('core.management.commands.ssl_images.check_https_availability',
bgneal@889 210 new=lambda r: None)
bgneal@889 211 def test_caching(self, upload_mock):
bgneal@889 212 old_src = [
bgneal@889 213 'http://example.com/images/my_image.jpg',
bgneal@889 214 'http://example.com/static/wow.gif',
bgneal@889 215 'http://example.com/images/my_image.jpg',
bgneal@889 216 ]
bgneal@889 217 new_src = [
bgneal@889 218 'https://cloud.com/some/path/012345.jpg',
bgneal@889 219 'https://cloud.com/some/path/6789AB.gif',
bgneal@889 220 'https://cloud.com/some/path/012345.jpg',
bgneal@889 221 ]
bgneal@889 222
bgneal@889 223 template = """Here is a really cool http: based image:
bgneal@889 224 ![flyer]({})
bgneal@889 225 Cool, right?
bgneal@889 226 Another one: ![pic]({})
bgneal@889 227 And finally
bgneal@889 228 ![an image]({})
bgneal@889 229 """
bgneal@889 230
bgneal@889 231 test_str = template.format(*old_src)
bgneal@889 232 expected = template.format(*new_src)
bgneal@889 233
bgneal@889 234 upload_mock.side_effect = new_src
bgneal@889 235 result = process_post(test_str)
bgneal@889 236 self.assertEqual(expected, result)
bgneal@889 237 expected_args = [mock.call(c) for c in old_src[:2]]
bgneal@889 238 self.assertEqual(upload_mock.call_args_list, expected_args)
bgneal@889 239
bgneal@889 240 @mock.patch('core.management.commands.ssl_images.check_https_availability')
bgneal@889 241 def test_https_availability(self, check_https_mock):
bgneal@889 242 old_src = [
bgneal@889 243 'http://example.com/images/my_image.jpg',
bgneal@889 244 'http://example.com/static/wow.gif',
bgneal@889 245 'http://example.com/images/another_image.jpg',
bgneal@889 246 ]
bgneal@889 247 new_src = [
bgneal@889 248 'https://example.com/images/my_image.jpg',
bgneal@889 249 'https://example.com/static/wow.gif',
bgneal@889 250 'https://example.com/images/another_image.jpg',
bgneal@889 251 ]
bgneal@889 252
bgneal@889 253 template = """Here is a really cool http: based image:
bgneal@889 254 ![flyer]({})
bgneal@889 255 Cool, right?
bgneal@889 256 Another one: ![pic]({})
bgneal@889 257 And finally
bgneal@889 258 ![an image]({})
bgneal@889 259 """
bgneal@889 260
bgneal@889 261 test_str = template.format(*old_src)
bgneal@889 262 expected = template.format(*new_src)
bgneal@889 263
bgneal@889 264 check_https_mock.side_effect = new_src
bgneal@889 265 result = process_post(test_str)
bgneal@889 266 self.assertEqual(expected, result)
bgneal@889 267 expected_args = [mock.call(urlparse(c)) for c in old_src]
bgneal@889 268 self.assertEqual(check_https_mock.call_args_list, expected_args)
bgneal@894 269
bgneal@894 270
bgneal@894 271 class HtmlCheckTestCase(unittest.TestCase):
bgneal@894 272
bgneal@894 273 def test_empty(self):
bgneal@894 274 self.assertFalse(html_check(''))
bgneal@894 275
bgneal@894 276 def test_no_images(self):
bgneal@894 277 self.assertFalse(html_check('<p>Hi there!</p>'))
bgneal@894 278 self.assertFalse(html_check('<p>Hi <b>there</b>!</p>'))
bgneal@894 279
bgneal@894 280 def test_safe_image(self):
bgneal@894 281 self.assertFalse(html_check('<img src="https://a.jpg" />'))
bgneal@894 282 self.assertFalse(html_check('<img src="" alt="stuff" />'))
bgneal@894 283 self.assertFalse(html_check('<img src="HTTPS://a.jpg" />'))
bgneal@894 284 self.assertFalse(html_check("""
bgneal@894 285 <div>
bgneal@894 286 <p>Look: <img src="https://a.jpg" alt="a" /></p>
bgneal@894 287 <p>Look again: <img src="https://b.jpg" alt="b" /></p>
bgneal@894 288 </div>
bgneal@894 289 """))
bgneal@894 290
bgneal@894 291 def test_one_image(self):
bgneal@894 292 self.assertTrue(html_check('<img src="http://a.jpg" alt="a" />'))
bgneal@894 293 self.assertTrue(html_check(
bgneal@894 294 '<p>Look: <img src="http://a.jpg" alt="a" /></p>'))
bgneal@894 295
bgneal@894 296 def test_two_images(self):
bgneal@894 297 self.assertTrue(html_check("""
bgneal@894 298 <p>Look: <img src="https://a.jpg" alt="a" /></p>
bgneal@894 299 <p>Look again: <img src="http://b.jpg" alt="b" /></p>
bgneal@894 300 """))
bgneal@894 301 self.assertTrue(html_check("""
bgneal@894 302 <p>Look: <img src="http://a.jpg" alt="a" /></p>
bgneal@894 303 <p>Look again: <img src="http://b.jpg" alt="b" /></p>
bgneal@894 304 """))
bgneal@894 305 self.assertTrue(html_check("""
bgneal@894 306 <div>
bgneal@894 307 <p>Look: <img src="http://a.jpg" alt="a" /></p>
bgneal@894 308 <p>Look again: <img src="http://b.jpg" alt="b" /></p>
bgneal@894 309 </div>
bgneal@894 310 """))
bgneal@894 311 self.assertTrue(html_check("""
bgneal@894 312 <div>
bgneal@894 313 <p>Look: <img src="http://a.jpg" alt="a" /></p>
bgneal@894 314 <p>Look again: <img src="https://b.jpg" alt="b" /></p>
bgneal@894 315 </div>
bgneal@894 316 """))