85 lines
2.6 KiB
Python
85 lines
2.6 KiB
Python
from fastapi.testclient import TestClient
|
|
|
|
_TEST_USER = {"username": "testuser", "output_dir": "/tmp", "is_admin": False}
|
|
|
|
|
|
def make_app():
|
|
from fastapi import FastAPI
|
|
from api.router import router, current_user
|
|
app = FastAPI()
|
|
# Override auth for tests — no real credentials needed
|
|
app.dependency_overrides[current_user] = lambda: _TEST_USER
|
|
app.include_router(router)
|
|
return app
|
|
|
|
|
|
def test_status_returns_idle():
|
|
client = TestClient(make_app())
|
|
r = client.get("/status")
|
|
assert r.status_code == 200
|
|
assert r.json()["status"] == "idle"
|
|
assert r.json()["username"] == "testuser"
|
|
|
|
|
|
def test_config_get_returns_dict():
|
|
client = TestClient(make_app())
|
|
r = client.get("/config")
|
|
assert r.status_code == 200
|
|
assert "ollama" in r.json()
|
|
|
|
|
|
def test_transcripts_returns_list():
|
|
client = TestClient(make_app())
|
|
r = client.get("/transcripts")
|
|
assert r.status_code == 200
|
|
assert isinstance(r.json(), list)
|
|
|
|
|
|
def test_status_requires_auth():
|
|
from fastapi import FastAPI
|
|
from api.router import router
|
|
app = FastAPI()
|
|
app.include_router(router)
|
|
client = TestClient(app, raise_server_exceptions=False)
|
|
r = client.get("/status")
|
|
assert r.status_code == 401
|
|
|
|
|
|
def make_app_for_dir(output_dir: str):
|
|
from fastapi import FastAPI
|
|
from api.router import router, current_user
|
|
app = FastAPI()
|
|
app.dependency_overrides[current_user] = lambda: {"username": "", "output_dir": output_dir, "is_admin": False}
|
|
app.include_router(router)
|
|
return app
|
|
|
|
|
|
def test_get_transcript_returns_content(tmp_path):
|
|
f = tmp_path / "2026-01-01-0900-test.md"
|
|
f.write_text("# Hello\n\ncontent here\n")
|
|
client = TestClient(make_app_for_dir(str(tmp_path)))
|
|
r = client.get("/transcripts/2026-01-01-0900-test.md")
|
|
assert r.status_code == 200
|
|
assert "Hello" in r.text
|
|
|
|
|
|
def test_get_transcript_rejects_path_traversal(tmp_path):
|
|
client = TestClient(make_app_for_dir(str(tmp_path)))
|
|
r = client.get("/transcripts/..%2Fsecret.md")
|
|
assert r.status_code == 404
|
|
|
|
|
|
def test_login_rejects_wrong_credentials():
|
|
import tempfile, os
|
|
from unittest.mock import patch
|
|
from fastapi import FastAPI
|
|
from api.router import router
|
|
app = FastAPI()
|
|
app.include_router(router)
|
|
client = TestClient(app, raise_server_exceptions=False)
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
users_path = os.path.join(tmpdir, "users.toml")
|
|
with patch("auth.USERS_PATH", users_path):
|
|
r = client.post("/login", json={"username": "nobody", "password": "wrong"})
|
|
assert r.status_code == 401
|