chu-iptv/tests/test_flow.sh

199 lines
9.6 KiB
Bash
Executable file

#!/bin/bash
# ═══════════════════════════════════════════════════════════════════════════════
# CHU-IPTV — Tests Automatisés du Flux Complet
# ═══════════════════════════════════════════════════════════════════════════════
# Usage: ./tests/test_flow.sh [API_URL]
# Par défaut: https://chu-iptv-api.cosmolan.fr
API_URL="${1:-https://chu-iptv-api.cosmolan.fr}"
PASS=0
FAIL=0
TOTAL=0
# Couleurs
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'
assert() {
TOTAL=$((TOTAL + 1))
local desc="$1"
local result="$2"
local expected="$3"
if [ "$result" = "$expected" ]; then
echo -e " ${GREEN}${NC} $desc"
PASS=$((PASS + 1))
else
echo -e " ${RED}${NC} $desc (attendu: $expected, obtenu: $result)"
FAIL=$((FAIL + 1))
fi
}
assert_contains() {
TOTAL=$((TOTAL + 1))
local desc="$1"
local haystack="$2"
local needle="$3"
if echo "$haystack" | grep -q "$needle"; then
echo -e " ${GREEN}${NC} $desc"
PASS=$((PASS + 1))
else
echo -e " ${RED}${NC} $desc (ne contient pas: $needle)"
FAIL=$((FAIL + 1))
fi
}
echo -e "${YELLOW}═══ CHU-IPTV — Tests Automatisés ═══${NC}"
echo -e "API: $API_URL"
echo ""
# ─── 1. Santé de l'API ────────────────────────────────────────────────────────
echo -e "${YELLOW}[1/8] Santé de l'API${NC}"
HEALTH=$(curl -s "$API_URL/api/health")
assert "API répond" "$(echo $HEALTH | jq -r '.status' 2>/dev/null)" "ok"
assert_contains "Version présente" "$HEALTH" "status"
# ─── 2. Authentification Staff ────────────────────────────────────────────────
echo ""
echo -e "${YELLOW}[2/8] Authentification Staff${NC}"
LOGIN=$(curl -s -X POST "$API_URL/api/auth/staff/login" \
-H "Content-Type: application/json" \
-d '{"email":"admin@chu-iptv.cosmolan.fr","password":"admin2026"}')
STAFF_TOKEN=$(echo $LOGIN | jq -r '.token' 2>/dev/null)
assert "Login admin réussi" "$(echo $LOGIN | jq -r '.staff.role' 2>/dev/null)" "ADMIN"
assert "Token JWT reçu" "$([ -n "$STAFF_TOKEN" ] && [ "$STAFF_TOKEN" != "null" ] && echo "yes" || echo "no")" "yes"
# Test login invalide
BAD_LOGIN=$(curl -s -o /dev/null -w "%{http_code}" -X POST "$API_URL/api/auth/staff/login" \
-H "Content-Type: application/json" \
-d '{"email":"admin@chu-iptv.cosmolan.fr","password":"wrong"}')
assert "Login invalide rejeté (401)" "$BAD_LOGIN" "401"
# ─── 3. Chambres ──────────────────────────────────────────────────────────────
echo ""
echo -e "${YELLOW}[3/8] Gestion des Chambres${NC}"
ROOMS=$(curl -s "$API_URL/api/rooms" -H "Authorization: Bearer $STAFF_TOKEN")
ROOM_COUNT=$(echo $ROOMS | jq 'length' 2>/dev/null)
assert "10 chambres présentes" "$ROOM_COUNT" "10"
ROOM_ID=$(echo $ROOMS | jq -r '.[0].id' 2>/dev/null)
ROOM_NUM=$(echo $ROOMS | jq -r '.[0].number' 2>/dev/null)
assert "Première chambre identifiée" "$([ -n "$ROOM_ID" ] && [ "$ROOM_ID" != "null" ] && echo "yes" || echo "no")" "yes"
# Chambre par numéro
ROOM_DETAIL=$(curl -s "$API_URL/api/rooms/by-number/$ROOM_NUM")
assert "Chambre par numéro" "$(echo $ROOM_DETAIL | jq -r '.number' 2>/dev/null)" "$ROOM_NUM"
assert_contains "Domotique incluse" "$ROOM_DETAIL" "domotics"
# ─── 4. QR Pairing ───────────────────────────────────────────────────────────
echo ""
echo -e "${YELLOW}[4/8] QR Pairing${NC}"
QR=$(curl -s -X POST "$API_URL/api/auth/qr/generate" \
-H "Content-Type: application/json" \
-d "{\"roomId\":\"$ROOM_ID\"}")
PAIRING_CODE=$(echo $QR | jq -r '.pairingCode' 2>/dev/null)
assert "QR généré" "$([ -n "$PAIRING_CODE" ] && [ "$PAIRING_CODE" != "null" ] && echo "yes" || echo "no")" "yes"
assert_contains "URL mobile présente" "$QR" "mobileUrl"
# Pairing
PAIR=$(curl -s -X POST "$API_URL/api/auth/qr/pair" \
-H "Content-Type: application/json" \
-d "{\"pairingCode\":\"$PAIRING_CODE\"}")
PATIENT_TOKEN=$(echo $PAIR | jq -r '.token' 2>/dev/null)
assert "Pairing réussi" "$(echo $PAIR | jq -r '.room.number' 2>/dev/null)" "$ROOM_NUM"
assert "Token patient reçu" "$([ -n "$PATIENT_TOKEN" ] && [ "$PATIENT_TOKEN" != "null" ] && echo "yes" || echo "no")" "yes"
# Pairing code invalide
BAD_PAIR=$(curl -s -o /dev/null -w "%{http_code}" -X POST "$API_URL/api/auth/qr/pair" \
-H "Content-Type: application/json" \
-d '{"pairingCode":"INVALID0"}')
assert "Pairing invalide rejeté (404)" "$BAD_PAIR" "404"
# ─── 5. Chaînes IPTV ─────────────────────────────────────────────────────────
echo ""
echo -e "${YELLOW}[5/8] Chaînes IPTV${NC}"
CHANNELS=$(curl -s "$API_URL/api/iptv/channels")
CH_COUNT=$(echo $CHANNELS | jq 'length' 2>/dev/null)
assert "6 chaînes actives" "$CH_COUNT" "6"
assert_contains "Première chaîne a un streamUrl" "$CHANNELS" "streamUrl"
# Changement de chaîne
TUNE=$(curl -s -X POST "$API_URL/api/iptv/tune" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $PATIENT_TOKEN" \
-d "{\"roomId\":\"$ROOM_ID\",\"channelNumber\":2}")
assert "Changement de chaîne" "$(echo $TUNE | jq -r '.success' 2>/dev/null)" "true"
# Chaîne actuelle
CURRENT=$(curl -s "$API_URL/api/iptv/current/$ROOM_ID")
assert "Chaîne actuelle = 2" "$(echo $CURRENT | jq -r '.channel.number' 2>/dev/null)" "2"
# ─── 6. EPG ──────────────────────────────────────────────────────────────────
echo ""
echo -e "${YELLOW}[6/8] EPG (Guide des programmes)${NC}"
EPG=$(curl -s "$API_URL/api/iptv/epg")
assert_contains "EPG contient chaîne 1" "$EPG" "\"1\""
EPG_CH1=$(curl -s "$API_URL/api/iptv/epg/1")
assert_contains "Programme en cours" "$EPG_CH1" "isLive"
assert_contains "Programme suivant" "$EPG_CH1" "next"
assert_contains "Grille horaire" "$EPG_CH1" "schedule"
# ─── 7. Demandes Patient ──────────────────────────────────────────────────────
echo ""
echo -e "${YELLOW}[7/8] Demandes Patient${NC}"
REQ=$(curl -s -X POST "$API_URL/api/requests" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $PATIENT_TOKEN" \
-d '{"type":"WATER","message":"Un verre d eau svp","priority":"NORMAL"}')
REQ_ID=$(echo $REQ | jq -r '.id' 2>/dev/null)
assert "Demande créée" "$(echo $REQ | jq -r '.type' 2>/dev/null)" "WATER"
assert "Statut PENDING" "$(echo $REQ | jq -r '.status' 2>/dev/null)" "PENDING"
# Liste des demandes (staff)
REQS=$(curl -s "$API_URL/api/requests" -H "Authorization: Bearer $STAFF_TOKEN")
assert_contains "Demande dans la liste" "$REQS" "$REQ_ID"
# Mise à jour statut
if [ -n "$REQ_ID" ] && [ "$REQ_ID" != "null" ]; then
UPDATE=$(curl -s -X PATCH "$API_URL/api/requests/$REQ_ID/status" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $STAFF_TOKEN" \
-d '{"status":"COMPLETED"}')
assert "Demande complétée" "$(echo $UPDATE | jq -r '.status' 2>/dev/null)" "COMPLETED"
fi
# ─── 8. Chat ─────────────────────────────────────────────────────────────────
echo ""
echo -e "${YELLOW}[8/8] Chat Patient/Soignant${NC}"
MSG_P=$(curl -s -X POST "$API_URL/api/chat/message" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $PATIENT_TOKEN" \
-d "{\"roomId\":\"$ROOM_ID\",\"content\":\"Bonjour, quand est le prochain repas ?\"}")
assert "Message patient envoyé" "$(echo $MSG_P | jq -r '.sender' 2>/dev/null)" "PATIENT"
MSG_S=$(curl -s -X POST "$API_URL/api/chat/message" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $STAFF_TOKEN" \
-d "{\"roomId\":\"$ROOM_ID\",\"content\":\"Bonjour ! Le repas est a 12h30.\"}")
assert "Message soignant envoyé" "$(echo $MSG_S | jq -r '.sender' 2>/dev/null)" "STAFF"
# Historique
HISTORY=$(curl -s "$API_URL/api/chat/$ROOM_ID" -H "Authorization: Bearer $STAFF_TOKEN")
HIST_COUNT=$(echo $HISTORY | jq 'length' 2>/dev/null)
assert "Historique contient 2+ messages" "$([ "$HIST_COUNT" -ge 2 ] && echo "yes" || echo "no")" "yes"
# ─── Résumé ───────────────────────────────────────────────────────────────────
echo ""
echo -e "${YELLOW}═══════════════════════════════════════════════════${NC}"
echo -e " Résultats: ${GREEN}$PASS passés${NC} / ${RED}$FAIL échoués${NC} / $TOTAL total"
if [ $FAIL -eq 0 ]; then
echo -e " ${GREEN}✓ TOUS LES TESTS PASSENT${NC}"
else
echo -e " ${RED}$FAIL TEST(S) EN ÉCHEC${NC}"
fi
echo -e "${YELLOW}═══════════════════════════════════════════════════${NC}"
exit $FAIL