Skip to content

Commit

Permalink
✅ - fix: fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Roeland committed Jan 16, 2025
1 parent 7b02389 commit 4fe0390
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.test import override_settings

from rest_framework import status
Expand All @@ -19,12 +20,30 @@ def test_no_csrf_token(self):
login_url = reverse("api:authentication:login")

client = CSRFAPIClient()

# Preventing the edge case for this test. The edge case is:
# 1. No session cookie
# 2. API returns a 403 forbidden
# 3. API Will instead return a "session expired" message
# Setting the session cookie will prevent this edge case so we can actually test the CSRF
session_cookie_name = settings.SESSION_COOKIE_NAME
client.cookies[session_cookie_name] = "test"
response = client.post(login_url, data={})

self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertEqual(response.json()["detail"], "CSRF Failed: CSRF cookie not set.")

def test_no_session_cookie(self):
login_url = reverse("api:authentication:login")

client = CSRFAPIClient()

response = client.post(login_url, data={})

self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertEqual(
response.json()["detail"], "Your session has expired, please log in again."
)

def test_no_credentials_given(self):
login_url = reverse("api:authentication:login")

Expand Down

0 comments on commit 4fe0390

Please sign in to comment.