Skip to content

Commit

Permalink
Add TeamCreateAPI view
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksamies committed Jan 7, 2024
1 parent 463470a commit f5c8062
Show file tree
Hide file tree
Showing 4 changed files with 106 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 @@ -11,6 +11,7 @@
from .package_version_list import PackageVersionListAPIView
from .team import (
TeamAPIView,
TeamCreateAPIView,
TeamMemberAddAPIView,
TeamMemberListAPIView,
TeamServiceAccountListAPIView,
Expand All @@ -31,4 +32,5 @@
"TeamMemberAddAPIView",
"TeamMemberListAPIView",
"TeamServiceAccountListAPIView",
"TeamCreateAPIView",
]
39 changes: 38 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, CreateTeamForm
from thunderstore.repository.models.team import Team, TeamMember

User = get_user_model()
Expand Down Expand Up @@ -49,6 +50,42 @@ 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):
permission_classes = [IsAuthenticated]

@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):
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 @@ -12,6 +12,7 @@
PackageVersionListAPIView,
PackageVersionReadmeAPIView,
TeamAPIView,
TeamCreateAPIView,
TeamMemberAddAPIView,
TeamMemberListAPIView,
TeamServiceAccountListAPIView,
Expand Down Expand Up @@ -78,6 +79,11 @@
PackageVersionListAPIView.as_view(),
name="cyberstorm.package.versions",
),
path(
"teams/create/",
TeamCreateAPIView.as_view(),
name="cyberstorm.teams.create",
),
path(
"team/<str:team_id>/",
TeamAPIView.as_view(),
Expand Down

0 comments on commit f5c8062

Please sign in to comment.