174 lines
5.5 KiB
Python
174 lines
5.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test script for Clean-Tracks API.
|
|
"""
|
|
|
|
import sys
|
|
import json
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
# Add parent directory to path
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
# We'll test with the Flask test client
|
|
from src.api import create_app
|
|
from src.database import init_database, close_database
|
|
|
|
|
|
def test_api():
|
|
"""Test API endpoints."""
|
|
print("Testing Clean-Tracks API...")
|
|
print("="*60)
|
|
|
|
# Use temporary database for testing
|
|
with tempfile.NamedTemporaryFile(suffix='.db', delete=False) as tmp_db:
|
|
db_path = tmp_db.name
|
|
|
|
try:
|
|
# Create test app
|
|
config = {
|
|
'TESTING': True,
|
|
'DATABASE_URL': f'sqlite:///{db_path}'
|
|
}
|
|
app, socketio = create_app(config)
|
|
client = app.test_client()
|
|
|
|
# Initialize database
|
|
with app.app_context():
|
|
init_database(config['DATABASE_URL'])
|
|
|
|
# Test 1: Health check
|
|
print("\n1. Testing health check...")
|
|
response = client.get('/api/health')
|
|
assert response.status_code == 200
|
|
data = response.get_json()
|
|
assert data['status'] == 'healthy'
|
|
print(" ✓ Health check passed")
|
|
|
|
# Test 2: List word lists (should be empty)
|
|
print("\n2. Testing word list listing...")
|
|
response = client.get('/api/wordlists')
|
|
assert response.status_code == 200
|
|
data = response.get_json()
|
|
assert isinstance(data, list)
|
|
print(f" ✓ Found {len(data)} word lists")
|
|
|
|
# Test 3: Create word list
|
|
print("\n3. Testing word list creation...")
|
|
response = client.post('/api/wordlists',
|
|
json={
|
|
'name': 'Test List',
|
|
'description': 'A test word list',
|
|
'language': 'en'
|
|
}
|
|
)
|
|
assert response.status_code == 201
|
|
data = response.get_json()
|
|
list_id = data['id']
|
|
print(f" ✓ Created word list with ID: {list_id}")
|
|
|
|
# Test 4: Add words to list
|
|
print("\n4. Testing adding words...")
|
|
response = client.post(f'/api/wordlists/{list_id}/words',
|
|
json={
|
|
'words': {
|
|
'test1': {'severity': 'low', 'category': 'profanity'},
|
|
'test2': {'severity': 'high', 'category': 'slur'}
|
|
}
|
|
}
|
|
)
|
|
assert response.status_code == 200
|
|
print(" ✓ Added words to list")
|
|
|
|
# Test 5: Get word list statistics
|
|
print("\n5. Testing word list statistics...")
|
|
response = client.get(f'/api/wordlists/{list_id}')
|
|
assert response.status_code == 200
|
|
data = response.get_json()
|
|
assert data['total_words'] == 2
|
|
print(f" ✓ Word list has {data['total_words']} words")
|
|
|
|
# Test 6: Get user settings
|
|
print("\n6. Testing user settings...")
|
|
response = client.get('/api/settings?user_id=test')
|
|
assert response.status_code == 200
|
|
data = response.get_json()
|
|
assert data['user_id'] == 'test'
|
|
print(" ✓ Retrieved user settings")
|
|
|
|
# Test 7: Update user settings
|
|
print("\n7. Testing settings update...")
|
|
response = client.put('/api/settings',
|
|
json={
|
|
'user_id': 'test',
|
|
'theme': 'dark',
|
|
'whisper_model_size': 'small'
|
|
}
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.get_json()
|
|
assert data['ui']['theme'] == 'dark'
|
|
print(" ✓ Updated user settings")
|
|
|
|
# Test 8: Get statistics
|
|
print("\n8. Testing statistics endpoint...")
|
|
response = client.get('/api/statistics')
|
|
assert response.status_code == 200
|
|
data = response.get_json()
|
|
assert 'total_jobs' in data
|
|
print(f" ✓ Statistics: {data['total_jobs']} total jobs")
|
|
|
|
# Test 9: List jobs
|
|
print("\n9. Testing job listing...")
|
|
response = client.get('/api/jobs')
|
|
assert response.status_code == 200
|
|
data = response.get_json()
|
|
assert isinstance(data, list)
|
|
print(f" ✓ Found {len(data)} jobs")
|
|
|
|
# Test 10: Export word list
|
|
print("\n10. Testing word list export...")
|
|
response = client.get(f'/api/wordlists/{list_id}/export?format=json')
|
|
assert response.status_code == 200
|
|
# Response should be a file download
|
|
assert response.headers.get('Content-Disposition')
|
|
print(" ✓ Word list exported successfully")
|
|
|
|
print("\n" + "="*60)
|
|
print("✅ All API tests passed successfully!")
|
|
|
|
except AssertionError as e:
|
|
print(f"\n❌ Test failed: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
except Exception as e:
|
|
print(f"\n❌ Unexpected error: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
finally:
|
|
# Cleanup
|
|
close_database()
|
|
Path(db_path).unlink(missing_ok=True)
|
|
|
|
return True
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# Install Flask if not available
|
|
try:
|
|
import flask
|
|
import flask_cors
|
|
import flask_socketio
|
|
except ImportError:
|
|
print("Installing required packages...")
|
|
import subprocess
|
|
subprocess.check_call([sys.executable, "-m", "pip", "install",
|
|
"flask", "flask-cors", "flask-socketio"])
|
|
|
|
success = test_api()
|
|
sys.exit(0 if success else 1) |