|
| 1 | +"""Test: GET /api/v1/threat-intel/ wired to ThreatIntelCorrelator.get_active_threats""" |
| 2 | +import sys |
| 3 | +sys.path.insert(0, "/Users/devops.ai/fixops/Fixops") |
| 4 | +sys.path.insert(0, "/Users/devops.ai/fixops/Fixops/suite-core") |
| 5 | +sys.path.insert(0, "/Users/devops.ai/fixops/Fixops/suite-api") |
| 6 | + |
| 7 | +from fastapi.testclient import TestClient |
| 8 | + |
| 9 | + |
| 10 | +def _make_app(): |
| 11 | + from fastapi import FastAPI |
| 12 | + from apps.api.threat_intel_router import router |
| 13 | + app = FastAPI() |
| 14 | + app.include_router(router) |
| 15 | + return app |
| 16 | + |
| 17 | + |
| 18 | +def test_threat_intel_index_calls_get_active_threats(): |
| 19 | + from core.threat_intel_correlator import ThreatActor |
| 20 | + fake_actor = ThreatActor( |
| 21 | + id="actor-1", name="APT-Test", aliases=["TestGroup"], |
| 22 | + ttps=["T1059"], motivation="espionage", origin_country="XX", |
| 23 | + active=True, associated_campaigns=[], iocs=[], |
| 24 | + ) |
| 25 | + app = _make_app() |
| 26 | + client = TestClient(app) |
| 27 | + import apps.api.threat_intel_router as mod |
| 28 | + original = mod._correlator.get_active_threats |
| 29 | + mod._correlator.get_active_threats = lambda org_id: [fake_actor] |
| 30 | + try: |
| 31 | + resp = client.get("/api/v1/threat-intel/") |
| 32 | + assert resp.status_code == 200, f"Expected 200, got {resp.status_code}: {resp.text}" |
| 33 | + data = resp.json() |
| 34 | + assert data["count"] == 1, f"count={data['count']}" |
| 35 | + assert len(data["items"]) == 1, f"items len={len(data['items'])}" |
| 36 | + assert data["items"][0]["name"] == "APT-Test" |
| 37 | + print("PASS: items returned from real engine, not hardcoded []") |
| 38 | + finally: |
| 39 | + mod._correlator.get_active_threats = original |
| 40 | + |
| 41 | + |
| 42 | +def test_threat_intel_index_empty_when_no_actors(): |
| 43 | + app = _make_app() |
| 44 | + client = TestClient(app) |
| 45 | + import apps.api.threat_intel_router as mod |
| 46 | + original = mod._correlator.get_active_threats |
| 47 | + mod._correlator.get_active_threats = lambda org_id: [] |
| 48 | + try: |
| 49 | + resp = client.get("/api/v1/threat-intel/") |
| 50 | + assert resp.status_code == 200 |
| 51 | + data = resp.json() |
| 52 | + assert data["count"] == 0 |
| 53 | + assert data["items"] == [] |
| 54 | + print("PASS: empty items when no actors") |
| 55 | + finally: |
| 56 | + mod._correlator.get_active_threats = original |
| 57 | + |
| 58 | + |
| 59 | +if __name__ == "__main__": |
| 60 | + test_threat_intel_index_calls_get_active_threats() |
| 61 | + test_threat_intel_index_empty_when_no_actors() |
| 62 | + print("ALL TESTS PASSED") |
0 commit comments