Files
VoiceStudio/tests/test_stories_encode.py
DeryFerd 91174516a6 fix(api): gate unprotected routers with require_admin
Sixteen API routers declared APIRouter() with no dependencies, so their
mutating routes relied solely on ASGI middleware. In server mode bound to
0.0.0.0 without OMNIVOICE_API_KEY or a share PIN those routes were
reachable unauthenticated from LAN.

Desktop default (bind 127.0.0.1) is unchanged: require_admin permits
loopback, matching the routers that were already gated.

Router-level choices after review: openai_compat stays on require_consumer
because /v1 is the documented consumption surface for PIN and
trusted-network clients; the side-effectful pending-sample materialization
inside GET /profiles/{id}/audio uses a media-action admin gate
(require_admin_media_action) because media elements cannot carry the CSRF
marker header that require_admin_action demands — cross-site GETs stay
rejected and bare-server anonymous callers stay refused.

Handler tests that exercised the changed routers with a default TestClient
host now use an explicit loopback peer, matching the existing pattern for
gated routers. test_admin_route_policy.py asserts require_admin on every
gated router, require_consumer on /v1, and the media-action guard on the
profiles materialization branch.

Excluded: events/tts_stream (websocket-only, covered by
BearerKeyMiddleware), speech_platform (read-only discovery intentionally
reachable from tailnet and reverse proxy clients).
2026-10-30 03:27:02 +07:00

57 lines
1.6 KiB
Python

"""Stories audio-encode endpoint (/stories/encode)."""
from __future__ import annotations
import io
import struct
import wave
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from api.routers import stories
from services.ffmpeg_utils import find_ffmpeg
def _app():
app = FastAPI()
app.include_router(stories.router)
return app
def _wav_bytes(sec=0.1, sr=8000):
buf = io.BytesIO()
with wave.open(buf, "wb") as w:
w.setnchannels(1)
w.setsampwidth(2)
w.setframerate(sr)
w.writeframes(b"".join(struct.pack("<h", 0) for _ in range(int(sr * sec))))
return buf.getvalue()
def test_rejects_unknown_format():
c = TestClient(_app(), client=("127.0.0.1", 50000))
r = c.post("/stories/encode", files={"file": ("s.wav", _wav_bytes(), "audio/wav")}, data={"format": "exe"})
assert r.status_code == 400
def test_501_when_ffmpeg_missing(monkeypatch):
monkeypatch.setattr(stories, "find_ffmpeg", lambda: None)
c = TestClient(_app(), client=("127.0.0.1", 50000))
r = c.post("/stories/encode", files={"file": ("s.wav", _wav_bytes(), "audio/wav")}, data={"format": "mp3"})
assert r.status_code == 501
def test_encodes_mp3_when_ffmpeg_present():
if not find_ffmpeg():
pytest.skip("ffmpeg not available in this environment")
c = TestClient(_app(), client=("127.0.0.1", 50000))
r = c.post(
"/stories/encode",
files={"file": ("s.wav", _wav_bytes(0.2), "audio/wav")},
data={"format": "mp3", "bitrate": "96k"},
)
assert r.status_code == 200
assert r.headers["content-type"] == "audio/mpeg"
assert len(r.content) > 0