Skip to content

Commit

Permalink
Add tests to UserDeleteView endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksamies committed Dec 13, 2023
1 parent 0eab37c commit 4e5ac17
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions django/thunderstore/api/cyberstorm/tests/test_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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:
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_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

0 comments on commit 4e5ac17

Please sign in to comment.