4d9db750be
- pyproject.toml: ruff target-version py311 → py313 (auto-fix 42 lint warnings via UP rules); aggiunto consider_namespace_packages = true che risolve la collisione conftest tra servizi e permette di lanciare pytest sull'intera suite cross-servizio. - mcp_common.audit: nuovo helper audit_write_op() con logger dedicato mcp.audit. Wirato su tutti i write endpoint di deribit, bybit, alpaca e hyperliquid (place_order, place_combo_order, cancel_*, set_*, close_*, transfer_*, switch_*, amend_*) con principal + target + payload non-sensibile + result summarizzato. - mcp_common.app_factory: ExchangeAppSpec + run_exchange_main() centralizza il boilerplate dei __main__.py (configure_root_logging, fail_fast_if_missing, summarize, load creds, resolve_environment, load token store, uvicorn). I 4 __main__.py exchange ridotti da ~60 LOC ognuno a ~25 LOC dichiarativi. mcp_common.env_validation promosso da mcp_deribit (mantenuto re-export shim per back-compat test_env_validation). - 8 test nuovi (4 audit + 4 app_factory). Suite full: 450/450 verdi. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
78 lines
2.2 KiB
Python
78 lines
2.2 KiB
Python
from __future__ import annotations
|
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
from fastapi.testclient import TestClient
|
|
from mcp_common.auth import Principal, TokenStore
|
|
from mcp_common.environment import EnvironmentInfo
|
|
from mcp_deribit.server import create_app
|
|
|
|
|
|
def _make_app(env_info, creds):
|
|
c = AsyncMock()
|
|
c.set_leverage = AsyncMock(return_value={"state": "ok"})
|
|
store = TokenStore(tokens={
|
|
"ct": Principal("core", {"core"}),
|
|
"ot": Principal("observer", {"observer"}),
|
|
})
|
|
return create_app(client=c, token_store=store, creds=creds, env_info=env_info)
|
|
|
|
|
|
def test_environment_info_full_shape():
|
|
env = EnvironmentInfo(
|
|
exchange="deribit",
|
|
environment="testnet",
|
|
source="env",
|
|
env_value="true",
|
|
base_url="https://test.deribit.com/api/v2",
|
|
)
|
|
app = _make_app(env, creds={"max_leverage": 3})
|
|
c = TestClient(app)
|
|
r = c.post(
|
|
"/tools/environment_info",
|
|
headers={"Authorization": "Bearer ot"},
|
|
)
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["exchange"] == "deribit"
|
|
assert body["environment"] == "testnet"
|
|
assert body["source"] == "env"
|
|
assert body["env_value"] == "true"
|
|
assert body["base_url"] == "https://test.deribit.com/api/v2"
|
|
assert body["max_leverage"] == 3
|
|
|
|
|
|
def test_environment_info_default_source():
|
|
env = EnvironmentInfo(
|
|
exchange="deribit",
|
|
environment="testnet",
|
|
source="default",
|
|
env_value=None,
|
|
base_url="https://test.deribit.com/api/v2",
|
|
)
|
|
app = _make_app(env, creds={"max_leverage": 1})
|
|
c = TestClient(app)
|
|
r = c.post(
|
|
"/tools/environment_info",
|
|
headers={"Authorization": "Bearer ct"},
|
|
)
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["source"] == "default"
|
|
assert body["env_value"] is None
|
|
assert body["max_leverage"] == 1
|
|
|
|
|
|
def test_environment_info_requires_auth():
|
|
env = EnvironmentInfo(
|
|
exchange="deribit",
|
|
environment="testnet",
|
|
source="default",
|
|
env_value=None,
|
|
base_url="https://test.deribit.com/api/v2",
|
|
)
|
|
app = _make_app(env, creds={"max_leverage": 3})
|
|
c = TestClient(app)
|
|
r = c.post("/tools/environment_info")
|
|
assert r.status_code == 401
|