def create(self, request): if not waffle.flag_is_active(request, 'accept-webapps'): return rc.BAD_REQUEST form = NewManifestForm(request.POST) if form.is_valid(): # This feels like an awful lot of work. # But first upload the file and do the validation. upload = FileUpload.objects.create() tasks.fetch_manifest(form.cleaned_data['manifest'], upload.pk) # We must reget the object here since the above has # saved changes to the object. upload = FileUpload.uncached.get(pk=upload.pk) # Check it validated correctly. if settings.VALIDATE_ADDONS: validation = json.loads(upload.validation) if validation['errors']: response = rc.BAD_REQUEST response.write(validation) return response # Fetch the addon, the icon and set the user. addon = Addon.from_upload(upload, [Platform.objects.get(id=amo.PLATFORM_ALL.id)]) tasks.fetch_icon(addon) AddonUser(addon=addon, user=request.amo_user).save() addon.update(status=amo.STATUS_PENDING if settings.WEBAPPS_RESTRICTED else amo.STATUS_PUBLIC) else: return _form_error(form) return addon
def test_response_too_large(self): response_mock = mock.Mock() content = 'x' * (settings.MAX_WEBAPP_UPLOAD_SIZE + 1) response_mock.read.return_value = content response_mock.headers = {'Content-Type': self.content_type} self.urlopen_mock.return_value = response_mock tasks.fetch_manifest('url', self.upload.pk) self.check_validation('Your manifest must be less than 2097152 bytes.')
def test_success_call_validator(self, validator_mock): response_mock = mock.Mock() response_mock.read.return_value = 'woo' ct = self.content_type + '; charset=utf-8' response_mock.headers = {'Content-Type': ct} self.urlopen_mock.return_value = response_mock tasks.fetch_manifest('http://xx.com/manifest.json', self.upload.pk) assert validator_mock.called
def test_bad_content_type(self): response_mock = mock.Mock() response_mock.read.return_value = 'woo' response_mock.headers = {'Content-Type': 'x'} self.urlopen_mock.return_value = response_mock 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_success_add_file(self, validator_mock): response_mock = mock.Mock() response_mock.read.return_value = 'woo' response_mock.headers = {'Content-Type': self.content_type} self.urlopen_mock.return_value = response_mock 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_(open(upload.path).read(), 'woo')
def test_no_content_type(self): response_mock = mock.Mock() response_mock.read.return_value = 'woo' response_mock.headers = {} self.urlopen_mock.return_value = response_mock with self.assertRaises(Exception): 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".')
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).')
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('Could not contact host at "url".')
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_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('Connection to "url" timed out.')