Skip to content

Commit

Permalink
Add TeamCreateAPI view
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksamies committed Sep 10, 2024
1 parent 6f34766 commit 18c615f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
60 changes: 60 additions & 0 deletions django/thunderstore/api/cyberstorm/tests/test_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,63 @@ def test_team_member_add_api_view__when_adding_a_member__fails_because_user_is_n
.count()
== 0
)


@pytest.mark.django_db
def test_team_create__when_creating_a_team__succeeds(
api_client: APIClient,
user: UserType,
):
api_client.force_authenticate(user)

response = api_client.post(
"/api/cyberstorm/teams/create/",
json.dumps({"name": "CoolestTeamNameEver"}),
content_type="application/json",
)

assert response.status_code == 200
response_json = response.json()
assert response_json["name"] == "CoolestTeamNameEver"
assert (
Team.objects.get(name="CoolestTeamNameEver")
.members.filter(user__username=user.username)
.count()
== 1
)


@pytest.mark.django_db
def test_team_create__when_creating_a_team__fails_because_user_is_not_authenticated(
api_client: APIClient,
user: UserType,
):
response = api_client.post(
"/api/cyberstorm/teams/create/",
json.dumps({"name": "CoolestTeamNameEver"}),
content_type="application/json",
)

assert response.status_code == 401
response_json = response.json()
assert response_json["detail"] == "Authentication credentials were not provided."
assert Team.objects.filter(name="CoolestTeamNameEver").count() == 0


@pytest.mark.django_db
def test_team_create__when_creating_a_team__fails_because_team_with_provided_name_exists(
api_client: APIClient,
user: UserType,
team: Team,
):
api_client.force_authenticate(user)

response = api_client.post(
"/api/cyberstorm/teams/create/",
json.dumps({"name": team.name}),
content_type="application/json",
)

assert response.status_code == 400
response_json = response.json()
assert "A team with the provided name already exists" in response_json["name"]
2 changes: 2 additions & 0 deletions django/thunderstore/api/cyberstorm/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .package_version_list import PackageVersionListAPIView
from .team import (
TeamAPIView,
TeamCreateAPIView,
TeamMemberAddAPIView,
TeamMemberListAPIView,
TeamServiceAccountListAPIView,
Expand All @@ -36,4 +37,5 @@
"PackageRatingRateAPIView",
"PackageDeprecateAPIView",
"PackageListingEditCategoriesAPIView",
"TeamCreateAPIView",
]
37 changes: 36 additions & 1 deletion django/thunderstore/api/cyberstorm/views/team.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db.models import Q, QuerySet
from django.http import HttpRequest
from rest_framework import serializers
from rest_framework.exceptions import PermissionDenied, ValidationError
from rest_framework.generics import ListAPIView, RetrieveAPIView, get_object_or_404
Expand All @@ -18,7 +19,7 @@
CyberstormAutoSchemaMixin,
conditional_swagger_auto_schema,
)
from thunderstore.repository.forms import AddTeamMemberForm
from thunderstore.repository.forms import AddTeamMemberForm, CreateTeamForm
from thunderstore.repository.models.team import Team, TeamMember


Expand All @@ -45,6 +46,40 @@ def check_permissions(self, request: Request) -> None:
raise PermissionDenied()


class CyberstormTeamCreateRequestSerialiazer(serializers.Serializer):
name = serializers.CharField(
max_length=Team._meta.get_field("name").max_length,
validators=Team._meta.get_field("name").validators,
)


class CyberstormTeamCreateResponseSerialiazer(serializers.Serializer):
name = serializers.CharField()


class TeamCreateAPIView(APIView):
@conditional_swagger_auto_schema(
request_body=CyberstormTeamCreateRequestSerialiazer,
responses={200: CyberstormTeamCreateResponseSerialiazer},
operation_id="cyberstorm.teams.create",
tags=["cyberstorm"],
)
def post(self, request: HttpRequest):
serializer = CyberstormTeamCreateRequestSerialiazer(data=request.data)
serializer.is_valid(raise_exception=True)

form = CreateTeamForm(
user=request.user,
data=serializer.validated_data,
)

if form.is_valid():
team = form.save()
return Response(CyberstormTeamCreateResponseSerialiazer(team).data)
else:
raise ValidationError(form.errors)


class TeamMemberListAPIView(CyberstormAutoSchemaMixin, TeamRestrictedAPIView):
permission_classes = [AllowAny]
serializer_class = CyberstormTeamMemberSerializer
Expand Down
6 changes: 6 additions & 0 deletions django/thunderstore/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
PackageVersionListAPIView,
PackageVersionReadmeAPIView,
TeamAPIView,
TeamCreateAPIView,
TeamMemberAddAPIView,
TeamMemberListAPIView,
TeamServiceAccountListAPIView,
Expand Down Expand Up @@ -97,6 +98,11 @@
PackageDeprecateAPIView.as_view(),
name="cyberstorm.package.deprecate",
),
path(
"teams/create/",
TeamCreateAPIView.as_view(),
name="cyberstorm.teams.create",
),
path(
"team/<str:team_id>/",
TeamAPIView.as_view(),
Expand Down

0 comments on commit 18c615f

Please sign in to comment.