Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for DisbandTeamAPIView #942

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions django/thunderstore/api/cyberstorm/tests/test_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,79 @@ def test_team_detail_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_membership_permission__for_unauthenticated_user__returns_401(
api_client: APIClient,
Expand Down
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 @@ -11,6 +11,7 @@
)
from .team import (
AddTeamMemberAPIView,
DisbandTeamAPIView,
TeamDetailAPIView,
TeamMembersAPIView,
TeamServiceAccountsAPIView,
Expand All @@ -29,6 +30,7 @@
"PackageVersionsAPIView",
"TeamDetailAPIView",
"AddTeamMemberAPIView",
"DisbandTeamAPIView",
"TeamMembersAPIView",
"TeamServiceAccountsAPIView",
]
41 changes: 40 additions & 1 deletion django/thunderstore/api/cyberstorm/views/team.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib.auth import get_user_model
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 @@ -19,7 +20,7 @@
CyberstormAutoSchemaMixin,
conditional_swagger_auto_schema,
)
from thunderstore.repository.forms import AddTeamMemberForm
from thunderstore.repository.forms import AddTeamMemberForm, DisbandTeamForm
from thunderstore.repository.models.team import Team, TeamMember

User = get_user_model()
Expand Down Expand Up @@ -49,6 +50,44 @@ def check_permissions(self, request: Request) -> None:
raise PermissionDenied()


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 TeamMembersAPIView(CyberstormAutoSchemaMixin, TeamRestrictedAPIView):
serializer_class = CyberstormTeamMemberSerializer
filter_backends = [StrictOrderingFilter]
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 @@ -6,6 +6,7 @@
CommunityFiltersAPIView,
CommunityListAPIView,
CommunityPackageListAPIView,
DisbandTeamAPIView,
NamespacePackageListAPIView,
PackageDependantsListAPIView,
PackageDetailAPIView,
Expand Down Expand Up @@ -58,6 +59,11 @@
TeamDetailAPIView.as_view(),
name="cyberstorm.team.detail",
),
path(
"team/<str:team_name>/disband/",
DisbandTeamAPIView.as_view(),
name="cyberstorm.team.disband",
),
path(
"team/<str:team_id>/members/",
TeamMembersAPIView.as_view(),
Expand Down
Loading