11import codecs
2- from contextlib import contextmanager
3- from cStringIO import StringIO
42import json
53import os
64import shutil
75import socket
86import tempfile
97import urllib2
8+ from contextlib import contextmanager
9+ from cStringIO import StringIO
1010
1111from django .conf import settings
1212from django .core import mail
1515import mock
1616from nose .tools import eq_
1717from PIL import Image
18+ from requests import RequestException
1819
1920import amo
2021import amo .tests
@@ -168,6 +169,8 @@ def test_validate_packaged_app(self, _zipfile, _mock):
168169
169170
170171storage_open = storage .open
172+
173+
171174def _mock_hide_64px_icon (path , * args , ** kwargs ):
172175 """
173176 A function that mocks `storage.open` and throws an IOError if you try to
@@ -214,8 +217,8 @@ def setUp(self):
214217 self .upload = FileUpload .objects .create ()
215218 self .content_type = 'application/x-web-app-manifest+json'
216219
217- patcher = mock .patch ('mkt.developers.tasks.urllib2.urlopen ' )
218- self .urlopen_mock = patcher .start ()
220+ patcher = mock .patch ('mkt.developers.tasks.requests.get ' )
221+ self .requests_mock = patcher .start ()
219222 self .addCleanup (patcher .stop )
220223
221224 def get_upload (self ):
@@ -225,19 +228,18 @@ def file(self, name):
225228 return os .path .join (os .path .dirname (__file__ ), 'addons' , name )
226229
227230 @contextmanager
228- def patch_urlopen (self ):
229- response_mock = mock .Mock ()
230- response_mock .getcode .return_value = 200
231- response_mock . read . return_value = '<default>'
232- response_mock .headers = {'Content-Type ' : self .content_type }
231+ def patch_requests (self ):
232+ response_mock = mock .Mock (status_code = 200 )
233+ response_mock .iter_content .return_value = mock . Mock (
234+ next = lambda : '<default>' )
235+ response_mock .headers = {'content-type ' : self .content_type }
233236 yield response_mock
234- self .urlopen_mock .return_value = response_mock
237+ self .requests_mock .return_value = response_mock
235238
236239 @mock .patch ('mkt.developers.tasks.validator' )
237240 def test_success_add_file (self , validator_mock ):
238- with self .patch_urlopen () as ur :
239- ur .read .return_value = 'woo'
240- ur .headers = {'Content-Type' : self .content_type }
241+ with self .patch_requests () as ur :
242+ ur .iter_content .return_value = mock .Mock (next = lambda : 'woo' )
241243
242244 tasks .fetch_manifest ('http://xx.com/manifest.json' , self .upload .pk )
243245 upload = FileUpload .objects .get (pk = self .upload .pk )
@@ -247,9 +249,9 @@ def test_success_add_file(self, validator_mock):
247249
248250 @mock .patch ('mkt.developers.tasks.validator' )
249251 def test_success_call_validator (self , validator_mock ):
250- with self .patch_urlopen () as ur :
252+ with self .patch_requests () as ur :
251253 ct = self .content_type + '; charset=utf-8'
252- ur .headers = {'Content-Type ' : ct }
254+ ur .headers = {'content-type ' : ct }
253255
254256 tasks .fetch_manifest ('http://xx.com/manifest.json' , self .upload .pk )
255257 assert validator_mock .called
@@ -273,31 +275,31 @@ def check_validation(self, msg=''):
273275
274276 def test_connection_error (self ):
275277 reason = socket .gaierror (8 , 'nodename nor servname provided' )
276- self .urlopen_mock .side_effect = urllib2 . URLError (reason )
278+ self .requests_mock .side_effect = RequestException (reason )
277279 tasks .fetch_manifest ('url' , self .upload .pk )
278280 self .check_validation (
279281 'No manifest was found at that URL. Check the address and try '
280282 'again.' )
281283
282284 def test_url_timeout (self ):
283285 reason = socket .timeout ('too slow' )
284- self .urlopen_mock .side_effect = urllib2 . URLError (reason )
286+ self .requests_mock .side_effect = RequestException (reason )
285287 tasks .fetch_manifest ('url' , self .upload .pk )
286288 self .check_validation (
287289 'No manifest was found at that URL. Check the address and try '
288290 'again.' )
289291
290292 def test_other_url_error (self ):
291293 reason = Exception ('Some other failure.' )
292- self .urlopen_mock .side_effect = urllib2 . URLError (reason )
294+ self .requests_mock .side_effect = RequestException (reason )
293295 tasks .fetch_manifest ('url' , self .upload .pk )
294296 self .check_validation (
295297 'No manifest was found at that URL. Check the address and try '
296298 'again.' )
297299
298300 @mock .patch ('mkt.developers.tasks.validator' , lambda uid , ** kw : None )
299301 def test_no_content_type (self ):
300- with self .patch_urlopen () as ur :
302+ with self .patch_requests () as ur :
301303 ur .headers = {}
302304
303305 tasks .fetch_manifest ('url' , self .upload .pk )
@@ -307,7 +309,7 @@ def test_no_content_type(self):
307309
308310 @mock .patch ('mkt.developers.tasks.validator' , lambda uid , ** kw : None )
309311 def test_bad_content_type (self ):
310- with self .patch_urlopen () as ur :
312+ with self .patch_requests () as ur :
311313 ur .headers = {'Content-Type' : 'x' }
312314
313315 tasks .fetch_manifest ('url' , self .upload .pk )
@@ -318,63 +320,67 @@ def test_bad_content_type(self):
318320
319321 @mock .patch ('mkt.developers.tasks.validator' , lambda uid , ** kw : None )
320322 def test_good_charset (self ):
321- with self .patch_urlopen () as ur :
323+ with self .patch_requests () as ur :
322324 ur .headers = {
323- 'Content-Type' : 'application/x-web-app-manifest+json;'
324- 'charset=utf-8' }
325+ 'content-type' : 'application/x-web-app-manifest+json;'
326+ 'charset=utf-8'
327+ }
325328
326329 tasks .fetch_manifest ('url' , self .upload .pk )
327330 self .check_validation ()
328331
329332 @mock .patch ('mkt.developers.tasks.validator' , lambda uid , ** kw : None )
330333 def test_bad_charset (self ):
331- with self .patch_urlopen () as ur :
334+ with self .patch_requests () as ur :
332335 ur .headers = {
333- 'Content-Type' : 'application/x-web-app-manifest+json;'
334- 'charset=ISO-1234567890-LOL' }
336+ 'content-type' : 'application/x-web-app-manifest+json;'
337+ 'charset=ISO-1234567890-LOL'
338+ }
335339
336340 tasks .fetch_manifest ('url' , self .upload .pk )
337341 self .check_validation ("The manifest's encoding does not match the "
338342 'charset provided in the HTTP Content-Type.' )
339343
340344 def test_response_too_large (self ):
341- with self .patch_urlopen () as ur :
345+ with self .patch_requests () as ur :
342346 content = 'x' * (settings .MAX_WEBAPP_UPLOAD_SIZE + 1 )
343- ur .read .return_value = content
347+ ur .iter_content .return_value = mock . Mock ( next = lambda : content )
344348
345349 tasks .fetch_manifest ('url' , self .upload .pk )
346350 max_webapp_size = settings .MAX_WEBAPP_UPLOAD_SIZE
347351 self .check_validation ('Your manifest must be less than %s bytes.' %
348352 max_webapp_size )
349353
350354 def test_http_error (self ):
351- self .urlopen_mock .side_effect = urllib2 .HTTPError (
355+ self .requests_mock .side_effect = urllib2 .HTTPError (
352356 'url' , 404 , 'Not Found' , [], None )
353357 tasks .fetch_manifest ('url' , self .upload .pk )
354358 self .check_validation (
355359 'No manifest was found at that URL. Check the address and try '
356360 'again.' )
357361
358362 def test_strip_utf8_bom (self ):
359- with self .patch_urlopen () as ur :
363+ with self .patch_requests () as ur :
360364 with open (self .file ('utf8bom.webapp' )) as fp :
361- ur .read .return_value = fp .read ()
365+ content = fp .read ()
366+ ur .iter_content .return_value = mock .Mock (next = lambda : content )
362367
363368 tasks .fetch_manifest ('url' , self .upload .pk )
364369 upload = self .get_upload ()
365370 with storage .open (upload .path , 'rb' ) as fp :
366371 manifest = fp .read ()
367- json .loads (manifest ) # no parse error
372+ json .loads (manifest ) # No parse error.
368373 assert not manifest .startswith (codecs .BOM_UTF8 )
369374
370375 def test_non_utf8_encoding (self ):
371- with self .patch_urlopen () as ur :
376+ with self .patch_requests () as ur :
372377 with open (self .file ('utf8bom.webapp' )) as fp :
373- # Set encoding to utf16 which will be invalid
374- ur .read .return_value = fp .read ().decode ('utf8' ).encode ('utf16' )
378+ # Set encoding to utf16 which will be invalid.
379+ content = fp .read ().decode ('utf8' ).encode ('utf16' )
380+ ur .iter_content .return_value = mock .Mock (next = lambda : content )
375381 tasks .fetch_manifest ('url' , self .upload .pk )
376382 self .check_validation (
377- 'Your manifest file was not encoded as valid UTF-8.' )
383+ 'Your manifest file was not encoded as valid UTF-8.' )
378384
379385
380386class TestFetchIcon (BaseWebAppTest ):
@@ -384,9 +390,9 @@ def setUp(self):
384390 self .content_type = 'image/png'
385391 self .apps_path = os .path .join (settings .ROOT , 'apps' , 'devhub' , 'tests' ,
386392 'addons' )
387- patcher = mock .patch ('mkt.developers.tasks.urllib2.urlopen ' )
388- self .urlopen_mock = patcher .start ()
389- self .urlopen_mock .return_value = StringIO ('mozballin' )
393+ patcher = mock .patch ('mkt.developers.tasks.requests.get ' )
394+ self .requests_mock = patcher .start ()
395+ self .requests_mock .return_value = StringIO ('mozballin' )
390396 self .addCleanup (patcher .stop )
391397
392398 def webapp_from_path (self , path ):
@@ -404,13 +410,13 @@ def test_no_icons(self):
404410 path = os .path .join (self .apps_path , 'noicon.webapp' )
405411 iconless_app = self .webapp_from_path (path )
406412 tasks .fetch_icon (iconless_app )
407- assert not self .urlopen_mock .called
413+ assert not self .requests_mock .called
408414
409415 def test_bad_icons (self ):
410416 path = os .path .join (self .apps_path , 'badicon.webapp' )
411417 iconless_app = self .webapp_from_path (path )
412418 tasks .fetch_icon (iconless_app )
413- assert not self .urlopen_mock .called
419+ assert not self .requests_mock .called
414420
415421 def check_icons (self , webapp ):
416422 manifest = webapp .get_manifest_json ()
0 commit comments