Spaces:
Runtime error
Runtime error
File size: 3,529 Bytes
46085c7 00033cb 327ea2d 46085c7 00033cb 46085c7 00033cb 46085c7 00033cb 46085c7 00033cb 327ea2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | from datetime import datetime, timezone
from fastapi.testclient import TestClient
from app import main
from app.core import security
from app.core.config import get_settings
from app.main import app
from app.ml.heuristic_model import HeuristicScorer
class FakeBroker:
def __init__(self) -> None:
self.messages: list[tuple[str, dict]] = []
async def publish_json(self, queue_name: str, payload: dict) -> None:
self.messages.append((queue_name, payload))
def test_analyze_text_publishes_inference_task(monkeypatch) -> None:
settings = get_settings()
monkeypatch.setattr(main.settings, "pipeline_mode", "queue")
fake_broker = FakeBroker()
app.state.broker = fake_broker
client = TestClient(app)
response = client.post(
"/api/v1/analyze/text",
headers={"X-Internal-Token": settings.backend_internal_token},
json={
"user_id": "author@example.com",
"target_user_id": "target@example.com",
"timestamp": datetime.now(timezone.utc).isoformat(),
"text": "You are awful",
},
)
assert response.status_code == 202
assert response.json()["tracking_id"]
assert fake_broker.messages[0][0] == settings.inference_task_queue
def test_analyze_text_direct_mode_processes_without_broker(monkeypatch) -> None:
calls = []
async def fake_process_text_direct(**kwargs):
calls.append(kwargs)
monkeypatch.setattr(main.settings, "pipeline_mode", "direct")
monkeypatch.setattr(main, "process_text_direct", fake_process_text_direct)
app.state.scorer = HeuristicScorer()
client = TestClient(app)
response = client.post(
"/api/v1/analyze/text",
headers={"X-Internal-Token": main.settings.backend_internal_token},
json={
"user_id": "author@example.com",
"target_user_id": "target@example.com",
"timestamp": datetime.now(timezone.utc).isoformat(),
"text": "You are awful",
},
)
assert response.status_code == 202
assert response.json()["tracking_id"]
assert calls
def test_analyze_text_accepts_supabase_bearer(monkeypatch) -> None:
calls = []
async def fake_process_text_direct(**kwargs):
calls.append(kwargs)
async def fake_fetch_supabase_user(token: str, apikey: str):
assert token == "token-123"
assert apikey == "publishable-key"
return {"id": "user-uuid-1", "email": "user@example.com"}
monkeypatch.setattr(main.settings, "pipeline_mode", "direct")
monkeypatch.setattr(main.settings, "supabase_url", "https://example.supabase.co")
monkeypatch.setattr(main.settings, "supabase_publishable_key", "publishable-key")
monkeypatch.setattr(main.settings, "backend_internal_token", "dev-internal-token")
monkeypatch.setattr(main, "process_text_direct", fake_process_text_direct)
monkeypatch.setattr(security, "_fetch_supabase_user", fake_fetch_supabase_user)
app.state.scorer = HeuristicScorer()
client = TestClient(app)
response = client.post(
"/api/v1/analyze/text",
headers={"Authorization": "Bearer token-123"},
json={
"user_id": "spoofed@example.com",
"target_user_id": "target@example.com",
"timestamp": datetime.now(timezone.utc).isoformat(),
"text": "You are awful",
},
)
assert response.status_code == 202
assert calls
task = calls[0]["task"]
assert task.user_id == "user-uuid-1"
|