annotate core/tests/test_ssl_images.py @ 975:8c3d52b7cbd1

Remove the image button on the MarkItUp! editor.
author Brian Neal <bgneal@gmail.com>
date Mon, 05 Oct 2015 20:06:24 -0500
parents 4619290d171d
children 26de15fb5a80
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@963 7 from django.conf import settings
bgneal@872 8
bgneal@894 9 from core.management.commands.ssl_images import html_check
bgneal@868 10 from core.management.commands.ssl_images import process_post
bgneal@889 11 import core.management.commands.ssl_images
bgneal@868 12
bgneal@868 13
bgneal@868 14 class ProcessPostTestCase(unittest.TestCase):
bgneal@868 15
bgneal@870 16 SG101_RE = re.compile(r'http://(?:www\.)?surfguitar101.com/', re.I)
bgneal@870 17
bgneal@963 18 def setUp(self):
bgneal@963 19 self.assertTrue(len(settings.USER_IMAGES_SOURCES) > 0)
bgneal@963 20 self.safe_host = settings.USER_IMAGES_SOURCES[0]
bgneal@963 21
bgneal@889 22 def tearDown(self):
bgneal@889 23 core.management.commands.ssl_images.url_cache = {}
bgneal@889 24
bgneal@868 25 def test_empty_string(self):
bgneal@868 26 s = process_post('')
bgneal@868 27 self.assertEqual(s, '')
bgneal@870 28
bgneal@870 29 def test_no_matches(self):
bgneal@870 30 test_str = """Here is a post that doesn't contain any image links at
bgneal@870 31 all. It also spans lines.
bgneal@870 32 """
bgneal@870 33 result = process_post(test_str)
bgneal@870 34 self.assertEqual(test_str, result)
bgneal@870 35
bgneal@870 36 def test_sg101_images(self):
bgneal@870 37 test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg)
bgneal@870 38 And another: ![pic](HTTP://SURFGUITAR101.COM/foo/bar/img.png).
bgneal@870 39 More stuff here."""
bgneal@870 40 expected = self.SG101_RE.sub('/', test_str)
bgneal@870 41 result = process_post(test_str)
bgneal@870 42 self.assertNotEqual(test_str, expected)
bgneal@870 43 self.assertEqual(expected, result)
bgneal@870 44
bgneal@871 45 def test_sg101_with_newlines(self):
bgneal@871 46 test_str = """An image: ![image](
bgneal@871 47 http://surfguitar101.com/media/zzz.jpg
bgneal@871 48 )
bgneal@871 49 with trailing text."""
bgneal@871 50 expected = """An image: ![image](/media/zzz.jpg)
bgneal@871 51 with trailing text."""
bgneal@871 52 result = process_post(test_str)
bgneal@871 53 self.assertNotEqual(test_str, expected)
bgneal@871 54 self.assertEqual(expected, result)
bgneal@871 55
bgneal@870 56 def test_https_already(self):
bgneal@871 57 test_str = """An image that is already using https:
bgneal@963 58 ![flyer](https://{}/zzz.png)
bgneal@871 59 It's cool.
bgneal@963 60 """.format(self.safe_host)
bgneal@871 61 result = process_post(test_str)
bgneal@871 62 self.assertEqual(test_str, result)
bgneal@871 63
bgneal@871 64 def test_https_sg101(self):
bgneal@871 65 test_str = """An image that is already using https:
bgneal@871 66 ![flyer](https://www.SURFGUITAR101.com/zzz.png)
bgneal@871 67 It's cool.
bgneal@871 68 """
bgneal@871 69 expected = """An image that is already using https:
bgneal@871 70 ![flyer](/zzz.png)
bgneal@871 71 It's cool.
bgneal@871 72 """
bgneal@871 73 result = process_post(test_str)
bgneal@871 74 self.assertEqual(expected, result)
bgneal@871 75
bgneal@871 76 def test_multiple_non_http(self):
bgneal@871 77 test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg)
bgneal@963 78 And another: ![pic](HTTPS://{}/foo/bar/img.png).
bgneal@963 79 More stuff here.""".format(self.safe_host)
bgneal@871 80 expected = """An image: ![image](/img.jpg)
bgneal@963 81 And another: ![pic](HTTPS://{}/foo/bar/img.png).
bgneal@963 82 More stuff here.""".format(self.safe_host)
bgneal@871 83 result = process_post(test_str)
bgneal@871 84 self.assertEqual(expected, result)
bgneal@871 85
bgneal@871 86 def test_https_already_with_title(self):
bgneal@871 87 test_str = """An image that is already using https:
bgneal@963 88 ![flyer](https://{}/zzz.png "the title")
bgneal@871 89 It's cool.
bgneal@963 90 """.format(self.safe_host)
bgneal@871 91 result = process_post(test_str)
bgneal@871 92 self.assertEqual(test_str, result)
bgneal@871 93
bgneal@871 94 def test_sg101_with_title(self):
bgneal@871 95 test_str = """An image on SG101:
bgneal@871 96 ![flyer](http://surfguitar101.com/zzz.png "the title")
bgneal@871 97 It's cool.
bgneal@871 98 """
bgneal@871 99 expected = """An image on SG101:
bgneal@871 100 ![flyer](/zzz.png "the title")
bgneal@871 101 It's cool.
bgneal@871 102 """
bgneal@871 103 result = process_post(test_str)
bgneal@871 104 self.assertEqual(expected, result)
bgneal@871 105
bgneal@871 106 def test_https_sg101_brackets(self):
bgneal@871 107 test_str = """An image that is already using https:
bgneal@871 108 ![flyer](<https://www.SURFGUITAR101.com/zzz.png>)
bgneal@871 109 It's cool.
bgneal@871 110 """
bgneal@871 111 expected = """An image that is already using https:
bgneal@871 112 ![flyer](/zzz.png)
bgneal@871 113 It's cool.
bgneal@871 114 """
bgneal@871 115 result = process_post(test_str)
bgneal@871 116 self.assertEqual(expected, result)
bgneal@871 117
bgneal@871 118 def test_https_already_brackets(self):
bgneal@871 119 test_str = """An image that is already using https:
bgneal@963 120 ![flyer](<https://{}/zzz.png>)
bgneal@871 121 It's cool.
bgneal@963 122 """.format(self.safe_host)
bgneal@871 123 expected = """An image that is already using https:
bgneal@963 124 ![flyer](https://{}/zzz.png)
bgneal@871 125 It's cool.
bgneal@963 126 """.format(self.safe_host)
bgneal@871 127 result = process_post(test_str)
bgneal@871 128 self.assertEqual(expected, result)
bgneal@872 129
bgneal@872 130 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
bgneal@872 131 def test_simple_replacement(self, upload_mock):
bgneal@872 132 old_src = 'http://example.com/images/my_image.jpg'
bgneal@873 133 new_src = 'https://cloud.com/ABCDEF.jpg'
bgneal@872 134 test_str = """Here is a really cool http: based image:
bgneal@872 135 ![flyer]({})
bgneal@872 136 Cool, right?""".format(old_src)
bgneal@872 137 expected = """Here is a really cool http: based image:
bgneal@872 138 ![flyer]({})
bgneal@872 139 Cool, right?""".format(new_src)
bgneal@872 140
bgneal@872 141 upload_mock.return_value = new_src
bgneal@872 142 result = process_post(test_str)
bgneal@872 143 self.assertEqual(expected, result)
bgneal@927 144 upload_mock.assert_called_once_with(urlparse(old_src))
bgneal@873 145
bgneal@873 146 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
bgneal@873 147 def test_multiple_replacement(self, upload_mock):
bgneal@873 148 old_src = [
bgneal@873 149 'http://example.com/images/my_image.jpg',
bgneal@873 150 'http://example.com/static/wow.gif',
bgneal@873 151 'http://example.com/media/a/b/c/pic.png',
bgneal@873 152 ]
bgneal@873 153 new_src = [
bgneal@873 154 'https://cloud.com/some/path/012345.jpg',
bgneal@873 155 'https://cloud.com/some/path/6789AB.gif',
bgneal@873 156 'https://cloud.com/some/path/CDEF01.png',
bgneal@873 157 ]
bgneal@873 158
bgneal@873 159 template = """Here is a really cool http: based image:
bgneal@873 160 ![flyer]({})
bgneal@873 161 Cool, right?
bgneal@873 162 Another one: ![pic]({})
bgneal@873 163 And finally
bgneal@873 164 ![an image]({})
bgneal@873 165 """
bgneal@873 166
bgneal@873 167 test_str = template.format(*old_src)
bgneal@873 168 expected = template.format(*new_src)
bgneal@873 169
bgneal@873 170 upload_mock.side_effect = new_src
bgneal@873 171 result = process_post(test_str)
bgneal@873 172 self.assertEqual(expected, result)
bgneal@927 173 expected_args = [mock.call(urlparse(c)) for c in old_src]
bgneal@873 174 self.assertEqual(upload_mock.call_args_list, expected_args)
bgneal@873 175
bgneal@873 176 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
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@963 180 'https://{}/static/wow.gif'.format(self.safe_host),
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@963 186 'https://{}/static/wow.gif'.format(self.safe_host),
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@927 205 upload_mock.assert_called_once_with(urlparse(old_src[0]))
bgneal@889 206
bgneal@889 207 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
bgneal@889 208 def test_caching(self, upload_mock):
bgneal@889 209 old_src = [
bgneal@889 210 'http://example.com/images/my_image.jpg',
bgneal@889 211 'http://example.com/static/wow.gif',
bgneal@889 212 'http://example.com/images/my_image.jpg',
bgneal@889 213 ]
bgneal@889 214 new_src = [
bgneal@889 215 'https://cloud.com/some/path/012345.jpg',
bgneal@889 216 'https://cloud.com/some/path/6789AB.gif',
bgneal@889 217 'https://cloud.com/some/path/012345.jpg',
bgneal@889 218 ]
bgneal@889 219
bgneal@889 220 template = """Here is a really cool http: based image:
bgneal@889 221 ![flyer]({})
bgneal@889 222 Cool, right?
bgneal@889 223 Another one: ![pic]({})
bgneal@889 224 And finally
bgneal@889 225 ![an image]({})
bgneal@889 226 """
bgneal@889 227
bgneal@889 228 test_str = template.format(*old_src)
bgneal@889 229 expected = template.format(*new_src)
bgneal@889 230
bgneal@889 231 upload_mock.side_effect = new_src
bgneal@889 232 result = process_post(test_str)
bgneal@889 233 self.assertEqual(expected, result)
bgneal@927 234 expected_args = [mock.call(urlparse(c)) for c in old_src[:2]]
bgneal@889 235 self.assertEqual(upload_mock.call_args_list, expected_args)
bgneal@889 236
bgneal@894 237
bgneal@894 238 class HtmlCheckTestCase(unittest.TestCase):
bgneal@894 239
bgneal@894 240 def test_empty(self):
bgneal@894 241 self.assertFalse(html_check(''))
bgneal@894 242
bgneal@894 243 def test_no_images(self):
bgneal@894 244 self.assertFalse(html_check('<p>Hi there!</p>'))
bgneal@894 245 self.assertFalse(html_check('<p>Hi <b>there</b>!</p>'))
bgneal@894 246
bgneal@894 247 def test_safe_image(self):
bgneal@894 248 self.assertFalse(html_check('<img src="https://a.jpg" />'))
bgneal@894 249 self.assertFalse(html_check('<img src="" alt="stuff" />'))
bgneal@894 250 self.assertFalse(html_check('<img src="HTTPS://a.jpg" />'))
bgneal@894 251 self.assertFalse(html_check("""
bgneal@894 252 <div>
bgneal@894 253 <p>Look: <img src="https://a.jpg" alt="a" /></p>
bgneal@894 254 <p>Look again: <img src="https://b.jpg" alt="b" /></p>
bgneal@894 255 </div>
bgneal@894 256 """))
bgneal@894 257
bgneal@894 258 def test_one_image(self):
bgneal@894 259 self.assertTrue(html_check('<img src="http://a.jpg" alt="a" />'))
bgneal@894 260 self.assertTrue(html_check(
bgneal@894 261 '<p>Look: <img src="http://a.jpg" alt="a" /></p>'))
bgneal@894 262
bgneal@894 263 def test_two_images(self):
bgneal@894 264 self.assertTrue(html_check("""
bgneal@894 265 <p>Look: <img src="https://a.jpg" alt="a" /></p>
bgneal@894 266 <p>Look again: <img src="http://b.jpg" alt="b" /></p>
bgneal@894 267 """))
bgneal@894 268 self.assertTrue(html_check("""
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 """))
bgneal@894 272 self.assertTrue(html_check("""
bgneal@894 273 <div>
bgneal@894 274 <p>Look: <img src="http://a.jpg" alt="a" /></p>
bgneal@894 275 <p>Look again: <img src="http://b.jpg" alt="b" /></p>
bgneal@894 276 </div>
bgneal@894 277 """))
bgneal@894 278 self.assertTrue(html_check("""
bgneal@894 279 <div>
bgneal@894 280 <p>Look: <img src="http://a.jpg" alt="a" /></p>
bgneal@894 281 <p>Look again: <img src="https://b.jpg" alt="b" /></p>
bgneal@894 282 </div>
bgneal@894 283 """))