bgneal@868
|
1 """Unit tests for the ssl_images management command."""
|
bgneal@870
|
2 import re
|
bgneal@868
|
3 import unittest
|
bgneal@868
|
4
|
bgneal@868
|
5 from core.management.commands.ssl_images import process_post
|
bgneal@868
|
6
|
bgneal@868
|
7
|
bgneal@868
|
8 class ProcessPostTestCase(unittest.TestCase):
|
bgneal@868
|
9
|
bgneal@870
|
10 SG101_RE = re.compile(r'http://(?:www\.)?surfguitar101.com/', re.I)
|
bgneal@870
|
11
|
bgneal@868
|
12 def test_empty_string(self):
|
bgneal@868
|
13 s = process_post('')
|
bgneal@868
|
14 self.assertEqual(s, '')
|
bgneal@870
|
15
|
bgneal@870
|
16 def test_no_matches(self):
|
bgneal@870
|
17 test_str = """Here is a post that doesn't contain any image links at
|
bgneal@870
|
18 all. It also spans lines.
|
bgneal@870
|
19 """
|
bgneal@870
|
20 result = process_post(test_str)
|
bgneal@870
|
21 self.assertEqual(test_str, result)
|
bgneal@870
|
22
|
bgneal@870
|
23 def test_sg101_images(self):
|
bgneal@870
|
24 test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg)
|
bgneal@870
|
25 And another: ![pic](HTTP://SURFGUITAR101.COM/foo/bar/img.png).
|
bgneal@870
|
26 More stuff here."""
|
bgneal@870
|
27 expected = self.SG101_RE.sub('/', test_str)
|
bgneal@870
|
28 result = process_post(test_str)
|
bgneal@870
|
29 self.assertNotEqual(test_str, expected)
|
bgneal@870
|
30 self.assertEqual(expected, result)
|
bgneal@870
|
31
|
bgneal@871
|
32 def test_sg101_with_newlines(self):
|
bgneal@871
|
33 test_str = """An image: ![image](
|
bgneal@871
|
34 http://surfguitar101.com/media/zzz.jpg
|
bgneal@871
|
35 )
|
bgneal@871
|
36 with trailing text."""
|
bgneal@871
|
37 expected = """An image: ![image](/media/zzz.jpg)
|
bgneal@871
|
38 with trailing text."""
|
bgneal@871
|
39 result = process_post(test_str)
|
bgneal@871
|
40 self.assertNotEqual(test_str, expected)
|
bgneal@871
|
41 self.assertEqual(expected, result)
|
bgneal@871
|
42
|
bgneal@870
|
43 def test_https_already(self):
|
bgneal@871
|
44 test_str = """An image that is already using https:
|
bgneal@871
|
45 ![flyer](https://example.com/zzz.png)
|
bgneal@871
|
46 It's cool.
|
bgneal@871
|
47 """
|
bgneal@871
|
48 result = process_post(test_str)
|
bgneal@871
|
49 self.assertEqual(test_str, result)
|
bgneal@871
|
50
|
bgneal@871
|
51 def test_https_sg101(self):
|
bgneal@871
|
52 test_str = """An image that is already using https:
|
bgneal@871
|
53 ![flyer](https://www.SURFGUITAR101.com/zzz.png)
|
bgneal@871
|
54 It's cool.
|
bgneal@871
|
55 """
|
bgneal@871
|
56 expected = """An image that is already using https:
|
bgneal@871
|
57 ![flyer](/zzz.png)
|
bgneal@871
|
58 It's cool.
|
bgneal@871
|
59 """
|
bgneal@871
|
60 result = process_post(test_str)
|
bgneal@871
|
61 self.assertEqual(expected, result)
|
bgneal@871
|
62
|
bgneal@871
|
63 def test_multiple_non_http(self):
|
bgneal@871
|
64 test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg)
|
bgneal@871
|
65 And another: ![pic](HTTPS://example.com/foo/bar/img.png).
|
bgneal@871
|
66 More stuff here."""
|
bgneal@871
|
67 expected = """An image: ![image](/img.jpg)
|
bgneal@871
|
68 And another: ![pic](HTTPS://example.com/foo/bar/img.png).
|
bgneal@871
|
69 More stuff here."""
|
bgneal@871
|
70 result = process_post(test_str)
|
bgneal@871
|
71 self.assertEqual(expected, result)
|
bgneal@871
|
72
|
bgneal@871
|
73 def test_https_already_with_title(self):
|
bgneal@871
|
74 test_str = """An image that is already using https:
|
bgneal@871
|
75 ![flyer](https://example.com/zzz.png "the title")
|
bgneal@871
|
76 It's cool.
|
bgneal@871
|
77 """
|
bgneal@871
|
78 result = process_post(test_str)
|
bgneal@871
|
79 self.assertEqual(test_str, result)
|
bgneal@871
|
80
|
bgneal@871
|
81 def test_sg101_with_title(self):
|
bgneal@871
|
82 test_str = """An image on SG101:
|
bgneal@871
|
83 ![flyer](http://surfguitar101.com/zzz.png "the title")
|
bgneal@871
|
84 It's cool.
|
bgneal@871
|
85 """
|
bgneal@871
|
86 expected = """An image on SG101:
|
bgneal@871
|
87 ![flyer](/zzz.png "the title")
|
bgneal@871
|
88 It's cool.
|
bgneal@871
|
89 """
|
bgneal@871
|
90 result = process_post(test_str)
|
bgneal@871
|
91 self.assertEqual(expected, result)
|
bgneal@871
|
92
|
bgneal@871
|
93 def test_https_sg101_brackets(self):
|
bgneal@871
|
94 test_str = """An image that is already using https:
|
bgneal@871
|
95 ![flyer](<https://www.SURFGUITAR101.com/zzz.png>)
|
bgneal@871
|
96 It's cool.
|
bgneal@871
|
97 """
|
bgneal@871
|
98 expected = """An image that is already using https:
|
bgneal@871
|
99 ![flyer](/zzz.png)
|
bgneal@871
|
100 It's cool.
|
bgneal@871
|
101 """
|
bgneal@871
|
102 result = process_post(test_str)
|
bgneal@871
|
103 self.assertEqual(expected, result)
|
bgneal@871
|
104
|
bgneal@871
|
105 def test_https_already_brackets(self):
|
bgneal@871
|
106 test_str = """An image that is already using https:
|
bgneal@871
|
107 ![flyer](<https://example.com/zzz.png>)
|
bgneal@871
|
108 It's cool.
|
bgneal@871
|
109 """
|
bgneal@871
|
110 expected = """An image that is already using https:
|
bgneal@871
|
111 ![flyer](https://example.com/zzz.png)
|
bgneal@871
|
112 It's cool.
|
bgneal@871
|
113 """
|
bgneal@871
|
114 result = process_post(test_str)
|
bgneal@871
|
115 self.assertEqual(expected, result)
|