|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | +from django.contrib.sessions.backends.cache import SessionStore |
| 5 | +from django.test import RequestFactory |
| 6 | +from requests import codes |
| 7 | + |
| 8 | +from apps.core.api.csrf import get_csrf_token |
| 9 | + |
| 10 | + |
| 11 | +class TestGetCSRFTokenView: |
| 12 | + @pytest.fixture(autouse=True) |
| 13 | + def setup_client(self): |
| 14 | + self.factory = RequestFactory() |
| 15 | + |
| 16 | + def _make_request_with_session(self, path="/", method="get"): |
| 17 | + request = self.factory.post(path) if method.lower() == "post" else self.factory.get(path) |
| 18 | + |
| 19 | + request.session = SessionStore() |
| 20 | + request.session.create() |
| 21 | + |
| 22 | + return request |
| 23 | + |
| 24 | + def _assert_valid_csrf_response(self, response): |
| 25 | + assert response.status_code == codes.ok |
| 26 | + assert response["Content-Type"] == "application/json" |
| 27 | + |
| 28 | + data = json.loads(response.content) |
| 29 | + assert "csrftoken" in data |
| 30 | + assert isinstance(data["csrftoken"], str) |
| 31 | + assert len(data["csrftoken"]) > 0 |
| 32 | + |
| 33 | + def test_get_csrf_token_success(self): |
| 34 | + request = self._make_request_with_session() |
| 35 | + response = get_csrf_token(request) |
| 36 | + |
| 37 | + self._assert_valid_csrf_response(response) |
| 38 | + |
| 39 | + def test_get_csrf_token_returns_json(self): |
| 40 | + request = self._make_request_with_session() |
| 41 | + response = get_csrf_token(request) |
| 42 | + |
| 43 | + data = json.loads(response.content) |
| 44 | + assert isinstance(data, dict) |
| 45 | + assert len(data) == 1 # Should only contain csrftoken key |
| 46 | + assert "csrftoken" in data |
| 47 | + |
| 48 | + def test_get_csrf_token_different_requests(self): |
| 49 | + request1 = self._make_request_with_session() |
| 50 | + request2 = self._make_request_with_session() |
| 51 | + |
| 52 | + response1 = get_csrf_token(request1) |
| 53 | + response2 = get_csrf_token(request2) |
| 54 | + |
| 55 | + self._assert_valid_csrf_response(response1) |
| 56 | + self._assert_valid_csrf_response(response2) |
| 57 | + |
| 58 | + data1 = json.loads(response1.content) |
| 59 | + data2 = json.loads(response2.content) |
| 60 | + |
| 61 | + # Different sessions should have different tokens |
| 62 | + assert data1["csrftoken"] != data2["csrftoken"] |
| 63 | + |
| 64 | + def test_get_csrf_token_response_structure(self): |
| 65 | + request = self._make_request_with_session() |
| 66 | + response = get_csrf_token(request) |
| 67 | + |
| 68 | + assert response["Content-Type"] == "application/json" |
| 69 | + |
| 70 | + data = json.loads(response.content) |
| 71 | + assert isinstance(data, dict) |
| 72 | + assert "csrftoken" in data |
| 73 | + assert len(data["csrftoken"]) > 0 |
| 74 | + |
| 75 | + def test_get_csrf_token_no_session(self): |
| 76 | + request = self.factory.get("/") |
| 77 | + |
| 78 | + response = get_csrf_token(request) |
| 79 | + self._assert_valid_csrf_response(response) |
| 80 | + |
| 81 | + def test_get_csrf_token_json_response_format(self): |
| 82 | + request = self._make_request_with_session() |
| 83 | + response = get_csrf_token(request) |
| 84 | + |
| 85 | + assert hasattr(response, "content") |
| 86 | + assert response["Content-Type"] == "application/json" |
| 87 | + |
| 88 | + data = json.loads(response.content) |
| 89 | + assert isinstance(data, dict) |
| 90 | + |
| 91 | + assert list(data.keys()) == ["csrftoken"] |
| 92 | + assert isinstance(data["csrftoken"], str) |
| 93 | + assert data["csrftoken"] != "" |
0 commit comments