videogen/tests/reelforge/test_api_docs_gate.py
caelum0x d56639a594
Some checks are pending
build-images / api (push) Waiting to run
build-images / worker-gpu (push) Waiting to run
build-images / web (push) Waiting to run
codeql / Analyze (javascript-typescript) (push) Waiting to run
codeql / Analyze (python) (push) Waiting to run
perf / API hot-path micro-smoke (budgeted) (push) Waiting to run
perf / Desktop engine crates (release build check) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Waiting to run
vision2026-ci / API phase modules (ruff + pytest) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Waiting to run
vision2026-ci / Python SDK resource tests (push) Waiting to run
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Waiting to run
vision2026-ci / Alembic upgrade + drift check (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Waiting to run
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

91 lines
3.1 KiB
Python

"""M0.5: /docs and /openapi.json are staff-only by default; the API is "Menivor"."""
from __future__ import annotations
from contextlib import contextmanager
from types import SimpleNamespace
import pytest
from starlette.testclient import TestClient
import app.core.auth.principal as principal
import app.models.db.base as db_base
from app.asgi import get_application
from app.core import api_docs
from app.models.exception import HttpException
@contextmanager
def _no_db():
yield object()
def _client(monkeypatch, mode: str | None) -> TestClient:
if mode is None:
monkeypatch.delenv("MENIVOR_API_DOCS", raising=False)
else:
monkeypatch.setenv("MENIVOR_API_DOCS", mode)
return TestClient(get_application(), raise_server_exceptions=False)
def _as_user(monkeypatch, *, staff: bool) -> None:
monkeypatch.setattr(db_base, "get_session", _no_db)
monkeypatch.setattr(
principal, "resolve_principal", lambda *_a: SimpleNamespace(id="u1", is_staff=staff)
)
def test_default_and_unknown_modes_are_staff(monkeypatch):
monkeypatch.delenv("MENIVOR_API_DOCS", raising=False)
assert api_docs.docs_mode() == api_docs.STAFF
monkeypatch.setenv("MENIVOR_API_DOCS", "yes-please")
assert api_docs.docs_mode() == api_docs.STAFF
@pytest.mark.parametrize("path", ["/docs", "/openapi.json", "/redoc"])
def test_anonymous_gets_404_by_default(monkeypatch, path):
def reject(_request, _session, request_id):
raise HttpException(task_id=request_id, status_code=401, message="missing")
monkeypatch.setattr(db_base, "get_session", _no_db)
monkeypatch.setattr(principal, "resolve_principal", reject)
res = _client(monkeypatch, None).get(path)
assert res.status_code == 404
assert "paths" not in res.text
def test_ordinary_user_gets_404(monkeypatch):
_as_user(monkeypatch, staff=False)
client = _client(monkeypatch, "staff")
assert client.get("/docs", headers={"x-api-key": "rk_user"}).status_code == 404
assert client.get("/openapi.json", headers={"x-api-key": "rk_user"}).status_code == 404
def test_staff_reads_schema_and_docs(monkeypatch):
_as_user(monkeypatch, staff=True)
client = _client(monkeypatch, "staff")
schema = client.get("/openapi.json", headers={"x-api-key": "rk_staff"})
assert schema.status_code == 200
assert schema.json()["info"]["title"] == "Menivor"
page = client.get("/docs", headers={"x-api-key": "rk_staff"})
assert page.status_code == 200
assert "SwaggerUIBundle" in page.text and "</script" not in page.text.split("spec:")[1].split("dom_id")[0]
def test_public_mode_serves_docs_for_local_development(monkeypatch):
client = _client(monkeypatch, "public")
assert client.get("/openapi.json").status_code == 200
assert client.get("/docs").status_code == 200
def test_off_mode_has_no_docs_even_for_staff(monkeypatch):
_as_user(monkeypatch, staff=True)
client = _client(monkeypatch, "off")
assert client.get("/openapi.json", headers={"x-api-key": "rk_staff"}).status_code == 404
def test_api_title_is_menivor():
app = get_application()
assert app.title == "Menivor"
assert "MoneyPrinterTurbo" not in (app.description or "")