comparison core/tests/test_ssl_images.py @ 987:76525f5ac2b1

Modify ssl_images to update news models.
author Brian Neal <bgneal@gmail.com>
date Wed, 28 Oct 2015 21:06:13 -0500
parents 26de15fb5a80
children
comparison
equal deleted inserted replaced
986:26de15fb5a80 987:76525f5ac2b1
5 5
6 import mock 6 import mock
7 from django.conf import settings 7 from django.conf import settings
8 8
9 from core.management.commands.ssl_images import html_check 9 from core.management.commands.ssl_images import html_check
10 from core.management.commands.ssl_images import process_post 10 from core.management.commands.ssl_images import process_post, process_html
11 import core.management.commands.ssl_images 11 import core.management.commands.ssl_images
12 12
13 13
14 class ProcessPostTestCase(unittest.TestCase): 14 class ProcessPostTestCase(unittest.TestCase):
15 15
288 <div> 288 <div>
289 <p>Look: <img src="http://a.jpg" alt="a" /></p> 289 <p>Look: <img src="http://a.jpg" alt="a" /></p>
290 <p>Look again: <img src="https://b.jpg" alt="b" /></p> 290 <p>Look again: <img src="https://b.jpg" alt="b" /></p>
291 </div> 291 </div>
292 """)) 292 """))
293
294
295 class ProcessHtmlTestCase(unittest.TestCase):
296
297 SG101_RE = re.compile(r'http://(?:www\.)?surfguitar101.com/', re.I)
298
299 def setUp(self):
300 self.assertTrue(len(settings.USER_IMAGES_SOURCES) > 0)
301 self.safe_host = settings.USER_IMAGES_SOURCES[0]
302
303 def tearDown(self):
304 core.management.commands.ssl_images.url_cache = {}
305
306 def test_empty_string(self):
307 s = process_html('')
308 self.assertEqual(s, '')
309
310 def test_whitespace_string(self):
311 s = process_html('\r\n\r\n')
312 self.assertEqual(s, '')
313
314 def test_no_matches(self):
315 test_str = """<p>Here is a post that doesn't contain any image links at
316 all. It also spans lines.</p>
317 """
318 result = process_html(test_str)
319 self.assertEqual(test_str, result)
320
321 def test_multiple_paragraphs(self):
322 test_str = """<p>Here is a post that doesn't contain any image links at
323 all. It also spans lines.</p>
324 """
325 test_str += test_str
326 result = process_html(test_str)
327 self.assertEqual(test_str, result)
328
329 def test_sg101_images(self):
330 test_str = """<p>An image:
331 <img src="http://www.surfguitar101.com/img.jpg" alt="image">
332 And another: <img src="HTTP://SURFGUITAR101.COM/foo/bar/img.png" alt="pic">
333 More stuff here.</p>"""
334 expected = self.SG101_RE.sub('/', test_str)
335 result = process_html(test_str)
336 self.assertNotEqual(test_str, expected)
337 self.assertEqual(expected, result)
338
339 def test_https_already(self):
340 test_str = """<p>An image that is already using https:
341 <img src="https://{}/zzz.png" alt="pic">
342 It's cool.</p>""".format(self.safe_host)
343 result = process_html(test_str)
344 self.assertEqual(test_str, result)
345
346 def test_https_sg101(self):
347 test_str = """<p>An image that is already using https:
348 <img src="https://www.SURFGUITAR101.com/zzz.png" alt="pic">
349 It's cool.</p>
350 """
351 expected = """<p>An image that is already using https:
352 <img src="/zzz.png" alt="pic">
353 It's cool.</p>"""
354 result = process_html(test_str)
355 self.assertEqual(expected, result)
356
357 def test_multiple_non_http(self):
358 test_str = """<p>An image:
359 <img src="http://www.surfguitar101.com/img.jpg" alt="pic">
360 And another:
361 <img src="HTTPS://{}/foo/bar/img.png" alt="stuff">
362 More stuff here.</p>
363 """.format(self.safe_host)
364 expected = """<p>An image:
365 <img src="/img.jpg" alt="pic">
366 And another:
367 <img src="HTTPS://{}/foo/bar/img.png" alt="stuff">
368 More stuff here.</p>""".format(self.safe_host)
369 result = process_html(test_str)
370 self.assertEqual(expected, result)
371
372 def test_https_already_with_title(self):
373 test_str = """<p>An image that is already using https:
374 <img src="https://{}/zzz.png" alt="1" title="the title">
375 It's cool.</p>
376 """.format(self.safe_host)
377 result = process_html(test_str)
378 self.assertEqual(test_str, result)
379
380 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
381 def test_simple_replacement(self, upload_mock):
382 old_src = 'http://example.com/images/my_image.jpg'
383 new_src = 'https://cloud.com/ABCDEF.jpg'
384 test_str = """<p>Here is a really cool http: based image:
385 <img src="{}" alt="a">
386 Cool, right?</p>""".format(old_src)
387 expected = """<p>Here is a really cool http: based image:
388 <img src="{}" alt="a">
389 Cool, right?</p>""".format(new_src)
390
391 upload_mock.return_value = new_src
392 result = process_html(test_str)
393 self.assertEqual(expected, result)
394 upload_mock.assert_called_once_with(urlparse(old_src))
395
396 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
397 def test_multiple_replacement(self, upload_mock):
398 old_src = [
399 'http://example.com/images/my_image.jpg',
400 'http://example.com/static/wow.gif',
401 'http://example.com/media/a/b/c/pic.png',
402 ]
403 new_src = [
404 'https://cloud.com/some/path/012345.jpg',
405 'https://cloud.com/some/path/6789AB.gif',
406 'https://cloud.com/some/path/CDEF01.png',
407 ]
408
409 template = """<p>Here is a really cool http: based image:
410 <img src="{}" alt="a">
411 Cool, right?
412 Another one: <img src="{}" alt="b">
413 And finally
414 <img src="{}" alt="c">
415 </p>"""
416
417 test_str = template.format(*old_src)
418 expected = template.format(*new_src)
419
420 upload_mock.side_effect = new_src
421 result = process_html(test_str)
422 self.assertEqual(expected, result)
423 expected_args = [mock.call(urlparse(c)) for c in old_src]
424 self.assertEqual(upload_mock.call_args_list, expected_args)
425
426 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
427 def test_multiple_replacement_2(self, upload_mock):
428 old_src = [
429 'http://example.com/images/my_image.jpg',
430 'https://{}/static/wow.gif'.format(self.safe_host),
431 'http://www.surfguitar101.com/media/a/b/c/pic.png',
432 'http://surfguitar101.com/media/a/b/c/pic2.png',
433 ]
434 new_src = [
435 'https://cloud.com/some/path/012345.jpg',
436 'https://{}/static/wow.gif'.format(self.safe_host),
437 '/media/a/b/c/pic.png',
438 '/media/a/b/c/pic2.png',
439 ]
440
441 template = """<p>Here is a really cool http: based image:
442 <img src="{}" alt="a">
443 Cool, right?
444 Another two: <img src="{}" alt="b"><img src="{}" alt="c">
445 And finally
446 <img src="{}" alt="d"></p>"""
447
448 test_str = template.format(*old_src)
449 expected = template.format(*new_src)
450
451 upload_mock.side_effect = new_src
452 result = process_html(test_str)
453 self.assertEqual(expected, result)
454 upload_mock.assert_called_once_with(urlparse(old_src[0]))
455
456 @mock.patch('core.management.commands.ssl_images.convert_to_ssl')
457 def test_change_img_to_a(self, convert_mock):
458 convert_mock.return_value = None
459 test_str = """<p>A bad image:
460 <img src="http://example.com/zzz.png" alt="1" title="the title">
461 It's cool.</p>"""
462
463 result = process_html(test_str)
464
465 expected = """<p>A bad image:
466 <a href="http://example.com/zzz.png">Image</a>
467 It's cool.</p>"""
468 self.assertEqual(result, expected)