def get_object(self): queryset = self.filter_queryset(self.get_queryset()) try: reference = PackageReference( namespace=self.kwargs["namespace"], name=self.kwargs["name"], ) except ValueError as e: raise ValidationError(str(e)) obj = get_object_or_404(queryset, **reference.get_filter_kwargs()) self.check_object_permissions(self.request, obj) return obj
def reference(self): from thunderstore.repository.package_reference import PackageReference return PackageReference( namespace=self.owner.name, name=self.name, version=self.version_number, )
def test_api_experimental_upload_package_fail_no_permission( api_client, user, manifest_v1_data, package_category, uploader_identity, community, ): zip_data = _create_test_zip(manifest_v1_data) api_client.force_authenticate(user=user) response = api_client.post( "/api/experimental/package/upload/", { "metadata": json.dumps( { "author_name": uploader_identity.name, "categories": [package_category.slug], "communities": [community.identifier], "has_nsfw_content": True, }, ), "file": SimpleUploadedFile("mod.zip", zip_data), }, HTTP_ACCEPT="application/json", ) print(response.content) assert response.status_code == 400 assert response.json() == { "metadata": {"author_name": ["Object with name=Test_Identity does not exist."]} } namespace = uploader_identity.name name = "name" version = "1.0.0" assert PackageReference(namespace, name, version).exists is False
def get_package(self, package_name): if package_name is None: raise NotFound() reference = PackageReference.parse(package_name) if not reference.instance: raise NotFound() return reference.instance
def __call__(self, value): from thunderstore.repository.package_reference import PackageReference try: reference = PackageReference.parse(value) except ValueError as exc: raise ValidationError(str(exc)) if reference.version is None and self.require_version: raise ValidationError( f"Package reference is missing version: {reference}") if self.resolve and reference.instance is None: raise ValidationError( f"No matching package found for reference: {reference}")
def test_api_experimental_upload_package_fail_invalid_category( api_client, user, manifest_v1_data, package_category, uploader_identity, community, ): zip_data = _create_test_zip(manifest_v1_data) UploaderIdentityMember.objects.create( user=user, identity=uploader_identity, role=UploaderIdentityMemberRole.owner, ) api_client.force_authenticate(user=user) category_slug = f"invalid-{package_category.slug}" response = api_client.post( "/api/experimental/package/upload/", { "metadata": json.dumps( { "author_name": uploader_identity.name, "categories": [category_slug], "communities": [community.identifier], "has_nsfw_content": True, }, ), "file": SimpleUploadedFile("mod.zip", zip_data), }, HTTP_ACCEPT="application/json", ) assert response.status_code == 400 print(response.content) assert response.json() == { "metadata": { "categories": { "0": ["Object not found"] } } } namespace = uploader_identity.name name = "name" version = "1.0.0" assert PackageReference(namespace, name, version).exists is False
def validate(self, data): result = super().validate(data) if not self.uploader.can_user_upload(self.user): raise ValidationError( f"Missing privileges to upload under author {self.uploader.name}" ) reference = PackageReference(self.uploader.name, result["name"], result["version_number"]) if reference.exists: raise ValidationError( "Package of the same name and version already exists") if has_duplicate_packages(result["dependencies"]): raise ValidationError( "Cannot depend on multiple versions of the same package") if does_contain_package(result["dependencies"], reference): raise ValidationError("Package depending on itself is not allowed") return result
def test_manifest_v1_serializer_dependencies_valid(user, manifest_v1_data): reference = PackageReference.parse("actual_package-reference-1.0.0") PackageVersionFactory.create( package=PackageFactory.create( owner=UploaderIdentityFactory.create(name=reference.namespace, ), name=reference.name, ), name=reference.name, version_number=reference.version_str, ) identity = UploaderIdentity.get_or_create_for_user(user) manifest_v1_data["dependencies"] = [str(reference)] serializer = ManifestV1Serializer( user=user, uploader=identity, data=manifest_v1_data, ) assert serializer.is_valid() is True
def test_api_experimental_upload_package_success( api_client, user, manifest_v1_data, package_category, uploader_identity, community, ): zip_data = _create_test_zip(manifest_v1_data) UploaderIdentityMember.objects.create( user=user, identity=uploader_identity, role=UploaderIdentityMemberRole.owner, ) api_client.force_authenticate(user=user) response = api_client.post( "/api/experimental/package/upload/", { "metadata": json.dumps( { "author_name": uploader_identity.name, "categories": [package_category.slug], "communities": [community.identifier], "has_nsfw_content": True, }, ), "file": SimpleUploadedFile("mod.zip", zip_data), }, HTTP_ACCEPT="application/json", ) assert response.status_code == 200 response = response.json() namespace = uploader_identity.name # From manifest_v1_data fixture name = "name" version = "1.0.0" assert PackageReference(namespace, name, version).exists
def test_utils_has_duplicate_packages(collection, expected): collection = [PackageReference.parse(x) for x in collection] assert has_duplicate_packages(collection) == expected
def test_utils_does_contain_package(collection, reference, expected): collection = [PackageReference.parse(x) for x in collection] reference = PackageReference.parse(reference) assert does_contain_package(collection, reference) == expected
def to_internal_value(self, data): try: return PackageReference.parse(str(data)) except ValueError as exc: raise ValidationError(str(exc))