diff --git a/django/thunderstore/api/cyberstorm/tests/test_package_listing.py b/django/thunderstore/api/cyberstorm/tests/test_package_listing.py index 6918795a5..007406e42 100644 --- a/django/thunderstore/api/cyberstorm/tests/test_package_listing.py +++ b/django/thunderstore/api/cyberstorm/tests/test_package_listing.py @@ -1,5 +1,6 @@ from datetime import datetime from typing import Optional +from unittest.mock import Mock, patch import pytest from rest_framework.test import APIClient @@ -9,11 +10,13 @@ get_custom_package_listing, ) from thunderstore.community.factories import ( + Community, CommunityFactory, PackageCategoryFactory, PackageListingFactory, ) from thunderstore.repository.factories import ( + NamespaceFactory, PackageRatingFactory, PackageVersionFactory, TeamMemberFactory, @@ -334,3 +337,39 @@ def test_dependency_serializer__when_dependency_is_not_active__censors_icon_and_ def _date_to_z(value: datetime) -> str: return value.strftime("%Y-%m-%dT%H:%M:%S.%fZ") + + +@pytest.mark.django_db +@pytest.mark.parametrize("return_val", [True, False]) +@patch("thunderstore.repository.models.package_version.PackageVersion.is_unavailable") +def test_package_listing_is_unavailable( + is_unavailable_func: Mock, + return_val: bool, + api_client: APIClient, + community: Community, +) -> None: + is_unavailable_func.return_value = return_val + + package = "Mod" + target_ns = NamespaceFactory() + + target_dependency = PackageListingFactory( + community_=community, + package_kwargs={"name": package, "namespace": target_ns}, + ) + + target_package = PackageListingFactory(community_=community) + target_package.package.latest.dependencies.set( + [target_dependency.package.latest.id], + ) + + community_id = target_package.community.identifier + namespace = target_package.package.namespace.name + package_name = target_package.package.name + + url = f"/api/cyberstorm/listing/{community_id}/{namespace}/{package_name}/" + response = api_client.get(url) + response_dependencies = response.json()["dependencies"][0] + + assert "is_unavailable" in response_dependencies + assert response_dependencies["is_unavailable"] == return_val diff --git a/django/thunderstore/api/cyberstorm/views/package_listing.py b/django/thunderstore/api/cyberstorm/views/package_listing.py index 5a8722cde..fdd3c7518 100644 --- a/django/thunderstore/api/cyberstorm/views/package_listing.py +++ b/django/thunderstore/api/cyberstorm/views/package_listing.py @@ -23,6 +23,7 @@ from thunderstore.community.models.package_listing import PackageListing from thunderstore.repository.models.package import get_package_dependants from thunderstore.repository.models.package_version import PackageVersion +from thunderstore.community.models.community import Community class DependencySerializer(serializers.Serializer): @@ -44,6 +45,7 @@ class DependencySerializer(serializers.Serializer): name = serializers.CharField() namespace = serializers.CharField(source="package.namespace.name") version_number = serializers.CharField() + is_unavailable = serializers.SerializerMethodField() def get_description(self, obj: PackageVersion) -> str: return ( @@ -55,6 +57,13 @@ def get_description(self, obj: PackageVersion) -> str: def get_icon_url(self, obj: PackageVersion) -> Optional[str]: return obj.icon.url if obj.is_effectively_active else None + def get_is_unavailable(self, obj: PackageVersion) -> bool: + community = Community.objects.filter( + identifier=obj.community_identifier + ).first() + + return obj.is_unavailable(community) + class TeamSerializer(serializers.Serializer): """