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.

4.5 KB
Raw Blame
"""
pytest-compatible API tests for REST API endpoints.
Tests basic functionality and programmatic access integration.
"""


# test-time bypass and uses Flask app.config[]. After Wave 9, CSRF is
# fail-closed even for tests; the new pattern is to fetch a real token
# (see tests/web/routers/test_state_changing_flows.py).

import json  # noqa: E402


class TestRestAPIBasic:
    """Basic tests for REST API endpoints."""

    def test_health_check(self, authenticated_client):
        """Test the health check endpoint returns OK status."""
        response = authenticated_client.get("/api/v1/health")
        assert response.status_code == 200

        data = json.loads(response.data)
        assert data["status"] == "ok"
        assert "timestamp" in data
        assert isinstance(data["timestamp"], (int, float))

    def test_api_documentation(self, authenticated_client):
        """Test the API documentation endpoint returns proper structure."""
        response = authenticated_client.get("/api/v1/")
        assert response.status_code == 200

        data = json.loads(response.data)
        assert data["api_version"] == "v1"
        assert "description" in data
        assert "endpoints" in data
        assert len(data["endpoints"]) >= 3

        # Verify endpoint structure
        endpoints = data["endpoints"]
        if isinstance(endpoints, list):
            # For list format, just check basic fields
            for endpoint in endpoints:
                assert "path" in endpoint or "endpoint" in endpoint
                assert "method" in endpoint or "methods" in endpoint
                assert "description" in endpoint
                # auth_required field is optional in API v1
        else:
            # For dict format
            for endpoint in endpoints.values():
                assert "path" in endpoint
                assert "methods" in endpoint
                assert "description" in endpoint
                assert "requires_auth" in endpoint

    def test_quick_summary_validation(self, authenticated_client):
        """Test quick_summary endpoint validates required fields."""
        # Test missing query
        response = authenticated_client.post(
            "/api/v1/quick_summary",
            json={},
            content_type="application/json",
        )
        assert response.status_code == 400
        data = json.loads(response.data)
        assert "error" in data
        assert "query" in data["error"].lower()

    def test_analyze_documents_validation(self, authenticated_client):
        """Test analyze_documents endpoint validates required fields."""
        # Test missing collection_name
        response = authenticated_client.post(
            "/api/v1/analyze_documents",
            json={"query": "test query"},
            content_type="application/json",
        )
        assert response.status_code == 400
        data = json.loads(response.data)
        assert "error" in data
        assert "collection_name" in data["error"].lower()

        # Test missing query
        response = authenticated_client.post(
            "/api/v1/analyze_documents",
            json={"collection_name": "test_collection"},
            content_type="application/json",
        )
        assert response.status_code == 400
        data = json.loads(response.data)
        assert "error" in data
        assert "query" in data["error"].lower()

    def test_generate_report_validation(self, authenticated_client):
        """Test generate_report endpoint validates required fields."""
        # Test missing research_id or query
        response = authenticated_client.post(
            "/api/v1/generate_report",
            json={},
            content_type="application/json",
        )
        assert response.status_code == 400
        data = json.loads(response.data)
        assert "error" in data
        # Check for either research_id or query in error message
        error_msg = data["error"].lower()
        assert (
            "research_id" in error_msg
            or "query" in error_msg
            or "required" in error_msg
        )

    def test_error_response_format(self, authenticated_client):
        """Test that error responses follow consistent format."""
        # Trigger a 404 error
        response = authenticated_client.get("/api/v1/nonexistent_endpoint")
        assert response.status_code == 404

        if "application/json" in response.headers.get("content-type", ""):
            data = json.loads(response.data)
            assert "error" in data or "message" in data or "detail" in data