diff --git a/django/thunderstore/api/cyberstorm/tests/test_team.py b/django/thunderstore/api/cyberstorm/tests/test_team.py index fdd6f962c..64cd9a210 100644 --- a/django/thunderstore/api/cyberstorm/tests/test_team.py +++ b/django/thunderstore/api/cyberstorm/tests/test_team.py @@ -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, diff --git a/django/thunderstore/api/cyberstorm/views/__init__.py b/django/thunderstore/api/cyberstorm/views/__init__.py index 3b216d6e4..238d9f2c5 100644 --- a/django/thunderstore/api/cyberstorm/views/__init__.py +++ b/django/thunderstore/api/cyberstorm/views/__init__.py @@ -12,6 +12,7 @@ from .package_rating import PackageRatingRateAPIView from .package_version_list import PackageVersionListAPIView from .team import ( + EditTeamAPIView, EditTeamMemberAPIView, RemoveTeamMemberAPIView, TeamAPIView, @@ -42,4 +43,5 @@ "TeamCreateAPIView", "RemoveTeamMemberAPIView", "EditTeamMemberAPIView", + "EditTeamAPIView", ] diff --git a/django/thunderstore/api/cyberstorm/views/team.py b/django/thunderstore/api/cyberstorm/views/team.py index 86877870a..07e2300da 100644 --- a/django/thunderstore/api/cyberstorm/views/team.py +++ b/django/thunderstore/api/cyberstorm/views/team.py @@ -22,6 +22,7 @@ from thunderstore.repository.forms import ( AddTeamMemberForm, CreateTeamForm, + DonationLinkTeamForm, EditTeamMemberForm, RemoveTeamMemberForm, ) @@ -52,6 +53,43 @@ def check_permissions(self, request: Request) -> None: 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( max_length=Team._meta.get_field("name").max_length, diff --git a/django/thunderstore/api/urls.py b/django/thunderstore/api/urls.py index 13392fd49..cb82a8252 100644 --- a/django/thunderstore/api/urls.py +++ b/django/thunderstore/api/urls.py @@ -4,6 +4,7 @@ CommunityAPIView, CommunityFiltersAPIView, CommunityListAPIView, + EditTeamAPIView, EditTeamMemberAPIView, PackageDeprecateAPIView, PackageListingAPIView, @@ -110,6 +111,11 @@ TeamAPIView.as_view(), name="cyberstorm.team", ), + path( + "team//edit/", + EditTeamAPIView.as_view(), + name="cyberstorm.team.edit", + ), path( "team//member/", TeamMemberListAPIView.as_view(),