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 tests to UserDeleteView endpoint #935

Closed
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
64 changes: 64 additions & 0 deletions django/thunderstore/api/cyberstorm/tests/test_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import json

import pytest
from django.contrib.auth import get_user_model
from rest_framework.test import APIClient

from thunderstore.core.types import UserType
from thunderstore.repository.factories import UserFactory

User = get_user_model()


@pytest.mark.django_db
def test_user_delete__when_deleting_own_account__succeeds(
api_client: APIClient,
user: UserType,
):
api_client.force_authenticate(user)
response = api_client.post(
"/api/cyberstorm/current-user/delete/",
json.dumps({"verification": user.username}),
content_type="application/json",
)

assert response.status_code == 200

with pytest.raises(User.DoesNotExist) as e:
anttimaki marked this conversation as resolved.
Show resolved Hide resolved
User.objects.get(pk=user.pk)
assert "User matching query does not exist." in str(e.value)


@pytest.mark.django_db
def test_user_delete__when_deleting_own_account__fails_because_user_is_not_authenticated(
api_client: APIClient,
user: UserType,
):
response = api_client.post(
"/api/cyberstorm/current-user/delete/",
json.dumps({"verification": user.username}),
content_type="application/json",
)

assert response.status_code == 401
anttimaki marked this conversation as resolved.
Show resolved Hide resolved
response_json = response.json()
assert response_json["detail"] == "Authentication credentials were not provided."
assert User.objects.filter(pk=user.pk).count() == 1


@pytest.mark.django_db
def test_user_delete__when_missing_verification_parameter__fails(
api_client: APIClient,
user: UserType,
):
api_client.force_authenticate(user)
response = api_client.post(
"/api/cyberstorm/current-user/delete/",
json.dumps({"verification": "TotallyNotCorrectUsername"}),
content_type="application/json",
)

assert response.status_code == 400
response_json = response.json()
assert "Invalid verification" in response_json["verification"]
assert User.objects.filter(pk=user.pk).count() == 1
40 changes: 40 additions & 0 deletions django/thunderstore/api/cyberstorm/views/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from django.http import HttpRequest
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from thunderstore.api.utils import conditional_swagger_auto_schema
from thunderstore.social.views import DeleteAccountForm


class CyberstormUserDeleteRequestSerialiazer(serializers.Serializer):
verification = serializers.CharField()


class CyberstormUserDeleteResponseSerialiazer(serializers.Serializer):
username = serializers.CharField()


class UserDeleteAPIView(APIView):
permission_classes = [IsAuthenticated]

@conditional_swagger_auto_schema(
request_body=CyberstormUserDeleteRequestSerialiazer,
responses={200: CyberstormUserDeleteResponseSerialiazer},
operation_id="cyberstorm.current-user.delete",
tags=["cyberstorm"],
)
def post(self, request: HttpRequest):
serializer = CyberstormUserDeleteRequestSerialiazer(data=request.data)
serializer.is_valid(raise_exception=True)
form = DeleteAccountForm(
user=request.user,
data=serializer.validated_data,
)
if form.is_valid():
form.delete_user()
return Response()
else:
raise ValidationError(form.errors)
6 changes: 6 additions & 0 deletions django/thunderstore/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
TeamMembersAPIView,
TeamServiceAccountsAPIView,
)
from thunderstore.api.cyberstorm.views.user import UserDeleteAPIView

cyberstorm_urls = [
path(
Expand Down Expand Up @@ -101,4 +102,9 @@
PackageVersionsAPIView.as_view(),
name="cyberstorm.package.versions",
),
path(
"current-user/delete/",
UserDeleteAPIView.as_view(),
name="cyberstorm.current-user.delete",
),
]
5 changes: 4 additions & 1 deletion django/thunderstore/social/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def clean_verification(self):
raise forms.ValidationError("Invalid verification")
return data

def delete_user(self):
self.user.delete()


class DeleteAccountView(SettingsViewMixin, RequireAuthenticationMixin, FormView):
template_name = "settings/delete_account.html"
Expand All @@ -66,5 +69,5 @@ def get_form_kwargs(self, *args, **kwargs):
return kwargs

def form_valid(self, form):
self.request.user.delete()
form.delete_user()
return super().form_valid(form)
Loading