Skip to content

Commit

Permalink
Add EditTeamAPIView
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksamies committed Jun 24, 2024
1 parent 2084bd7 commit 2f11553
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
64 changes: 64 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,70 @@ def test_team_api_view__for_inactive_team__returns_404(
assert response.status_code == 404


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

new_donation_link = "https://example.com"

response = api_client.post(
f"/api/cyberstorm/team/{team.name}/edit/",
json.dumps({"donation_link": new_donation_link}),
content_type="application/json",
)

assert response.status_code == 200
response_json = response.json()
assert response_json["donation_link"] == new_donation_link
assert Team.objects.get(pk=team.pk).donation_link == new_donation_link


@pytest.mark.django_db
def test_team_edit__when_editing_donation_link__fails_because_user_is_not_authenticated(
api_client: APIClient,
team: Team,
):
new_donation_link = "https://example.com"

response = api_client.post(
f"/api/cyberstorm/team/{team.name}/edit/",
json.dumps({"donation_link": new_donation_link}),
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.get(pk=team.pk).donation_link == None


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

new_bad_donation_link = "example.com"

response = api_client.post(
f"/api/cyberstorm/team/{team.name}/edit/",
json.dumps({"donation_link": new_bad_donation_link}),
content_type="application/json",
)

assert response.status_code == 400
response_json = response.json()
assert "Enter a valid URL." in response_json["donation_link"]


@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 @@ -15,6 +15,7 @@
from .team import (
RemoveTeamMemberAPIView,
EditTeamMemberAPIView,
EditTeamAPIView,
TeamAPIView,
TeamCreateAPIView,
TeamMemberAddAPIView,
Expand Down Expand Up @@ -43,4 +44,5 @@
"TeamCreateAPIView",
"RemoveTeamMemberAPIView",
"EditTeamMemberAPIView",
"EditTeamAPIView",
]
37 changes: 37 additions & 0 deletions django/thunderstore/api/cyberstorm/views/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
conditional_swagger_auto_schema,
)
from thunderstore.repository.forms import AddTeamMemberForm, EditTeamMemberForm, CreateTeamForm, RemoveTeamMemberForm
from thunderstore.repository.forms import AddTeamMemberForm, DonationLinkTeamForm
from thunderstore.repository.models.team import Team, TeamMember


Expand All @@ -46,6 +47,42 @@ def check_permissions(self, request: Request) -> None:
if not team.can_user_access(request.user):
raise PermissionDenied()

class CyberstormEditTeamRequestSerialiazer(serializers.Serializer):
donation_link = serializers.CharField(
max_length=Team._meta.get_field("donation_link").max_length,
validators=Team._meta.get_field("donation_link").validators,
)


class CyberstormEditTeamResponseSerialiazer(serializers.Serializer):
donation_link = serializers.CharField()


class EditTeamAPIView(APIView):
permission_classes = [IsAuthenticated]

@conditional_swagger_auto_schema(
request_body=CyberstormEditTeamRequestSerialiazer,
responses={200: CyberstormEditTeamResponseSerialiazer},
operation_id="cyberstorm.team.edit",
tags=["cyberstorm"],
)
def post(self, request: HttpRequest, team_name: str):
serializer = CyberstormEditTeamRequestSerialiazer(data=request.data)
serializer.is_valid(raise_exception=True)
team = get_object_or_404(Team, name__iexact=team_name)
form = DonationLinkTeamForm(
user=request.user,
instance=team,
data=serializer.validated_data,
)

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


class CyberstormTeamCreateRequestSerialiazer(serializers.Serializer):
name = serializers.CharField(
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 @@
CommunityListAPIView,
PackageDeprecateAPIView,
EditTeamMemberAPIView,
EditTeamAPIView,
PackageListingAPIView,
PackageListingByCommunityListAPIView,
PackageListingByDependencyListAPIView,
Expand Down Expand Up @@ -110,6 +111,11 @@
TeamAPIView.as_view(),
name="cyberstorm.team",
),
path(
"team/<str:team_name>/edit/",
EditTeamAPIView.as_view(),
name="cyberstorm.team.edit",
),
path(
"team/<str:team_id>/member/",
TeamMemberListAPIView.as_view(),
Expand Down

0 comments on commit 2f11553

Please sign in to comment.