Files
qr-code_manager/test_api.py
2025-07-15 13:25:34 +03:00

209 lines
5.9 KiB
Python

#!/usr/bin/env python3
"""
Test script for QR Code Manager API
Run this script while the server is running to test API functionality
"""
import requests
import json
import base64
from PIL import Image
import io
# Server URL
BASE_URL = "http://localhost:5000"
def test_text_qr():
"""Test generating a text QR code"""
print("Testing text QR code generation...")
data = {
"type": "text",
"content": "Hello, World! This is a test QR code.",
"foreground_color": "#000000",
"background_color": "#FFFFFF",
"style": "square",
"size": 10
}
response = requests.post(f"{BASE_URL}/api/generate", json=data)
if response.status_code == 200:
result = response.json()
if result['success']:
print(f"✅ Text QR generated successfully! ID: {result['qr_id']}")
return result['qr_id']
else:
print(f"❌ Error: {result['error']}")
else:
print(f"❌ HTTP Error: {response.status_code}")
return None
def test_url_qr():
"""Test generating a URL QR code"""
print("Testing URL QR code generation...")
data = {
"type": "url",
"content": "https://github.com",
"foreground_color": "#0066cc",
"background_color": "#ffffff",
"style": "rounded",
"size": 12
}
response = requests.post(f"{BASE_URL}/api/generate", json=data)
if response.status_code == 200:
result = response.json()
if result['success']:
print(f"✅ URL QR generated successfully! ID: {result['qr_id']}")
return result['qr_id']
else:
print(f"❌ Error: {result['error']}")
else:
print(f"❌ HTTP Error: {response.status_code}")
return None
def test_wifi_qr():
"""Test generating a WiFi QR code"""
print("Testing WiFi QR code generation...")
data = {
"type": "wifi",
"wifi": {
"ssid": "TestNetwork",
"password": "TestPassword123",
"security": "WPA"
},
"foreground_color": "#ff6600",
"background_color": "#ffffff",
"style": "circle",
"size": 8
}
response = requests.post(f"{BASE_URL}/api/generate", json=data)
if response.status_code == 200:
result = response.json()
if result['success']:
print(f"✅ WiFi QR generated successfully! ID: {result['qr_id']}")
return result['qr_id']
else:
print(f"❌ Error: {result['error']}")
else:
print(f"❌ HTTP Error: {response.status_code}")
return None
def test_list_qr_codes():
"""Test listing all QR codes"""
print("Testing QR codes listing...")
response = requests.get(f"{BASE_URL}/api/qr_codes")
if response.status_code == 200:
qr_codes = response.json()
print(f"✅ Found {len(qr_codes)} QR codes")
for qr in qr_codes:
print(f" - {qr['id']}: {qr['type']} (created: {qr['created_at']})")
return qr_codes
else:
print(f"❌ HTTP Error: {response.status_code}")
return []
def test_download_qr(qr_id):
"""Test downloading a QR code"""
print(f"Testing QR code download for ID: {qr_id}")
response = requests.get(f"{BASE_URL}/api/download/{qr_id}")
if response.status_code == 200:
print(f"✅ QR code downloaded successfully! Size: {len(response.content)} bytes")
# Verify it's a valid PNG image
try:
image = Image.open(io.BytesIO(response.content))
print(f" Image format: {image.format}, Size: {image.size}")
except Exception as e:
print(f"❌ Invalid image data: {e}")
else:
print(f"❌ HTTP Error: {response.status_code}")
def test_get_qr_details(qr_id):
"""Test getting QR code details"""
print(f"Testing QR code details for ID: {qr_id}")
response = requests.get(f"{BASE_URL}/api/qr_codes/{qr_id}")
if response.status_code == 200:
qr_data = response.json()
print(f"✅ QR code details retrieved!")
print(f" Type: {qr_data['type']}")
print(f" Created: {qr_data['created_at']}")
print(f" Settings: {qr_data['settings']}")
else:
print(f"❌ HTTP Error: {response.status_code}")
def test_delete_qr(qr_id):
"""Test deleting a QR code"""
print(f"Testing QR code deletion for ID: {qr_id}")
response = requests.delete(f"{BASE_URL}/api/qr_codes/{qr_id}")
if response.status_code == 200:
result = response.json()
if result['success']:
print(f"✅ QR code deleted successfully!")
else:
print(f"❌ Error: {result['error']}")
else:
print(f"❌ HTTP Error: {response.status_code}")
def main():
"""Run all tests"""
print("🚀 Starting QR Code Manager API Tests")
print("=" * 50)
# Test generating different types of QR codes
text_qr_id = test_text_qr()
print()
url_qr_id = test_url_qr()
print()
wifi_qr_id = test_wifi_qr()
print()
# Test listing QR codes
qr_codes = test_list_qr_codes()
print()
# Test operations on generated QR codes
if text_qr_id:
test_download_qr(text_qr_id)
print()
test_get_qr_details(text_qr_id)
print()
# Test deletion (only delete one to keep some for manual testing)
if wifi_qr_id:
test_delete_qr(wifi_qr_id)
print()
print("🏁 Tests completed!")
print("You can now open http://localhost:5000 in your browser to test the web interface.")
if __name__ == "__main__":
try:
main()
except requests.exceptions.ConnectionError:
print("❌ Cannot connect to the server. Make sure the QR Code Manager is running on localhost:5000")
print("Start the server with: python app.py")
except Exception as e:
print(f"❌ Unexpected error: {e}")