def test_package_upload(user, manifest_v1_data, community): icon_raw = io.BytesIO() icon = Image.new("RGB", (256, 256), "#FF0000") icon.save(icon_raw, format="PNG") readme = "# Test readme".encode("utf-8") manifest = json.dumps(manifest_v1_data).encode("utf-8") files = [ ("README.md", readme), ("icon.png", icon_raw.getvalue()), ("manifest.json", manifest), ] zip_raw = io.BytesIO() with ZipFile(zip_raw, "a", ZIP_DEFLATED, False) as zip_file: for name, data in files: zip_file.writestr(name, data) file_data = {"file": SimpleUploadedFile("mod.zip", zip_raw.getvalue())} identity = UploaderIdentity.get_or_create_for_user(user) form = PackageUploadForm( user=user, identity=identity, files=file_data, community=community, ) assert form.is_valid() version = form.save() assert version.name == manifest_v1_data["name"] assert version.package.owner == identity
def test_package_upload_with_extra_data(user, community, manifest_v1_data): icon_raw = io.BytesIO() icon = Image.new("RGB", (256, 256), "#FF0000") icon.save(icon_raw, format="PNG") readme = "# Test readme".encode("utf-8") manifest = json.dumps(manifest_v1_data).encode("utf-8") files = [ ("README.md", readme), ("icon.png", icon_raw.getvalue()), ("manifest.json", manifest), ] zip_raw = io.BytesIO() with ZipFile(zip_raw, "a", ZIP_DEFLATED, False) as zip_file: for name, data in files: zip_file.writestr(name, data) category = PackageCategory.objects.create( name="Test Category", slug="test-category", community=community, ) file_data = {"file": SimpleUploadedFile("mod.zip", zip_raw.getvalue())} identity = UploaderIdentity.get_or_create_for_user(user) form = PackageUploadForm( user=user, files=file_data, community=community, data={ "categories": [category.pk], "has_nsfw_content": True, "team": identity.name, "communities": [community.identifier], }, ) assert form.is_valid() version = form.save() assert version.name == manifest_v1_data["name"] assert version.package.owner == identity listing = PackageListing.objects.filter(package=version.package).first() assert listing.categories.count() == 1 assert listing.categories.first() == category assert listing.has_nsfw_content is True
def _create_form(self, data) -> PackageUploadForm: request = self.context["request"] metadata = data.get("metadata", {}) return PackageUploadForm( user=request.user, community=request.community, data={ "categories": metadata.get("categories"), "has_nsfw_content": metadata.get("has_nsfw_content"), "team": metadata.get("author_name"), "communities": metadata.get("communities"), }, files={"file": data.get("file")}, )