annotate core/tests/test_ssl_images.py @ 968:23990469c5bf

New NSSR Spanish Top 61 banner and link.
author Brian Neal <bgneal@gmail.com>
date Mon, 07 Sep 2015 13:07:52 -0500
parents 37e75385e931
children 4619290d171d
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@872 126 def test_simple_replacement(self, upload_mock):
bgneal@872 127 old_src = 'http://example.com/images/my_image.jpg'
bgneal@873 128 new_src = 'https://cloud.com/ABCDEF.jpg'
bgneal@872 129 test_str = """Here is a really cool http: based image:
bgneal@872 130 ![flyer]({})
bgneal@872 131 Cool, right?""".format(old_src)
bgneal@872 132 expected = """Here is a really cool http: based image:
bgneal@872 133 ![flyer]({})
bgneal@872 134 Cool, right?""".format(new_src)
bgneal@872 135
bgneal@872 136 upload_mock.return_value = new_src
bgneal@872 137 result = process_post(test_str)
bgneal@872 138 self.assertEqual(expected, result)
bgneal@927 139 upload_mock.assert_called_once_with(urlparse(old_src))
bgneal@873 140
bgneal@873 141 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
bgneal@873 142 def test_multiple_replacement(self, upload_mock):
bgneal@873 143 old_src = [
bgneal@873 144 'http://example.com/images/my_image.jpg',
bgneal@873 145 'http://example.com/static/wow.gif',
bgneal@873 146 'http://example.com/media/a/b/c/pic.png',
bgneal@873 147 ]
bgneal@873 148 new_src = [
bgneal@873 149 'https://cloud.com/some/path/012345.jpg',
bgneal@873 150 'https://cloud.com/some/path/6789AB.gif',
bgneal@873 151 'https://cloud.com/some/path/CDEF01.png',
bgneal@873 152 ]
bgneal@873 153
bgneal@873 154 template = """Here is a really cool http: based image:
bgneal@873 155 ![flyer]({})
bgneal@873 156 Cool, right?
bgneal@873 157 Another one: ![pic]({})
bgneal@873 158 And finally
bgneal@873 159 ![an image]({})
bgneal@873 160 """
bgneal@873 161
bgneal@873 162 test_str = template.format(*old_src)
bgneal@873 163 expected = template.format(*new_src)
bgneal@873 164
bgneal@873 165 upload_mock.side_effect = new_src
bgneal@873 166 result = process_post(test_str)
bgneal@873 167 self.assertEqual(expected, result)
bgneal@927 168 expected_args = [mock.call(urlparse(c)) for c in old_src]
bgneal@873 169 self.assertEqual(upload_mock.call_args_list, expected_args)
bgneal@873 170
bgneal@873 171 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
bgneal@873 172 def test_multiple_replacement_2(self, upload_mock):
bgneal@873 173 old_src = [
bgneal@873 174 'http://example.com/images/my_image.jpg',
bgneal@873 175 'https://example.com/static/wow.gif',
bgneal@873 176 'http://www.surfguitar101.com/media/a/b/c/pic.png',
bgneal@873 177 'http://surfguitar101.com/media/a/b/c/pic2.png',
bgneal@873 178 ]
bgneal@873 179 new_src = [
bgneal@873 180 'https://cloud.com/some/path/012345.jpg',
bgneal@873 181 'https://example.com/static/wow.gif',
bgneal@873 182 '/media/a/b/c/pic.png',
bgneal@873 183 '/media/a/b/c/pic2.png',
bgneal@873 184 ]
bgneal@873 185
bgneal@873 186 template = """Here is a really cool http: based image:
bgneal@873 187 ![flyer]({})
bgneal@873 188 Cool, right?
bgneal@873 189 Another two: ![pic]({}) ![photo]({})
bgneal@873 190 And finally
bgneal@873 191 ![an image]({}).
bgneal@873 192 """
bgneal@873 193
bgneal@873 194 test_str = template.format(*old_src)
bgneal@873 195 expected = template.format(*new_src)
bgneal@873 196
bgneal@873 197 upload_mock.side_effect = new_src
bgneal@873 198 result = process_post(test_str)
bgneal@873 199 self.assertEqual(expected, result)
bgneal@927 200 upload_mock.assert_called_once_with(urlparse(old_src[0]))
bgneal@889 201
bgneal@889 202 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
bgneal@889 203 def test_caching(self, upload_mock):
bgneal@889 204 old_src = [
bgneal@889 205 'http://example.com/images/my_image.jpg',
bgneal@889 206 'http://example.com/static/wow.gif',
bgneal@889 207 'http://example.com/images/my_image.jpg',
bgneal@889 208 ]
bgneal@889 209 new_src = [
bgneal@889 210 'https://cloud.com/some/path/012345.jpg',
bgneal@889 211 'https://cloud.com/some/path/6789AB.gif',
bgneal@889 212 'https://cloud.com/some/path/012345.jpg',
bgneal@889 213 ]
bgneal@889 214
bgneal@889 215 template = """Here is a really cool http: based image:
bgneal@889 216 ![flyer]({})
bgneal@889 217 Cool, right?
bgneal@889 218 Another one: ![pic]({})
bgneal@889 219 And finally
bgneal@889 220 ![an image]({})
bgneal@889 221 """
bgneal@889 222
bgneal@889 223 test_str = template.format(*old_src)
bgneal@889 224 expected = template.format(*new_src)
bgneal@889 225
bgneal@889 226 upload_mock.side_effect = new_src
bgneal@889 227 result = process_post(test_str)
bgneal@889 228 self.assertEqual(expected, result)
bgneal@927 229 expected_args = [mock.call(urlparse(c)) for c in old_src[:2]]
bgneal@889 230 self.assertEqual(upload_mock.call_args_list, expected_args)
bgneal@889 231
bgneal@894 232
bgneal@894 233 class HtmlCheckTestCase(unittest.TestCase):
bgneal@894 234
bgneal@894 235 def test_empty(self):
bgneal@894 236 self.assertFalse(html_check(''))
bgneal@894 237
bgneal@894 238 def test_no_images(self):
bgneal@894 239 self.assertFalse(html_check('<p>Hi there!</p>'))
bgneal@894 240 self.assertFalse(html_check('<p>Hi <b>there</b>!</p>'))
bgneal@894 241
bgneal@894 242 def test_safe_image(self):
bgneal@894 243 self.assertFalse(html_check('<img src="https://a.jpg" />'))
bgneal@894 244 self.assertFalse(html_check('<img src="" alt="stuff" />'))
bgneal@894 245 self.assertFalse(html_check('<img src="HTTPS://a.jpg" />'))
bgneal@894 246 self.assertFalse(html_check("""
bgneal@894 247 <div>
bgneal@894 248 <p>Look: <img src="https://a.jpg" alt="a" /></p>
bgneal@894 249 <p>Look again: <img src="https://b.jpg" alt="b" /></p>
bgneal@894 250 </div>
bgneal@894 251 """))
bgneal@894 252
bgneal@894 253 def test_one_image(self):
bgneal@894 254 self.assertTrue(html_check('<img src="http://a.jpg" alt="a" />'))
bgneal@894 255 self.assertTrue(html_check(
bgneal@894 256 '<p>Look: <img src="http://a.jpg" alt="a" /></p>'))
bgneal@894 257
bgneal@894 258 def test_two_images(self):
bgneal@894 259 self.assertTrue(html_check("""
bgneal@894 260 <p>Look: <img src="https://a.jpg" alt="a" /></p>
bgneal@894 261 <p>Look again: <img src="http://b.jpg" alt="b" /></p>
bgneal@894 262 """))
bgneal@894 263 self.assertTrue(html_check("""
bgneal@894 264 <p>Look: <img src="http://a.jpg" alt="a" /></p>
bgneal@894 265 <p>Look again: <img src="http://b.jpg" alt="b" /></p>
bgneal@894 266 """))
bgneal@894 267 self.assertTrue(html_check("""
bgneal@894 268 <div>
bgneal@894 269 <p>Look: <img src="http://a.jpg" alt="a" /></p>
bgneal@894 270 <p>Look again: <img src="http://b.jpg" alt="b" /></p>
bgneal@894 271 </div>
bgneal@894 272 """))
bgneal@894 273 self.assertTrue(html_check("""
bgneal@894 274 <div>
bgneal@894 275 <p>Look: <img src="http://a.jpg" alt="a" /></p>
bgneal@894 276 <p>Look again: <img src="https://b.jpg" alt="b" /></p>
bgneal@894 277 </div>
bgneal@894 278 """))