Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

2.8 KB
Raw Blame
# allow: no-sut-import — black-box HTTP test; drives real routes through the test client
"""
End-to-end CSRF flow test for browser-facing API endpoints.

Verifies the full flow under the always-on FastAPI CSRFMiddleware:
register (with a session token) → fetch the API CSRF token → a
browser-facing POST WITH the token passes CSRF, and the same POST WITHOUT a
token is rejected (403). Browser-facing endpoints like /api/start_research
are NOT exempt (only /ws + the auth token-mint endpoints are).
"""

import uuid


class TestCSRFEndToEndFlow:
    """Test the complete CSRF flow from registration through API usage."""

    def test_full_csrf_flow_register_through_api_call(self, client):
        """register → get CSRF token → token-bearing API call passes; bare one is rejected."""
        # Step 1: stamp the session and fetch a CSRF token for registration.
        # (Under FastAPI the token comes from /auth/csrf-token, not a scraped
        # WTForms hidden field.)
        client.get("/auth/login")
        reg_token = client.get("/auth/csrf-token").json()["csrf_token"]

        # Step 2: Register (auto-logs in the user).
        username = f"csrf_e2e_{uuid.uuid4().hex[:12]}"
        password = "TestPass123!"
        register_response = client.post(
            "/auth/register",
            data={
                "username": username,
                "password": password,
                "confirm_password": password,
                "acknowledge": "true",
                "csrf_token": reg_token,
            },
            follow_redirects=False,
        )
        assert register_response.status_code == 302, (
            f"Registration should redirect, got {register_response.status_code}"
        )

        # Step 3: Get the post-login API CSRF token.
        api_csrf_token = client.get("/auth/csrf-token").json()["csrf_token"]

        # Step 4: POST a browser-facing endpoint WITH the token — must pass
        # CSRF (reaches business logic: 200, or 4xx for bad input — never a
        # CSRF/403 rejection).
        response = client.post(
            "/api/start_research",
            json={"query": "test csrf flow"},
            headers={"X-CSRFToken": api_csrf_token},
        )
        assert response.status_code != 403, (
            f"CSRF should have passed with a valid token, got 403: "
            f"{response.text[:200]}"
        )
        assert "csrf" not in response.text.lower()

        # Step 5: POST the same endpoint WITHOUT a token — CSRF rejection (403).
        response = client.post(
            "/api/start_research",
            json={"query": "test csrf flow"},
        )
        assert response.status_code == 403, (
            f"Expected CSRF rejection (403), got {response.status_code}"
        )
        assert "csrf" in response.text.lower()