|
1 | 1 | from datetime import datetime
|
2 | 2 | from typing import Optional
|
| 3 | +from unittest.mock import Mock, patch |
3 | 4 |
|
4 | 5 | import pytest
|
5 | 6 | from rest_framework.test import APIClient
|
|
9 | 10 | get_custom_package_listing,
|
10 | 11 | )
|
11 | 12 | from thunderstore.community.factories import (
|
| 13 | + Community, |
12 | 14 | CommunityFactory,
|
13 | 15 | PackageCategoryFactory,
|
14 | 16 | PackageListingFactory,
|
15 | 17 | )
|
16 | 18 | from thunderstore.repository.factories import (
|
| 19 | + NamespaceFactory, |
17 | 20 | PackageRatingFactory,
|
18 | 21 | PackageVersionFactory,
|
19 | 22 | TeamMemberFactory,
|
@@ -334,3 +337,39 @@ def test_dependency_serializer__when_dependency_is_not_active__censors_icon_and_
|
334 | 337 |
|
335 | 338 | def _date_to_z(value: datetime) -> str:
|
336 | 339 | return value.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
|
| 340 | + |
| 341 | + |
| 342 | +@pytest.mark.django_db |
| 343 | +@pytest.mark.parametrize("return_val", [True, False]) |
| 344 | +@patch("thunderstore.repository.models.package_version.PackageVersion.is_unavailable") |
| 345 | +def test_package_listing_is_unavailable( |
| 346 | + is_unavailable_func: Mock, |
| 347 | + return_val: bool, |
| 348 | + api_client: APIClient, |
| 349 | + community: Community, |
| 350 | +) -> None: |
| 351 | + is_unavailable_func.return_value = return_val |
| 352 | + |
| 353 | + package = "Mod" |
| 354 | + target_ns = NamespaceFactory() |
| 355 | + |
| 356 | + target_dependency = PackageListingFactory( |
| 357 | + community_=community, |
| 358 | + package_kwargs={"name": package, "namespace": target_ns}, |
| 359 | + ) |
| 360 | + |
| 361 | + target_package = PackageListingFactory(community_=community) |
| 362 | + target_package.package.latest.dependencies.set( |
| 363 | + [target_dependency.package.latest.id], |
| 364 | + ) |
| 365 | + |
| 366 | + community_id = target_package.community.identifier |
| 367 | + namespace = target_package.package.namespace.name |
| 368 | + package_name = target_package.package.name |
| 369 | + |
| 370 | + url = f"/api/cyberstorm/listing/{community_id}/{namespace}/{package_name}/" |
| 371 | + response = api_client.get(url) |
| 372 | + response_dependencies = response.json()["dependencies"][0] |
| 373 | + |
| 374 | + assert "is_unavailable" in response_dependencies |
| 375 | + assert response_dependencies["is_unavailable"] == return_val |
0 commit comments