from __future__ import annotations from unittest.mock import AsyncMock, MagicMock import pytest from fastapi.testclient import TestClient from mcp_alpaca.server import create_app from mcp_common.auth import Principal, TokenStore @pytest.fixture def token_store(): return TokenStore( tokens={ "core-tok": Principal("core", {"core"}), "obs-tok": Principal("observer", {"observer"}), } ) @pytest.fixture def mock_client(): c = MagicMock() c.get_account = AsyncMock(return_value={"equity": 100000}) c.get_positions = AsyncMock(return_value=[]) c.get_activities = AsyncMock(return_value=[]) c.get_assets = AsyncMock(return_value=[]) c.get_ticker = AsyncMock(return_value={"symbol": "AAPL"}) c.get_bars = AsyncMock(return_value={"bars": []}) c.get_snapshot = AsyncMock(return_value={}) c.get_option_chain = AsyncMock(return_value={"contracts": []}) c.get_open_orders = AsyncMock(return_value=[]) c.get_clock = AsyncMock(return_value={"is_open": True}) c.get_calendar = AsyncMock(return_value=[]) c.place_order = AsyncMock(return_value={"id": "o1"}) c.amend_order = AsyncMock(return_value={"id": "o1"}) c.cancel_order = AsyncMock(return_value={"canceled": True}) c.cancel_all_orders = AsyncMock(return_value=[]) c.close_position = AsyncMock(return_value={"id": "close1"}) c.close_all_positions = AsyncMock(return_value=[]) return c @pytest.fixture def http(mock_client, token_store): app = create_app(client=mock_client, token_store=token_store, creds={"max_leverage": 1}) return TestClient(app) CORE = {"Authorization": "Bearer core-tok"} OBS = {"Authorization": "Bearer obs-tok"} READ_ENDPOINTS = [ ("/tools/get_account", {}), ("/tools/get_positions", {}), ("/tools/get_activities", {}), ("/tools/get_assets", {}), ("/tools/get_ticker", {"symbol": "AAPL"}), ("/tools/get_bars", {"symbol": "AAPL"}), ("/tools/get_snapshot", {"symbol": "AAPL"}), ("/tools/get_option_chain", {"underlying": "AAPL"}), ("/tools/get_open_orders", {}), ("/tools/get_clock", {}), ("/tools/get_calendar", {}), ] WRITE_ENDPOINTS = [ ("/tools/place_order", {"symbol": "AAPL", "side": "buy", "qty": 1}), ("/tools/amend_order", {"order_id": "o1", "qty": 2}), ("/tools/cancel_order", {"order_id": "o1"}), ("/tools/cancel_all_orders", {}), ("/tools/close_position", {"symbol": "AAPL"}), ("/tools/close_all_positions", {}), ] @pytest.mark.parametrize("path,payload", READ_ENDPOINTS) def test_read_core_ok(http, path, payload): r = http.post(path, json=payload, headers=CORE) assert r.status_code == 200, (path, r.text) @pytest.mark.parametrize("path,payload", READ_ENDPOINTS) def test_read_observer_ok(http, path, payload): r = http.post(path, json=payload, headers=OBS) assert r.status_code == 200, (path, r.text) @pytest.mark.parametrize("path,payload", READ_ENDPOINTS) def test_read_no_auth_401(http, path, payload): r = http.post(path, json=payload) assert r.status_code == 401, (path, r.text) @pytest.mark.parametrize("path,payload", WRITE_ENDPOINTS) def test_write_core_ok(http, path, payload): r = http.post(path, json=payload, headers=CORE) assert r.status_code == 200, (path, r.text) @pytest.mark.parametrize("path,payload", WRITE_ENDPOINTS) def test_write_observer_403(http, path, payload): r = http.post(path, json=payload, headers=OBS) assert r.status_code == 403, (path, r.text) @pytest.mark.parametrize("path,payload", WRITE_ENDPOINTS) def test_write_no_auth_401(http, path, payload): r = http.post(path, json=payload) assert r.status_code == 401, (path, r.text)