def test_success_call_validator(self, validator_mock): with self.patch_urlopen() as ur: ct = self.content_type + '; charset=utf-8' ur.headers = {'Content-Type': ct} tasks.fetch_manifest('http://xx.com/manifest.json', self.upload.pk) assert validator_mock.called
def create(self, request, *args, **kwargs): """ Custom create method allowing us to re-use form logic and distinguish packaged app from hosted apps, applying delays to the validation task if necessary. Doesn't rely on any serializer, just forms. """ data = self.request.DATA packaged = 'upload' in data form = (NewPackagedForm(data) if packaged else NewManifestForm(data)) if not form.is_valid(): return Response(form.errors, status=HTTP_400_BAD_REQUEST) if not packaged: upload = FileUpload.objects.create( user=request.user if request.user.is_authenticated() else None) # The hosted app validator is pretty fast. tasks.fetch_manifest(form.cleaned_data['manifest'], upload.pk) else: upload = form.file_upload # The packaged app validator is much heavier. tasks.validator.delay(upload.pk) log.info('Validation created: %s' % upload.pk) self.kwargs = {'pk': upload.pk} # Re-fetch the object, fetch_manifest() might have altered it. upload = self.get_object() serializer = self.get_serializer(upload) status = HTTP_201_CREATED if upload.processed else HTTP_202_ACCEPTED return Response(serializer.data, status=status)
def test_other_url_error(self): reason = Exception('Some other failure.') self.requests_mock.side_effect = RequestException(reason) tasks.fetch_manifest('url', self.upload.pk) self.check_validation( 'No manifest was found at that URL. Check the address and try ' 'again.')
def test_success_call_validator(self, validator_mock): with self.patch_requests() as ur: ct = self.content_type + "; charset=utf-8" ur.headers = {"content-type": ct} tasks.fetch_manifest("http://xx.com/manifest.json", self.upload.pk) assert validator_mock.called
def test_response_too_large(self): with self.patch_urlopen() as ur: content = 'x' * (settings.MAX_WEBAPP_UPLOAD_SIZE + 1) ur.read.return_value = content tasks.fetch_manifest('url', self.upload.pk) self.check_validation('Your manifest must be less than 2097152 bytes.')
def test_http_error(self): self.urlopen_mock.side_effect = urllib2.HTTPError( 'url', 404, 'Not Found', [], None) tasks.fetch_manifest('url', self.upload.pk) self.check_validation( 'No manifest was found at that URL. Check the address and try ' 'again.')
def test_connection_error(self): reason = socket.gaierror(8, 'nodename nor servname provided') self.requests_mock.side_effect = RequestException(reason) tasks.fetch_manifest('url', self.upload.pk) self.check_validation( 'No manifest was found at that URL. Check the address and try ' 'again.')
def test_detail_for_free_extension_webapp(self, validator_mock, requests_mock): content = self.file_content('mozball.owa') response_mock = mock.Mock(status_code=200) response_mock.iter_content.return_value = mock.Mock( next=lambda: content) response_mock.headers = {'content-type': self.content_type} yield response_mock requests_mock.return_value = response_mock validator_mock.return_value = json.dumps(self.validation_ok()) self.upload_file('mozball.owa') upload = FileUpload.objects.get() tasks.fetch_manifest('http://xx.com/manifest.owa', upload.pk) r = self.client.get(reverse('mkt.developers.upload_detail', args=[upload.uuid, 'json'])) data = json.loads(r.content) eq_(data['validation']['messages'], []) # no errors assert_no_validation_errors(data) # no exception eq_(r.status_code, 200) eq_(data['url'], reverse('mkt.developers.upload_detail', args=[upload.uuid, 'json'])) eq_(data['full_report_url'], reverse('mkt.developers.upload_detail', args=[upload.uuid]))
def create(self, request, *args, **kwargs): """ Custom create method allowing us to re-use form logic and distinguish packaged app from hosted apps, applying delays to the validation task if necessary. Doesn't rely on any serializer, just forms. """ data = self.request.data packaged = 'upload' in data form = (NewPackagedForm(data) if packaged else NewManifestForm(data)) if not form.is_valid(): return Response(form.errors, status=HTTP_400_BAD_REQUEST) if not packaged: upload = FileUpload.objects.create( user=request.user if request.user.is_authenticated() else None) # The hosted app validator is pretty fast. tasks.fetch_manifest(form.cleaned_data['manifest'], upload.pk) else: upload = form.file_upload # The packaged app validator is much heavier. tasks.validator.delay(upload.pk) log.info('Validation created: %s' % upload.pk) self.kwargs = {'pk': upload.pk} # Re-fetch the object, fetch_manifest() might have altered it. upload = self.get_object() serializer = self.get_serializer(upload) status = HTTP_201_CREATED if upload.processed else HTTP_202_ACCEPTED return Response(serializer.data, status=status)
def test_url_timeout(self): reason = socket.timeout('too slow') self.requests_mock.side_effect = RequestException(reason) tasks.fetch_manifest('url', self.upload.pk) self.check_validation( 'No manifest was found at that URL. Check the address and try ' 'again.')
def test_connection_error(self): reason = socket.gaierror(8, 'nodename nor servname provided') self.urlopen_mock.side_effect = urllib2.URLError(reason) tasks.fetch_manifest('url', self.upload.pk) self.check_validation( 'No manifest was found at that URL. Check the address and try ' 'again.')
def test_url_timeout(self): reason = socket.timeout('too slow') self.urlopen_mock.side_effect = urllib2.URLError(reason) tasks.fetch_manifest('url', self.upload.pk) self.check_validation( 'No manifest was found at that URL. Check the address and try ' 'again.')
def test_other_url_error(self): reason = Exception('Some other failure.') self.urlopen_mock.side_effect = urllib2.URLError(reason) tasks.fetch_manifest('url', self.upload.pk) self.check_validation( 'No manifest was found at that URL. Check the address and try ' 'again.')
def test_response_too_large(self): with self.patch_requests() as ur: content = "x" * (settings.MAX_WEBAPP_UPLOAD_SIZE + 1) ur.iter_content.return_value = mock.Mock(next=lambda: content) tasks.fetch_manifest("url", self.upload.pk) max_webapp_size = settings.MAX_WEBAPP_UPLOAD_SIZE self.check_validation("Your manifest must be less than %s bytes." % max_webapp_size)
def test_other_url_error(self): reason = Exception('Some other failure.') self.urlopen_mock.side_effect = urllib2.URLError(reason) tasks.fetch_manifest('url', self.upload.pk) self.check_validation( 'No manifest was found at that URL. Check the address and make' ' sure the manifest is served with the HTTP header ' '"Content-Type: application/x-web-app-manifest+json".')
def test_connection_error(self): reason = socket.gaierror(8, 'nodename nor servname provided') self.urlopen_mock.side_effect = urllib2.URLError(reason) tasks.fetch_manifest('url', self.upload.pk) self.check_validation( 'No manifest was found at that URL. Check the address and make' ' sure the manifest is served with the HTTP header ' '"Content-Type: application/x-web-app-manifest+json".')
def test_bad_content_type(self): with self.patch_urlopen() as ur: ur.headers = {'Content-Type': 'x'} tasks.fetch_manifest('url', self.upload.pk) self.check_validation( 'Your manifest must be served with the HTTP header ' '"Content-Type: application/x-web-app-manifest+json". We saw "x".')
def test_non_utf8_encoding(self): with self.patch_requests() as ur: with open(self.file("utf8bom.webapp")) as fp: # Set encoding to utf16 which will be invalid. content = fp.read().decode("utf8").encode("utf16") ur.iter_content.return_value = mock.Mock(next=lambda: content) tasks.fetch_manifest("url", self.upload.pk) self.check_validation("Your manifest file was not encoded as valid UTF-8.")
def test_no_content_type(self): with self.patch_requests() as ur: ur.headers = {} tasks.fetch_manifest('url', self.upload.pk) self.check_validation( 'No manifest was found at that URL. Check the address and try ' 'again.')
def test_success_add_file(self, validator_mock): with self.patch_requests() as ur: ur.iter_content.return_value = mock.Mock(next=lambda: 'woo') tasks.fetch_manifest('http://xx.com/manifest.json', self.upload.pk) upload = FileUpload.objects.get(pk=self.upload.pk) eq_(upload.name, 'http://xx.com/manifest.json') eq_(private_storage.open(upload.path).read(), 'woo')
def test_success_add_file(self, validator_mock): with self.patch_requests() as ur: ur.iter_content.return_value = mock.Mock(next=lambda: 'woo') tasks.fetch_manifest('http://xx.com/manifest.json', self.upload.pk) upload = FileUpload.objects.get(pk=self.upload.pk) eq_(upload.name, 'http://xx.com/manifest.json') eq_(storage.open(upload.path).read(), 'woo')
def test_http_error(self): self.urlopen_mock.side_effect = urllib2.HTTPError( 'url', 404, 'Not Found', [], None) tasks.fetch_manifest('url', self.upload.pk) self.check_validation( 'No manifest was found at that URL. Check the address and make' ' sure the manifest is served with the HTTP header ' '"Content-Type: application/x-web-app-manifest+json".')
def test_bad_charset(self): with self.patch_requests() as ur: ur.headers = {"content-type": "application/x-web-app-manifest+json;" "charset=ISO-1234567890-LOL"} tasks.fetch_manifest("url", self.upload.pk) self.check_validation( "The manifest's encoding does not match the " "charset provided in the HTTP Content-Type." )
def test_non_utf8_encoding(self): with self.patch_urlopen() as ur: with open(self.file('utf8bom.webapp')) as fp: # Set encoding to utf16 which will be invalid ur.read.return_value = fp.read().decode('utf8').encode('utf16') tasks.fetch_manifest('url', self.upload.pk) self.check_validation( 'Your manifest file was not encoded as valid UTF-8')
def test_non_utf8_encoding(self): with self.patch_urlopen() as ur: with open(self.file('utf8bom.webapp')) as fp: # Set encoding to utf16 which will be invalid ur.read.return_value = fp.read().decode('utf8').encode('utf16') tasks.fetch_manifest('url', self.upload.pk) self.check_validation( 'Your manifest file was not encoded as valid UTF-8.')
def test_good_charset(self): with self.patch_urlopen() as ur: ur.headers = { 'Content-Type': 'application/x-web-app-manifest+json;' 'charset=utf-8'} tasks.fetch_manifest('url', self.upload.pk) self.check_validation()
def test_http_error(self): with self.patch_requests() as ur: ur.status_code = 404 tasks.fetch_manifest('url', self.upload.pk) self.check_validation( 'No manifest was found at that URL. Check the address and try ' 'again.')
def test_bad_content_type(self): with self.patch_urlopen() as ur: ur.headers = {'Content-Type': 'x'} tasks.fetch_manifest('url', self.upload.pk) self.check_validation( 'Manifests must be served with the HTTP header "Content-Type: ' 'application/x-web-app-manifest+json". See %s for more ' 'information.' % tasks.CT_URL)
def test_success_add_file(self, validator_mock): with self.patch_urlopen() as ur: ur.read.return_value = 'woo' tasks.fetch_manifest('http://xx.com/manifest.json', self.upload.pk) upload = FileUpload.objects.get(pk=self.upload.pk) eq_(upload.name, 'http://xx.com/manifest.json') eq_(upload.is_webapp, True) eq_(storage.open(upload.path).read(), 'woo')
def test_bad_content_type(self): with self.patch_urlopen() as ur: ur.headers = {'Content-Type': 'x'} tasks.fetch_manifest('url', self.upload.pk) self.check_validation( 'No manifest was found at that URL. Check the address and make' ' sure the manifest is served with the HTTP header ' '"Content-Type: application/x-web-app-manifest+json".')
def obj_create(self, bundle, request=None, **kwargs): form = NewManifestForm(bundle.data) if not form.is_valid(): raise self.form_errors(form) bundle.obj = FileUpload.objects.create(user=amo.get_user()) tasks.fetch_manifest(form.cleaned_data['manifest'], bundle.obj.pk) log.info('Validation created: %s' % bundle.obj.pk) return bundle
def test_bad_charset(self): with self.patch_urlopen() as ur: ur.headers = { 'Content-Type': 'application/x-web-app-manifest+json;' 'charset=ISO-1234567890-LOL'} tasks.fetch_manifest('url', self.upload.pk) self.check_validation("The manifest's encoding does not match the " 'charset provided in the HTTP Content-Type.')
def obj_create(self, bundle, request=None, **kwargs): form = NewManifestForm(bundle.data) if not form.is_valid(): raise self.form_errors(form) bundle.obj = FileUpload.objects.create() tasks.fetch_manifest(form.cleaned_data['manifest'], bundle.obj.pk) log.info('Validation created: %s' % bundle.obj.pk) return bundle
def test_good_charset(self): with self.patch_requests() as ur: ur.headers = { 'content-type': 'application/x-web-app-manifest+json;' 'charset=utf-8' } tasks.fetch_manifest('url', self.upload.pk) self.check_validation()
def test_success_call_validator(self, validator_mock): with self.patch_requests() as ur: ct = self.content_type + '; charset=utf-8' ur.headers = {'content-type': ct} tasks.fetch_manifest('http://xx.com/manifest.json', self.upload.pk) assert validator_mock.called assert self.requests_mock.called eq_(self.requests_mock.call_args[1]['headers'], tasks.REQUESTS_HEADERS)
def test_non_utf8_encoding(self): with self.patch_requests() as ur: with open(self.file('utf8bom.webapp')) as fp: # Set encoding to utf16 which will be invalid. content = fp.read().decode('utf8').encode('utf16') ur.iter_content.return_value = mock.Mock(next=lambda: content) tasks.fetch_manifest('url', self.upload.pk) self.check_validation( 'Your manifest file was not encoded as valid UTF-8.')
def test_bad_content_type(self): with self.patch_requests() as ur: ur.headers = {'Content-Type': 'x'} tasks.fetch_manifest('url', self.upload.pk) self.check_validation( 'Manifests must be served with the HTTP header "Content-Type: ' 'application/x-web-app-manifest+json". See %s for more ' 'information.' % tasks.CT_URL)
def test_response_too_large(self): with self.patch_requests() as ur: content = 'x' * (settings.MAX_WEBAPP_UPLOAD_SIZE + 1) ur.iter_content.return_value = mock.Mock(next=lambda: content) tasks.fetch_manifest('url', self.upload.pk) max_webapp_size = settings.MAX_WEBAPP_UPLOAD_SIZE self.check_validation('Your manifest must be less than %s bytes.' % max_webapp_size)
def test_bad_content_type(self): with self.patch_requests() as ur: ur.headers = {"Content-Type": "x"} tasks.fetch_manifest("url", self.upload.pk) self.check_validation( 'Manifests must be served with the HTTP header "Content-Type: ' 'application/x-web-app-manifest+json". See %s for more ' "information." % tasks.CT_URL )
def test_success_add_file(self, validator_mock): with self.patch_urlopen() as ur: ur.read.return_value = 'woo' ur.headers = {'Content-Type': self.content_type} tasks.fetch_manifest('http://xx.com/manifest.json', self.upload.pk) upload = FileUpload.objects.get(pk=self.upload.pk) eq_(upload.name, 'http://xx.com/manifest.json') eq_(upload.is_webapp, True) eq_(storage.open(upload.path).read(), 'woo')
def test_bad_charset(self): with self.patch_requests() as ur: ur.headers = { 'content-type': 'application/x-web-app-manifest+json;' 'charset=ISO-1234567890-LOL' } tasks.fetch_manifest('url', self.upload.pk) self.check_validation("The manifest's encoding does not match the " 'charset provided in the HTTP Content-Type.')
def test_strip_utf8_bom(self): with self.patch_urlopen() as ur: with open(self.file('utf8bom.webapp')) as fp: ur.read.return_value = fp.read() tasks.fetch_manifest('url', self.upload.pk) upload = self.get_upload() with storage.open(upload.path, 'rb') as fp: manifest = fp.read() json.loads(manifest) # no parse error assert not manifest.startswith(codecs.BOM_UTF8)
def test_strip_utf8_bom(self): with self.patch_requests() as ur: with open(self.file('utf8bom.webapp')) as fp: content = fp.read() ur.iter_content.return_value = mock.Mock(next=lambda: content) tasks.fetch_manifest('url', self.upload.pk) upload = self.get_upload() with storage.open(upload.path, 'rb') as fp: manifest = fp.read() json.loads(manifest) # No parse error. assert not manifest.startswith(codecs.BOM_UTF8)
def test_strip_utf8_bom(self): with self.patch_requests() as ur: with open(self.file('utf8bom.webapp')) as fp: content = fp.read() ur.iter_content.return_value = mock.Mock(next=lambda: content) tasks.fetch_manifest('url', self.upload.pk) # Should not be called with anything else (e.g., `decode_unicode`). ur.iter_content.assert_called_with( chunk_size=settings.MAX_WEBAPP_UPLOAD_SIZE + 1) upload = self.get_upload() with private_storage.open(upload.path, 'rb') as fp: manifest = fp.read() json.loads(manifest) # No parse error. assert not manifest.startswith(codecs.BOM_UTF8)
def test_detail_for_free_extension_webapp(self, validator_mock, urlopen_mock): rs = mock.Mock() rs.read.return_value = self.file_content('mozball.owa') rs.headers = {'Content-Type': 'application/x-web-app-manifest+json'} urlopen_mock.return_value = rs validator_mock.return_value = json.dumps(self.validation_ok()) self.upload_file('mozball.owa') upload = FileUpload.objects.get() tasks.fetch_manifest('http://xx.com/manifest.owa', upload.pk) r = self.client.get(reverse('mkt.developers.upload_detail', args=[upload.uuid, 'json'])) data = json.loads(r.content) eq_(data['validation']['messages'], []) # no errors assert_no_validation_errors(data) # no exception eq_(r.status_code, 200) eq_(data['url'], reverse('mkt.developers.upload_detail', args=[upload.uuid, 'json'])) eq_(data['full_report_url'], reverse('mkt.developers.upload_detail', args=[upload.uuid]))
def obj_create(self, bundle, request=None, **kwargs): packaged = 'upload' in bundle.data form = (NewPackagedForm(bundle.data) if packaged else NewManifestForm(bundle.data)) if not form.is_valid(): raise self.form_errors(form) if not packaged: upload = FileUpload.objects.create( user=getattr(request, 'amo_user', None)) # The hosted app validator is pretty fast. tasks.fetch_manifest(form.cleaned_data['manifest'], upload.pk) else: upload = form.file_upload # The packaged app validator is much heavier. tasks.validator.delay(upload.pk) # This is a reget of the object, we do this to get the refreshed # results if not celery delayed. bundle.obj = FileUpload.uncached.get(pk=upload.pk) log.info('Validation created: %s' % bundle.obj.pk) return bundle
def test_other_url_error(self): reason = Exception('Some other failure.') self.urlopen_mock.side_effect = urllib2.URLError(reason) tasks.fetch_manifest('url', self.upload.pk) self.check_validation('Some other failure.')
def test_http_error(self): self.urlopen_mock.side_effect = urllib2.HTTPError( 'url', 404, 'Not Found', [], None) tasks.fetch_manifest('url', self.upload.pk) self.check_validation('url responded with 404 (Not Found).')