Skip to content

Commit

Permalink
Add CS Package Listing Edit Categories form and view
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksamies committed Jun 24, 2024
1 parent 8d8bd97 commit 3d21030
Show file tree
Hide file tree
Showing 8 changed files with 682 additions and 4 deletions.
14 changes: 13 additions & 1 deletion django/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from copy import copy, deepcopy
from http.server import BaseHTTPRequestHandler
from http.server import HTTPServer as SuperHTTPServer
from typing import Any
from typing import Any, List
from zipfile import ZIP_DEFLATED, ZipFile

import pytest
Expand Down Expand Up @@ -276,6 +276,18 @@ def package_category(community):
)


@pytest.fixture()
def package_categories(community) -> List[PackageCategory]:
return [
PackageCategory.objects.create(community=community, slug=slug, name=name)
for slug, name in [
("cat-1", "Category One"),
("cat-2", "Category Two"),
("cat-3", "Category Three"),
]
]


@pytest.fixture()
def package_listing_section(community):
return PackageListingSection.objects.create(
Expand Down
272 changes: 271 additions & 1 deletion django/thunderstore/api/cyberstorm/tests/test_package_listing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from datetime import datetime
from typing import Optional
from typing import List, Optional

import pytest
from rest_framework.test import APIClient
Expand All @@ -13,11 +14,14 @@
PackageCategoryFactory,
PackageListingFactory,
)
from thunderstore.community.models.package_category import PackageCategory
from thunderstore.community.models.package_listing import PackageListing
from thunderstore.repository.factories import (
PackageRatingFactory,
PackageVersionFactory,
TeamMemberFactory,
)
from thunderstore.repository.models.team import TeamMember


@pytest.mark.django_db
Expand Down Expand Up @@ -332,5 +336,271 @@ def test_dependency_serializer__when_dependency_is_not_active__censors_icon_and_
assert actual["icon_url"] is None


@pytest.mark.django_db
def test_package_listing_edit_categories_view__returns_error_for_non_existent_package_listing(
api_client: APIClient,
team_member: TeamMember,
active_package_listing: PackageListing,
package_categories: List[PackageCategory],
) -> None:
active_package_listing.categories.set(package_categories)
active_package_listing.save()
apl_categories = active_package_listing.categories.all()
assert len(apl_categories) == 3

api_client.force_authenticate(team_member.user)
response = api_client.post(
f"/api/cyberstorm/listing/BAD/{active_package_listing.package.namespace.name}/{active_package_listing.package.name}/edit/categories/",
json.dumps(
{
"current_categories": [x.slug for x in apl_categories],
"new_categories": [
x.slug for x in [apl_categories[0], apl_categories[1]]
],
}
),
content_type="application/json",
)
actual = response.json()

assert actual["detail"] == "Not found."
assert (
len(
set(apl_categories).symmetric_difference(
PackageListing.objects.get(
pk=active_package_listing.pk
).categories.all()
)
)
== 0
)

response = api_client.post(
f"/api/cyberstorm/listing/{active_package_listing.community.identifier}/BAD/{active_package_listing.package.name}/edit/categories/",
json.dumps(
{
"current_categories": [x.slug for x in apl_categories],
"new_categories": [
x.slug for x in [apl_categories[0], apl_categories[1]]
],
}
),
content_type="application/json",
)
actual = response.json()

assert actual["detail"] == "Not found."
assert (
len(
set(apl_categories).symmetric_difference(
PackageListing.objects.get(
pk=active_package_listing.pk
).categories.all()
)
)
== 0
)

response = api_client.post(
f"/api/cyberstorm/listing/{active_package_listing.community.identifier}/{active_package_listing.package.namespace.name}/BAD/edit/categories/",
json.dumps(
{
"current_categories": [x.slug for x in apl_categories],
"new_categories": [
x.slug for x in [apl_categories[0], apl_categories[1]]
],
}
),
content_type="application/json",
)
actual = response.json()

assert actual["detail"] == "Not found."
assert (
len(
set(apl_categories).symmetric_difference(
PackageListing.objects.get(
pk=active_package_listing.pk
).categories.all()
)
)
== 0
)


@pytest.mark.django_db
def test_package_listing_edit_categories_view__correct_values__remove_one_category__succeeds(
api_client: APIClient,
team_member: TeamMember,
active_package_listing: PackageListing,
package_categories: List[PackageCategory],
) -> None:
active_package_listing.categories.set(package_categories)
active_package_listing.save()
apl_categories = active_package_listing.categories.all()
assert len(apl_categories) == 3

api_client.force_authenticate(team_member.user)
response = api_client.post(
f"/api/cyberstorm/listing/{active_package_listing.community.identifier}/{active_package_listing.package.namespace.name}/{active_package_listing.package.name}/edit/categories/",
json.dumps(
{
"current_categories": [x.slug for x in apl_categories],
"new_categories": [
x.slug for x in [apl_categories[0], apl_categories[1]]
],
}
),
content_type="application/json",
)
actual = response.json()

