Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions testing/backend/unit/test_saved_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,32 @@
from fastapi import FastAPI
from backend.secuscan.saved_views import saved_views_router
from backend.secuscan.database import Database, get_db
from backend.secuscan.auth import require_api_key
import backend.secuscan.database as _db_module
import backend.secuscan.auth as _auth_module
from pathlib import Path


# ─── Fixtures ─────────────────────────────────────────────────────────────────

async def _mock_require_api_key() -> str:
"""Mock auth dependency that always succeeds."""
return "test-owner-id"


@pytest_asyncio.fixture
async def app_client():
"""
Spin up an isolated FastAPI app with an in-memory SQLite database
and the saved_views_router registered. The client authenticates as
the "default" owner (no X-User-Id header) using a real API key,
matching how routes.py's own tests exercise auth (issue #1743).

require_api_key is exercised for real (not mocked) so the auth
negative-path tests below actually verify enforcement instead of
always observing whatever the mock returns.
"""
# In-memory DB — isolated per test function
test_db = Database(":memory:")
await test_db.connect()
_db_module.db = test_db

# Minimal app with auth override
_app = FastAPI()
_app.include_router(saved_views_router)

# Override auth dependency to bypass authentication in tests
_app.dependency_overrides[require_api_key] = _mock_require_api_key

with tempfile.TemporaryDirectory() as tmp_data_dir:
api_key = _auth_module.init_api_key(tmp_data_dir)
Expand Down Expand Up @@ -354,7 +348,6 @@ async def test_filter_json_with_null_values_rejected(app_client: AsyncClient):

# ─── Auth & owner isolation (issue #1743) ────────────────────────────────────

@pytest.mark.skip(reason="pre-existing upstream issue: app_client overrides auth so 401 cannot be tested here")
@pytest.mark.asyncio
async def test_unauthenticated_request_rejected(app_client: AsyncClient):
"""Requests without a valid API key/session are rejected, not served."""
Expand All @@ -364,7 +357,6 @@ async def test_unauthenticated_request_rejected(app_client: AsyncClient):
assert res.status_code == 401


@pytest.mark.skip(reason="pre-existing upstream issue: app_client overrides auth so 401 cannot be tested here")
@pytest.mark.asyncio
async def test_wrong_api_key_rejected(app_client: AsyncClient):
"""A malformed/incorrect API key is rejected."""
Expand Down
Loading