Files
ss-tools/backend/tests/api/test_load_testing_approval.py
busya 488a8f349b test(backend): raise coverage to 95%+ statements and branches (97.8%/95.0%)
- ~60 new/extended test files across api, core, plugins, services, schemas:
  routes, superset clients, task_manager, lineage, git, translate,
  dashboard-testing, load-testing, migration, llm_analysis, scheduler, ssl
- .coveragerc: enable branch coverage; exclude src/__tests__ (test files)
  and src/scripts (CLI/ops tools) from the denominator
- bug fixes found while testing:
  * settings: PUT /settings/reports registered under duplicated prefix
  * schemas/lineage: FleetReportDTO missing run_status (route always 500)
  * dashboard_testing/baseline_inheritance: visual entry read wrong field
  * superset_client/_databases: logger extra name shadowed LogRecord attr
  * routes/datasets: _yaml_string_paths recursion without yield from
  * translate/sql_generator: restore explicit-type timestamp contract
  * baseline_catalog: remove unreachable dashboard_id fallback
- conftest fixes: pytest_plugins to rootdir conftest (pytest 9), test
  filename collision, TMPDIR-safe integration fixtures
2026-08-19 17:14:32 +03:00

114 lines
4.8 KiB
Python

# #region Test.LoadTesting.ApiApproval [C:3] [TYPE Module] [SEMANTICS test,load-testing,api,approval]
# @BRIEF 040 PROD approval endpoint contract tests (LOAD-FR-014): two-party approval creation.
# @TEST_EDGE: non_prod_environment -> 400 PROD approval requires a production environment
# @TEST_EDGE: pool_no_safe_slot -> 400 cap rejected
# @TEST_EDGE: invalid_profile -> 422
# @TEST_EDGE: missing_reason -> 422 reason required for PROD approval
# @TEST_EDGE: separation_of_duties -> 422 requester == approver rejected
# @RELATION BINDS_TO -> [Api.LoadTesting.Routes]
from unittest.mock import patch
import pytest
from fastapi import HTTPException
from sqlalchemy import create_engine, event
from sqlalchemy.orm import Session
from src.api.routes.load_testing import RunStartInput, create_prod_approval_endpoint
from src.models.load_testing import ProdApproval
from src.models.mapping import Base, Environment
from src.services.environment_resolver import ResolvedEnvironment
PROD_ENV = ResolvedEnvironment(id="env-prod", stage="PROD", is_production=True)
@pytest.fixture()
def db():
engine = create_engine("sqlite:///:memory:", connect_args={"check_same_thread": False})
event.listen(engine, "connect", lambda c, _: c.execute("PRAGMA foreign_keys=ON"))
Base.metadata.create_all(bind=engine)
session = Session(bind=engine)
session.add(Environment(id="env-dev", name="dev", url="https://dev.example", credentials_id="cred"))
session.commit()
yield session
session.close()
engine.dispose()
class _Requester:
id = "requester-1"
username = "requester"
roles = []
def _body(**overrides):
base = {
"dashboard_id": 400, "environment_id": "env-prod", "stage": "PROD",
"pool_size": 20, "concurrency_requested": 2, "execution_mode": "iterations", "iterations": 5,
"axes": {"filters": [{"region": "north"}]},
"approval_reason": "release window", "approval_id": "approver-1",
}
base.update(overrides)
return base
def test_approval_success_persists_two_party_approval(db: Session):
with patch("src.api.routes.load_testing.resolve_environment", return_value=PROD_ENV):
resp = create_prod_approval_endpoint(RunStartInput(**_body()), db=db, current_user=_Requester(), _=None)
assert resp["status"] == "approved"
assert resp["environment_id"] == "env-prod"
approval = db.get(ProdApproval, resp["approval_id"])
assert approval is not None
assert approval.requester_id == "requester-1"
assert approval.approver_id == "approver-1"
assert approval.reason == "release window"
assert approval.effective_cap == 2
assert approval.request_estimate == 5
def test_approval_requires_prod_environment_400(db: Session):
with pytest.raises(HTTPException) as exc_info:
create_prod_approval_endpoint(
RunStartInput(**_body(environment_id="env-dev", stage="dev")),
db=db, current_user=_Requester(), _=None,
)
assert exc_info.value.status_code == 400
assert "PROD approval requires a production environment" in exc_info.value.detail
def test_approval_cap_rejected_400(db: Session):
with patch("src.api.routes.load_testing.resolve_environment", return_value=PROD_ENV):
with pytest.raises(HTTPException) as exc_info:
create_prod_approval_endpoint(RunStartInput(**_body(pool_size=5)), db=db, current_user=_Requester(), _=None)
assert exc_info.value.status_code == 400
assert "no safe load slot" in exc_info.value.detail
def test_approval_invalid_profile_422(db: Session):
with patch("src.api.routes.load_testing.resolve_environment", return_value=PROD_ENV):
with pytest.raises(HTTPException) as exc_info:
create_prod_approval_endpoint(
RunStartInput(**_body(execution_mode="bogus")), db=db, current_user=_Requester(), _=None
)
assert exc_info.value.status_code == 422
def test_approval_empty_reason_422(db: Session):
with patch("src.api.routes.load_testing.resolve_environment", return_value=PROD_ENV):
with pytest.raises(HTTPException) as exc_info:
create_prod_approval_endpoint(
RunStartInput(**_body(approval_reason="")), db=db, current_user=_Requester(), _=None
)
assert exc_info.value.status_code == 422
assert "reason required" in exc_info.value.detail
def test_approval_separation_of_duties_422(db: Session):
with patch("src.api.routes.load_testing.resolve_environment", return_value=PROD_ENV):
with pytest.raises(HTTPException) as exc_info:
create_prod_approval_endpoint(
RunStartInput(**_body(approval_id="requester-1")), db=db, current_user=_Requester(), _=None
)
assert exc_info.value.status_code == 422
assert "separation of duties" in exc_info.value.detail
# #endregion Test.LoadTesting.ApiApproval