Repository navigation
Expand file tree
/
Copy pathtest_api_integration.py
More file actions
161 lines (136 loc) · 5.33 KB
/
Copy pathtest_api_integration.py
File metadata and controls
161 lines (136 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python3
"""
MCP server integration test against production API.
Tests oauth and query_library tools with API comparison.
"""
import asyncio
import httpx
import json
import logging
import os
import sys
from pathlib import Path
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
logger = logging.getLogger(__name__)
# Test configuration
SERVICE_URL = "https://yoto-smart-stream-production.up.railway.app"
ADMIN_USER = os.getenv("ADMIN_USERNAME", "admin")
ADMIN_PASS = os.getenv("ADMIN_PASSWORD", "yoto")
async def test_api_health():
"""Test API health check."""
logger.info(f"Testing API health check at {SERVICE_URL}...")
try:
async with httpx.AsyncClient(timeout=10.0) as client:
response = await client.get(f"{SERVICE_URL}/api/health")
if response.status_code == 200:
data = response.json()
logger.info(f"✅ API health check passed: {data}")
return True
else:
logger.error(f"❌ API health returned {response.status_code}")
return False
except Exception as e:
logger.error(f"❌ API health check failed: {e}")
return False
async def test_api_authentication():
"""Test admin authentication."""
logger.info("Testing API authentication...")
try:
async with httpx.AsyncClient(timeout=10.0) as client:
response = await client.post(
f"{SERVICE_URL}/api/user/login",
json={"username": ADMIN_USER, "password": ADMIN_PASS}
)
if response.status_code == 200:
logger.info(f"✅ Authentication successful")
return response.cookies
else:
logger.error(f"❌ Authentication failed: {response.status_code}")
logger.error(f" Response: {response.text[:200]}")
return None
except Exception as e:
logger.error(f"❌ Authentication error: {e}")
return None
async def test_library_data():
"""Test library data retrieval."""
logger.info("Testing library data retrieval...")
try:
cookies = await test_api_authentication()
if not cookies:
return None
async with httpx.AsyncClient(timeout=30.0) as client:
# Try /api/library first
response = await client.get(
f"{SERVICE_URL}/api/library",
cookies=cookies
)
if response.status_code == 200:
data = response.json()
cards = data.get("cards", [])
playlists = data.get("playlists", [])
logger.info(f"✅ Library data: {len(cards)} cards, {len(playlists)} playlists")
return data
# Try alternative /api/cards
logger.info(" Trying /api/cards endpoint...")
response = await client.get(
f"{SERVICE_URL}/api/cards",
cookies=cookies
)
if response.status_code == 200:
cards = response.json()
logger.info(f"✅ Cards endpoint: {len(cards)} cards")
return {"cards": cards, "playlists": []}
logger.error(f"❌ Library data retrieval failed: {response.status_code}")
return None
except Exception as e:
logger.error(f"❌ Library data error: {e}")
return None
async def test_oauth_endpoints():
"""Test OAuth endpoints."""
logger.info("Testing OAuth endpoints...")
try:
cookies = await test_api_authentication()
if not cookies:
return False
async with httpx.AsyncClient(timeout=10.0) as client:
# Check if OAuth start endpoint exists
response = await client.post(
f"{SERVICE_URL}/api/auth/start",
cookies=cookies
)
if response.status_code in [200, 400]: # 400 is ok if already authenticated
logger.info(f"✅ OAuth endpoints available")
return True
else:
logger.warning(f"⚠️ OAuth endpoint returned {response.status_code}")
return False
except Exception as e:
logger.error(f"❌ OAuth endpoint test failed: {e}")
return False
async def run_all_tests():
"""Run all integration tests."""
logger.info("=" * 60)
logger.info("MCP SERVER INTEGRATION TESTS (Production API)")
logger.info("=" * 60)
results = {
"API Health Check": await test_api_health(),
"API Authentication": (await test_api_authentication()) is not None,
"Library Data Retrieval": (await test_library_data()) is not None,
"OAuth Endpoints": await test_oauth_endpoints(),
}
logger.info("=" * 60)
logger.info("TEST RESULTS")
logger.info("=" * 60)
passed = 0
for test_name, result in results.items():
status = "✅ PASS" if result else "❌ FAIL"
logger.info(f"{status}: {test_name}")
if result:
passed += 1
logger.info("=" * 60)
logger.info(f"Total: {passed}/{len(results)} tests passed")
logger.info("=" * 60)
return all(results.values())
if __name__ == "__main__":
success = asyncio.run(run_all_tests())
sys.exit(0 if success else 1)