Spaces:
Sleeping
Sleeping
| """ | |
| Integration tests for new API endpoints | |
| """ | |
| import sys | |
| from pathlib import Path | |
| # Add backend to path | |
| backend_dir = Path(__file__).parent.parent | |
| sys.path.insert(0, str(backend_dir)) | |
| # Add root directory to path for backend.api imports | |
| root_dir = Path(__file__).resolve().parents[2] | |
| sys.path.insert(0, str(root_dir)) | |
| import pytest | |
| from fastapi.testclient import TestClient | |
| from fastapi import FastAPI | |
| try: | |
| from backend.api.main import app | |
| except ImportError: | |
| # Fallback if backend.api.main doesn't work | |
| from api.main import app | |
| def client(): | |
| """Create a test client.""" | |
| return TestClient(app) | |
| def test_analytics_overview_endpoint(client): | |
| """Test /analytics/overview endpoint.""" | |
| response = client.get( | |
| "/analytics/overview", | |
| headers={"x-tenant-id": "test_tenant"}, | |
| params={"days": 30} | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert "tenant_id" in data | |
| assert "overview" in data | |
| assert "total_queries" in data["overview"] | |
| assert "tool_usage" in data["overview"] | |
| assert "redflag_count" in data["overview"] | |
| def test_analytics_tool_usage_endpoint(client): | |
| """Test /analytics/tool-usage endpoint.""" | |
| response = client.get( | |
| "/analytics/tool-usage", | |
| headers={"x-tenant-id": "test_tenant"}, | |
| params={"days": 30} | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert "tenant_id" in data | |
| assert "tool_usage" in data | |
| assert "period_days" in data | |
| def test_analytics_rag_quality_endpoint(client): | |
| """Test /analytics/rag-quality endpoint.""" | |
| response = client.get( | |
| "/analytics/rag-quality", | |
| headers={"x-tenant-id": "test_tenant"}, | |
| params={"days": 30} | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert "tenant_id" in data | |
| assert "rag_quality" in data | |
| def test_admin_rules_with_regex(client): | |
| """Test adding admin rule with regex pattern and severity.""" | |
| response = client.post( | |
| "/admin/rules", | |
| headers={"x-tenant-id": "test_tenant"}, | |
| json={ | |
| "rule": "Block password queries", | |
| "pattern": ".*password.*", | |
| "severity": "high", | |
| "description": "Blocks password-related queries" | |
| } | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert data["severity"] == "high" | |
| assert ".*password.*" in data["pattern"] | |
| # Get detailed rules | |
| response = client.get( | |
| "/admin/rules", | |
| headers={"x-tenant-id": "test_tenant"}, | |
| params={"detailed": True} | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert "rules" in data | |
| assert len(data["rules"]) > 0 | |
| assert data["rules"][0]["severity"] == "high" | |
| def test_admin_violations_endpoint(client): | |
| """Test /admin/violations endpoint.""" | |
| response = client.get( | |
| "/admin/violations", | |
| headers={"x-tenant-id": "test_tenant"}, | |
| params={"limit": 50, "days": 30} | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert "tenant_id" in data | |
| assert "violations" in data | |
| assert "count" in data | |
| def test_admin_tools_logs_endpoint(client): | |
| """Test /admin/tools/logs endpoint.""" | |
| response = client.get( | |
| "/admin/tools/logs", | |
| headers={"x-tenant-id": "test_tenant"}, | |
| params={"tool_name": "rag", "days": 7} | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert "tenant_id" in data | |
| assert "tool_usage" in data | |
| def test_agent_debug_endpoint(client): | |
| """Test /agent/debug endpoint.""" | |
| # Note: This will fail if LLM/MCP servers are not running | |
| # But we can at least test the endpoint structure | |
| response = client.post( | |
| "/agent/debug", | |
| json={ | |
| "tenant_id": "test_tenant", | |
| "message": "Test message", | |
| "temperature": 0.0 | |
| } | |
| ) | |
| # Might fail if services not available, but should have proper error handling | |
| assert response.status_code in [200, 500, 503] # Accept various status codes | |
| def test_agent_plan_endpoint(client): | |
| """Test /agent/plan endpoint.""" | |
| # Note: This will fail if LLM/MCP servers are not running | |
| response = client.post( | |
| "/agent/plan", | |
| json={ | |
| "tenant_id": "test_tenant", | |
| "message": "What is the company policy?", | |
| "temperature": 0.0 | |
| } | |
| ) | |
| # Might fail if services not available | |
| assert response.status_code in [200, 500, 503] | |
| def test_missing_tenant_id_returns_400(client): | |
| """Test that endpoints return 400 when tenant ID is missing.""" | |
| endpoints = [ | |
| "/analytics/overview", | |
| "/analytics/tool-usage", | |
| "/admin/rules", | |
| "/admin/violations" | |
| ] | |
| for endpoint in endpoints: | |
| response = client.get(endpoint) | |
| assert response.status_code == 400, f"Endpoint {endpoint} should return 400" | |
| def test_admin_tenants_endpoints(client): | |
| """Test tenant management endpoints (placeholders).""" | |
| # List tenants | |
| response = client.get("/admin/tenants") | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert "tenants" in data | |
| # Create tenant (placeholder) | |
| response = client.post("/admin/tenants", params={"tenant_id": "new_tenant"}) | |
| assert response.status_code == 200 | |
| # Delete tenant (placeholder) | |
| response = client.delete("/admin/tenants/new_tenant") | |
| assert response.status_code == 200 | |