assert actual["categories"] == [
{"id": str(x.id), "name": x.name, "slug": x.slug}
for x in [apl_categories[0], apl_categories[1]]
]


@pytest.mark.django_db
def test_package_listing_edit_categories_view__correct_values__no_user__fails(
api_client: APIClient,
active_package_listing: PackageListing,
package_categories: List[PackageCategory],
) -> None:
active_package_listing.categories.set(package_categories)
active_package_listing.save()
apl_categories = active_package_listing.categories.all()
assert len(apl_categories) == 3

response = api_client.post(
f"/api/cyberstorm/listing/{active_package_listing.community.identifier}/{active_package_listing.package.namespace.name}/{active_package_listing.package.name}/edit/categories/",
json.dumps(
{
"current_categories": [x.slug for x in apl_categories],
"new_categories": [
x.slug for x in [apl_categories[0], apl_categories[1]]
],
}
),
content_type="application/json",
)
actual = response.json()

assert actual["detail"] == "Authentication credentials were not provided."


@pytest.mark.django_db
def test_package_listing_edit_categories_view__wrong_current_categories__fails(
api_client: APIClient,
team_member: TeamMember,
active_package_listing: PackageListing,
package_categories: List[PackageCategory],
) -> None:
active_package_listing.categories.set(package_categories)
active_package_listing.save()
apl_categories = active_package_listing.categories.all()
assert len(apl_categories) == 3

api_client.force_authenticate(team_member.user)
response = api_client.post(
f"/api/cyberstorm/listing/{active_package_listing.community.identifier}/{active_package_listing.package.namespace.name}/{active_package_listing.package.name}/edit/categories/",
json.dumps(
{"current_categories": [apl_categories[0].slug], "new_categories": []}
),
content_type="application/json",
)
actual = response.json()

assert actual["__all__"] == [
"Listings current categories do not match provided ones"
]
assert (
len(
set(apl_categories).symmetric_difference(
PackageListing.objects.get(
pk=active_package_listing.pk
).categories.all()
)
)
== 0
)


@pytest.mark.django_db
def test_package_listing_edit_categories_view__correct_values__remove_all_categories__succeeds(
api_client: APIClient,
team_member: TeamMember,
active_package_listing: PackageListing,
package_categories: List[PackageCategory],
) -> None:
active_package_listing.categories.set(package_categories)
active_package_listing.save()
apl_categories = active_package_listing.categories.all()
assert len(apl_categories) == 3

api_client.force_authenticate(team_member.user)
response = api_client.post(
f"/api/cyberstorm/listing/{active_package_listing.community.identifier}/{active_package_listing.package.namespace.name}/{active_package_listing.package.name}/edit/categories/",
json.dumps(
{
"current_categories": [x.slug for x in apl_categories],
"new_categories": [],
}
),
content_type="application/json",
)
actual = response.json()

assert len(actual["categories"]) == 0


@pytest.mark.django_db
def test_package_listing_edit_categories_view__bad_values__fails(
api_client: APIClient,
team_member: TeamMember,
active_package_listing: PackageListing,
package_categories: List[PackageCategory],
) -> None:
active_package_listing.categories.set(package_categories)
active_package_listing.save()
apl_categories = active_package_listing.categories.all()
assert len(apl_categories) == 3

api_client.force_authenticate(team_member.user)
response = api_client.post(
f"/api/cyberstorm/listing/{active_package_listing.community.identifier}/{active_package_listing.package.namespace.name}/{active_package_listing.package.name}/edit/categories/",
json.dumps(
{
"current_categories": [x.slug for x in apl_categories],
"new_categories": "bad",
}
),
content_type="application/json",
)
actual = response.json()

assert 'Expected a list of items but got type "str".' in str(
actual["new_categories"]
)

response = api_client.post(
f"/api/cyberstorm/listing/{active_package_listing.community.identifier}/{active_package_listing.package.namespace.name}/{active_package_listing.package.name}/edit/categories/",
json.dumps(
{
"current_categories": "bad",
"new_categories": [
x.slug for x in [apl_categories[0], apl_categories[1]]
],
}
),
content_type="application/json",
)
actual = response.json()

assert 'Expected a list of items but got type "str".' in str(
actual["current_categories"]
)


def _date_to_z(value: datetime) -> str:
return value.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
3 changes: 2 additions & 1 deletion django/thunderstore/api/cyberstorm/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .community_list import CommunityListAPIView
from .markdown import PackageVersionChangelogAPIView, PackageVersionReadmeAPIView
from .package import PackageDeprecateAPIView
from .package_listing import PackageListingAPIView
from .package_listing import PackageListingAPIView, PackageListingEditCategoriesAPIView
from .package_listing_list import (
PackageListingByCommunityListAPIView,
PackageListingByDependencyListAPIView,
Expand Down Expand Up @@ -35,4 +35,5 @@
"TeamServiceAccountListAPIView",
"PackageRatingRateAPIView",
"PackageDeprecateAPIView",
"PackageListingEditCategoriesAPIView",
]
Loading

0 comments on commit 3d21030

Please sign in to comment.