I have an Django API that I build quickly, and now I want to add some tests to ensure is stable. The problem is that Django simply refuses to let me access any resource on the server. It can't find any URL.
I have some models, but this test is with the User
model.
# auth/urls.py
urlpatterns = [
...
path(
"user/<str:email>/",
views.UserView.as_view(),
name="public staff user",
),
...
]
# auth/views/UserView.py (Folder working as a submodule)
... # Needed imports
from django.contrib.auth import get_user_model
UserModel = get_user_model()
class UserView(
generics.RetrieveAPIView,
generics.UpdateAPIView,
generics.DestroyAPIView,
):
serializer_class = UserSerializer
lookup_field = "email"
def get_queryset(self):
return UserModel.objects.all()
# auth/tests/tests.py (Folder workign as a submodule, tests.py is a temporary name)
from django.test import TestCase
from django.urls import reverse
from rest_framework.test import APIClient, APITestCase
from authentication.models import User
import os
# Create your tests here.
class TestTestCase(APITestCase):
def setUp(self):
self.staff_user = User.objects.create(
email="test@account.me", is_staff=True, is_active=True
)
self.staff_user_password = "staff_password"
self.staff_user.set_password(self.staff_user_password)
def test_testing(self):
print(User.objects.all())
url = reverse("public staff user", kwargs={"email": self.staff_user.email})
print(url)
response = self.client.get(
url,
)
print(response.content)
I have a few unused imports, but those don't matter. I create a User
instance, and change it's password. Then, in the test, I try to retrieve it's data.
I can confirm the user exists (the shell returns <QuerySet [<User: Employee#000>]>
) and the url works (/api/user/test@account.me/
works with another email in the dev database).
However, response.content
returns the following:
# Escaped for ease of reading
b'\n<!doctype html>\n<html lang="en">\n<head>\n
<title>Not Found</title>\n</head>\n<body>\n
<h1>Not Found</h1>
<p>The requested resource was not found on this server.</p>
\n</body>\n</html>\n'
I confirmed that the client can access any url, whether reversed or hard-coded. Can't POST or PATCH to anything either.
I've been scratching my head for a while now, but I can't find what I'm missing
Edit 1: I've added an edited version of the view. The logic is the same, but I'm not allowed to share the exact code.
Edit 2: I get the UserModel from Django's get_user_model. Added clarification
Edit 3: format='json'
does not change anything