395 lines
13 KiB
Python
Executable File
395 lines
13 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Testskript für den externen Gruppenpositions-Endpunkt.
|
|
|
|
Der Endpunkt erwartet einen JSON-POST an:
|
|
|
|
/<tenant>/api/groups/position
|
|
|
|
Beispiel:
|
|
|
|
python scripts/test_group_position_api.py \
|
|
--base-url http://127.0.0.1:5000 \
|
|
--tenant RV \
|
|
--alias "Gruppe 1"
|
|
|
|
Ohne --token wird der erste API-Token des Mandanten aus der SQLite-Datenbank
|
|
gelesen. Ein Token kann im Admin-Dashboard unter "API-Zugriffstoken" erzeugt
|
|
werden.
|
|
|
|
Das Skript verwendet Koordinaten aus ../data/rv-hnr.csv und prüft:
|
|
|
|
1. Eine neue Position wird gespeichert und als Code 300 verteilt.
|
|
2. Ein älterer Zeitstempel wird verworfen.
|
|
3. Ein ungültiger Token wird abgelehnt.
|
|
4. Ein neuerer Zeitstempel überschreibt die Position wieder.
|
|
"""
|
|
import argparse
|
|
import csv
|
|
import json
|
|
import sqlite3
|
|
import sys
|
|
import urllib.error
|
|
import urllib.request
|
|
from datetime import datetime, timedelta, timezone
|
|
from pathlib import Path
|
|
|
|
PROJECT_DIR = Path(__file__).resolve().parent.parent
|
|
DEFAULT_DB = PROJECT_DIR / 'instance' / 'cmbnet.sqlite3'
|
|
DEFAULT_CSV = PROJECT_DIR.parent / 'data' / 'rv-hnr.csv'
|
|
|
|
|
|
def parse_args():
|
|
parser = argparse.ArgumentParser(
|
|
description='Testet den API-Endpunkt für externe Gruppenpositionen.'
|
|
)
|
|
parser.add_argument(
|
|
'--base-url', default='http://127.0.0.1:5000',
|
|
help='Basis-URL der Anwendung (Default: http://127.0.0.1:5000)'
|
|
)
|
|
parser.add_argument(
|
|
'--tenant', default='RV',
|
|
help='Mandanten-Code in der URL (Default: RV)'
|
|
)
|
|
parser.add_argument(
|
|
'--alias',
|
|
help='Gruppen-Aliasname. Ohne Angabe wird die erste Gruppe des Mandanten verwendet.'
|
|
)
|
|
parser.add_argument(
|
|
'--token',
|
|
help='API-Zugriffstoken. Ohne Angabe wird der erste Token aus der DB gelesen.'
|
|
)
|
|
parser.add_argument(
|
|
'--db', type=Path, default=DEFAULT_DB,
|
|
help=f'Pfad zur SQLite-Datenbank (Default: {DEFAULT_DB})'
|
|
)
|
|
parser.add_argument(
|
|
'--csv', type=Path, default=DEFAULT_CSV,
|
|
help=f'CSV-Datei mit Koordinaten (Default: {DEFAULT_CSV})'
|
|
)
|
|
parser.add_argument(
|
|
'--timeout', type=float, default=10.0,
|
|
help='HTTP-Timeout in Sekunden (Default: 10)'
|
|
)
|
|
parser.add_argument(
|
|
'--use-proxy', action='store_true',
|
|
help='Umgebungs-Proxy (http_proxy/https_proxy) verwenden. '
|
|
'Standardmäßig wird direkt verbunden, damit localhost-Tests '
|
|
'nicht über einen Proxy laufen.'
|
|
)
|
|
return parser.parse_args()
|
|
|
|
|
|
def load_coordinates(path, count=2):
|
|
"""Liest die ersten gültigen Koordinaten aus der Hausnummern-CSV."""
|
|
if not path.exists():
|
|
raise SystemExit(f'CSV-Datei nicht gefunden: {path}')
|
|
|
|
with path.open(encoding='utf-8-sig') as handle:
|
|
lines = [line for line in handle if line.strip()]
|
|
|
|
if not lines:
|
|
raise SystemExit(f'CSV-Datei ist leer: {path}')
|
|
|
|
# In rv-hnr.csv beginnt die Kopfzeile mit "#". Das Rautenzeichen wird
|
|
# entfernt, damit DictReader die Spaltennamen korrekt erkennt. Weitere
|
|
# Kommentarzeilen werden übersprungen.
|
|
if lines[0].lstrip().startswith('#'):
|
|
lines[0] = lines[0].lstrip()[1:]
|
|
lines = [lines[0]] + [
|
|
line for line in lines[1:]
|
|
if not line.lstrip().startswith('#')
|
|
]
|
|
|
|
reader = csv.DictReader(lines, delimiter=';')
|
|
if reader.fieldnames:
|
|
reader.fieldnames = [
|
|
name.strip().lstrip('#').strip() for name in reader.fieldnames
|
|
]
|
|
|
|
coordinates = []
|
|
for row in reader:
|
|
try:
|
|
lat = float(row.get('Latitude') or row.get('latitude') or '')
|
|
lon = float(row.get('Longitude') or row.get('longitude') or '')
|
|
except (TypeError, ValueError):
|
|
continue
|
|
|
|
coordinates.append({
|
|
'lat': lat,
|
|
'lon': lon,
|
|
'street': (row.get('Straßenname') or '').strip(),
|
|
'number': (row.get('Hausnummer') or '').strip()
|
|
})
|
|
if len(coordinates) >= count:
|
|
break
|
|
|
|
if len(coordinates) < count:
|
|
raise SystemExit(
|
|
f'Die CSV-Datei enthält weniger als {count} gültige Koordinaten.'
|
|
)
|
|
return coordinates
|
|
|
|
|
|
def parse_db_timestamp(value):
|
|
"""Parst den SQLite-Zeitstempel der Gruppe als UTC-datetime."""
|
|
if not value:
|
|
return None
|
|
text = str(value).strip().replace(' ', 'T')
|
|
try:
|
|
parsed = datetime.fromisoformat(text)
|
|
except ValueError:
|
|
return None
|
|
if parsed.tzinfo is None:
|
|
parsed = parsed.replace(tzinfo=timezone.utc)
|
|
return parsed.astimezone(timezone.utc)
|
|
|
|
|
|
def load_context(db_path, tenant_code, alias, token_override):
|
|
"""Liest Mandant, Gruppe und ggf. den ersten API-Token aus der DB."""
|
|
if not db_path.exists():
|
|
raise SystemExit(f'Datenbank nicht gefunden: {db_path}')
|
|
|
|
connection = sqlite3.connect(str(db_path))
|
|
connection.row_factory = sqlite3.Row
|
|
try:
|
|
tenant = connection.execute(
|
|
'SELECT id FROM tenants WHERE code = ?',
|
|
(tenant_code,)
|
|
).fetchone()
|
|
if not tenant:
|
|
raise SystemExit(f'Mandant nicht gefunden: {tenant_code}')
|
|
|
|
if alias:
|
|
group = connection.execute(
|
|
'''SELECT id, aliasname, last_pos_at
|
|
FROM groups
|
|
WHERE tenant_id = ? AND aliasname = ?''',
|
|
(tenant['id'], alias)
|
|
).fetchone()
|
|
else:
|
|
group = connection.execute(
|
|
'''SELECT id, aliasname, last_pos_at
|
|
FROM groups
|
|
WHERE tenant_id = ?
|
|
AND aliasname IS NOT NULL
|
|
AND TRIM(aliasname) <> ''
|
|
ORDER BY id
|
|
LIMIT 1''',
|
|
(tenant['id'],)
|
|
).fetchone()
|
|
|
|
if not group:
|
|
hint = f' mit Alias „{alias}“' if alias else ''
|
|
raise SystemExit(
|
|
f'Keine Gruppe{hint} im Mandanten {tenant_code} gefunden.'
|
|
)
|
|
|
|
token_row = None
|
|
if not token_override:
|
|
token_row = connection.execute(
|
|
'''SELECT id, token
|
|
FROM api_tokens
|
|
WHERE tenant_id = ?
|
|
ORDER BY id
|
|
LIMIT 1''',
|
|
(tenant['id'],)
|
|
).fetchone()
|
|
if not token_row:
|
|
raise SystemExit(
|
|
'Kein API-Zugriffstoken in der Datenbank. Bitte im '
|
|
'Admin-Dashboard unter „API-Zugriffstoken“ einen Token '
|
|
'erzeugen oder --token angeben.'
|
|
)
|
|
|
|
return tenant['id'], group, token_row
|
|
finally:
|
|
connection.close()
|
|
|
|
|
|
def read_group_position(db_path, tenant_id, group_id):
|
|
"""Liest die aktuelle Position direkt aus der Datenbank."""
|
|
connection = sqlite3.connect(str(db_path))
|
|
connection.row_factory = sqlite3.Row
|
|
try:
|
|
row = connection.execute(
|
|
'''SELECT last_lat, last_lon, last_pos_at
|
|
FROM groups
|
|
WHERE tenant_id = ? AND id = ?''',
|
|
(tenant_id, group_id)
|
|
).fetchone()
|
|
return dict(row) if row else None
|
|
finally:
|
|
connection.close()
|
|
|
|
|
|
def post_json(opener, url, payload, timeout):
|
|
"""Sendet einen JSON-POST und liefert (HTTP-Status, JSON-Antwort)."""
|
|
body = json.dumps(payload).encode('utf-8')
|
|
request = urllib.request.Request(
|
|
url,
|
|
data=body,
|
|
method='POST',
|
|
headers={
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json'
|
|
}
|
|
)
|
|
|
|
try:
|
|
with opener.open(request, timeout=timeout) as response:
|
|
raw = response.read().decode('utf-8')
|
|
return response.status, json.loads(raw) if raw else {}
|
|
except urllib.error.HTTPError as error:
|
|
raw = error.read().decode('utf-8', errors='replace')
|
|
try:
|
|
parsed = json.loads(raw) if raw else {}
|
|
except json.JSONDecodeError:
|
|
parsed = {'raw': raw[:1000]}
|
|
return error.code, parsed
|
|
except urllib.error.URLError as error:
|
|
raise SystemExit(
|
|
f'Server nicht erreichbar unter {url}: {error.reason}'
|
|
) from error
|
|
|
|
|
|
def format_timestamp(value):
|
|
return value.astimezone(timezone.utc).isoformat().replace('+00:00', 'Z')
|
|
|
|
|
|
def print_response(label, status, data):
|
|
print(f'\n{label}: HTTP {status}')
|
|
print(json.dumps(data, ensure_ascii=False, indent=2))
|
|
|
|
|
|
def main():
|
|
args = parse_args()
|
|
|
|
coordinates = load_coordinates(args.csv)
|
|
tenant_id, group, token_row = load_context(
|
|
args.db, args.tenant, args.alias, args.token
|
|
)
|
|
|
|
aliasname = group['aliasname']
|
|
token = args.token or token_row['token']
|
|
endpoint = (
|
|
f"{args.base_url.rstrip('/')}/{args.tenant}/api/groups/position"
|
|
)
|
|
|
|
if args.use_proxy:
|
|
opener = urllib.request.build_opener()
|
|
else:
|
|
# Tests gegen einen lokalen Flask-Server sollen nicht über einen
|
|
# eventuell gesetzten System-Proxy laufen.
|
|
opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
|
|
|
|
print(f'Endpunkt: {endpoint}')
|
|
print(f'Mandant: {args.tenant} (ID {tenant_id})')
|
|
print(f'Gruppe: {group["id"]} / {aliasname}')
|
|
if token_row:
|
|
print(f'Token: Datenbank-Token #{token_row["id"]}')
|
|
else:
|
|
print('Token: aus --token')
|
|
print(
|
|
f'CSV: {args.csv} '
|
|
f'({coordinates[0]["street"]} {coordinates[0]["number"]} u. a.)'
|
|
)
|
|
|
|
stored_timestamp = parse_db_timestamp(group['last_pos_at'])
|
|
now = datetime.now(timezone.utc).replace(microsecond=0)
|
|
base_timestamp = now
|
|
if stored_timestamp is not None and stored_timestamp >= base_timestamp:
|
|
base_timestamp = stored_timestamp + timedelta(seconds=1)
|
|
|
|
first_timestamp = base_timestamp
|
|
stale_timestamp = base_timestamp - timedelta(hours=1)
|
|
newer_timestamp = base_timestamp + timedelta(seconds=1)
|
|
|
|
failures = []
|
|
|
|
def send(label, coordinate, timestamp, token_value=None):
|
|
payload = {
|
|
'lat': coordinate['lat'],
|
|
'lon': coordinate['lon'],
|
|
'timestamp': format_timestamp(timestamp),
|
|
'token': token_value if token_value is not None else token,
|
|
'aliasname': aliasname
|
|
}
|
|
status, data = post_json(opener, endpoint, payload, args.timeout)
|
|
print_response(label, status, data)
|
|
return status, data
|
|
|
|
# 1. Neue Position setzen.
|
|
status, data = send('Neue Position', coordinates[0], first_timestamp)
|
|
if status != 200 or not data.get('updated'):
|
|
failures.append('Neue Position wurde nicht übernommen.')
|
|
if data.get('distributed') is not True:
|
|
failures.append('Code-300-Nachricht wurde nicht verteilt.')
|
|
|
|
position_after_first = read_group_position(
|
|
args.db, tenant_id, group['id']
|
|
)
|
|
|
|
# 2. Älteren Zeitstempel senden: darf nichts ändern.
|
|
status, data = send(
|
|
'Älterer Zeitstempel (muss verworfen werden)',
|
|
coordinates[1], stale_timestamp
|
|
)
|
|
if status != 200 or data.get('updated') is not False:
|
|
failures.append('Älterer Zeitstempel wurde nicht verworfen.')
|
|
if data.get('reason') != 'stale_timestamp':
|
|
failures.append('Antwort enthält nicht reason=stale_timestamp.')
|
|
|
|
position_after_stale = read_group_position(
|
|
args.db, tenant_id, group['id']
|
|
)
|
|
if position_after_first != position_after_stale:
|
|
failures.append(
|
|
'Die Datenbankposition wurde trotz älterem Zeitstempel geändert.'
|
|
)
|
|
|
|
# 3. Ungültigen Token testen.
|
|
status, data = send(
|
|
'Ungültiger Token (muss 401 liefern)',
|
|
coordinates[1], newer_timestamp, token_value='invalid-token'
|
|
)
|
|
if status != 401:
|
|
failures.append(f'Ungültiger Token lieferte HTTP {status} statt 401.')
|
|
|
|
# 4. Neuere Position setzen.
|
|
status, data = send('Neuere Position', coordinates[1], newer_timestamp)
|
|
if status != 200 or not data.get('updated'):
|
|
failures.append('Neuere Position wurde nicht übernommen.')
|
|
if data.get('distributed') is not True:
|
|
failures.append('Code-300-Nachricht wurde nicht verteilt.')
|
|
|
|
final_position = read_group_position(args.db, tenant_id, group['id'])
|
|
expected = coordinates[1]
|
|
if not final_position or final_position.get('last_lat') is None \
|
|
or final_position.get('last_lon') is None:
|
|
failures.append('Gruppe konnte nach dem Test nicht gelesen werden.')
|
|
else:
|
|
if abs(float(final_position['last_lat']) - expected['lat']) > 1e-9:
|
|
failures.append('Gespeicherte Latitude stimmt nicht mit der CSV überein.')
|
|
if abs(float(final_position['last_lon']) - expected['lon']) > 1e-9:
|
|
failures.append('Gespeicherte Longitude stimmt nicht mit der CSV überein.')
|
|
|
|
print('\n' + '=' * 70)
|
|
if failures:
|
|
print('Test fehlgeschlagen:')
|
|
for failure in failures:
|
|
print(f' - {failure}')
|
|
return 1
|
|
|
|
print('Alle Tests erfolgreich.')
|
|
print(
|
|
f'Aktuelle Position: {final_position["last_lat"]}, '
|
|
f'{final_position["last_lon"]} '
|
|
f'({expected["street"]} {expected["number"]})'
|
|
)
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|