Skip to content

A modern platform for visual, flexible, and extensible graph-based investigations. For cybersecurity analysts and investigators.

test(api): SSE auth-gate and endpoint-removal tests (red)

dextmorgn9db1e675d67d
diff --git a/flowsint-api/tests/conftest.py b/flowsint-api/tests/conftest.py
new file mode 100644
index 00000000..144fb230
--- /dev/null
+++ b/flowsint-api/tests/conftest.py
@@ -0,0 +1,38 @@
+"""Pytest harness for flowsint-api. Provides a TestClient backed by an
+in-memory SQLite database, overriding the real Postgres get_db dependency."""
+
+import os
+
+# Env required at import time by flowsint-core modules. Set before importing app.
+os.environ.setdefault("AUTH_SECRET", "test-secret-please-ignore")
+os.environ.setdefault(
+ "MASTER_VAULT_KEY_V1", "base64:qnHTmwYb+uoygIw9MsRMY22vS5YPchY+QOi/E79GAvM="
+)
+
+import pytest
+from fastapi.testclient import TestClient
+from sqlalchemy import create_engine
+from sqlalchemy.orm import sessionmaker
+
+from flowsint_core.core.models import Base
+from flowsint_core.core.postgre_db import get_db
+from app.main import app
+
+
+def db_session():
+ engine = create_engine(
+ "sqlite:///:memory:", connect_args={"check_same_thread": False}
+ )
+ Base.metadata.create_all(engine)
+ Session = sessionmaker(bind=engine)
+ session = Session()
+ yield session
+ session.close()
+
+
+def client(db_session):
+ app.dependency_overrides[get_db] = lambda: db_session
+ yield TestClient(app)
+ app.dependency_overrides.clear()
diff --git a/flowsint-api/tests/test_events_auth.py b/flowsint-api/tests/test_events_auth.py
new file mode 100644
index 00000000..58e8bbc3
--- /dev/null
+++ b/flowsint-api/tests/test_events_auth.py
@@ -0,0 +1,50 @@
+"""Auth and shape contract for the SSE event endpoints."""
+
+import importlib
+
+import pytest
+
+from flowsint_core.core.auth import create_access_token
+from flowsint_core.core.models import Profile
+
+
+def test_get_current_user_sse_is_removed():
+ """The query-param SSE auth helper must no longer exist."""
+ deps = importlib.import_module("app.api.deps")
+ assert not hasattr(deps, "get_current_user_sse")
+
+
+def test_log_stream_requires_auth_header(client):
+ """No Authorization header -> 401 (no token in URL accepted)."""
+ res = client.get("/api/events/sketch/abc/stream")
+ assert res.status_code == 401
+
+
+def test_status_stream_requires_auth_header(client):
+ res = client.get("/api/events/sketch/abc/status/stream")
+ assert res.status_code == 401
+
+
+def test_log_stream_rejects_token_query_param(client):
+ """A token in the URL must NOT authenticate the request anymore."""
+ token = create_access_token({"sub": "[email protected]"})
+ res = client.get(f"/api/events/sketch/abc/stream?token={token}")
+ assert res.status_code == 401
+
+
+def test_dead_scan_stream_endpoint_removed(client):
+ """The unused scan status stream endpoint must be gone."""
+ res = client.get("/api/events/status/scan/abc/stream")
+ assert res.status_code == 404
+
+
+def test_get_current_user_accepts_valid_bearer(db_session):
+ """The dependency the streams now use authenticates via a valid JWT."""
+ from app.api.deps import get_current_user
+
+ db_session.add(Profile(email="[email protected]", hashed_password="x"))
+ db_session.commit()
+
+ token = create_access_token({"sub": "[email protected]"})
+ user = get_current_user(token=token, db=db_session)
+ assert user.email == "[email protected]"