Mercurial > public > sg101
comparison core/tests/test_ssl_images.py @ 902:4dee923a2f6d
Merge with upstream.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 07 Mar 2015 14:56:41 -0600 |
parents | 101728976f9c |
children | 37e75385e931 |
comparison
equal
deleted
inserted
replaced
901:147a66da9cbc | 902:4dee923a2f6d |
---|---|
1 """Unit tests for the ssl_images management command.""" | |
2 import re | |
3 import unittest | |
4 from urlparse import urlparse | |
5 | |
6 import mock | |
7 | |
8 from core.management.commands.ssl_images import html_check | |
9 from core.management.commands.ssl_images import process_post | |
10 import core.management.commands.ssl_images | |
11 | |
12 | |
13 class ProcessPostTestCase(unittest.TestCase): | |
14 | |
15 SG101_RE = re.compile(r'http://(?:www\.)?surfguitar101.com/', re.I) | |
16 | |
17 def tearDown(self): | |
18 core.management.commands.ssl_images.url_cache = {} | |
19 | |
20 def test_empty_string(self): | |
21 s = process_post('') | |
22 self.assertEqual(s, '') | |
23 | |
24 def test_no_matches(self): | |
25 test_str = """Here is a post that doesn't contain any image links at | |
26 all. It also spans lines. | |
27 """ | |
28 result = process_post(test_str) | |
29 self.assertEqual(test_str, result) | |
30 | |
31 def test_sg101_images(self): | |
32 test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg) | |
33 And another: ![pic](HTTP://SURFGUITAR101.COM/foo/bar/img.png). | |
34 More stuff here.""" | |
35 expected = self.SG101_RE.sub('/', test_str) | |
36 result = process_post(test_str) | |
37 self.assertNotEqual(test_str, expected) | |
38 self.assertEqual(expected, result) | |
39 | |
40 def test_sg101_with_newlines(self): | |
41 test_str = """An image: ![image]( | |
42 http://surfguitar101.com/media/zzz.jpg | |
43 ) | |
44 with trailing text.""" | |
45 expected = """An image: ![image](/media/zzz.jpg) | |
46 with trailing text.""" | |
47 result = process_post(test_str) | |
48 self.assertNotEqual(test_str, expected) | |
49 self.assertEqual(expected, result) | |
50 | |
51 def test_https_already(self): | |
52 test_str = """An image that is already using https: | |
53 ![flyer](https://example.com/zzz.png) | |
54 It's cool. | |
55 """ | |
56 result = process_post(test_str) | |
57 self.assertEqual(test_str, result) | |
58 | |
59 def test_https_sg101(self): | |
60 test_str = """An image that is already using https: | |
61 ![flyer](https://www.SURFGUITAR101.com/zzz.png) | |
62 It's cool. | |
63 """ | |
64 expected = """An image that is already using https: | |
65 ![flyer](/zzz.png) | |
66 It's cool. | |
67 """ | |
68 result = process_post(test_str) | |
69 self.assertEqual(expected, result) | |
70 | |
71 def test_multiple_non_http(self): | |
72 test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg) | |
73 And another: ![pic](HTTPS://example.com/foo/bar/img.png). | |
74 More stuff here.""" | |
75 expected = """An image: ![image](/img.jpg) | |
76 And another: ![pic](HTTPS://example.com/foo/bar/img.png). | |
77 More stuff here.""" | |
78 result = process_post(test_str) | |
79 self.assertEqual(expected, result) | |
80 | |
81 def test_https_already_with_title(self): | |
82 test_str = """An image that is already using https: | |
83 ![flyer](https://example.com/zzz.png "the title") | |
84 It's cool. | |
85 """ | |
86 result = process_post(test_str) | |
87 self.assertEqual(test_str, result) | |
88 | |
89 def test_sg101_with_title(self): | |
90 test_str = """An image on SG101: | |
91 ![flyer](http://surfguitar101.com/zzz.png "the title") | |
92 It's cool. | |
93 """ | |
94 expected = """An image on SG101: | |
95 ![flyer](/zzz.png "the title") | |
96 It's cool. | |
97 """ | |
98 result = process_post(test_str) | |
99 self.assertEqual(expected, result) | |
100 | |
101 def test_https_sg101_brackets(self): | |
102 test_str = """An image that is already using https: | |
103 ![flyer](<https://www.SURFGUITAR101.com/zzz.png>) | |
104 It's cool. | |
105 """ | |
106 expected = """An image that is already using https: | |
107 ![flyer](/zzz.png) | |
108 It's cool. | |
109 """ | |
110 result = process_post(test_str) | |
111 self.assertEqual(expected, result) | |
112 | |
113 def test_https_already_brackets(self): | |
114 test_str = """An image that is already using https: | |
115 ![flyer](<https://example.com/zzz.png>) | |
116 It's cool. | |
117 """ | |
118 expected = """An image that is already using https: | |
119 ![flyer](https://example.com/zzz.png) | |
120 It's cool. | |
121 """ | |
122 result = process_post(test_str) | |
123 self.assertEqual(expected, result) | |
124 | |
125 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud') | |
126 @mock.patch('core.management.commands.ssl_images.check_https_availability', | |
127 new=lambda r: None) | |
128 def test_simple_replacement(self, upload_mock): | |
129 old_src = 'http://example.com/images/my_image.jpg' | |
130 new_src = 'https://cloud.com/ABCDEF.jpg' | |
131 test_str = """Here is a really cool http: based image: | |
132 ![flyer]({}) | |
133 Cool, right?""".format(old_src) | |
134 expected = """Here is a really cool http: based image: | |
135 ![flyer]({}) | |
136 Cool, right?""".format(new_src) | |
137 | |
138 upload_mock.return_value = new_src | |
139 result = process_post(test_str) | |
140 self.assertEqual(expected, result) | |
141 upload_mock.assert_called_once_with(old_src) | |
142 | |
143 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud') | |
144 @mock.patch('core.management.commands.ssl_images.check_https_availability', | |
145 new=lambda r: None) | |
146 def test_multiple_replacement(self, upload_mock): | |
147 old_src = [ | |
148 'http://example.com/images/my_image.jpg', | |
149 'http://example.com/static/wow.gif', | |
150 'http://example.com/media/a/b/c/pic.png', | |
151 ] | |
152 new_src = [ | |
153 'https://cloud.com/some/path/012345.jpg', | |
154 'https://cloud.com/some/path/6789AB.gif', | |
155 'https://cloud.com/some/path/CDEF01.png', | |
156 ] | |
157 | |
158 template = """Here is a really cool http: based image: | |
159 ![flyer]({}) | |
160 Cool, right? | |
161 Another one: ![pic]({}) | |
162 And finally | |
163 ![an image]({}) | |
164 """ | |
165 | |
166 test_str = template.format(*old_src) | |
167 expected = template.format(*new_src) | |
168 | |
169 upload_mock.side_effect = new_src | |
170 result = process_post(test_str) | |
171 self.assertEqual(expected, result) | |
172 expected_args = [mock.call(c) for c in old_src] | |
173 self.assertEqual(upload_mock.call_args_list, expected_args) | |
174 | |
175 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud') | |
176 @mock.patch('core.management.commands.ssl_images.check_https_availability', | |
177 new=lambda r: None) | |
178 def test_multiple_replacement_2(self, upload_mock): | |
179 old_src = [ | |
180 'http://example.com/images/my_image.jpg', | |
181 'https://example.com/static/wow.gif', | |
182 'http://www.surfguitar101.com/media/a/b/c/pic.png', | |
183 'http://surfguitar101.com/media/a/b/c/pic2.png', | |
184 ] | |
185 new_src = [ | |
186 'https://cloud.com/some/path/012345.jpg', | |
187 'https://example.com/static/wow.gif', | |
188 '/media/a/b/c/pic.png', | |
189 '/media/a/b/c/pic2.png', | |
190 ] | |
191 | |
192 template = """Here is a really cool http: based image: | |
193 ![flyer]({}) | |
194 Cool, right? | |
195 Another two: ![pic]({}) ![photo]({}) | |
196 And finally | |
197 ![an image]({}). | |
198 """ | |
199 | |
200 test_str = template.format(*old_src) | |
201 expected = template.format(*new_src) | |
202 | |
203 upload_mock.side_effect = new_src | |
204 result = process_post(test_str) | |
205 self.assertEqual(expected, result) | |
206 upload_mock.assert_called_once_with(old_src[0]) | |
207 | |
208 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud') | |
209 @mock.patch('core.management.commands.ssl_images.check_https_availability', | |
210 new=lambda r: None) | |
211 def test_caching(self, upload_mock): | |
212 old_src = [ | |
213 'http://example.com/images/my_image.jpg', | |
214 'http://example.com/static/wow.gif', | |
215 'http://example.com/images/my_image.jpg', | |
216 ] | |
217 new_src = [ | |
218 'https://cloud.com/some/path/012345.jpg', | |
219 'https://cloud.com/some/path/6789AB.gif', | |
220 'https://cloud.com/some/path/012345.jpg', | |
221 ] | |
222 | |
223 template = """Here is a really cool http: based image: | |
224 ![flyer]({}) | |
225 Cool, right? | |
226 Another one: ![pic]({}) | |
227 And finally | |
228 ![an image]({}) | |
229 """ | |
230 | |
231 test_str = template.format(*old_src) | |
232 expected = template.format(*new_src) | |
233 | |
234 upload_mock.side_effect = new_src | |
235 result = process_post(test_str) | |
236 self.assertEqual(expected, result) | |
237 expected_args = [mock.call(c) for c in old_src[:2]] | |
238 self.assertEqual(upload_mock.call_args_list, expected_args) | |
239 | |
240 @mock.patch('core.management.commands.ssl_images.check_https_availability') | |
241 def test_https_availability(self, check_https_mock): | |
242 old_src = [ | |
243 'http://example.com/images/my_image.jpg', | |
244 'http://example.com/static/wow.gif', | |
245 'http://example.com/images/another_image.jpg', | |
246 ] | |
247 new_src = [ | |
248 'https://example.com/images/my_image.jpg', | |
249 'https://example.com/static/wow.gif', | |
250 'https://example.com/images/another_image.jpg', | |
251 ] | |
252 | |
253 template = """Here is a really cool http: based image: | |
254 ![flyer]({}) | |
255 Cool, right? | |
256 Another one: ![pic]({}) | |
257 And finally | |
258 ![an image]({}) | |
259 """ | |
260 | |
261 test_str = template.format(*old_src) | |
262 expected = template.format(*new_src) | |
263 | |
264 check_https_mock.side_effect = new_src | |
265 result = process_post(test_str) | |
266 self.assertEqual(expected, result) | |
267 expected_args = [mock.call(urlparse(c)) for c in old_src] | |
268 self.assertEqual(check_https_mock.call_args_list, expected_args) | |
269 | |
270 | |
271 class HtmlCheckTestCase(unittest.TestCase): | |
272 | |
273 def test_empty(self): | |
274 self.assertFalse(html_check('')) | |
275 | |
276 def test_no_images(self): | |
277 self.assertFalse(html_check('<p>Hi there!</p>')) | |
278 self.assertFalse(html_check('<p>Hi <b>there</b>!</p>')) | |
279 | |
280 def test_safe_image(self): | |
281 self.assertFalse(html_check('<img src="https://a.jpg" />')) | |
282 self.assertFalse(html_check('<img src="" alt="stuff" />')) | |
283 self.assertFalse(html_check('<img src="HTTPS://a.jpg" />')) | |
284 self.assertFalse(html_check(""" | |
285 <div> | |
286 <p>Look: <img src="https://a.jpg" alt="a" /></p> | |
287 <p>Look again: <img src="https://b.jpg" alt="b" /></p> | |
288 </div> | |
289 """)) | |
290 | |
291 def test_one_image(self): | |
292 self.assertTrue(html_check('<img src="http://a.jpg" alt="a" />')) | |
293 self.assertTrue(html_check( | |
294 '<p>Look: <img src="http://a.jpg" alt="a" /></p>')) | |
295 | |
296 def test_two_images(self): | |
297 self.assertTrue(html_check(""" | |
298 <p>Look: <img src="https://a.jpg" alt="a" /></p> | |
299 <p>Look again: <img src="http://b.jpg" alt="b" /></p> | |
300 """)) | |
301 self.assertTrue(html_check(""" | |
302 <p>Look: <img src="http://a.jpg" alt="a" /></p> | |
303 <p>Look again: <img src="http://b.jpg" alt="b" /></p> | |
304 """)) | |
305 self.assertTrue(html_check(""" | |
306 <div> | |
307 <p>Look: <img src="http://a.jpg" alt="a" /></p> | |
308 <p>Look again: <img src="http://b.jpg" alt="b" /></p> | |
309 </div> | |
310 """)) | |
311 self.assertTrue(html_check(""" | |
312 <div> | |
313 <p>Look: <img src="http://a.jpg" alt="a" /></p> | |
314 <p>Look again: <img src="https://b.jpg" alt="b" /></p> | |
315 </div> | |
316 """)) |