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 TeamCreateAPI view #937

Open
wants to merge 1 commit into
base: add-delete-user-endpoint
Choose a base branch
from
Open
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
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing permission classes (user needs to be authenticated)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the new views should now have 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):
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
Loading