Skip to content

Commit

Permalink
Merge pull request #928 from thunderstore-io/12-05-Add_AddTeamMember_…
Browse files Browse the repository at this point in the history
…APIView

Add AddTeamMember APIView
  • Loading branch information
MythicManiac authored Dec 12, 2023
2 parents f716447 + 3cc16ca commit f2a2960
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 3 deletions.
73 changes: 73 additions & 0 deletions django/thunderstore/api/cyberstorm/tests/test_team.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

import pytest
from django.contrib.auth import get_user_model
from rest_framework.test import APIClient
Expand Down Expand Up @@ -223,3 +225,74 @@ def test_team_service_accounts_api_view__for_member__sorts_results(
assert result[0]["name"] == alice.first_name
assert result[1]["name"] == bob.first_name
assert result[2]["name"] == charlie.first_name


@pytest.mark.django_db
def test_team_add_member__when_adding_a_member__succeeds(
api_client: APIClient,
team: Team,
user: UserType,
):
teamMember = TeamMemberFactory(team=team, role="owner")
api_client.force_authenticate(teamMember.user)

response = api_client.post(
f"/api/cyberstorm/team/{team.name}/members/add/",
json.dumps({"username": user.username, "role": "owner"}),
content_type="application/json",
)

assert response.status_code == 200
response_json = response.json()
assert response_json["username"] == user.username
assert response_json["role"] == "owner"
assert response_json["team"] == team.name


@pytest.mark.django_db
def test_team_add_member__when_adding_a_member__fails_because_team_doesnt_exist(
api_client: APIClient,
team: Team,
user: UserType,
):
teamMember = TeamMemberFactory(team=team, role="owner")
api_client.force_authenticate(teamMember.user)

response = api_client.post(
"/api/cyberstorm/team/FakeTeam/members/add/",
json.dumps({"username": user.username, "role": "owner"}),
content_type="application/json",
)

assert response.status_code == 404
assert response.json()["detail"] == "Not found."


@pytest.mark.django_db
def test_team_add_member__when_adding_a_member__fails_because_user_is_already_in_team(
api_client: APIClient,
team: Team,
user: UserType,
):
teamMember = TeamMemberFactory(team=team, role="owner")
api_client.force_authenticate(teamMember.user)

response1 = api_client.post(
f"/api/cyberstorm/team/{team.name}/members/add/",
json.dumps({"username": user.username, "role": "owner"}),
content_type="application/json",
)

assert response1.status_code == 200

response2 = api_client.post(
f"/api/cyberstorm/team/{team.name}/members/add/",
json.dumps({"username": user.username, "role": "owner"}),
content_type="application/json",
)

assert response2.status_code == 400
assert (
"Team Member with this User and Team already exists."
in response2.json()["__all__"]
)
8 changes: 7 additions & 1 deletion django/thunderstore/api/cyberstorm/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
NamespacePackageListAPIView,
PackageDependantsListAPIView,
)
from .team import TeamDetailAPIView, TeamMembersAPIView, TeamServiceAccountsAPIView
from .team import (
AddTeamMemberAPIView,
TeamDetailAPIView,
TeamMembersAPIView,
TeamServiceAccountsAPIView,
)

__all__ = [
"CommunityDetailAPIView",
Expand All @@ -23,6 +28,7 @@
"PackageVersionReadmeAPIView",
"PackageVersionsAPIView",
"TeamDetailAPIView",
"AddTeamMemberAPIView",
"TeamMembersAPIView",
"TeamServiceAccountsAPIView",
]
57 changes: 55 additions & 2 deletions django/thunderstore/api/cyberstorm/views/team.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from django.contrib.auth import get_user_model
from django.db.models import Q, QuerySet
from rest_framework.exceptions import PermissionDenied
from rest_framework import serializers
from rest_framework.exceptions import PermissionDenied, ValidationError
from rest_framework.generics import ListAPIView, RetrieveAPIView, get_object_or_404
from rest_framework.permissions import IsAuthenticated
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView

from thunderstore.account.models.service_account import ServiceAccount
from thunderstore.api.cyberstorm.serializers import (
Expand All @@ -11,9 +15,15 @@
CyberstormTeamSerializer,
)
from thunderstore.api.ordering import StrictOrderingFilter
from thunderstore.api.utils import CyberstormAutoSchemaMixin
from thunderstore.api.utils import (
CyberstormAutoSchemaMixin,
conditional_swagger_auto_schema,
)
from thunderstore.repository.forms import AddTeamMemberForm
from thunderstore.repository.models.team import Team, TeamMember

User = get_user_model()


class TeamDetailAPIView(CyberstormAutoSchemaMixin, RetrieveAPIView):
serializer_class = CyberstormTeamSerializer
Expand Down Expand Up @@ -52,6 +62,49 @@ def get_queryset(self) -> QuerySet[TeamMember]:
)


class CyberstormTeamAddMemberRequestSerialiazer(serializers.Serializer):
username = serializers.CharField()
role = serializers.ChoiceField(
choices=AddTeamMemberForm.base_fields["role"].choices
)


class CyberstormTeamAddMemberResponseSerialiazer(serializers.Serializer):
username = serializers.CharField(source="user")
role = serializers.CharField()
team = serializers.CharField()


class AddTeamMemberAPIView(APIView):
@conditional_swagger_auto_schema(
request_body=CyberstormTeamAddMemberRequestSerialiazer,
responses={200: CyberstormTeamAddMemberResponseSerialiazer},
operation_id="cyberstorm.team.members.add",
tags=["cyberstorm"],
)
def post(self, request, team_name, format=None):
team = get_object_or_404(Team, name__iexact=team_name)
serializer = CyberstormTeamAddMemberRequestSerialiazer(data=request.data)
serializer.is_valid(raise_exception=True)

form = AddTeamMemberForm(
user=request.user,
data={
**serializer.validated_data,
"team": team.pk,
"user": serializer.validated_data["username"],
},
)

if form.is_valid():
team_member = form.save()
return Response(
CyberstormTeamAddMemberResponseSerialiazer(team_member).data
)
else:
raise ValidationError(form.errors)


class TeamServiceAccountsAPIView(CyberstormAutoSchemaMixin, TeamRestrictedAPIView):
serializer_class = CyberstormServiceAccountSerializer
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
@@ -1,6 +1,7 @@
from django.urls import path

from thunderstore.api.cyberstorm.views import (
AddTeamMemberAPIView,
CommunityDetailAPIView,
CommunityFiltersAPIView,
CommunityListAPIView,
Expand Down Expand Up @@ -62,6 +63,11 @@
TeamMembersAPIView.as_view(),
name="cyberstorm.team.members",
),
path(
"team/<str:team_name>/members/add/",
AddTeamMemberAPIView.as_view(),
name="cyberstorm.team.members.add",
),
path(
"team/<str:team_id>/service-accounts/",
TeamServiceAccountsAPIView.as_view(),
Expand Down

0 comments on commit f2a2960

Please sign in to comment.