#!/usr/bin/env bash # quick-261008-who: Vorladen-Schalter gegen den laufenden Stack pruefen (API). set -euo pipefail API=http://localhost:3001 JAR=$(mktemp) CREATED=() cleanup() { for id in "${CREATED[@]:-}"; do [ -n "$id" ] && curl -s -o /dev/null -b "$JAR" -X DELETE "$API/custom-modules/$id" || true done rm -f "$JAR" "$JAR.out" } trap cleanup EXIT fail() { echo "FEHLER: $*" >&2; exit 1; } for _ in $(seq 1 90); do curl -sf "$API/health" >/dev/null 2>&1 && break sleep 2 done curl -sf "$API/health" >/dev/null || fail "API nicht erreichbar" code=$(curl -s -o /dev/null -w '%{http_code}' -c "$JAR" -H 'Content-Type: application/json' \ -d '{"username":"admin","password":"admin123"}' "$API/auth/login") case "$code" in 200|201) ;; *) fail "Anmeldung: HTTP $code" ;; esac CAT=$(curl -sf -b "$JAR" "$API/module-categories" | python3 -c 'import sys,json; print(json.load(sys.stdin)[0]["key"])') # Altlasten aus abgebrochenen Laeufen entfernen for id in $(curl -sf -b "$JAR" "$API/custom-modules" | python3 -c 'import sys,json; [print(m["id"]) for m in json.load(sys.stdin) if m["name"].startswith("Probe who ")]'); do curl -s -o /dev/null -b "$JAR" -X DELETE "$API/custom-modules/$id" done IDS=() for n in 1 2 3 4 5 6 7 8 9; do body=$(printf '{"name":"Probe who %s","url":"https://example.com/?who=%s","category":"%s"}' "$n" "$n" "$CAT") id=$(curl -sf -b "$JAR" -H 'Content-Type: application/json' -d "$body" "$API/custom-modules" \ | python3 -c 'import sys,json; print(json.load(sys.stdin)["id"])') IDS+=("$id") CREATED+=("$id") done put() { # id json -> Statuscode, Antwort in $JAR.out curl -s -o "$JAR.out" -w '%{http_code}' -b "$JAR" -X PUT -H 'Content-Type: application/json' -d "$2" "$API/custom-modules/$1/preload" } for i in 0 1 2 3 4 5 6 7; do c=$(put "${IDS[$i]}" '{"preload":true}') [ "$c" = 200 ] || fail "preload true Nr. $((i + 1)): HTTP $c" grep -q '"preload":true' "$JAR.out" || fail "Antwort Nr. $((i + 1)) ohne preload:true" done c=$(put "${IDS[8]}" '{"preload":true}') [ "$c" = 409 ] || fail "neunter Versuch: erwartet 409, HTTP $c" LIST=$(curl -sf -b "$JAR" "$API/custom-modules") echo "$LIST" | python3 -c ' import sys, json ids = sys.argv[1:] by = {m["id"]: m for m in json.load(sys.stdin)} assert by[ids[0]]["preload"] is True, "Nr. 1 nicht vorgeladen" assert by[ids[8]]["preload"] is False, "Nr. 9 faelschlich vorgeladen" ' "${IDS[@]}" || fail "Liste zeigt falsche preload-Werte" c=$(put "${IDS[0]}" '{"preload":false}') [ "$c" = 200 ] || fail "preload false Nr. 1: HTTP $c" c=$(put "${IDS[8]}" '{"preload":true}') [ "$c" = 200 ] || fail "Nr. 9 nach Freigabe: HTTP $c" c=$(put "${IDS[1]}" '{"preload":"ja"}') [ "$c" = 400 ] || fail "preload \"ja\": erwartet 400, HTTP $c" c=$(put "$(python3 -c 'import uuid; print(uuid.uuid4())')" '{"preload":true}') [ "$c" = 404 ] || fail "zufaellige Kennung: erwartet 404, HTTP $c" for id in "${IDS[@]}"; do curl -sf -o /dev/null -b "$JAR" -X DELETE "$API/custom-modules/$id" || fail "Loeschen $id" done CREATED=() LEFT=$(curl -sf -b "$JAR" "$API/custom-modules" | python3 -c 'import sys,json; print(sum(1 for m in json.load(sys.stdin) if m["name"].startswith("Probe who ")))') [ "$LEFT" = 0 ] || fail "$LEFT Probe-Module uebrig" rm -f "$JAR.out" echo "e2e preload api ok"