From 8c64e99b996543cfb1ffbc0aa2ee7cbb369d79a7 Mon Sep 17 00:00:00 2001 From: Oksamies Date: Sun, 7 Jan 2024 22:20:44 +0200 Subject: [PATCH] Add DisbandTeamAPIView --- .../api/cyberstorm/tests/test_team.py | 73 +++++++++++++++++++ .../api/cyberstorm/views/__init__.py | 2 + .../thunderstore/api/cyberstorm/views/team.py | 41 ++++++++++- django/thunderstore/api/urls.py | 6 ++ 4 files changed, 120 insertions(+), 2 deletions(-) diff --git a/django/thunderstore/api/cyberstorm/tests/test_team.py b/django/thunderstore/api/cyberstorm/tests/test_team.py index 836237d32..55bafab81 100644 --- a/django/thunderstore/api/cyberstorm/tests/test_team.py +++ b/django/thunderstore/api/cyberstorm/tests/test_team.py @@ -56,6 +56,79 @@ def test_team_api_view__for_inactive_team__returns_404( assert response.status_code == 404 +@pytest.mark.django_db +def test_team_disband__when_disbanding_team__succeeds( + api_client: APIClient, + user: UserType, + team: Team, +): + TeamMemberFactory(team=team, user=user, role="owner") + api_client.force_authenticate(user) + + response = api_client.post( + f"/api/cyberstorm/team/{team.name}/disband/", + json.dumps({"verification": team.name}), + content_type="application/json", + ) + + assert response.status_code == 200 + response_json = response.json() + assert response_json["name"] == team.name + + +@pytest.mark.django_db +def test_team_disband__when_disbanding_team__fails_because_verification_is_invalid( + api_client: APIClient, + user: UserType, + team: Team, +): + TeamMemberFactory(team=team, user=user, role="owner") + api_client.force_authenticate(user) + response = api_client.post( + f"/api/cyberstorm/team/{team.name}/disband/", + json.dumps({"verification": "Bad Verification"}), + content_type="application/json", + ) + + assert response.status_code == 400 + response_json = response.json() + assert "Invalid verification" in response_json["verification"] + + +@pytest.mark.django_db +def test_team_disband__when_disbanding_team__fails_because_team_doesnt_exist( + api_client: APIClient, + user: UserType, +): + api_client.force_authenticate(user) + response = api_client.post( + f"/api/cyberstorm/team/GhostTeam/disband/", + json.dumps({"verification": "GhostTeam"}), + content_type="application/json", + ) + + assert response.status_code == 404 + response_json = response.json() + assert response_json["detail"] == "Not found." + + +@pytest.mark.django_db +def test_team_disband__when_disbanding_team__fails_because_user_is_not_authenticated( + api_client: APIClient, + team: Team, +): + response = api_client.post( + f"/api/cyberstorm/team/{team.name}/disband/", + json.dumps({"verification": "Bad Verification"}), + 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=team.name).count() == 1 + + @pytest.mark.django_db def test_team_edit__when_editing_donation_link__succeeds( api_client: APIClient, diff --git a/django/thunderstore/api/cyberstorm/views/__init__.py b/django/thunderstore/api/cyberstorm/views/__init__.py index 77687a257..0264fe444 100644 --- a/django/thunderstore/api/cyberstorm/views/__init__.py +++ b/django/thunderstore/api/cyberstorm/views/__init__.py @@ -16,6 +16,7 @@ RemoveTeamMemberAPIView, EditTeamMemberAPIView, EditTeamAPIView, + DisbandTeamAPIView, TeamAPIView, TeamCreateAPIView, TeamMemberAddAPIView, @@ -45,4 +46,5 @@ "RemoveTeamMemberAPIView", "EditTeamMemberAPIView", "EditTeamAPIView", + "DisbandTeamAPIView", ] diff --git a/django/thunderstore/api/cyberstorm/views/team.py b/django/thunderstore/api/cyberstorm/views/team.py index 842d59c3d..34b01653f 100644 --- a/django/thunderstore/api/cyberstorm/views/team.py +++ b/django/thunderstore/api/cyberstorm/views/team.py @@ -19,8 +19,7 @@ CyberstormAutoSchemaMixin, conditional_swagger_auto_schema, ) -from thunderstore.repository.forms import AddTeamMemberForm, EditTeamMemberForm, CreateTeamForm, RemoveTeamMemberForm -from thunderstore.repository.forms import AddTeamMemberForm, DonationLinkTeamForm +from thunderstore.repository.forms import AddTeamMemberForm, EditTeamMemberForm, CreateTeamForm, RemoveTeamMemberForm, DonationLinkTeamForm, DisbandTeamForm from thunderstore.repository.models.team import Team, TeamMember @@ -120,6 +119,44 @@ def post(self, request: HttpRequest): raise ValidationError(form.errors) +class CyberstormDisbandTeamRequestSerialiazer(serializers.Serializer): + verification = serializers.CharField() + + +class CyberstormDisbandTeamResponseSerialiazer(serializers.Serializer): + name = serializers.CharField() + + +class DisbandTeamAPIView(APIView): + permission_classes = [IsAuthenticated] + + @conditional_swagger_auto_schema( + request_body=CyberstormDisbandTeamRequestSerialiazer, + responses={200: CyberstormDisbandTeamResponseSerialiazer}, + operation_id="cyberstorm.team.disband", + tags=["cyberstorm"], + ) + def post(self, request: HttpRequest, team_name: str): + serializer = CyberstormDisbandTeamRequestSerialiazer(data=request.data) + serializer.is_valid(raise_exception=True) + + team = get_object_or_404(Team, name__iexact=team_name) + + form = DisbandTeamForm( + user=request.user, + instance=team, + data=serializer.validated_data, + ) + + if form.is_valid(): + form.save() + return Response( + CyberstormDisbandTeamResponseSerialiazer({"name": team_name}).data + ) + else: + raise ValidationError(form.errors) + + class TeamMemberListAPIView(CyberstormAutoSchemaMixin, TeamRestrictedAPIView): serializer_class = CyberstormTeamMemberSerializer filter_backends = [StrictOrderingFilter] diff --git a/django/thunderstore/api/urls.py b/django/thunderstore/api/urls.py index 83a2dc78e..722dadfa7 100644 --- a/django/thunderstore/api/urls.py +++ b/django/thunderstore/api/urls.py @@ -7,6 +7,7 @@ PackageDeprecateAPIView, EditTeamMemberAPIView, EditTeamAPIView, + DisbandTeamAPIView, PackageListingAPIView, PackageListingByCommunityListAPIView, PackageListingByDependencyListAPIView, @@ -121,6 +122,11 @@ TeamMemberListAPIView.as_view(), name="cyberstorm.team.member.list", ), + path( + "team//disband/", + DisbandTeamAPIView.as_view(), + name="cyberstorm.team.disband", + ), path( "team//member/add/", TeamMemberAddAPIView.as_view(),