93 lines
3.0 KiB
Python
93 lines
3.0 KiB
Python
from types import SimpleNamespace
|
|
from uuid import uuid4
|
|
|
|
from fastapi.testclient import TestClient
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
|
|
from tests.conftest import build_test_app, install_stub, load_module_from_path
|
|
|
|
|
|
def _load_meta_module(monkeypatch):
|
|
install_stub(monkeypatch, "app.auth", package=True)
|
|
install_stub(
|
|
monkeypatch,
|
|
"app.auth.project_dependencies",
|
|
{
|
|
"ProjectContext": object,
|
|
"get_project_context": lambda: None,
|
|
"get_project_pg_session": lambda: None,
|
|
"get_project_timescale_connection": lambda: None,
|
|
"get_metadata_repository": lambda: None,
|
|
},
|
|
)
|
|
install_stub(
|
|
monkeypatch,
|
|
"app.auth.metadata_dependencies",
|
|
{"get_current_metadata_user": lambda: None},
|
|
)
|
|
return load_module_from_path(
|
|
"tests_meta_endpoints_module",
|
|
"app/api/v1/endpoints/meta.py",
|
|
)
|
|
|
|
|
|
def test_meta_project_returns_map_extent(monkeypatch):
|
|
module = _load_meta_module(monkeypatch)
|
|
project_id = uuid4()
|
|
repo = SimpleNamespace(
|
|
get_project_by_id=lambda _project_id: None,
|
|
get_geoserver_config=lambda _project_id: None,
|
|
)
|
|
|
|
async def get_project_by_id(_project_id):
|
|
return SimpleNamespace(
|
|
id=project_id,
|
|
name="Demo Project",
|
|
code="demo",
|
|
description="desc",
|
|
gs_workspace="workspace",
|
|
map_extent={"xmin": 1, "ymin": 2, "xmax": 3, "ymax": 4},
|
|
status="active",
|
|
)
|
|
|
|
async def get_geoserver_config(_project_id):
|
|
return None
|
|
|
|
repo.get_project_by_id = get_project_by_id
|
|
repo.get_geoserver_config = get_geoserver_config
|
|
|
|
app = build_test_app(module.router, "/api/v1")
|
|
app.dependency_overrides[module.get_project_context] = lambda: SimpleNamespace(
|
|
project_id=project_id,
|
|
project_role="editor",
|
|
)
|
|
app.dependency_overrides[module.get_metadata_repository] = lambda: repo
|
|
client = TestClient(app)
|
|
|
|
response = client.get("/api/v1/meta/project")
|
|
|
|
assert response.status_code == 200
|
|
assert response.json()["map_extent"] == {"xmin": 1, "ymin": 2, "xmax": 3, "ymax": 4}
|
|
|
|
|
|
def test_meta_db_health_returns_503_for_postgres_errors(monkeypatch):
|
|
module = _load_meta_module(monkeypatch)
|
|
|
|
class BrokenSession:
|
|
async def execute(self, _query):
|
|
raise SQLAlchemyError("pg unavailable")
|
|
|
|
class DummyTimescaleConnection:
|
|
def cursor(self):
|
|
raise AssertionError("timescale should not be queried after postgres failure")
|
|
|
|
app = build_test_app(module.router, "/api/v1")
|
|
app.dependency_overrides[module.get_project_pg_session] = lambda: BrokenSession()
|
|
app.dependency_overrides[module.get_project_timescale_connection] = lambda: DummyTimescaleConnection()
|
|
client = TestClient(app)
|
|
|
|
response = client.get("/api/v1/meta/db/health")
|
|
|
|
assert response.status_code == 503
|
|
assert response.json()["detail"] == "Project PostgreSQL health check failed: pg unavailable"
|