Skip to content

Commit

Permalink
Merge pull request #852 from thunderstore-io/cb-team
Browse files Browse the repository at this point in the history
Add CS team endpoint
  • Loading branch information
MythicManiac authored Jul 10, 2023
2 parents 40561ec + 6758cea commit 2e67279
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 5 deletions.
2 changes: 2 additions & 0 deletions django/thunderstore/api/cyberstorm/serializers/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .community import CyberstormCommunitySerializer
from .team import CyberstormTeamMemberSerializer, CyberstormTeamSerializer
from .user import CyberstormUserSerializer
15 changes: 15 additions & 0 deletions django/thunderstore/api/cyberstorm/serializers/team.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from rest_framework import serializers

from .user import CyberstormUserSerializer


class CyberstormTeamMemberSerializer(serializers.Serializer):
user = CyberstormUserSerializer()
role = serializers.CharField()


class CyberstormTeamSerializer(serializers.Serializer):
identifier = serializers.CharField(source="id")
name = serializers.CharField()
members = CyberstormTeamMemberSerializer(many=True)
donation_link = serializers.CharField(required=False)
6 changes: 6 additions & 0 deletions django/thunderstore/api/cyberstorm/serializers/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from rest_framework import serializers


class CyberstormUserSerializer(serializers.Serializer):
identifier = serializers.CharField(source="id")
username = serializers.CharField()
42 changes: 42 additions & 0 deletions django/thunderstore/api/cyberstorm/tests/test_team_detail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest
from rest_framework.test import APIClient

from thunderstore.community.models import CommunitySite
from thunderstore.repository.models.team import Team, TeamMember


@pytest.mark.django_db
def test_api_cyberstorm_team_detail_success(
client: APIClient,
community_site: CommunitySite,
team: Team,
team_member: TeamMember,
):
response = client.get(
f"/api/cyberstorm/team/{team.name}/",
HTTP_HOST=community_site.site.domain,
)
result = response.json()
assert response.status_code == 200
assert team.name == result["name"]
assert team.donation_link == result["donation_link"]

members = team.members.all()
assert len(members) == 1
assert len(members) == len(result["members"])

member = members.first()
assert result["members"][0]["role"] == member.role
assert result["members"][0]["user"]["identifier"] == str(member.user.pk)
assert result["members"][0]["user"]["username"] == member.user.username


@pytest.mark.django_db
def test_api_cyberstorm_team_detail_failure(
client: APIClient, community_site: CommunitySite
):
response = client.get(
f"/api/cyberstorm/team/bad/",
HTTP_HOST=community_site.site.domain,
)
assert response.status_code == 404
6 changes: 1 addition & 5 deletions django/thunderstore/api/cyberstorm/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
from .community_detail import CommunityDetailAPIView
from .community_list import CommunityListAPIView

__all__ = [
"CommunityDetailAPIView",
"CommunityListAPIView",
]
from .team_detail import TeamDetailAPIView
12 changes: 12 additions & 0 deletions django/thunderstore/api/cyberstorm/views/team_detail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from rest_framework.generics import RetrieveAPIView

from thunderstore.api.cyberstorm.serializers import CyberstormTeamSerializer
from thunderstore.repository.models.team import Team


class TeamDetailAPIView(RetrieveAPIView):
permission_classes = []
serializer_class = CyberstormTeamSerializer
queryset = Team.objects.exclude(is_active=False).prefetch_related("members")
lookup_field = "name"
lookup_url_kwarg = "team_id"
6 changes: 6 additions & 0 deletions django/thunderstore/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from thunderstore.api.cyberstorm.views import (
CommunityDetailAPIView,
CommunityListAPIView,
TeamDetailAPIView,
)

cyberstorm_urls = [
Expand All @@ -16,4 +17,9 @@
CommunityDetailAPIView.as_view(),
name="cyberstorm.community.detail",
),
path(
"team/<str:team_id>/",
TeamDetailAPIView.as_view(),
name="cyberstorm.team.detail",
),
]

0 comments on commit 2e67279

Please sign in to comment.