From 18c615f24f0871a8016c7c904a8790fd270bd80e Mon Sep 17 00:00:00 2001 From: Oksamies Date: Sun, 7 Jan 2024 22:22:25 +0200 Subject: [PATCH] Add TeamCreateAPI view --- .../api/cyberstorm/tests/test_team.py | 60 +++++++++++++++++++ .../api/cyberstorm/views/__init__.py | 2 + .../thunderstore/api/cyberstorm/views/team.py | 37 +++++++++++- django/thunderstore/api/urls.py | 6 ++ 4 files changed, 104 insertions(+), 1 deletion(-) diff --git a/django/thunderstore/api/cyberstorm/tests/test_team.py b/django/thunderstore/api/cyberstorm/tests/test_team.py index fdc417ec1..d5d3c3492 100644 --- a/django/thunderstore/api/cyberstorm/tests/test_team.py +++ b/django/thunderstore/api/cyberstorm/tests/test_team.py @@ -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"] diff --git a/django/thunderstore/api/cyberstorm/views/__init__.py b/django/thunderstore/api/cyberstorm/views/__init__.py index f0a887007..9aec44f17 100644 --- a/django/thunderstore/api/cyberstorm/views/__init__.py +++ b/django/thunderstore/api/cyberstorm/views/__init__.py @@ -13,6 +13,7 @@ from .package_version_list import PackageVersionListAPIView from .team import ( TeamAPIView, + TeamCreateAPIView, TeamMemberAddAPIView, TeamMemberListAPIView, TeamServiceAccountListAPIView, @@ -36,4 +37,5 @@ "PackageRatingRateAPIView", "PackageDeprecateAPIView", "PackageListingEditCategoriesAPIView", + "TeamCreateAPIView", ] diff --git a/django/thunderstore/api/cyberstorm/views/team.py b/django/thunderstore/api/cyberstorm/views/team.py index 71c2d8fc3..6c4d5d81c 100644 --- a/django/thunderstore/api/cyberstorm/views/team.py +++ b/django/thunderstore/api/cyberstorm/views/team.py @@ -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 @@ -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 @@ -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 diff --git a/django/thunderstore/api/urls.py b/django/thunderstore/api/urls.py index 82df98990..c740cdda9 100644 --- a/django/thunderstore/api/urls.py +++ b/django/thunderstore/api/urls.py @@ -15,6 +15,7 @@ PackageVersionListAPIView, PackageVersionReadmeAPIView, TeamAPIView, + TeamCreateAPIView, TeamMemberAddAPIView, TeamMemberListAPIView, TeamServiceAccountListAPIView, @@ -97,6 +98,11 @@ PackageDeprecateAPIView.as_view(), name="cyberstorm.package.deprecate", ), + path( + "teams/create/", + TeamCreateAPIView.as_view(), + name="cyberstorm.teams.create", + ), path( "team//", TeamAPIView.as_view(),