the-tip-top-backend/test-auth.sh
2025-11-17 23:47:54 +01:00

99 lines
2.6 KiB
Bash
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
echo "=========================================="
echo "🧪 TEST D'AUTHENTIFICATION JWT"
echo "=========================================="
echo ""
# Couleurs
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
BASE_URL="http://localhost:4000"
echo "1⃣ Test: Inscription d'un client"
echo "------------------------------------------"
REGISTER_RESPONSE=$(curl -s -X POST $BASE_URL/api/auth/register \
-H "Content-Type: application/json" \
-d @- << 'EOF'
{
"email": "test-client@test.com",
"password": "Test1234",
"confirmPassword": "Test1234",
"firstName": "Test",
"lastName": "Client",
"phone": "0612345678",
"address": "1 rue Test",
"city": "Paris",
"postalCode": "75001"
}
EOF
)
echo "$REGISTER_RESPONSE" | head -10
echo ""
echo "2⃣ Test: Connexion avec le client"
echo "------------------------------------------"
LOGIN_RESPONSE=$(curl -s -X POST $BASE_URL/api/auth/login \
-H "Content-Type: application/json" \
-d @- << 'EOF'
{
"email": "test-client@test.com",
"password": "Test1234"
}
EOF
)
echo "$LOGIN_RESPONSE" | head -10
echo ""
# Extraire le token
TOKEN=$(echo $LOGIN_RESPONSE | grep -o '"token":"[^"]*' | cut -d'"' -f4)
if [ -z "$TOKEN" ]; then
echo "❌ Pas de token reçu"
else
echo "✅ Token reçu: ${TOKEN:0:30}..."
fi
echo ""
echo "3⃣ Test: Accès à une route protégée avec token"
echo "------------------------------------------"
if [ ! -z "$TOKEN" ]; then
PROFILE_RESPONSE=$(curl -s -X GET $BASE_URL/api/users/profile \
-H "Authorization: Bearer $TOKEN")
echo "$PROFILE_RESPONSE" | head -10
else
echo "❌ Impossible de tester - pas de token"
fi
echo ""
echo "4⃣ Test: Accès sans token (doit échouer)"
echo "------------------------------------------"
NO_TOKEN_RESPONSE=$(curl -s -X GET $BASE_URL/api/users/profile)
echo "$NO_TOKEN_RESPONSE"
echo ""
echo "5⃣ Test: Accès avec token invalide (doit échouer)"
echo "------------------------------------------"
INVALID_TOKEN_RESPONSE=$(curl -s -X GET $BASE_URL/api/users/profile \
-H "Authorization: Bearer invalid_token_12345")
echo "$INVALID_TOKEN_RESPONSE"
echo ""
echo "6⃣ Test: Route nécessitant rôle EMPLOYEE avec CLIENT (doit échouer)"
echo "------------------------------------------"
if [ ! -z "$TOKEN" ]; then
EMPLOYEE_RESPONSE=$(curl -s -X GET $BASE_URL/api/employee/pending-tickets \
-H "Authorization: Bearer $TOKEN")
echo "$EMPLOYEE_RESPONSE"
else
echo "❌ Impossible de tester - pas de token"
fi
echo ""
echo "=========================================="
echo "✅ Tests terminés"
echo "=========================================="