changeset 871:6900040df0f8

More WIP on ssl_images command & unit test.
author Brian Neal <bgneal@gmail.com>
date Tue, 16 Dec 2014 20:59:49 -0600
parents ee56c8c8cf98
children 1bd9dadcd4d9
files core/management/commands/ssl_images.py core/tests/test_ssl_images.py
diffstat 2 files changed, 93 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/core/management/commands/ssl_images.py	Mon Dec 15 21:16:47 2014 -0600
+++ b/core/management/commands/ssl_images.py	Tue Dec 16 20:59:49 2014 -0600
@@ -24,8 +24,10 @@
 LOGFILE = os.path.join(settings.PROJECT_PATH, 'logs', 'ssl_images.log')
 logger = logging.getLogger(__name__)
 
-IMAGE_LINK_RE = re.compile(markdown.inlinepatterns.IMAGE_LINK_RE, re.UNICODE)
-IMAGE_REF_RE = re.compile(markdown.inlinepatterns.IMAGE_REFERENCE_RE, re.UNICODE)
+IMAGE_LINK_RE = re.compile(markdown.inlinepatterns.IMAGE_LINK_RE,
+                           re.DOTALL | re.UNICODE)
+IMAGE_REF_RE = re.compile(markdown.inlinepatterns.IMAGE_REFERENCE_RE,
+                          re.DOTALL | re.UNICODE)
 
 SG101_HOSTS = set(['www.surfguitar101.com', 'surfguitar101.com'])
 MODEL_CHOICES = ['comments', 'posts']
@@ -67,20 +69,19 @@
         title = " ".join(src_parts[1:])
     alt = match.group(1)
 
-    new_src = ''
+    new_src = None
     if src:
         r = urlparse.urlparse(src)
-        if r.scheme == 'http':
-            if r.hostname in SG101_HOSTS:
-                new_src = r.path    # convert to relative path
-            else:
-                new_src = save_image_to_cloud(src)
+        if r.hostname in SG101_HOSTS:
+            new_src = r.path        # convert to relative path
+        elif r.scheme == 'http':
+            new_src = save_image_to_cloud(src)
         elif r.scheme == 'https':
             new_src = src       # already https, accept it as-is
 
     if new_src:
         if title:
-            s = u'![{alt}]({src} "{title}")'.format(alt=alt, src=new_src, title=title)
+            s = u'![{alt}]({src} {title})'.format(alt=alt, src=new_src, title=title)
         else:
             s = u'![{alt}]({src})'.format(alt=alt, src=new_src)
     else:
--- a/core/tests/test_ssl_images.py	Mon Dec 15 21:16:47 2014 -0600
+++ b/core/tests/test_ssl_images.py	Tue Dec 16 20:59:49 2014 -0600
@@ -29,5 +29,87 @@
         self.assertNotEqual(test_str, expected)
         self.assertEqual(expected, result)
 
+    def test_sg101_with_newlines(self):
+        test_str = """An image: ![image](
+http://surfguitar101.com/media/zzz.jpg
+)
+    with trailing text."""
+        expected = """An image: ![image](/media/zzz.jpg)
+    with trailing text."""
+        result = process_post(test_str)
+        self.assertNotEqual(test_str, expected)
+        self.assertEqual(expected, result)
+
     def test_https_already(self):
-        pass
+        test_str = """An image that is already using https:
+            ![flyer](https://example.com/zzz.png)
+            It's cool.
+            """
+        result = process_post(test_str)
+        self.assertEqual(test_str, result)
+
+    def test_https_sg101(self):
+        test_str = """An image that is already using https:
+            ![flyer](https://www.SURFGUITAR101.com/zzz.png)
+            It's cool.
+            """
+        expected = """An image that is already using https:
+            ![flyer](/zzz.png)
+            It's cool.
+            """
+        result = process_post(test_str)
+        self.assertEqual(expected, result)
+
+    def test_multiple_non_http(self):
+        test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg)
+        And another: ![pic](HTTPS://example.com/foo/bar/img.png).
+        More stuff here."""
+        expected = """An image: ![image](/img.jpg)
+        And another: ![pic](HTTPS://example.com/foo/bar/img.png).
+        More stuff here."""
+        result = process_post(test_str)
+        self.assertEqual(expected, result)
+
+    def test_https_already_with_title(self):
+        test_str = """An image that is already using https:
+            ![flyer](https://example.com/zzz.png "the title")
+            It's cool.
+            """
+        result = process_post(test_str)
+        self.assertEqual(test_str, result)
+
+    def test_sg101_with_title(self):
+        test_str = """An image on SG101:
+            ![flyer](http://surfguitar101.com/zzz.png "the title")
+            It's cool.
+            """
+        expected = """An image on SG101:
+            ![flyer](/zzz.png "the title")
+            It's cool.
+            """
+        result = process_post(test_str)
+        self.assertEqual(expected, result)
+
+    def test_https_sg101_brackets(self):
+        test_str = """An image that is already using https:
+            ![flyer](<https://www.SURFGUITAR101.com/zzz.png>)
+            It's cool.
+            """
+        expected = """An image that is already using https:
+            ![flyer](/zzz.png)
+            It's cool.
+            """
+        result = process_post(test_str)
+        self.assertEqual(expected, result)
+
+    def test_https_already_brackets(self):
+        test_str = """An image that is already using https:
+            ![flyer](<https://example.com/zzz.png>)
+            It's cool.
+            """
+        expected = """An image that is already using https:
+            ![flyer](https://example.com/zzz.png)
+            It's cool.
+            """
+        result = process_post(test_str)
+        self.assertEqual(expected, result)