Removed unnecessary test for username case sensitivity since django usernames are case sensitive by default. Also made the redirecting to the login url more dynamic by fetching the defined login_url from settings
import pytest
+from django.conf import settings
from django.contrib import messages
from django.contrib.auth.models import AnonymousUser
from django.contrib.messages.middleware import MessageMiddleware
from django.contrib.sessions.middleware import SessionMiddleware
from django.http.response import Http404
from django.test import RequestFactory
+from django.urls import reverse
from {{ cookiecutter.project_slug }}.users.forms import UserChangeForm
from {{ cookiecutter.project_slug }}.users.models import User
@@ -90,13 +92,7 @@ class TestUserDetailView:
request.user = AnonymousUser()
response = user_detail_view(request, username=user.username)
+ login_url = reverse(settings.LOGIN_URL)
assert response.status_code == 302
- assert response.url == "/accounts/login/?next=/fake-url/"
-
- def test_case_sensitivity(self, rf: RequestFactory):
- request = rf.get("/fake-url/")
- request.user = UserFactory(username="UserName")
-
- with pytest.raises(Http404):
- user_detail_view(request, username="username")
+ assert response.url == f"{login_url}?next=/fake-url/"
