def test_remove_signed_file_not_exists(self, public_storage_mock): extension = Extension(pk=42, slug='mocked_ext') public_storage_mock.exists.return_value = False extension.remove_signed_file() eq_(public_storage_mock.exists.call_args[0][0], extension.signed_file_path) eq_(public_storage_mock.delete.call_count, 0)
def fake_object(self, data): """Create a fake instance of Extension from ES data.""" obj = Extension(id=data['id']) # Create a fake ExtensionVersion for latest_public_version. obj.latest_public_version = ExtensionVersion( extension=obj, pk=data['latest_public_version']['id'], size=data['latest_public_version'].get('size', 0), status=STATUS_PUBLIC, version=data['latest_public_version']['version'],) # Set basic attributes we'll need on the fake instance using the data # from ES. self._attach_fields( obj, data, ('default_language', 'last_updated', 'slug', 'status', 'version')) obj.disabled = data['is_disabled'] obj.uuid = data['guid'] # Attach translations for all translated attributes. # obj.default_language should be set first for this to work. self._attach_translations( obj, data, ('name', 'description', )) # Some methods might need the raw data from ES, put it on obj. obj.es_data = data return obj
def test_sign_and_move_file_error(self, remove_signed_file_mock, private_storage_mock, sign_app_mock): extension = Extension(uuid='12345678123456781234567812345678') sign_app_mock.side_effect = SigningError with self.assertRaises(SigningError): extension.sign_and_move_file() eq_(remove_signed_file_mock.call_count, 1)
def test_is_public(self): extension = Extension(disabled=False, status=STATUS_PUBLIC) eq_(extension.is_public(), True) for status in (STATUS_NULL, STATUS_PENDING): extension.status = status eq_(extension.is_public(), False)
def fake_object(self, data): """Create a fake instance of Extension from ES data.""" obj = Extension(id=data["id"]) # Create a fake ExtensionVersion for latest_public_version. obj.latest_public_version = ExtensionVersion( extension=obj, pk=data["latest_public_version"]["id"], status=STATUS_PUBLIC, version=data["latest_public_version"]["version"], ) # Set basic attributes we'll need on the fake instance using the data # from ES. self._attach_fields(obj, data, ("default_language", "slug", "status", "version")) obj.uuid = data["guid"] # Attach translations for all translated attributes. # obj.default_language should be set first for this to work. self._attach_translations(obj, data, ("name", "description")) # Some methods might need the raw data from ES, put it on obj. obj.es_data = data return obj
def test_remove_signed_file(self, mocked_public_storage): extension = Extension(pk=42, slug='mocked_ext') mocked_public_storage.exists.return_value = True extension.remove_signed_file() eq_(mocked_public_storage.exists.call_args[0][0], extension.signed_file_path) eq_(mocked_public_storage.delete.call_args[0][0], extension.signed_file_path)
def test_mini_manifest(self): manifest = {'foo': {'bar': 1}} extension = Extension(pk=44, version='0.44.0', manifest=manifest, uuid='abcdefabcdefabcdefabcdefabcdef12') expected_manifest = { 'foo': {'bar': 1}, 'package_path': extension.download_url, } eq_(extension.mini_manifest, expected_manifest) # Make sure that mini_manifest is a deepcopy. extension.mini_manifest['foo']['bar'] = 42 eq_(extension.manifest['foo']['bar'], 1)
def test_sign_and_move_file(self, remove_signed_file_mock, private_storage_mock, sign_app_mock): extension = Extension(uuid='12345678123456781234567812345678') extension.sign_and_move_file() expected_args = ( private_storage_mock.open.return_value, extension.signed_file_path, json.dumps({ 'id': extension.uuid, 'version': 1 }) ) eq_(sign_app_mock.call_args[0], expected_args) eq_(remove_signed_file_mock.call_count, 0)
def tearDown(self): for o in Webapp.objects.all(): o.delete() for o in Extension.objects.all(): o.delete() super(TestMultiSearchView, self).tearDown() # Make sure to delete and unindex *all* things. Normally we wouldn't # care about stray deleted content staying in the index, but they can # have an impact on relevancy scoring so we need to make sure. This # needs to happen after super() has been called since it'll process the # indexing tasks that should happen post_request, and we need to wait # for ES to have done everything before continuing. Webapp.get_indexer().unindexer(_all=True) Extension.get_indexer().unindexer(_all=True) HomescreenIndexer.unindexer(_all=True) self.refresh(('webapp', 'extension', 'homescreen'))
def fake_object(self, data): """Create a fake instance of Extension from ES data.""" obj = Extension(id=data['id']) # Set basic attributes we'll need on the fake instance using the data # from ES. self._attach_fields( obj, data, ('default_language', 'slug', 'status', 'version')) obj.uuid = data['guid'] # Attach translations for all translated attributes. # obj.default_language should be set first for this to work. self._attach_translations( obj, data, ('name', )) # Some methods might need the raw data from ES, put it on obj. obj.es_data = data return obj
def test_upload_new(self): eq_(Extension.objects.count(), 0) upload = self.upload('extension') extension = Extension.from_upload(upload) eq_(extension.version, '0.1') eq_(extension.name, u'My Lîttle Extension') eq_(extension.default_language, 'en-GB') eq_(extension.slug, u'my-lîttle-extension') eq_(extension.filename, 'extension-%s.zip' % extension.version) ok_(extension.filename in extension.file_path) ok_(extension.file_path.startswith(extension.path_prefix)) ok_(private_storage.exists(extension.file_path)) eq_(extension.manifest, self.expected_manifest) eq_(Extension.objects.count(), 1)
def test_upload_new(self): eq_(Extension.objects.count(), 0) upload = self.upload('extension') extension = Extension.from_upload(upload, user=self.user) eq_(extension.version, '0.1') eq_(list(extension.authors.all()), [self.user]) eq_(extension.name, u'My Lîttle Extension') eq_(extension.default_language, 'en-GB') eq_(extension.slug, u'my-lîttle-extension') eq_(extension.filename, 'extension-%s.zip' % extension.version) ok_(extension.filename in extension.file_path) ok_(private_storage.exists(extension.file_path)) eq_(extension.manifest, self.expected_manifest) eq_(Extension.objects.count(), 1)
def test_mini_manifest(self): manifest = { 'author': 'Me', 'description': 'Blah', 'manifest_version': 2, 'name': u'Ëxtension', 'version': '0.44', } extension = Extension(pk=44, version='0.44.0', manifest=manifest, uuid='abcdefabcdefabcdefabcdefabcdef12') expected_manifest = { 'description': 'Blah', 'developer': { 'name': 'Me' }, 'name': u'Ëxtension', 'package_path': extension.download_url, 'version': '0.44', } eq_(extension.mini_manifest, expected_manifest) # Make sure that mini_manifest is a deepcopy. extension.mini_manifest['name'] = u'Faîl' eq_(extension.manifest['name'], u'Ëxtension')
def fake_object(self, data): """Create a fake instance of Extension from ES data.""" obj = Extension(id=data['id']) data['created'] = es_to_datetime(data['created']) data['last_updated'] = es_to_datetime(data['last_updated']) data['modified'] = es_to_datetime(data['modified']) # Create a fake ExtensionVersion for latest_public_version. if data['latest_public_version']: obj.latest_public_version = ExtensionVersion( extension=obj, id=data['latest_public_version']['id'], created=es_to_datetime( data['latest_public_version']['created']), size=data['latest_public_version'].get('size', 0), status=STATUS_PUBLIC, version=data['latest_public_version']['version'], ) # Set basic attributes we'll need on the fake instance using the data # from ES. self._attach_fields( obj, data, ('author', 'created', 'default_language', 'icon_hash', 'last_updated', 'modified', 'slug', 'status', 'version')) obj.deleted = data['is_deleted'] obj.disabled = data['is_disabled'] obj.uuid = data['guid'] # Attach translations for all translated attributes. # obj.default_language should be set first for this to work. self._attach_translations(obj, data, ( 'name', 'description', )) # Some methods might need the raw data from ES, put it on obj. obj.es_data = data return obj
def create(self, request, *args, **kwargs): upload_pk = request.DATA.get('upload', '') if not upload_pk: raise exceptions.ParseError(_('No upload identifier specified.')) if not request.user.is_authenticated(): raise exceptions.PermissionDenied( _('You need to be authenticated to perform this action.')) try: upload = FileUpload.objects.get(pk=upload_pk, user=request.user) except FileUpload.DoesNotExist: raise Http404(_('No such upload.')) if not upload.valid: raise exceptions.ParseError( _('The specified upload has not passed validation.')) try: obj = Extension.from_upload(upload, user=request.user) except ValidationError as e: raise exceptions.ParseError(unicode(e)) log.info('Extension created: %s' % obj.pk) serializer = self.get_serializer(obj) return Response(serializer.data, status=status.HTTP_201_CREATED)
def test_upload_new(self): eq_(Extension.objects.count(), 0) upload = self.upload('extension') extension = Extension.from_upload(upload, user=self.user) ok_(extension.pk) eq_(extension.latest_version, ExtensionVersion.objects.latest('pk')) eq_(Extension.objects.count(), 1) eq_(ExtensionVersion.objects.count(), 1) eq_(list(extension.authors.all()), [self.user]) eq_(extension.name, u'My Lîttle Extension') eq_(extension.default_language, 'en-GB') eq_(extension.description, u'A Dummÿ Extension') eq_(extension.slug, u'my-lîttle-extension') eq_(extension.status, STATUS_PENDING) ok_(extension.uuid) version = extension.latest_version eq_(version.version, '0.1') eq_(version.default_language, 'en-GB') eq_(version.filename, 'extension-%s.zip' % version.version) ok_(version.filename in version.file_path) ok_(private_storage.exists(version.file_path)) eq_(version.manifest, self.expected_manifest)
def tearDown(self): Extension.get_indexer().unindexer(_all=True) super(TestExtensionSearchView, self).tearDown()
def setUp(self): self.indexer = Extension.get_indexer()()
def test_upload_existing(self): extension = self.create_extension() upload = self.upload('extension') with self.assertRaises(NotImplementedError): Extension.from_upload(upload, instance=extension)
def test_upload_no_name(self, manifest_mock): manifest_mock.__get__ = mock.Mock(return_value={'version': '0.1'}) upload = self.upload('extension') with self.assertRaises(ValidationError): Extension.from_upload(upload)
def test_upload_no_name(self, validate_mock): validate_mock.return_value = {'version': '0.1'} upload = self.upload('extension') with self.assertRaises(ParseError): Extension.from_upload(upload, user=self.user)
def test_sign_and_move_file_no_uuid(self, private_storage_mock, sign_app_mock): extension = Extension(uuid='') with self.assertRaises(SigningError): extension.sign_and_move_file()