From a983b4bd4694b5c4d86f4aaba1a8fa1c9c001825 Mon Sep 17 00:00:00 2001 From: Oksamies Date: Fri, 8 Dec 2023 20:29:26 +0200 Subject: [PATCH] Add tests to UserDeleteView endpoint --- .../api/cyberstorm/tests/test_user.py | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 django/thunderstore/api/cyberstorm/tests/test_user.py diff --git a/django/thunderstore/api/cyberstorm/tests/test_user.py b/django/thunderstore/api/cyberstorm/tests/test_user.py new file mode 100644 index 000000000..0ae810d2f --- /dev/null +++ b/django/thunderstore/api/cyberstorm/tests/test_user.py @@ -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