def test_ascii_names(self): upload = FileUpload.from_post('', u'jétpack.xpi', 0) assert 'xpi' in upload.name upload = FileUpload.from_post('', u'мозила_србија-0.11-fx.xpi', 0) assert 'xpi' in upload.name upload = FileUpload.from_post('', u'フォクすけといっしょ.xpi', 0) assert 'xpi' in upload.name upload = FileUpload.from_post('', u'\u05d0\u05d5\u05e1\u05e3.xpi', 0) assert 'xpi' in upload.name
def get_upload(self, filename=None, abspath=None, validation=None, addon=None, user=None, version=None, with_validation=True): fpath = self.file_fixture_path(filename) with open(abspath if abspath else fpath, 'rb') as fobj: xpi = fobj.read() upload = FileUpload.from_post([xpi], filename=abspath or filename, size=1234) upload.addon = addon upload.user = user upload.version = version if with_validation: # Simulate what fetch_manifest() does after uploading an app. upload.validation = validation or json.dumps({ 'errors': 0, 'warnings': 1, 'notices': 2, 'metadata': {}, 'messages': [] }) upload.save() return upload
def test_webextension_crx_not_a_crx(self): """Test to ensure we raise an explicit exception when a .crx file isn't a true crx (doesn't have to be caught, showing a 500 error is fine).""" data = b'Cr42\x02\x00\x00\x00&\x01\x00\x00\x00\x01\x00\x00' upload = FileUpload.from_post([data], filename='test.crx', size=1234) # We couldn't convert it as it's an invalid or unsupported crx, so # re storing the file as-is. assert upload.hash == 'sha256:%s' % hashlib.sha256(data).hexdigest() storage.delete(upload.path)
def get_upload(self, filename=None, abspath=None, validation=None): xpi = open(abspath if abspath else self.file_path(filename)).read() upload = FileUpload.from_post([xpi], filename=abspath or filename, size=1234) # Simulate what fetch_manifest() does after uploading an app. upload.validation = (validation or json.dumps(dict(errors=0, warnings=1, notices=2, metadata={}, messages=[]))) upload.save() return upload
def get_upload(self, filename=None, abspath=None, validation=None, addon=None, user=None, version=None, with_validation=True): fpath = self.file_fixture_path(filename) with open(abspath if abspath else fpath, 'rb') as fobj: xpi = fobj.read() upload = FileUpload.from_post( [xpi], filename=abspath or filename, size=1234) upload.addon = addon upload.user = user upload.version = version if with_validation: # Simulate what fetch_manifest() does after uploading an app. upload.validation = validation or json.dumps({ 'errors': 0, 'warnings': 1, 'notices': 2, 'metadata': {}, 'messages': [] }) upload.save() return upload
def upload(self, **params): # The data should be in chunks. data = [''.join(x) for x in chunked(self.data, 3)] return FileUpload.from_post(data, 'filename.xpi', len(self.data), **params)
def test_file_upload_passed_auto_validation_failed(): upload = FileUpload(validation=json.dumps({ 'passed_auto_validation': False, })) assert not upload.passed_auto_validation
def test_file_upload_passed_auto_validation_passed(): upload = FileUpload(validation=json.dumps({ 'passed_auto_validation': True, })) assert upload.passed_auto_validation
chunks = [] size = 0 for chunk in req.iter_content(settings.LANGPACK_MAX_SIZE): size += len(chunk) # `requests` doesn't respect the Content-Length header # so we need to check twice. if size > settings.LANGPACK_MAX_SIZE: raise Exception('Response to big') chunks.append(chunk) except Exception, e: log.error('[@None] Error fetching "{0}" language pack: {1}' .format(xpi, e)) return upload = FileUpload() upload.add_file(chunks, xpi, size) lang = os.path.splitext(xpi)[0] # Activate the correct locale for the language pack so it # will be used as the add-on's default locale if available. with translation.override(lang): try: data = parse_addon(upload, check=False) allowed_guid = re.compile(r'^langpack-{0}@' r'[a-z]+\.mozilla\.org$'.format(lang)) assert allowed_guid.match(data['guid']), 'Unexpected GUID' except Exception, e: log.error('[@None] Error parsing "{0}" language pack: {1}'
def create_version(self, addon=None): from olympia.addons.models import Addon from olympia.files.models import FileUpload from olympia.files.utils import parse_addon from olympia.versions.models import Version from olympia.versions.utils import get_next_version_number version_number = '1.0' # If passing an existing add-on, we need to bump the version number # to avoid clashes, and also perform a few checks. if addon is not None: # Obviously we want an add-on with the right type. if addon.type != amo.ADDON_SITE_PERMISSION: raise ImproperlyConfigured( 'SitePermissionVersionCreator was instantiated with non ' 'site-permission add-on' ) # If the user isn't an author, something is wrong. if not addon.authors.filter(pk=self.user.pk).exists(): raise ImproperlyConfigured( 'SitePermissionVersionCreator was instantiated with a ' 'bogus addon/user' ) # Changing the origins isn't supported at the moment. latest_version = addon.find_latest_version( exclude=(), channel=amo.RELEASE_CHANNEL_UNLISTED ) previous_origins = sorted( latest_version.installorigin_set.all().values_list('origin', flat=True) ) if previous_origins != self.install_origins: raise ImproperlyConfigured( 'SitePermissionVersionCreator was instantiated with an ' 'addon that has different origins' ) version_number = get_next_version_number(addon) # Create the manifest, with more user-friendly name & description built # from install_origins/site_permissions, and then the zipfile with that # manifest inside. manifest_data = self._create_manifest(version_number) file_obj = self._create_zipfile(manifest_data) # Parse the zip we just created. The user needs to be the Mozilla User # because regular submissions of this type of add-on is forbidden to # normal users. parsed_data = parse_addon( file_obj, addon=addon, user=get_task_user(), ) with core.override_remote_addr(self.remote_addr): if addon is None: # Create the Addon instance (without a Version/File at this point). addon = Addon.initialize_addon_from_upload( data=parsed_data, upload=file_obj, channel=amo.RELEASE_CHANNEL_UNLISTED, user=self.user, ) # Create the FileUpload that will become the File+Version. upload = FileUpload.from_post( file_obj, filename=file_obj.name, size=file_obj.size, addon=addon, version=version_number, channel=amo.RELEASE_CHANNEL_UNLISTED, user=self.user, source=amo.UPLOAD_SOURCE_GENERATED, ) # And finally create the Version instance from the FileUpload. return Version.from_upload( upload, addon, amo.RELEASE_CHANNEL_UNLISTED, selected_apps=[x[0] for x in amo.APPS_CHOICES], parsed_data=parsed_data, )