Fix jwt-token

This commit is contained in:
2025-09-07 12:45:59 +02:00
parent 06c3390a1f
commit 953a206522
4 changed files with 349 additions and 3 deletions

View File

@@ -17,10 +17,61 @@ warnings.filterwarnings('ignore', message='Unverified HTTPS request')
API_BASE_URL = os.getenv('API_BASE_URL', 'http://localhost:3002/api')
BASE_PATH = os.getenv('VITE_BASE_PATH', '').rstrip('/')
# Authentication configuration
USERNAME = os.getenv('TEST_USERNAME', 'admin')
PASSWORD = os.getenv('TEST_PASSWORD', 'admin123')
SKIP_AUTH = os.getenv('SKIP_AUTH', 'false').lower() == 'true'
if BASE_PATH and not API_BASE_URL.endswith('/api'):
domain = API_BASE_URL.replace('/api', '').replace('/drones/api', '').replace('/uggla/api', '')
API_BASE_URL = f"{domain}{BASE_PATH}/api"
# Global variable to store authentication token
AUTH_TOKEN = None
def authenticate():
"""Authenticate with the API and get access token"""
global AUTH_TOKEN
if SKIP_AUTH:
print("🔓 Authentication skipped (SKIP_AUTH=true)")
return True
try:
login_data = {
"username": USERNAME,
"password": PASSWORD
}
response = requests.post(f"{API_BASE_URL}/users/login", json=login_data, verify=False, timeout=10)
if response.status_code == 200:
data = response.json()
if data.get('success') and 'data' in data and 'token' in data['data']:
AUTH_TOKEN = data['data']['token']
print(f"✅ Authentication successful")
return True
else:
print(f"❌ Invalid login response: {data}")
return False
else:
print(f"❌ Authentication failed: HTTP {response.status_code}")
return False
except Exception as e:
print(f"❌ Authentication error: {e}")
return False
def get_auth_headers():
"""Get headers with authentication token"""
headers = {
'Content-Type': 'application/json'
}
if not SKIP_AUTH and AUTH_TOKEN:
headers['Authorization'] = f'Bearer {AUTH_TOKEN}'
return headers
def debug_device_status():
"""Debug device status calculation"""
print("🔍 DEVICE STATUS DEBUG")
@@ -28,7 +79,7 @@ def debug_device_status():
try:
# Get all devices
response = requests.get(f"{API_BASE_URL}/devices", verify=False, timeout=10)
response = requests.get(f"{API_BASE_URL}/devices", headers=get_auth_headers(), verify=False, timeout=10)
if response.status_code == 200:
data = response.json()
@@ -107,6 +158,7 @@ def send_test_heartbeat():
response = requests.post(
f"{API_BASE_URL}/detectors",
json=payload,
headers=get_auth_headers(),
verify=False,
timeout=10
)
@@ -126,6 +178,12 @@ def main():
print(f"⏰ Test Time: {datetime.now()}")
print()
# Authenticate first
print("🔐 Authenticating...")
if not authenticate():
print("❌ Authentication failed, tests may not work")
return
debug_device_status()
send_test_heartbeat()