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>
128 lines
3.4 KiB
Python
128 lines
3.4 KiB
Python
from __future__ import annotations
|
|
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from mcp_common.auth import Principal, TokenStore
|
|
from mcp_macro.server import create_app
|
|
|
|
|
|
@pytest.fixture
|
|
def http():
|
|
store = TokenStore(
|
|
tokens={
|
|
"ct": Principal("core", {"core"}),
|
|
"ot": Principal("observer", {"observer"}),
|
|
}
|
|
)
|
|
app = create_app(fred_api_key="testfred", finnhub_api_key="testfinn", token_store=store)
|
|
return TestClient(app)
|
|
|
|
|
|
# --- Health ---
|
|
|
|
def test_health(http):
|
|
assert http.get("/health").status_code == 200
|
|
|
|
|
|
# --- get_economic_indicators ---
|
|
|
|
def test_get_economic_indicators_core_ok(http):
|
|
with patch(
|
|
"mcp_macro.server.fetch_economic_indicators",
|
|
new=AsyncMock(return_value={"fed_rate": 5.25, "updated_at": "2024-01-01T00:00:00+00:00"}),
|
|
):
|
|
r = http.post(
|
|
"/tools/get_economic_indicators",
|
|
headers={"Authorization": "Bearer ct"},
|
|
json={},
|
|
)
|
|
assert r.status_code == 200
|
|
assert r.json()["fed_rate"] == 5.25
|
|
|
|
|
|
def test_get_economic_indicators_observer_ok(http):
|
|
with patch(
|
|
"mcp_macro.server.fetch_economic_indicators",
|
|
new=AsyncMock(return_value={"fed_rate": 5.25}),
|
|
):
|
|
r = http.post(
|
|
"/tools/get_economic_indicators",
|
|
headers={"Authorization": "Bearer ot"},
|
|
json={},
|
|
)
|
|
assert r.status_code == 200
|
|
|
|
|
|
def test_get_economic_indicators_no_auth_401(http):
|
|
r = http.post("/tools/get_economic_indicators", json={})
|
|
assert r.status_code == 401
|
|
|
|
|
|
# --- get_macro_calendar ---
|
|
|
|
def test_get_macro_calendar_core_ok(http):
|
|
with patch(
|
|
"mcp_macro.server.fetch_macro_calendar",
|
|
new=AsyncMock(return_value={"events": []}),
|
|
):
|
|
r = http.post(
|
|
"/tools/get_macro_calendar",
|
|
headers={"Authorization": "Bearer ct"},
|
|
json={"days": 7},
|
|
)
|
|
assert r.status_code == 200
|
|
|
|
|
|
def test_get_macro_calendar_observer_ok(http):
|
|
with patch(
|
|
"mcp_macro.server.fetch_macro_calendar",
|
|
new=AsyncMock(return_value={"events": []}),
|
|
):
|
|
r = http.post(
|
|
"/tools/get_macro_calendar",
|
|
headers={"Authorization": "Bearer ot"},
|
|
json={},
|
|
)
|
|
assert r.status_code == 200
|
|
|
|
|
|
def test_get_macro_calendar_no_auth_401(http):
|
|
r = http.post("/tools/get_macro_calendar", json={})
|
|
assert r.status_code == 401
|
|
|
|
|
|
# --- get_market_overview ---
|
|
|
|
def test_get_market_overview_core_ok(http):
|
|
with patch(
|
|
"mcp_macro.server.fetch_market_overview",
|
|
new=AsyncMock(return_value={"btc_dominance": 52.0, "btc_price": 65000}),
|
|
):
|
|
r = http.post(
|
|
"/tools/get_market_overview",
|
|
headers={"Authorization": "Bearer ct"},
|
|
json={},
|
|
)
|
|
assert r.status_code == 200
|
|
assert r.json()["btc_price"] == 65000
|
|
|
|
|
|
def test_get_market_overview_observer_ok(http):
|
|
with patch(
|
|
"mcp_macro.server.fetch_market_overview",
|
|
new=AsyncMock(return_value={"btc_dominance": 52.0}),
|
|
):
|
|
r = http.post(
|
|
"/tools/get_market_overview",
|
|
headers={"Authorization": "Bearer ot"},
|
|
json={},
|
|
)
|
|
assert r.status_code == 200
|
|
|
|
|
|
def test_get_market_overview_no_auth_401(http):
|
|
r = http.post("/tools/get_market_overview", json={})
|
|
assert r.status_code == 401
|