Test user detail view
import pytest
+from django.contrib.auth.models import AnonymousUser
+from django.http.response import Http404
from django.test import RequestFactory
from {{ cookiecutter.project_slug }}.users.models import User
-from {{ cookiecutter.project_slug }}.users.views import UserRedirectView, UserUpdateView
+from {{ cookiecutter.project_slug }}.users.tests.factories import UserFactory
+from {{ cookiecutter.project_slug }}.users.views import (
+ UserRedirectView,
+ UserUpdateView,
+ user_detail_view,
+)
pytestmark = pytest.mark.django_db
@@ -44,3 +51,29 @@ class TestUserRedirectView:
view.request = request
assert view.get_redirect_url() == f"/users/{user.username}/"
+
+
+class TestUserDetailView:
+ def test_authenticated(self, user: User, rf: RequestFactory):
+ request = rf.get("/fake-url/")
+ request.user = UserFactory()
+
+ response = user_detail_view(request, username=user.username)
+
+ assert response.status_code == 200
+
+ def test_not_authenticated(self, user: User, rf: RequestFactory):
+ request = rf.get("/fake-url/")
+ request.user = AnonymousUser() # type: ignore
+
+ response = user_detail_view(request, username=user.username)
+
+ assert response.status_code == 302
+ assert response.url == f"/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")
