150 lines
4.6 KiB
Bash
150 lines
4.6 KiB
Bash
#!/bin/bash
|
||
|
||
# 🧪 Script de test - InnotexBoard API
|
||
# Ce script teste tous les endpoints avec curl
|
||
|
||
# Couleurs
|
||
GREEN='\033[0;32m'
|
||
RED='\033[0;31m'
|
||
YELLOW='\033[1;33m'
|
||
NC='\033[0m' # No Color
|
||
|
||
# Configuration
|
||
API_URL="http://localhost:8000/api/v1"
|
||
TEST_USER="${1:-your_user}"
|
||
TEST_PASS="${2:-your_pass}"
|
||
|
||
echo -e "${YELLOW}🧪 Démarrage des tests InnotexBoard${NC}\n"
|
||
|
||
# 1. Vérifier la santé de l'API
|
||
echo -e "${YELLOW}1️⃣ Test de santé${NC}"
|
||
response=$(curl -s -X GET "$API_URL/../health")
|
||
if echo "$response" | grep -q "healthy"; then
|
||
echo -e "${GREEN}✅ API en bonne santé${NC}\n"
|
||
else
|
||
echo -e "${RED}❌ API pas accessible${NC}"
|
||
echo "Response: $response\n"
|
||
fi
|
||
|
||
# 2. Login et obtenir le token
|
||
echo -e "${YELLOW}2️⃣ Test de connexion (PAM)${NC}"
|
||
echo "Utilisateur: $TEST_USER"
|
||
|
||
login_response=$(curl -s -X POST "$API_URL/auth/login" \
|
||
-H "Content-Type: application/x-www-form-urlencoded" \
|
||
-d "username=$TEST_USER&password=$TEST_PASS")
|
||
|
||
TOKEN=$(echo "$login_response" | grep -o '"access_token":"[^"]*' | cut -d'"' -f4)
|
||
|
||
if [ -n "$TOKEN" ]; then
|
||
echo -e "${GREEN}✅ Connexion réussie${NC}"
|
||
echo "Token: ${TOKEN:0:50}...\n"
|
||
else
|
||
echo -e "${RED}❌ Connexion échouée${NC}"
|
||
echo "Response: $login_response"
|
||
exit 1
|
||
fi
|
||
|
||
# 3. Test /auth/me
|
||
echo -e "${YELLOW}3️⃣ Test de l'utilisateur courant${NC}"
|
||
curl -s -X GET "$API_URL/auth/me" \
|
||
-H "Authorization: Bearer $TOKEN" | python3 -m json.tool
|
||
echo ""
|
||
|
||
# 4. Test /system/stats
|
||
echo -e "${YELLOW}4️⃣ Test des statistiques système${NC}"
|
||
echo "Récupération du CPU, RAM et processus..."
|
||
|
||
stats_response=$(curl -s -X GET "$API_URL/system/stats" \
|
||
-H "Authorization: Bearer $TOKEN")
|
||
|
||
if echo "$stats_response" | grep -q "cpu"; then
|
||
echo -e "${GREEN}✅ Stats système disponibles${NC}"
|
||
echo "$stats_response" | python3 -m json.tool | head -30
|
||
echo "... (truncated)"
|
||
else
|
||
echo -e "${RED}❌ Erreur lors de la récupération des stats${NC}"
|
||
echo "$stats_response"
|
||
fi
|
||
echo ""
|
||
|
||
# 5. Test /system/cpu uniquement
|
||
echo -e "${YELLOW}5️⃣ Test CPU uniquement${NC}"
|
||
curl -s -X GET "$API_URL/system/cpu" \
|
||
-H "Authorization: Bearer $TOKEN" | python3 -m json.tool
|
||
echo ""
|
||
|
||
# 6. Test /system/memory uniquement
|
||
echo -e "${YELLOW}6️⃣ Test Mémoire uniquement${NC}"
|
||
curl -s -X GET "$API_URL/system/memory" \
|
||
-H "Authorization: Bearer $TOKEN" | python3 -m json.tool
|
||
echo ""
|
||
|
||
# 7. Test /system/processes
|
||
echo -e "${YELLOW}7️⃣ Test des processus (Top 5)${NC}"
|
||
curl -s -X GET "$API_URL/system/processes?limit=5" \
|
||
-H "Authorization: Bearer $TOKEN" | python3 -m json.tool | head -50
|
||
echo ""
|
||
|
||
# 8. Test Docker status
|
||
echo -e "${YELLOW}8️⃣ Test de l'état Docker${NC}"
|
||
docker_status=$(curl -s -X GET "$API_URL/docker/status" \
|
||
-H "Authorization: Bearer $TOKEN")
|
||
|
||
echo "$docker_status" | python3 -m json.tool
|
||
|
||
if echo "$docker_status" | grep -q "true"; then
|
||
DOCKER_OK=true
|
||
echo -e "${GREEN}✅ Docker accessible${NC}\n"
|
||
else
|
||
DOCKER_OK=false
|
||
echo -e "${YELLOW}⚠️ Docker non accessible (c'est normal en dev)${NC}\n"
|
||
fi
|
||
|
||
# 9. Test Docker containers (si Docker est actif)
|
||
if [ "$DOCKER_OK" = true ]; then
|
||
echo -e "${YELLOW}9️⃣ Test lister les conteneurs${NC}"
|
||
containers=$(curl -s -X GET "$API_URL/docker/containers" \
|
||
-H "Authorization: Bearer $TOKEN")
|
||
|
||
echo "$containers" | python3 -m json.tool | head -50
|
||
echo ""
|
||
|
||
# Compter les conteneurs
|
||
count=$(echo "$containers" | grep -o '"id"' | wc -l)
|
||
echo "Total: $count conteneur(s)"
|
||
echo ""
|
||
fi
|
||
|
||
# 10. Test sans token (doit échouer)
|
||
echo -e "${YELLOW}🔟 Test de sécurité (sans token)${NC}"
|
||
no_token=$(curl -s -X GET "$API_URL/system/stats")
|
||
|
||
if echo "$no_token" | grep -q "401\|Unauthorized\|invalid"; then
|
||
echo -e "${GREEN}✅ Accès sans token refusé (sécurisé)${NC}"
|
||
else
|
||
echo -e "${RED}❌ Accès sans token autorisé (DANGER!)${NC}"
|
||
fi
|
||
echo ""
|
||
|
||
# 11. Test avec token invalide
|
||
echo -e "${YELLOW}1️⃣1️⃣ Test de sécurité (token invalide)${NC}"
|
||
bad_token=$(curl -s -X GET "$API_URL/system/stats" \
|
||
-H "Authorization: Bearer invalid_token_12345")
|
||
|
||
if echo "$bad_token" | grep -q "401\|Unauthorized"; then
|
||
echo -e "${GREEN}✅ Token invalide refusé (sécurisé)${NC}"
|
||
else
|
||
echo -e "${RED}❌ Token invalide accepté (DANGER!)${NC}"
|
||
fi
|
||
echo ""
|
||
|
||
# Résumé
|
||
echo -e "${YELLOW}📊 Résumé des tests${NC}"
|
||
echo -e "${GREEN}✅ Tous les tests sont terminés${NC}"
|
||
echo -e "\n${YELLOW}💡 Tips:${NC}"
|
||
echo "- Accédez à la documentation Swagger: http://localhost:8000/docs"
|
||
echo "- Accédez à l'interface web: http://localhost:3000"
|
||
echo "- Vérifiez les logs du backend: tail -f backend/logs.log"
|
||
echo ""
|