Skip to content

Commit 018e671

Browse files
committed
Add tests to UserDeleteView endpoint
1 parent c81572c commit 018e671

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import json
2+
3+
import pytest
4+
from django.contrib.auth import get_user_model
5+
from rest_framework.test import APIClient
6+
7+
from thunderstore.core.types import UserType
8+
from thunderstore.repository.factories import UserFactory
9+
10+
User = get_user_model()
11+
12+
13+
@pytest.mark.django_db
14+
def test_user_delete__when_deleting_own_account__succeeds(
15+
api_client: APIClient,
16+
user: UserType,
17+
):
18+
api_client.force_authenticate(user)
19+
response = api_client.post(
20+
"/api/cyberstorm/current-user/delete/",
21+
json.dumps({"verification": user.username}),
22+
content_type="application/json",
23+
)
24+
25+
assert response.status_code == 200
26+
27+
with pytest.raises(User.DoesNotExist) as e:
28+
User.objects.get(pk=user.pk)
29+
assert "User matching query does not exist." in str(e.value)
30+
31+
32+
@pytest.mark.django_db
33+
def test_user_delete__when_deleting_own_account__fails_because_user_is_not_authenticated(
34+
api_client: APIClient,
35+
user: UserType,
36+
):
37+
response = api_client.post(
38+
"/api/cyberstorm/current-user/delete/",
39+
json.dumps({"verification": user.username}),
40+
content_type="application/json",
41+
)
42+
43+
assert response.status_code == 401
44+
response_json = response.json()
45+
assert response_json["detail"] == "Authentication credentials were not provided."
46+
assert User.objects.filter(pk=user.pk).count() == 1
47+
48+
49+
@pytest.mark.django_db
50+
def test_user_delete__when_missing_verification_parameter__fails(
51+
api_client: APIClient,
52+
user: UserType,
53+
):
54+
api_client.force_authenticate(user)
55+
response = api_client.post(
56+
"/api/cyberstorm/current-user/delete/",
57+
json.dumps({"verification": "TotallyNotCorrectUsername"}),
58+
content_type="application/json",
59+
)
60+
61+
assert response.status_code == 400
62+
response_json = response.json()
63+
assert "Invalid verification" in response_json["verification"]
64+
assert User.objects.filter(pk=user.pk).count() == 1

0 commit comments

Comments
 (0)