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 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.assertEquals(extension.uuid, "*****@*****.**") self.assertEquals(extension.name, "Test Extension") self.assertEquals(extension.description, "Simple test metadata") self.assertEquals(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.assertEquals(extension.uuid, "*****@*****.**") self.assertEquals(extra["extra"], "This is some good data") self.assertTrue("description" not in extra) self.assertTrue("url" not in extra)
def upload_file(request, pk): if pk is None: extension = models.Extension(creator=request.user) else: extension = models.Extension.objects.get(pk=pk) if extension.creator != request.user: return HttpResponseForbidden() if request.method == 'POST': form = UploadForm(request.POST, request.FILES) if form.is_valid(): file_source = form.cleaned_data['source'] try: metadata = models.parse_zipfile_metadata(file_source) uuid = metadata['uuid'] except (models.InvalidExtensionData, KeyError), e: messages.error(request, "Invalid extension data.") return redirect('extensions-upload-file', pk=pk) existing = models.Extension.objects.filter(uuid=uuid) if pk is None and existing.exists(): # Error out if we already have an extension with the same # uuid -- or correct their mistake if they're the same user. ext = existing.get() if request.user == ext.creator: return redirect('extensions-upload-file', pk=ext.pk) else: messages.error(request, "An extension with that UUID has already been added.") return redirect('extensions-upload-file') version = models.ExtensionVersion() version.extension = extension version.parse_metadata_json(metadata) extension.creator = request.user extension.save() version.extension = extension version.source = file_source version.status = models.STATUS_NEW version.save() return redirect('extensions-version-detail', pk=version.pk, ext_pk=extension.pk, slug=extension.slug)
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.assertEquals(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.assertEquals(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.assertEquals(cm.exception.message, "Invalid JSON data")