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@872
|
5 import mock
|
bgneal@872
|
6
|
bgneal@868
|
7 from core.management.commands.ssl_images import process_post
|
bgneal@868
|
8
|
bgneal@868
|
9
|
bgneal@868
|
10 class ProcessPostTestCase(unittest.TestCase):
|
bgneal@868
|
11
|
bgneal@870
|
12 SG101_RE = re.compile(r'http://(?:www\.)?surfguitar101.com/', re.I)
|
bgneal@870
|
13
|
bgneal@868
|
14 def test_empty_string(self):
|
bgneal@868
|
15 s = process_post('')
|
bgneal@868
|
16 self.assertEqual(s, '')
|
bgneal@870
|
17
|
bgneal@870
|
18 def test_no_matches(self):
|
bgneal@870
|
19 test_str = """Here is a post that doesn't contain any image links at
|
bgneal@870
|
20 all. It also spans lines.
|
bgneal@870
|
21 """
|
bgneal@870
|
22 result = process_post(test_str)
|
bgneal@870
|
23 self.assertEqual(test_str, result)
|
bgneal@870
|
24
|
bgneal@870
|
25 def test_sg101_images(self):
|
bgneal@870
|
26 test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg)
|
bgneal@870
|
27 And another: ![pic](HTTP://SURFGUITAR101.COM/foo/bar/img.png).
|
bgneal@870
|
28 More stuff here."""
|
bgneal@870
|
29 expected = self.SG101_RE.sub('/', test_str)
|
bgneal@870
|
30 result = process_post(test_str)
|
bgneal@870
|
31 self.assertNotEqual(test_str, expected)
|
bgneal@870
|
32 self.assertEqual(expected, result)
|
bgneal@870
|
33
|
bgneal@871
|
34 def test_sg101_with_newlines(self):
|
bgneal@871
|
35 test_str = """An image: ![image](
|
bgneal@871
|
36 http://surfguitar101.com/media/zzz.jpg
|
bgneal@871
|
37 )
|
bgneal@871
|
38 with trailing text."""
|
bgneal@871
|
39 expected = """An image: ![image](/media/zzz.jpg)
|
bgneal@871
|
40 with trailing text."""
|
bgneal@871
|
41 result = process_post(test_str)
|
bgneal@871
|
42 self.assertNotEqual(test_str, expected)
|
bgneal@871
|
43 self.assertEqual(expected, result)
|
bgneal@871
|
44
|
bgneal@870
|
45 def test_https_already(self):
|
bgneal@871
|
46 test_str = """An image that is already using https:
|
bgneal@871
|
47 ![flyer](https://example.com/zzz.png)
|
bgneal@871
|
48 It's cool.
|
bgneal@871
|
49 """
|
bgneal@871
|
50 result = process_post(test_str)
|
bgneal@871
|
51 self.assertEqual(test_str, result)
|
bgneal@871
|
52
|
bgneal@871
|
53 def test_https_sg101(self):
|
bgneal@871
|
54 test_str = """An image that is already using https:
|
bgneal@871
|
55 ![flyer](https://www.SURFGUITAR101.com/zzz.png)
|
bgneal@871
|
56 It's cool.
|
bgneal@871
|
57 """
|
bgneal@871
|
58 expected = """An image that is already using https:
|
bgneal@871
|
59 ![flyer](/zzz.png)
|
bgneal@871
|
60 It's cool.
|
bgneal@871
|
61 """
|
bgneal@871
|
62 result = process_post(test_str)
|
bgneal@871
|
63 self.assertEqual(expected, result)
|
bgneal@871
|
64
|
bgneal@871
|
65 def test_multiple_non_http(self):
|
bgneal@871
|
66 test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg)
|
bgneal@871
|
67 And another: ![pic](HTTPS://example.com/foo/bar/img.png).
|
bgneal@871
|
68 More stuff here."""
|
bgneal@871
|
69 expected = """An image: ![image](/img.jpg)
|
bgneal@871
|
70 And another: ![pic](HTTPS://example.com/foo/bar/img.png).
|
bgneal@871
|
71 More stuff here."""
|
bgneal@871
|
72 result = process_post(test_str)
|
bgneal@871
|
73 self.assertEqual(expected, result)
|
bgneal@871
|
74
|
bgneal@871
|
75 def test_https_already_with_title(self):
|
bgneal@871
|
76 test_str = """An image that is already using https:
|
bgneal@871
|
77 ![flyer](https://example.com/zzz.png "the title")
|
bgneal@871
|
78 It's cool.
|
bgneal@871
|
79 """
|
bgneal@871
|
80 result = process_post(test_str)
|
bgneal@871
|
81 self.assertEqual(test_str, result)
|
bgneal@871
|
82
|
bgneal@871
|
83 def test_sg101_with_title(self):
|
bgneal@871
|
84 test_str = """An image on SG101:
|
bgneal@871
|
85 ![flyer](http://surfguitar101.com/zzz.png "the title")
|
bgneal@871
|
86 It's cool.
|
bgneal@871
|
87 """
|
bgneal@871
|
88 expected = """An image on SG101:
|
bgneal@871
|
89 ![flyer](/zzz.png "the title")
|
bgneal@871
|
90 It's cool.
|
bgneal@871
|
91 """
|
bgneal@871
|
92 result = process_post(test_str)
|
bgneal@871
|
93 self.assertEqual(expected, result)
|
bgneal@871
|
94
|
bgneal@871
|
95 def test_https_sg101_brackets(self):
|
bgneal@871
|
96 test_str = """An image that is already using https:
|
bgneal@871
|
97 ![flyer](<https://www.SURFGUITAR101.com/zzz.png>)
|
bgneal@871
|
98 It's cool.
|
bgneal@871
|
99 """
|
bgneal@871
|
100 expected = """An image that is already using https:
|
bgneal@871
|
101 ![flyer](/zzz.png)
|
bgneal@871
|
102 It's cool.
|
bgneal@871
|
103 """
|
bgneal@871
|
104 result = process_post(test_str)
|
bgneal@871
|
105 self.assertEqual(expected, result)
|
bgneal@871
|
106
|
bgneal@871
|
107 def test_https_already_brackets(self):
|
bgneal@871
|
108 test_str = """An image that is already using https:
|
bgneal@871
|
109 ![flyer](<https://example.com/zzz.png>)
|
bgneal@871
|
110 It's cool.
|
bgneal@871
|
111 """
|
bgneal@871
|
112 expected = """An image that is already using https:
|
bgneal@871
|
113 ![flyer](https://example.com/zzz.png)
|
bgneal@871
|
114 It's cool.
|
bgneal@871
|
115 """
|
bgneal@871
|
116 result = process_post(test_str)
|
bgneal@871
|
117 self.assertEqual(expected, result)
|
bgneal@872
|
118
|
bgneal@872
|
119 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
|
bgneal@872
|
120 def test_simple_replacement(self, upload_mock):
|
bgneal@872
|
121 old_src = 'http://example.com/images/my_image.jpg'
|
bgneal@873
|
122 new_src = 'https://cloud.com/ABCDEF.jpg'
|
bgneal@872
|
123 test_str = """Here is a really cool http: based image:
|
bgneal@872
|
124 ![flyer]({})
|
bgneal@872
|
125 Cool, right?""".format(old_src)
|
bgneal@872
|
126 expected = """Here is a really cool http: based image:
|
bgneal@872
|
127 ![flyer]({})
|
bgneal@872
|
128 Cool, right?""".format(new_src)
|
bgneal@872
|
129
|
bgneal@872
|
130 upload_mock.return_value = new_src
|
bgneal@872
|
131 result = process_post(test_str)
|
bgneal@872
|
132 self.assertEqual(expected, result)
|
bgneal@872
|
133 upload_mock.assert_called_once_with(old_src)
|
bgneal@873
|
134
|
bgneal@873
|
135 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
|
bgneal@873
|
136 def test_multiple_replacement(self, upload_mock):
|
bgneal@873
|
137 old_src = [
|
bgneal@873
|
138 'http://example.com/images/my_image.jpg',
|
bgneal@873
|
139 'http://example.com/static/wow.gif',
|
bgneal@873
|
140 'http://example.com/media/a/b/c/pic.png',
|
bgneal@873
|
141 ]
|
bgneal@873
|
142 new_src = [
|
bgneal@873
|
143 'https://cloud.com/some/path/012345.jpg',
|
bgneal@873
|
144 'https://cloud.com/some/path/6789AB.gif',
|
bgneal@873
|
145 'https://cloud.com/some/path/CDEF01.png',
|
bgneal@873
|
146 ]
|
bgneal@873
|
147
|
bgneal@873
|
148 template = """Here is a really cool http: based image:
|
bgneal@873
|
149 ![flyer]({})
|
bgneal@873
|
150 Cool, right?
|
bgneal@873
|
151 Another one: ![pic]({})
|
bgneal@873
|
152 And finally
|
bgneal@873
|
153 ![an image]({})
|
bgneal@873
|
154 """
|
bgneal@873
|
155
|
bgneal@873
|
156 test_str = template.format(*old_src)
|
bgneal@873
|
157 expected = template.format(*new_src)
|
bgneal@873
|
158
|
bgneal@873
|
159 upload_mock.side_effect = new_src
|
bgneal@873
|
160 result = process_post(test_str)
|
bgneal@873
|
161 self.assertEqual(expected, result)
|
bgneal@873
|
162 expected_args = [mock.call(c) for c in old_src]
|
bgneal@873
|
163 self.assertEqual(upload_mock.call_args_list, expected_args)
|
bgneal@873
|
164
|
bgneal@873
|
165 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
|
bgneal@873
|
166 def test_multiple_replacement_2(self, upload_mock):
|
bgneal@873
|
167 old_src = [
|
bgneal@873
|
168 'http://example.com/images/my_image.jpg',
|
bgneal@873
|
169 'https://example.com/static/wow.gif',
|
bgneal@873
|
170 'http://www.surfguitar101.com/media/a/b/c/pic.png',
|
bgneal@873
|
171 'http://surfguitar101.com/media/a/b/c/pic2.png',
|
bgneal@873
|
172 ]
|
bgneal@873
|
173 new_src = [
|
bgneal@873
|
174 'https://cloud.com/some/path/012345.jpg',
|
bgneal@873
|
175 'https://example.com/static/wow.gif',
|
bgneal@873
|
176 '/media/a/b/c/pic.png',
|
bgneal@873
|
177 '/media/a/b/c/pic2.png',
|
bgneal@873
|
178 ]
|
bgneal@873
|
179
|
bgneal@873
|
180 template = """Here is a really cool http: based image:
|
bgneal@873
|
181 ![flyer]({})
|
bgneal@873
|
182 Cool, right?
|
bgneal@873
|
183 Another two: ![pic]({}) ![photo]({})
|
bgneal@873
|
184 And finally
|
bgneal@873
|
185 ![an image]({}).
|
bgneal@873
|
186 """
|
bgneal@873
|
187
|
bgneal@873
|
188 test_str = template.format(*old_src)
|
bgneal@873
|
189 expected = template.format(*new_src)
|
bgneal@873
|
190
|
bgneal@873
|
191 upload_mock.side_effect = new_src
|
bgneal@873
|
192 result = process_post(test_str)
|
bgneal@873
|
193 self.assertEqual(expected, result)
|
bgneal@873
|
194 upload_mock.assert_called_once_with(old_src[0])
|