videogen/test/services/test_controller_base.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

80 lines
2.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import unittest
from types import SimpleNamespace
from app.config import config
from app.controllers import base
from app.controllers.v1.base import new_router
from app.models.exception import HttpException
class TestControllerAuthentication(unittest.TestCase):
def setUp(self):
self.original_app_config = dict(config.app)
def tearDown(self):
config.app.clear()
config.app.update(self.original_app_config)
@staticmethod
def _request(headers=None):
return SimpleNamespace(
headers=headers or {},
url="http://localhost/api/v1/tasks",
)
def test_get_task_id_reuses_header_or_generates_uuid(self):
"""
客户端提供 request ID 时需要原样保留,缺失时则生成可记录到日志和
错误响应中的 UUID,保证两种入口都有可追踪标识。
"""
self.assertEqual(
base.get_task_id(self._request({"x-task-id": "request-123"})),
"request-123",
)
generated = base.get_task_id(self._request())
self.assertEqual(len(generated), 36)
self.assertEqual(generated.count("-"), 4)
def test_verify_token_accepts_matching_key(self):
"""配置了 API Key 时,相同请求头必须正常通过鉴权。"""
config.app["api_key"] = "secret"
result = base.verify_token(self._request({"x-api-key": "secret"}))
self.assertIsNone(result)
def test_verify_token_rejects_missing_or_wrong_key(self):
"""
缺失和错误的 API Key 都必须返回 401,并保留客户端 request ID,
避免鉴权失败在日志中无法与调用方请求对应。
"""
config.app["api_key"] = "secret"
for provided_key in (None, "wrong"):
with self.subTest(provided_key=provided_key):
headers = {"x-task-id": "auth-request"}
if provided_key is not None:
headers["x-api-key"] = provided_key
with self.assertRaises(HttpException) as raised:
base.verify_token(self._request(headers))
self.assertEqual(raised.exception.status_code, 401)
self.assertIn("invalid token", raised.exception.message)
def test_new_router_preserves_common_prefix_and_dependencies(self):
"""所有 V1 路由都应复用统一前缀,并仅在传入时设置鉴权依赖。"""
dependency = object()
plain_router = new_router()
protected_router = new_router(dependencies=[dependency])
self.assertEqual(plain_router.prefix, "/api/v1")
self.assertEqual(plain_router.tags, ["V1"])
self.assertEqual(protected_router.dependencies, [dependency])
if __name__ == "__main__":
unittest.main()