bgneal@868
|
1 """Unit tests for the ssl_images management command."""
|
bgneal@870
|
2 import re
|
bgneal@868
|
3 import unittest
|
bgneal@889
|
4 from urlparse import urlparse
|
bgneal@868
|
5
|
bgneal@872
|
6 import mock
|
bgneal@963
|
7 from django.conf import settings
|
bgneal@872
|
8
|
bgneal@894
|
9 from core.management.commands.ssl_images import html_check
|
bgneal@987
|
10 from core.management.commands.ssl_images import process_post, process_html
|
bgneal@889
|
11 import core.management.commands.ssl_images
|
bgneal@868
|
12
|
bgneal@868
|
13
|
bgneal@868
|
14 class ProcessPostTestCase(unittest.TestCase):
|
bgneal@868
|
15
|
bgneal@870
|
16 SG101_RE = re.compile(r'http://(?:www\.)?surfguitar101.com/', re.I)
|
bgneal@870
|
17
|
bgneal@963
|
18 def setUp(self):
|
bgneal@963
|
19 self.assertTrue(len(settings.USER_IMAGES_SOURCES) > 0)
|
bgneal@963
|
20 self.safe_host = settings.USER_IMAGES_SOURCES[0]
|
bgneal@963
|
21
|
bgneal@889
|
22 def tearDown(self):
|
bgneal@889
|
23 core.management.commands.ssl_images.url_cache = {}
|
bgneal@889
|
24
|
bgneal@868
|
25 def test_empty_string(self):
|
bgneal@868
|
26 s = process_post('')
|
bgneal@868
|
27 self.assertEqual(s, '')
|
bgneal@870
|
28
|
bgneal@870
|
29 def test_no_matches(self):
|
bgneal@870
|
30 test_str = """Here is a post that doesn't contain any image links at
|
bgneal@870
|
31 all. It also spans lines.
|
bgneal@870
|
32 """
|
bgneal@870
|
33 result = process_post(test_str)
|
bgneal@870
|
34 self.assertEqual(test_str, result)
|
bgneal@870
|
35
|
bgneal@870
|
36 def test_sg101_images(self):
|
bgneal@870
|
37 test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg)
|
bgneal@870
|
38 And another: ![pic](HTTP://SURFGUITAR101.COM/foo/bar/img.png).
|
bgneal@870
|
39 More stuff here."""
|
bgneal@870
|
40 expected = self.SG101_RE.sub('/', test_str)
|
bgneal@870
|
41 result = process_post(test_str)
|
bgneal@870
|
42 self.assertNotEqual(test_str, expected)
|
bgneal@870
|
43 self.assertEqual(expected, result)
|
bgneal@870
|
44
|
bgneal@871
|
45 def test_sg101_with_newlines(self):
|
bgneal@871
|
46 test_str = """An image: ![image](
|
bgneal@871
|
47 http://surfguitar101.com/media/zzz.jpg
|
bgneal@871
|
48 )
|
bgneal@871
|
49 with trailing text."""
|
bgneal@871
|
50 expected = """An image: ![image](/media/zzz.jpg)
|
bgneal@871
|
51 with trailing text."""
|
bgneal@871
|
52 result = process_post(test_str)
|
bgneal@871
|
53 self.assertNotEqual(test_str, expected)
|
bgneal@871
|
54 self.assertEqual(expected, result)
|
bgneal@871
|
55
|
bgneal@870
|
56 def test_https_already(self):
|
bgneal@871
|
57 test_str = """An image that is already using https:
|
bgneal@963
|
58 ![flyer](https://{}/zzz.png)
|
bgneal@871
|
59 It's cool.
|
bgneal@963
|
60 """.format(self.safe_host)
|
bgneal@871
|
61 result = process_post(test_str)
|
bgneal@871
|
62 self.assertEqual(test_str, result)
|
bgneal@871
|
63
|
bgneal@871
|
64 def test_https_sg101(self):
|
bgneal@871
|
65 test_str = """An image that is already using https:
|
bgneal@871
|
66 ![flyer](https://www.SURFGUITAR101.com/zzz.png)
|
bgneal@871
|
67 It's cool.
|
bgneal@871
|
68 """
|
bgneal@871
|
69 expected = """An image that is already using https:
|
bgneal@871
|
70 ![flyer](/zzz.png)
|
bgneal@871
|
71 It's cool.
|
bgneal@871
|
72 """
|
bgneal@871
|
73 result = process_post(test_str)
|
bgneal@871
|
74 self.assertEqual(expected, result)
|
bgneal@871
|
75
|
bgneal@871
|
76 def test_multiple_non_http(self):
|
bgneal@871
|
77 test_str = """An image: ![image](http://www.surfguitar101.com/img.jpg)
|
bgneal@963
|
78 And another: ![pic](HTTPS://{}/foo/bar/img.png).
|
bgneal@963
|
79 More stuff here.""".format(self.safe_host)
|
bgneal@871
|
80 expected = """An image: ![image](/img.jpg)
|
bgneal@963
|
81 And another: ![pic](HTTPS://{}/foo/bar/img.png).
|
bgneal@963
|
82 More stuff here.""".format(self.safe_host)
|
bgneal@871
|
83 result = process_post(test_str)
|
bgneal@871
|
84 self.assertEqual(expected, result)
|
bgneal@871
|
85
|
bgneal@871
|
86 def test_https_already_with_title(self):
|
bgneal@871
|
87 test_str = """An image that is already using https:
|
bgneal@963
|
88 ![flyer](https://{}/zzz.png "the title")
|
bgneal@871
|
89 It's cool.
|
bgneal@963
|
90 """.format(self.safe_host)
|
bgneal@871
|
91 result = process_post(test_str)
|
bgneal@871
|
92 self.assertEqual(test_str, result)
|
bgneal@871
|
93
|
bgneal@871
|
94 def test_sg101_with_title(self):
|
bgneal@871
|
95 test_str = """An image on SG101:
|
bgneal@871
|
96 ![flyer](http://surfguitar101.com/zzz.png "the title")
|
bgneal@871
|
97 It's cool.
|
bgneal@871
|
98 """
|
bgneal@871
|
99 expected = """An image on SG101:
|
bgneal@871
|
100 ![flyer](/zzz.png "the title")
|
bgneal@871
|
101 It's cool.
|
bgneal@871
|
102 """
|
bgneal@871
|
103 result = process_post(test_str)
|
bgneal@871
|
104 self.assertEqual(expected, result)
|
bgneal@871
|
105
|
bgneal@871
|
106 def test_https_sg101_brackets(self):
|
bgneal@871
|
107 test_str = """An image that is already using https:
|
bgneal@871
|
108 ![flyer](<https://www.SURFGUITAR101.com/zzz.png>)
|
bgneal@871
|
109 It's cool.
|
bgneal@871
|
110 """
|
bgneal@871
|
111 expected = """An image that is already using https:
|
bgneal@871
|
112 ![flyer](/zzz.png)
|
bgneal@871
|
113 It's cool.
|
bgneal@871
|
114 """
|
bgneal@871
|
115 result = process_post(test_str)
|
bgneal@871
|
116 self.assertEqual(expected, result)
|
bgneal@871
|
117
|
bgneal@871
|
118 def test_https_already_brackets(self):
|
bgneal@871
|
119 test_str = """An image that is already using https:
|
bgneal@963
|
120 ![flyer](<https://{}/zzz.png>)
|
bgneal@871
|
121 It's cool.
|
bgneal@963
|
122 """.format(self.safe_host)
|
bgneal@871
|
123 expected = """An image that is already using https:
|
bgneal@963
|
124 ![flyer](https://{}/zzz.png)
|
bgneal@871
|
125 It's cool.
|
bgneal@963
|
126 """.format(self.safe_host)
|
bgneal@871
|
127 result = process_post(test_str)
|
bgneal@871
|
128 self.assertEqual(expected, result)
|
bgneal@872
|
129
|
bgneal@872
|
130 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
|
bgneal@872
|
131 def test_simple_replacement(self, upload_mock):
|
bgneal@872
|
132 old_src = 'http://example.com/images/my_image.jpg'
|
bgneal@873
|
133 new_src = 'https://cloud.com/ABCDEF.jpg'
|
bgneal@872
|
134 test_str = """Here is a really cool http: based image:
|
bgneal@872
|
135 ![flyer]({})
|
bgneal@872
|
136 Cool, right?""".format(old_src)
|
bgneal@872
|
137 expected = """Here is a really cool http: based image:
|
bgneal@872
|
138 ![flyer]({})
|
bgneal@872
|
139 Cool, right?""".format(new_src)
|
bgneal@872
|
140
|
bgneal@872
|
141 upload_mock.return_value = new_src
|
bgneal@872
|
142 result = process_post(test_str)
|
bgneal@872
|
143 self.assertEqual(expected, result)
|
bgneal@927
|
144 upload_mock.assert_called_once_with(urlparse(old_src))
|
bgneal@873
|
145
|
bgneal@873
|
146 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
|
bgneal@873
|
147 def test_multiple_replacement(self, upload_mock):
|
bgneal@873
|
148 old_src = [
|
bgneal@873
|
149 'http://example.com/images/my_image.jpg',
|
bgneal@873
|
150 'http://example.com/static/wow.gif',
|
bgneal@873
|
151 'http://example.com/media/a/b/c/pic.png',
|
bgneal@873
|
152 ]
|
bgneal@873
|
153 new_src = [
|
bgneal@873
|
154 'https://cloud.com/some/path/012345.jpg',
|
bgneal@873
|
155 'https://cloud.com/some/path/6789AB.gif',
|
bgneal@873
|
156 'https://cloud.com/some/path/CDEF01.png',
|
bgneal@873
|
157 ]
|
bgneal@873
|
158
|
bgneal@873
|
159 template = """Here is a really cool http: based image:
|
bgneal@873
|
160 ![flyer]({})
|
bgneal@873
|
161 Cool, right?
|
bgneal@873
|
162 Another one: ![pic]({})
|
bgneal@873
|
163 And finally
|
bgneal@873
|
164 ![an image]({})
|
bgneal@873
|
165 """
|
bgneal@873
|
166
|
bgneal@873
|
167 test_str = template.format(*old_src)
|
bgneal@873
|
168 expected = template.format(*new_src)
|
bgneal@873
|
169
|
bgneal@873
|
170 upload_mock.side_effect = new_src
|
bgneal@873
|
171 result = process_post(test_str)
|
bgneal@873
|
172 self.assertEqual(expected, result)
|
bgneal@927
|
173 expected_args = [mock.call(urlparse(c)) for c in old_src]
|
bgneal@873
|
174 self.assertEqual(upload_mock.call_args_list, expected_args)
|
bgneal@873
|
175
|
bgneal@873
|
176 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
|
bgneal@873
|
177 def test_multiple_replacement_2(self, upload_mock):
|
bgneal@873
|
178 old_src = [
|
bgneal@873
|
179 'http://example.com/images/my_image.jpg',
|
bgneal@963
|
180 'https://{}/static/wow.gif'.format(self.safe_host),
|
bgneal@873
|
181 'http://www.surfguitar101.com/media/a/b/c/pic.png',
|
bgneal@873
|
182 'http://surfguitar101.com/media/a/b/c/pic2.png',
|
bgneal@873
|
183 ]
|
bgneal@873
|
184 new_src = [
|
bgneal@873
|
185 'https://cloud.com/some/path/012345.jpg',
|
bgneal@963
|
186 'https://{}/static/wow.gif'.format(self.safe_host),
|
bgneal@873
|
187 '/media/a/b/c/pic.png',
|
bgneal@873
|
188 '/media/a/b/c/pic2.png',
|
bgneal@873
|
189 ]
|
bgneal@873
|
190
|
bgneal@873
|
191 template = """Here is a really cool http: based image:
|
bgneal@873
|
192 ![flyer]({})
|
bgneal@873
|
193 Cool, right?
|
bgneal@873
|
194 Another two: ![pic]({}) ![photo]({})
|
bgneal@873
|
195 And finally
|
bgneal@873
|
196 ![an image]({}).
|
bgneal@873
|
197 """
|
bgneal@873
|
198
|
bgneal@873
|
199 test_str = template.format(*old_src)
|
bgneal@873
|
200 expected = template.format(*new_src)
|
bgneal@873
|
201
|
bgneal@873
|
202 upload_mock.side_effect = new_src
|
bgneal@873
|
203 result = process_post(test_str)
|
bgneal@873
|
204 self.assertEqual(expected, result)
|
bgneal@927
|
205 upload_mock.assert_called_once_with(urlparse(old_src[0]))
|
bgneal@889
|
206
|
bgneal@889
|
207 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
|
bgneal@889
|
208 def test_caching(self, upload_mock):
|
bgneal@889
|
209 old_src = [
|
bgneal@889
|
210 'http://example.com/images/my_image.jpg',
|
bgneal@889
|
211 'http://example.com/static/wow.gif',
|
bgneal@889
|
212 'http://example.com/images/my_image.jpg',
|
bgneal@889
|
213 ]
|
bgneal@889
|
214 new_src = [
|
bgneal@889
|
215 'https://cloud.com/some/path/012345.jpg',
|
bgneal@889
|
216 'https://cloud.com/some/path/6789AB.gif',
|
bgneal@889
|
217 'https://cloud.com/some/path/012345.jpg',
|
bgneal@889
|
218 ]
|
bgneal@889
|
219
|
bgneal@889
|
220 template = """Here is a really cool http: based image:
|
bgneal@889
|
221 ![flyer]({})
|
bgneal@889
|
222 Cool, right?
|
bgneal@889
|
223 Another one: ![pic]({})
|
bgneal@889
|
224 And finally
|
bgneal@889
|
225 ![an image]({})
|
bgneal@889
|
226 """
|
bgneal@889
|
227
|
bgneal@889
|
228 test_str = template.format(*old_src)
|
bgneal@889
|
229 expected = template.format(*new_src)
|
bgneal@889
|
230
|
bgneal@889
|
231 upload_mock.side_effect = new_src
|
bgneal@889
|
232 result = process_post(test_str)
|
bgneal@889
|
233 self.assertEqual(expected, result)
|
bgneal@927
|
234 expected_args = [mock.call(urlparse(c)) for c in old_src[:2]]
|
bgneal@889
|
235 self.assertEqual(upload_mock.call_args_list, expected_args)
|
bgneal@889
|
236
|
bgneal@986
|
237 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
|
bgneal@986
|
238 def test_bad_image_url(self, upload_mock):
|
bgneal@986
|
239 test_str = u'![image](http://[url=http://www.flickr.com/photos/85447101@N04/8025176073/][img]http://farm9.staticflickr.com/8456/8025176073_91ac0c3d18_b.jpg[/img][/url] [url=http://www.flickr.com/photos/85447101@N04/8025176073/]IMG_0398[/url] by [url=http://www.flickr.com/people/85447101@N04/]friendshipmaster[/url], on Flick)'
|
bgneal@986
|
240 expected = u'{bad image}'
|
bgneal@986
|
241
|
bgneal@986
|
242 result = process_post(test_str)
|
bgneal@986
|
243 self.assertEqual(expected, result)
|
bgneal@986
|
244 self.assertEqual(upload_mock.call_count, 0)
|
bgneal@986
|
245
|
bgneal@894
|
246
|
bgneal@894
|
247 class HtmlCheckTestCase(unittest.TestCase):
|
bgneal@894
|
248
|
bgneal@894
|
249 def test_empty(self):
|
bgneal@894
|
250 self.assertFalse(html_check(''))
|
bgneal@894
|
251
|
bgneal@894
|
252 def test_no_images(self):
|
bgneal@894
|
253 self.assertFalse(html_check('<p>Hi there!</p>'))
|
bgneal@894
|
254 self.assertFalse(html_check('<p>Hi <b>there</b>!</p>'))
|
bgneal@894
|
255
|
bgneal@894
|
256 def test_safe_image(self):
|
bgneal@894
|
257 self.assertFalse(html_check('<img src="https://a.jpg" />'))
|
bgneal@894
|
258 self.assertFalse(html_check('<img src="" alt="stuff" />'))
|
bgneal@894
|
259 self.assertFalse(html_check('<img src="HTTPS://a.jpg" />'))
|
bgneal@894
|
260 self.assertFalse(html_check("""
|
bgneal@894
|
261 <div>
|
bgneal@894
|
262 <p>Look: <img src="https://a.jpg" alt="a" /></p>
|
bgneal@894
|
263 <p>Look again: <img src="https://b.jpg" alt="b" /></p>
|
bgneal@894
|
264 </div>
|
bgneal@894
|
265 """))
|
bgneal@894
|
266
|
bgneal@894
|
267 def test_one_image(self):
|
bgneal@894
|
268 self.assertTrue(html_check('<img src="http://a.jpg" alt="a" />'))
|
bgneal@894
|
269 self.assertTrue(html_check(
|
bgneal@894
|
270 '<p>Look: <img src="http://a.jpg" alt="a" /></p>'))
|
bgneal@894
|
271
|
bgneal@894
|
272 def test_two_images(self):
|
bgneal@894
|
273 self.assertTrue(html_check("""
|
bgneal@894
|
274 <p>Look: <img src="https://a.jpg" alt="a" /></p>
|
bgneal@894
|
275 <p>Look again: <img src="http://b.jpg" alt="b" /></p>
|
bgneal@894
|
276 """))
|
bgneal@894
|
277 self.assertTrue(html_check("""
|
bgneal@894
|
278 <p>Look: <img src="http://a.jpg" alt="a" /></p>
|
bgneal@894
|
279 <p>Look again: <img src="http://b.jpg" alt="b" /></p>
|
bgneal@894
|
280 """))
|
bgneal@894
|
281 self.assertTrue(html_check("""
|
bgneal@894
|
282 <div>
|
bgneal@894
|
283 <p>Look: <img src="http://a.jpg" alt="a" /></p>
|
bgneal@894
|
284 <p>Look again: <img src="http://b.jpg" alt="b" /></p>
|
bgneal@894
|
285 </div>
|
bgneal@894
|
286 """))
|
bgneal@894
|
287 self.assertTrue(html_check("""
|
bgneal@894
|
288 <div>
|
bgneal@894
|
289 <p>Look: <img src="http://a.jpg" alt="a" /></p>
|
bgneal@894
|
290 <p>Look again: <img src="https://b.jpg" alt="b" /></p>
|
bgneal@894
|
291 </div>
|
bgneal@894
|
292 """))
|
bgneal@987
|
293
|
bgneal@987
|
294
|
bgneal@987
|
295 class ProcessHtmlTestCase(unittest.TestCase):
|
bgneal@987
|
296
|
bgneal@987
|
297 SG101_RE = re.compile(r'http://(?:www\.)?surfguitar101.com/', re.I)
|
bgneal@987
|
298
|
bgneal@987
|
299 def setUp(self):
|
bgneal@987
|
300 self.assertTrue(len(settings.USER_IMAGES_SOURCES) > 0)
|
bgneal@987
|
301 self.safe_host = settings.USER_IMAGES_SOURCES[0]
|
bgneal@987
|
302
|
bgneal@987
|
303 def tearDown(self):
|
bgneal@987
|
304 core.management.commands.ssl_images.url_cache = {}
|
bgneal@987
|
305
|
bgneal@987
|
306 def test_empty_string(self):
|
bgneal@987
|
307 s = process_html('')
|
bgneal@987
|
308 self.assertEqual(s, '')
|
bgneal@987
|
309
|
bgneal@987
|
310 def test_whitespace_string(self):
|
bgneal@987
|
311 s = process_html('\r\n\r\n')
|
bgneal@987
|
312 self.assertEqual(s, '')
|
bgneal@987
|
313
|
bgneal@987
|
314 def test_no_matches(self):
|
bgneal@987
|
315 test_str = """<p>Here is a post that doesn't contain any image links at
|
bgneal@987
|
316 all. It also spans lines.</p>
|
bgneal@987
|
317 """
|
bgneal@987
|
318 result = process_html(test_str)
|
bgneal@987
|
319 self.assertEqual(test_str, result)
|
bgneal@987
|
320
|
bgneal@987
|
321 def test_multiple_paragraphs(self):
|
bgneal@987
|
322 test_str = """<p>Here is a post that doesn't contain any image links at
|
bgneal@987
|
323 all. It also spans lines.</p>
|
bgneal@987
|
324 """
|
bgneal@987
|
325 test_str += test_str
|
bgneal@987
|
326 result = process_html(test_str)
|
bgneal@987
|
327 self.assertEqual(test_str, result)
|
bgneal@987
|
328
|
bgneal@987
|
329 def test_sg101_images(self):
|
bgneal@987
|
330 test_str = """<p>An image:
|
bgneal@987
|
331 <img src="http://www.surfguitar101.com/img.jpg" alt="image">
|
bgneal@987
|
332 And another: <img src="HTTP://SURFGUITAR101.COM/foo/bar/img.png" alt="pic">
|
bgneal@987
|
333 More stuff here.</p>"""
|
bgneal@987
|
334 expected = self.SG101_RE.sub('/', test_str)
|
bgneal@987
|
335 result = process_html(test_str)
|
bgneal@987
|
336 self.assertNotEqual(test_str, expected)
|
bgneal@987
|
337 self.assertEqual(expected, result)
|
bgneal@987
|
338
|
bgneal@987
|
339 def test_https_already(self):
|
bgneal@987
|
340 test_str = """<p>An image that is already using https:
|
bgneal@987
|
341 <img src="https://{}/zzz.png" alt="pic">
|
bgneal@987
|
342 It's cool.</p>""".format(self.safe_host)
|
bgneal@987
|
343 result = process_html(test_str)
|
bgneal@987
|
344 self.assertEqual(test_str, result)
|
bgneal@987
|
345
|
bgneal@987
|
346 def test_https_sg101(self):
|
bgneal@987
|
347 test_str = """<p>An image that is already using https:
|
bgneal@987
|
348 <img src="https://www.SURFGUITAR101.com/zzz.png" alt="pic">
|
bgneal@987
|
349 It's cool.</p>
|
bgneal@987
|
350 """
|
bgneal@987
|
351 expected = """<p>An image that is already using https:
|
bgneal@987
|
352 <img src="/zzz.png" alt="pic">
|
bgneal@987
|
353 It's cool.</p>"""
|
bgneal@987
|
354 result = process_html(test_str)
|
bgneal@987
|
355 self.assertEqual(expected, result)
|
bgneal@987
|
356
|
bgneal@987
|
357 def test_multiple_non_http(self):
|
bgneal@987
|
358 test_str = """<p>An image:
|
bgneal@987
|
359 <img src="http://www.surfguitar101.com/img.jpg" alt="pic">
|
bgneal@987
|
360 And another:
|
bgneal@987
|
361 <img src="HTTPS://{}/foo/bar/img.png" alt="stuff">
|
bgneal@987
|
362 More stuff here.</p>
|
bgneal@987
|
363 """.format(self.safe_host)
|
bgneal@987
|
364 expected = """<p>An image:
|
bgneal@987
|
365 <img src="/img.jpg" alt="pic">
|
bgneal@987
|
366 And another:
|
bgneal@987
|
367 <img src="HTTPS://{}/foo/bar/img.png" alt="stuff">
|
bgneal@987
|
368 More stuff here.</p>""".format(self.safe_host)
|
bgneal@987
|
369 result = process_html(test_str)
|
bgneal@987
|
370 self.assertEqual(expected, result)
|
bgneal@987
|
371
|
bgneal@987
|
372 def test_https_already_with_title(self):
|
bgneal@987
|
373 test_str = """<p>An image that is already using https:
|
bgneal@987
|
374 <img src="https://{}/zzz.png" alt="1" title="the title">
|
bgneal@987
|
375 It's cool.</p>
|
bgneal@987
|
376 """.format(self.safe_host)
|
bgneal@987
|
377 result = process_html(test_str)
|
bgneal@987
|
378 self.assertEqual(test_str, result)
|
bgneal@987
|
379
|
bgneal@987
|
380 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
|
bgneal@987
|
381 def test_simple_replacement(self, upload_mock):
|
bgneal@987
|
382 old_src = 'http://example.com/images/my_image.jpg'
|
bgneal@987
|
383 new_src = 'https://cloud.com/ABCDEF.jpg'
|
bgneal@987
|
384 test_str = """<p>Here is a really cool http: based image:
|
bgneal@987
|
385 <img src="{}" alt="a">
|
bgneal@987
|
386 Cool, right?</p>""".format(old_src)
|
bgneal@987
|
387 expected = """<p>Here is a really cool http: based image:
|
bgneal@987
|
388 <img src="{}" alt="a">
|
bgneal@987
|
389 Cool, right?</p>""".format(new_src)
|
bgneal@987
|
390
|
bgneal@987
|
391 upload_mock.return_value = new_src
|
bgneal@987
|
392 result = process_html(test_str)
|
bgneal@987
|
393 self.assertEqual(expected, result)
|
bgneal@987
|
394 upload_mock.assert_called_once_with(urlparse(old_src))
|
bgneal@987
|
395
|
bgneal@987
|
396 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
|
bgneal@987
|
397 def test_multiple_replacement(self, upload_mock):
|
bgneal@987
|
398 old_src = [
|
bgneal@987
|
399 'http://example.com/images/my_image.jpg',
|
bgneal@987
|
400 'http://example.com/static/wow.gif',
|
bgneal@987
|
401 'http://example.com/media/a/b/c/pic.png',
|
bgneal@987
|
402 ]
|
bgneal@987
|
403 new_src = [
|
bgneal@987
|
404 'https://cloud.com/some/path/012345.jpg',
|
bgneal@987
|
405 'https://cloud.com/some/path/6789AB.gif',
|
bgneal@987
|
406 'https://cloud.com/some/path/CDEF01.png',
|
bgneal@987
|
407 ]
|
bgneal@987
|
408
|
bgneal@987
|
409 template = """<p>Here is a really cool http: based image:
|
bgneal@987
|
410 <img src="{}" alt="a">
|
bgneal@987
|
411 Cool, right?
|
bgneal@987
|
412 Another one: <img src="{}" alt="b">
|
bgneal@987
|
413 And finally
|
bgneal@987
|
414 <img src="{}" alt="c">
|
bgneal@987
|
415 </p>"""
|
bgneal@987
|
416
|
bgneal@987
|
417 test_str = template.format(*old_src)
|
bgneal@987
|
418 expected = template.format(*new_src)
|
bgneal@987
|
419
|
bgneal@987
|
420 upload_mock.side_effect = new_src
|
bgneal@987
|
421 result = process_html(test_str)
|
bgneal@987
|
422 self.assertEqual(expected, result)
|
bgneal@987
|
423 expected_args = [mock.call(urlparse(c)) for c in old_src]
|
bgneal@987
|
424 self.assertEqual(upload_mock.call_args_list, expected_args)
|
bgneal@987
|
425
|
bgneal@987
|
426 @mock.patch('core.management.commands.ssl_images.save_image_to_cloud')
|
bgneal@987
|
427 def test_multiple_replacement_2(self, upload_mock):
|
bgneal@987
|
428 old_src = [
|
bgneal@987
|
429 'http://example.com/images/my_image.jpg',
|
bgneal@987
|
430 'https://{}/static/wow.gif'.format(self.safe_host),
|
bgneal@987
|
431 'http://www.surfguitar101.com/media/a/b/c/pic.png',
|
bgneal@987
|
432 'http://surfguitar101.com/media/a/b/c/pic2.png',
|
bgneal@987
|
433 ]
|
bgneal@987
|
434 new_src = [
|
bgneal@987
|
435 'https://cloud.com/some/path/012345.jpg',
|
bgneal@987
|
436 'https://{}/static/wow.gif'.format(self.safe_host),
|
bgneal@987
|
437 '/media/a/b/c/pic.png',
|
bgneal@987
|
438 '/media/a/b/c/pic2.png',
|
bgneal@987
|
439 ]
|
bgneal@987
|
440
|
bgneal@987
|
441 template = """<p>Here is a really cool http: based image:
|
bgneal@987
|
442 <img src="{}" alt="a">
|
bgneal@987
|
443 Cool, right?
|
bgneal@987
|
444 Another two: <img src="{}" alt="b"><img src="{}" alt="c">
|
bgneal@987
|
445 And finally
|
bgneal@987
|
446 <img src="{}" alt="d"></p>"""
|
bgneal@987
|
447
|
bgneal@987
|
448 test_str = template.format(*old_src)
|
bgneal@987
|
449 expected = template.format(*new_src)
|
bgneal@987
|
450
|
bgneal@987
|
451 upload_mock.side_effect = new_src
|
bgneal@987
|
452 result = process_html(test_str)
|
bgneal@987
|
453 self.assertEqual(expected, result)
|
bgneal@987
|
454 upload_mock.assert_called_once_with(urlparse(old_src[0]))
|
bgneal@987
|
455
|
bgneal@987
|
456 @mock.patch('core.management.commands.ssl_images.convert_to_ssl')
|
bgneal@987
|
457 def test_change_img_to_a(self, convert_mock):
|
bgneal@987
|
458 convert_mock.return_value = None
|
bgneal@987
|
459 test_str = """<p>A bad image:
|
bgneal@987
|
460 <img src="http://example.com/zzz.png" alt="1" title="the title">
|
bgneal@987
|
461 It's cool.</p>"""
|
bgneal@987
|
462
|
bgneal@987
|
463 result = process_html(test_str)
|
bgneal@987
|
464
|
bgneal@987
|
465 expected = """<p>A bad image:
|
bgneal@987
|
466 <a href="http://example.com/zzz.png">Image</a>
|
bgneal@987
|
467 It's cool.</p>"""
|
bgneal@987
|
468 self.assertEqual(result, expected)
|