"feat: Introduce test suite for printer functionality in backend"
This commit is contained in:
287
backend/app/test_implementation.py
Normal file
287
backend/app/test_implementation.py
Normal file
@@ -0,0 +1,287 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Test-Skript für die MYP Platform Implementierung
|
||||
Testet alle neu implementierten Funktionen:
|
||||
1. Font Awesome lokale Installation
|
||||
2. Drucker-Status-Management (Notfall-Abruf)
|
||||
3. Admin-Gastaufträge-APIs
|
||||
4. Datenbank-Integrität
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import requests
|
||||
import json
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
# Farben für Ausgabe
|
||||
class Colors:
|
||||
GREEN = '\033[92m'
|
||||
RED = '\033[91m'
|
||||
YELLOW = '\033[93m'
|
||||
BLUE = '\033[94m'
|
||||
BOLD = '\033[1m'
|
||||
END = '\033[0m'
|
||||
|
||||
def print_status(message, status="INFO"):
|
||||
color = Colors.GREEN if status == "SUCCESS" else Colors.RED if status == "ERROR" else Colors.YELLOW if status == "WARNING" else Colors.BLUE
|
||||
print(f"{color}[{status}]{Colors.END} {message}")
|
||||
|
||||
def test_font_awesome_installation():
|
||||
"""Test 1: Font Awesome lokale Installation"""
|
||||
print_status("=== TESTE FONT AWESOME INSTALLATION ===", "INFO")
|
||||
|
||||
# Überprüfe ob Font Awesome Dateien existieren
|
||||
base_path = Path(".")
|
||||
fa_path = base_path / "static" / "fontawesome"
|
||||
|
||||
required_files = [
|
||||
fa_path / "css" / "all.min.css",
|
||||
fa_path / "webfonts" / "fa-solid-900.woff2",
|
||||
fa_path / "webfonts" / "fa-regular-400.woff2",
|
||||
fa_path / "webfonts" / "fa-brands-400.woff2"
|
||||
]
|
||||
|
||||
all_exist = True
|
||||
for file_path in required_files:
|
||||
if file_path.exists():
|
||||
print_status(f"✓ {file_path.name} gefunden", "SUCCESS")
|
||||
else:
|
||||
print_status(f"✗ {file_path.name} fehlt", "ERROR")
|
||||
all_exist = False
|
||||
|
||||
# Überprüfe base.html Integration
|
||||
base_html = base_path / "templates" / "base.html"
|
||||
if base_html.exists():
|
||||
with open(base_html, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
if 'fontawesome/css/all.min.css' in content:
|
||||
print_status("✓ Font Awesome in base.html eingebunden", "SUCCESS")
|
||||
else:
|
||||
print_status("✗ Font Awesome nicht in base.html gefunden", "ERROR")
|
||||
all_exist = False
|
||||
else:
|
||||
print_status("✗ base.html nicht gefunden", "ERROR")
|
||||
all_exist = False
|
||||
|
||||
return all_exist
|
||||
|
||||
def test_database_schema():
|
||||
"""Test 2: Datenbank-Schema für neue Funktionen"""
|
||||
print_status("=== TESTE DATENBANK-SCHEMA ===", "INFO")
|
||||
|
||||
try:
|
||||
# Verbindung zur Datenbank
|
||||
db_path = "database.db"
|
||||
if not os.path.exists(db_path):
|
||||
print_status("✗ Datenbank nicht gefunden", "ERROR")
|
||||
return False
|
||||
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Teste GuestRequest Tabelle
|
||||
cursor.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='guest_requests'")
|
||||
guest_table = cursor.fetchone()
|
||||
|
||||
if guest_table:
|
||||
print_status("✓ GuestRequest Tabelle existiert", "SUCCESS")
|
||||
|
||||
# Überprüfe wichtige Spalten
|
||||
required_columns = [
|
||||
'processed_by', 'processed_at', 'approval_notes',
|
||||
'rejection_reason', 'otp_code', 'otp_used_at'
|
||||
]
|
||||
|
||||
table_sql = guest_table[0].lower()
|
||||
missing_columns = []
|
||||
|
||||
for col in required_columns:
|
||||
if col.lower() not in table_sql:
|
||||
missing_columns.append(col)
|
||||
|
||||
if missing_columns:
|
||||
print_status(f"✗ Fehlende Spalten: {', '.join(missing_columns)}", "ERROR")
|
||||
conn.close()
|
||||
return False
|
||||
else:
|
||||
print_status("✓ Alle erforderlichen Spalten vorhanden", "SUCCESS")
|
||||
else:
|
||||
print_status("✗ GuestRequest Tabelle nicht gefunden", "ERROR")
|
||||
conn.close()
|
||||
return False
|
||||
|
||||
# Teste Printer Tabelle
|
||||
cursor.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='printers'")
|
||||
printer_table = cursor.fetchone()
|
||||
|
||||
if printer_table:
|
||||
print_status("✓ Printer Tabelle existiert", "SUCCESS")
|
||||
|
||||
# Überprüfe Status-Spalten
|
||||
table_sql = printer_table[0].lower()
|
||||
if 'last_checked' in table_sql and 'status' in table_sql:
|
||||
print_status("✓ Drucker-Status-Spalten vorhanden", "SUCCESS")
|
||||
else:
|
||||
print_status("✗ Drucker-Status-Spalten fehlen", "ERROR")
|
||||
conn.close()
|
||||
return False
|
||||
else:
|
||||
print_status("✗ Printer Tabelle nicht gefunden", "ERROR")
|
||||
conn.close()
|
||||
return False
|
||||
|
||||
conn.close()
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print_status(f"✗ Datenbankfehler: {str(e)}", "ERROR")
|
||||
return False
|
||||
|
||||
def test_api_endpoints():
|
||||
"""Test 3: Neue API-Endpunkte"""
|
||||
print_status("=== TESTE API-ENDPUNKTE ===", "INFO")
|
||||
|
||||
base_url = "http://localhost:5000"
|
||||
|
||||
# Test-Endpunkte (ohne Authentifizierung)
|
||||
test_endpoints = [
|
||||
"/api/printers/status/emergency",
|
||||
"/api/admin/requests",
|
||||
"/api/printers/status/summary"
|
||||
]
|
||||
|
||||
available_endpoints = []
|
||||
|
||||
for endpoint in test_endpoints:
|
||||
try:
|
||||
response = requests.get(f"{base_url}{endpoint}", timeout=5)
|
||||
# 401/403 ist OK (Authentifizierung erforderlich), 404 ist nicht OK
|
||||
if response.status_code in [200, 401, 403]:
|
||||
print_status(f"✓ {endpoint} - Endpunkt verfügbar (HTTP {response.status_code})", "SUCCESS")
|
||||
available_endpoints.append(endpoint)
|
||||
elif response.status_code == 404:
|
||||
print_status(f"✗ {endpoint} - Endpunkt nicht gefunden (HTTP 404)", "ERROR")
|
||||
else:
|
||||
print_status(f"⚠ {endpoint} - Unerwarteter Status (HTTP {response.status_code})", "WARNING")
|
||||
available_endpoints.append(endpoint)
|
||||
|
||||
except requests.exceptions.ConnectionError:
|
||||
print_status(f"⚠ {endpoint} - Server nicht erreichbar", "WARNING")
|
||||
except Exception as e:
|
||||
print_status(f"✗ {endpoint} - Fehler: {str(e)}", "ERROR")
|
||||
|
||||
return len(available_endpoints) > 0
|
||||
|
||||
def test_template_integration():
|
||||
"""Test 4: Template-Integration"""
|
||||
print_status("=== TESTE TEMPLATE-INTEGRATION ===", "INFO")
|
||||
|
||||
templates_to_check = [
|
||||
("templates/admin.html", "admin_guest_requests"),
|
||||
("templates/admin_guest_requests.html", "fas fa-"),
|
||||
("templates/base.html", "fontawesome/css/all.min.css")
|
||||
]
|
||||
|
||||
all_ok = True
|
||||
|
||||
for template_path, search_term in templates_to_check:
|
||||
if os.path.exists(template_path):
|
||||
with open(template_path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
if search_term in content:
|
||||
print_status(f"✓ {template_path} - {search_term} gefunden", "SUCCESS")
|
||||
else:
|
||||
print_status(f"✗ {template_path} - {search_term} nicht gefunden", "ERROR")
|
||||
all_ok = False
|
||||
else:
|
||||
print_status(f"✗ {template_path} - Datei nicht gefunden", "ERROR")
|
||||
all_ok = False
|
||||
|
||||
return all_ok
|
||||
|
||||
def test_app_imports():
|
||||
"""Test 5: App.py Importe und Funktionen"""
|
||||
print_status("=== TESTE APP.PY INTEGRATION ===", "INFO")
|
||||
|
||||
if not os.path.exists("app.py"):
|
||||
print_status("✗ app.py nicht gefunden", "ERROR")
|
||||
return False
|
||||
|
||||
with open("app.py", 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
required_functions = [
|
||||
"get_printers_status_emergency",
|
||||
"force_update_all_printer_status",
|
||||
"admin_get_guest_requests",
|
||||
"admin_approve_guest_request",
|
||||
"admin_deny_guest_request"
|
||||
]
|
||||
|
||||
missing_functions = []
|
||||
for func in required_functions:
|
||||
if f"def {func}" in content:
|
||||
print_status(f"✓ Funktion {func} gefunden", "SUCCESS")
|
||||
else:
|
||||
print_status(f"✗ Funktion {func} fehlt", "ERROR")
|
||||
missing_functions.append(func)
|
||||
|
||||
# Teste Import-Statements
|
||||
required_imports = ["desc", "GuestRequest", "Notification"]
|
||||
missing_imports = []
|
||||
|
||||
for imp in required_imports:
|
||||
if imp in content:
|
||||
print_status(f"✓ Import {imp} gefunden", "SUCCESS")
|
||||
else:
|
||||
print_status(f"✗ Import {imp} fehlt", "ERROR")
|
||||
missing_imports.append(imp)
|
||||
|
||||
return len(missing_functions) == 0 and len(missing_imports) == 0
|
||||
|
||||
def run_comprehensive_test():
|
||||
"""Führt alle Tests aus"""
|
||||
print_status("MYP PLATFORM - IMPLEMENTATION TEST", "INFO")
|
||||
print_status("=" * 50, "INFO")
|
||||
|
||||
test_results = {
|
||||
"Font Awesome Installation": test_font_awesome_installation(),
|
||||
"Datenbank Schema": test_database_schema(),
|
||||
"Template Integration": test_template_integration(),
|
||||
"App.py Integration": test_app_imports(),
|
||||
"API Endpunkte": test_api_endpoints()
|
||||
}
|
||||
|
||||
print_status("\n=== ZUSAMMENFASSUNG ===", "INFO")
|
||||
|
||||
passed = 0
|
||||
total = len(test_results)
|
||||
|
||||
for test_name, result in test_results.items():
|
||||
status = "SUCCESS" if result else "ERROR"
|
||||
print_status(f"{test_name}: {'BESTANDEN' if result else 'FEHLGESCHLAGEN'}", status)
|
||||
if result:
|
||||
passed += 1
|
||||
|
||||
print_status(f"\nErgebnis: {passed}/{total} Tests bestanden", "SUCCESS" if passed == total else "WARNING")
|
||||
|
||||
if passed == total:
|
||||
print_status("\n🎉 ALLE TESTS BESTANDEN! Die Implementierung ist vollständig.", "SUCCESS")
|
||||
print_status("Folgende Funktionen sind jetzt verfügbar:", "INFO")
|
||||
print_status("• Font Awesome Icons (lokal installiert)", "INFO")
|
||||
print_status("• Erweiterte Drucker-Status-Verwaltung", "INFO")
|
||||
print_status("• Admin-Gastaufträge-Verwaltung", "INFO")
|
||||
print_status("• Robuste Datenbank-Integration", "INFO")
|
||||
print_status("• Notfall-Drucker-Status-Abruf", "INFO")
|
||||
else:
|
||||
print_status(f"\n⚠️ {total - passed} Test(s) fehlgeschlagen. Bitte überprüfen Sie die Implementierung.", "WARNING")
|
||||
|
||||
return passed == total
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = run_comprehensive_test()
|
||||
sys.exit(0 if success else 1)
|
Reference in New Issue
Block a user