Spaces:
Runtime error
Runtime error
File size: 10,570 Bytes
18b952c | 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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | #!/usr/bin/env python3
"""
Test Script for Telegram Multi-Part File Streamer
Validates setup and performs basic functionality tests
"""
import asyncio
import os
import sys
import time
from io import BytesIO
import httpx
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
class Colors:
"""ANSI color codes"""
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BLUE = '\033[94m'
ENDC = '\033[0m'
BOLD = '\033[1m'
def print_header(text: str):
"""Print section header"""
print(f"\n{Colors.BOLD}{Colors.BLUE}{'=' * 60}{Colors.ENDC}")
print(f"{Colors.BOLD}{Colors.BLUE}{text.center(60)}{Colors.ENDC}")
print(f"{Colors.BOLD}{Colors.BLUE}{'=' * 60}{Colors.ENDC}\n")
def print_success(text: str):
"""Print success message"""
print(f"{Colors.GREEN}β {text}{Colors.ENDC}")
def print_error(text: str):
"""Print error message"""
print(f"{Colors.RED}β {text}{Colors.ENDC}")
def print_warning(text: str):
"""Print warning message"""
print(f"{Colors.YELLOW}β {text}{Colors.ENDC}")
def print_info(text: str):
"""Print info message"""
print(f"{Colors.BLUE}βΉ {text}{Colors.ENDC}")
async def test_environment_variables():
"""Test 1: Check environment variables"""
print_header("Test 1: Environment Variables")
required_vars = {
"API_ID": "Telegram API ID",
"API_HASH": "Telegram API Hash",
"BOT_TOKEN": "Telegram Bot Token",
"MONGO_URI": "MongoDB Connection String"
}
optional_vars = {
"SESSION_STRINGS": "Pyrogram Session Strings (for load balancing)"
}
all_valid = True
print_info("Checking required variables...")
for var, description in required_vars.items():
value = os.getenv(var)
if value:
masked_value = value[:8] + "..." if len(value) > 8 else value
print_success(f"{var}: {masked_value} ({description})")
else:
print_error(f"{var}: Missing! ({description})")
all_valid = False
print()
print_info("Checking optional variables...")
for var, description in optional_vars.items():
value = os.getenv(var)
if value:
count = len(value.split(","))
print_success(f"{var}: {count} session(s) configured ({description})")
else:
print_warning(f"{var}: Not set ({description})")
print()
return all_valid
async def test_mongodb_connection():
"""Test 2: MongoDB connection"""
print_header("Test 2: MongoDB Connection")
try:
from motor.motor_asyncio import AsyncIOMotorClient
mongo_uri = os.getenv("MONGO_URI")
if not mongo_uri:
print_error("MONGO_URI not set")
return False
print_info(f"Connecting to MongoDB...")
client = AsyncIOMotorClient(mongo_uri, serverSelectionTimeoutMS=5000)
# Test connection
await client.admin.command('ping')
# Get database info
db_name = os.getenv("MONGO_DATABASE", "telegram_streamer")
db = client[db_name]
print_success(f"Connected to MongoDB!")
print_info(f"Database: {db_name}")
# List collections
collections = await db.list_collection_names()
print_info(f"Collections: {collections if collections else 'None (fresh database)'}")
client.close()
return True
except Exception as e:
print_error(f"MongoDB connection failed: {str(e)}")
return False
async def test_api_server():
"""Test 3: API server availability"""
print_header("Test 3: API Server")
base_url = "http://localhost:8000"
print_info(f"Testing API server at {base_url}...")
try:
async with httpx.AsyncClient(timeout=10.0) as client:
# Test root endpoint
response = await client.get(base_url)
if response.status_code == 200:
data = response.json()
print_success(f"Root endpoint: {data.get('status', 'unknown')}")
else:
print_error(f"Root endpoint returned {response.status_code}")
return False
# Test health endpoint
response = await client.get(f"{base_url}/health")
if response.status_code == 200:
data = response.json()
print_success(f"Health check: {data.get('status', 'unknown')}")
print_info(f"Active sessions: {data.get('sessions', 0)}")
print_info(f"Database: {data.get('database', 'unknown')}")
else:
print_warning(f"Health endpoint returned {response.status_code}")
return True
except httpx.ConnectError:
print_error("Cannot connect to API server!")
print_info("Make sure the server is running:")
print_info(" uvicorn main:app --reload")
return False
except Exception as e:
print_error(f"API test failed: {str(e)}")
return False
async def test_upload_download():
"""Test 4: Upload and download functionality"""
print_header("Test 4: Upload/Download")
base_url = "http://localhost:8000"
print_info("Creating test file (1MB)...")
test_data = os.urandom(1024 * 1024) # 1MB random data
test_filename = "test_file.bin"
try:
async with httpx.AsyncClient(timeout=60.0) as client:
# Upload
print_info("Uploading test file...")
start_time = time.time()
response = await client.post(
f"{base_url}/upload",
params={"filename": test_filename},
content=test_data
)
upload_time = time.time() - start_time
if response.status_code != 200:
print_error(f"Upload failed: {response.status_code}")
print_error(f"Response: {response.text}")
return False
result = response.json()
unique_id = result.get("unique_id")
print_success(f"Upload completed in {upload_time:.2f}s")
print_info(f"Unique ID: {unique_id}")
print_info(f"Parts: {result.get('parts', 0)}")
print_info(f"Size: {result.get('total_size', 0)} bytes")
# Download
print_info("Downloading test file...")
start_time = time.time()
response = await client.get(f"{base_url}/dl/{unique_id}")
download_time = time.time() - start_time
if response.status_code != 200:
print_error(f"Download failed: {response.status_code}")
return False
downloaded_data = response.content
print_success(f"Download completed in {download_time:.2f}s")
# Verify
if downloaded_data == test_data:
print_success("Data integrity verified! β")
else:
print_error("Data integrity check failed!")
return False
# Test range request
print_info("Testing range request (bytes 0-1023)...")
response = await client.get(
f"{base_url}/dl/{unique_id}",
headers={"Range": "bytes=0-1023"}
)
if response.status_code == 206:
print_success("Range request supported! β")
if len(response.content) == 1024:
print_success("Range request data correct! β")
else:
print_error(f"Range request returned {len(response.content)} bytes, expected 1024")
else:
print_warning(f"Range request returned {response.status_code} (expected 206)")
# Cleanup
print_info("Cleaning up test file...")
response = await client.delete(f"{base_url}/delete/{unique_id}")
if response.status_code == 200:
print_success("Test file deleted")
return True
except Exception as e:
print_error(f"Upload/Download test failed: {str(e)}")
return False
async def main():
"""Run all tests"""
print()
print(f"{Colors.BOLD}{Colors.BLUE}")
print("ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ")
print("β Telegram Multi-Part File Streamer - Test Suite β")
print("ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ")
print(f"{Colors.ENDC}")
tests = [
("Environment Variables", test_environment_variables),
("MongoDB Connection", test_mongodb_connection),
("API Server", test_api_server),
("Upload/Download", test_upload_download)
]
results = []
for name, test_func in tests:
try:
result = await test_func()
results.append((name, result))
except Exception as e:
print_error(f"Test '{name}' crashed: {str(e)}")
results.append((name, False))
# Summary
print_header("Test Summary")
passed = sum(1 for _, result in results if result)
total = len(results)
for name, result in results:
if result:
print_success(f"{name}")
else:
print_error(f"{name}")
print()
if passed == total:
print_success(f"All tests passed! ({passed}/{total})")
print()
print_info("Your setup is ready! π")
print_info("You can now start uploading and streaming files.")
print()
return 0
else:
print_error(f"Some tests failed: {passed}/{total} passed")
print()
print_info("Please fix the issues above before proceeding.")
print()
return 1
if __name__ == "__main__":
try:
exit_code = asyncio.run(main())
sys.exit(exit_code)
except KeyboardInterrupt:
print(f"\n\n{Colors.YELLOW}Tests cancelled by user.{Colors.ENDC}\n")
sys.exit(1)
|