def test_replace_metadata(self): old_zip_file = get_test_zipfile('LotsOfFiles') metadata = models.parse_zipfile_metadata(old_zip_file) old_zip_file.seek(0) extension = models.Extension.objects.create_from_metadata(metadata, creator=self.user) version = models.ExtensionVersion(extension=extension, source=File(old_zip_file)) version.parse_metadata_json(metadata) new_zip = version.get_zipfile('r') old_zip = ZipFile(File(old_zip_file), 'r') self.assertEqual(len(old_zip.infolist()), len(new_zip.infolist())) self.assertEqual(new_zip.read("metadata.json"), version.make_metadata_json_string()) for old_info in old_zip.infolist(): if old_info.filename == "metadata.json": continue new_info = new_zip.getinfo(old_info.filename) self.assertEqual(old_zip.read(old_info), new_zip.read(new_info)) self.assertEqual(old_info.date_time, new_info.date_time) old_zip.close() new_zip.close()
def create_version(request, file_source): try: with transaction.atomic(): try: metadata = models.parse_zipfile_metadata(file_source) uuid = metadata['uuid'] except (models.InvalidExtensionData, KeyError), e: messages.error(request, "Invalid extension data: %s" % (e.message,)) raise DatabaseErrorWithMessages try: extension = models.Extension.objects.get(uuid=uuid) except models.Extension.DoesNotExist: extension = models.Extension(creator=request.user) else: if request.user != extension.creator and not request.user.is_superuser: messages.error(request, "An extension with that UUID has already been added.") raise DatabaseErrorWithMessages extension.parse_metadata_json(metadata) extension.save() try: extension.full_clean() except ValidationError, e: raise DatabaseErrorWithMessages(e.messages) version = models.ExtensionVersion.objects.create(extension=extension, source=file_source, status=models.STATUS_UNREVIEWED) version.parse_metadata_json(metadata) version.replace_metadata_json() version.save() return version, []
def test_simple_zipdata_data(self): with get_test_zipfile('SimpleExtension') as f: metadata = models.parse_zipfile_metadata(f) extension = models.Extension.objects.create_from_metadata(metadata, creator=self.user) version = models.ExtensionVersion(extension=extension) version.parse_metadata_json(metadata) self.assertEqual(extension.uuid, "*****@*****.**") self.assertEqual(extension.name, "Test Extension") self.assertEqual(extension.description, "Simple test metadata") self.assertEqual(extension.url, "http://test-metadata.gnome.org")
def test_extra_metadata(self): with get_test_zipfile('ExtraMetadata') as f: metadata = models.parse_zipfile_metadata(f) extension = models.Extension.objects.create_from_metadata(metadata, creator=self.user) version = models.ExtensionVersion(extension=extension) version.parse_metadata_json(metadata) extra = json.loads(version.extra_json_fields) self.assertEqual(extension.uuid, "*****@*****.**") self.assertEqual(extra["extra"], "This is some good data") self.assertTrue("description" not in extra) self.assertTrue("url" not in extra)
def test_extra_metadata(self): with get_test_zipfile('ExtraMetadata') as f: metadata = models.parse_zipfile_metadata(f) extension = models.Extension.objects.create_from_metadata( metadata, creator=self.user) version = models.ExtensionVersion(extension=extension) version.parse_metadata_json(metadata) extra = json.loads(version.extra_json_fields) self.assertEqual(extension.uuid, "*****@*****.**") self.assertEqual(extra["extra"], "This is some good data") self.assertTrue("description" not in extra) self.assertTrue("url" not in extra)
def create_version(request, file_source): try: with transaction.atomic(): try: metadata = models.parse_zipfile_metadata(file_source) uuid = metadata['uuid'] except (models.InvalidExtensionData, KeyError) as e: messages.error(request, "Invalid extension data: %s" % (e.message, )) raise DatabaseErrorWithMessages try: extension = models.Extension.objects.get(uuid=uuid) except models.Extension.DoesNotExist: extension = models.Extension(creator=request.user) else: if request.user != extension.creator and not request.user.is_superuser: messages.error( request, "An extension with that UUID has already been added.") raise DatabaseErrorWithMessages extension.parse_metadata_json(metadata) extension.save() try: extension.full_clean() except ValidationError as e: raise DatabaseErrorWithMessages(e.messages) version = models.ExtensionVersion.objects.create( extension=extension, source=file_source, status=models.STATUS_UNREVIEWED) version.parse_metadata_json(metadata) version.replace_metadata_json() version.save() return version, [] except DatabaseErrorWithMessages as e: return None, e.messages
def test_bad_zipfile_metadata(self): bad_data = StringIO("deadbeef") self.assertRaises(models.InvalidExtensionData, models.parse_zipfile_metadata, bad_data) with get_test_zipfile('TooLarge') as f: with self.assertRaises(models.InvalidExtensionData) as cm: models.parse_zipfile_metadata(f) self.assertEqual(cm.exception.message, "Zip file is too large") with get_test_zipfile('NoMetadata') as f: with self.assertRaises(models.InvalidExtensionData) as cm: models.parse_zipfile_metadata(f) self.assertEqual(cm.exception.message, "Missing metadata.json") with get_test_zipfile('BadMetadata') as f: with self.assertRaises(models.InvalidExtensionData) as cm: models.parse_zipfile_metadata(f) self.assertEqual(cm.exception.message, "Invalid JSON data")
def test_replace_metadata(self): old_zip_file = get_test_zipfile('LotsOfFiles') metadata = models.parse_zipfile_metadata(old_zip_file) old_zip_file.seek(0) extension = models.Extension.objects.create_from_metadata( metadata, creator=self.user) version = models.ExtensionVersion.objects.create( extension=extension, source=File(old_zip_file), status=models.STATUS_UNREVIEWED) version.parse_metadata_json(metadata) version.replace_metadata_json() version.save() new_zip = version.get_zipfile('r') old_zip = ZipFile(old_zip_file, 'r') self.assertEqual(len(old_zip.infolist()), len(new_zip.infolist())) self.assertEqual( new_zip.read("metadata.json").decode('utf-8'), version.make_metadata_json_string()) for old_info in old_zip.infolist(): if old_info.filename == "metadata.json": continue new_info = new_zip.getinfo(old_info.filename) self.assertEqual(old_zip.read(old_info), new_zip.read(new_info)) self.assertEqual(old_info.date_time, new_info.date_time) old_zip.close() new_zip.close()
def test_bad_zipfile_metadata(self): bad_data = BytesIO(b"deadbeef") self.assertRaises(models.InvalidExtensionData, models.parse_zipfile_metadata, bad_data) with get_test_zipfile('TooLarge') as f: with self.assertRaisesMessage(models.InvalidExtensionData, "Zip file is too large"): models.parse_zipfile_metadata(f) with get_test_zipfile('NoMetadata') as f: with self.assertRaisesMessage(models.InvalidExtensionData, "Missing metadata.json"): models.parse_zipfile_metadata(f) with get_test_zipfile('BadMetadata') as f: with self.assertRaisesMessage(models.InvalidExtensionData, "Invalid JSON data"): models.parse_zipfile_metadata(f)