Files
projet_gestion_commande/s2.sh
T

240 lines
8.8 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# ============================================
# SCRIPT DE SIMULATION RÉALISTE - E-COMMERCE
# Version avec validation GPS + Simulation déplacement livreur
# ============================================
BASE_URL="https://c80319bf2e28244c-90-50-148-138.serveousercontent.com"
ADMIN_TOKEN=""
CABINE_TOKEN=""
LIVREUR_TOKEN=""
NUM_PRODUCTS=13
# Couleurs pour l'affichage
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
declare -A PRODUCT_DETAILS
declare -a PRODUCT_IDS
# Compteurs de tests
TOTAL_TESTS=0
PASSED_TESTS=0
FAILED_TESTS=0
# Fonctions d'affichage
print_section() {
echo ""
echo -e "${BLUE}================================================${NC}"
echo -e "${BLUE}$1${NC}"
echo -e "${BLUE}================================================${NC}"
}
print_test() {
echo ""
echo -e "${YELLOW}$1${NC}"
((TOTAL_TESTS++))
}
print_success() {
echo -e "${GREEN}$1${NC}"
((PASSED_TESTS++))
}
print_error() {
echo -e "${RED}$1${NC}"
((FAILED_TESTS++))
}
print_info() {
echo -e "${MAGENTA} $1${NC}"
}
print_warning() {
echo -e "${YELLOW}$1${NC}"
}
# Fonction pour extraire le token
extract_token() {
local response="$1"
local token=$(echo "$response" | jq -r '.token // .access_token // empty' 2>/dev/null)
echo "$token"
}
# Fonction pour extraire un ID
extract_id() {
local response="$1"
local field="${2:-id}"
local id=$(echo "$response" | jq -r ".${field} // .product.id // .product_id // empty" 2>/dev/null)
echo "$id"
}
# Fonction pour vérifier les erreurs
check_error() {
local response="$1"
if echo "$response" | jq -e '.error' >/dev/null 2>&1; then
return 0
else
return 1
fi
}
# Fonction pour vérifier le succès
check_success() {
local response="$1"
if echo "$response" | jq -e '.id // .token // .success // .command_id' >/dev/null 2>&1; then
return 0
else
return 1
fi
}
# ============================================
# 0. VÉRIFICATION PRÉALABLE
# ============================================
print_section "0. VÉRIFICATION PRÉALABLE"
print_test "Vérification du serveur"
HEALTH_CHECK=$(curl -s -X GET "$BASE_URL/api/v1/health")
if [ -n "$HEALTH_CHECK" ]; then
print_success "Serveur accessible"
else
print_error "Serveur non accessible"
exit 1
fi
# ============================================
# 1. AUTHENTIFICATION ADMIN
# ============================================
print_section "0. AUTHENTIFICATION CLIENT"
CLIENT_TOKEN=$(extract_token "$CLIENT_LOGIN")
print_test "Création client"
CLIENT_REGISTER=$(curl -s -X POST "$BASE_URL/api/v1/auth/register" \
-H "Content-Type: application/json" \
-d "{\"username\":\"salut\",\"password\":\"salut1234_\",\"role\":\"client\",\"nom\": \"Dupont\",\"prenom\": \"Jean\",\"telephone\": \"+33612345678\"}")
CLIENT_TOKEN=$(extract_token "$CLIENT_REGISTER")
if [ -n "$CLIENT_TOKEN" ]; then
print_success "Client créé et authentifié"
else
print_error "Échec authentification client"
exit 1
fi
# ============================================
# 1. AUTHENTIFICATION ADMIN
# ============================================
print_section "1. AUTHENTIFICATION ADMIN"
ADMIN_TOKEN=$(extract_token "$ADMIN_LOGIN")
print_test "Création admin"
UNIQUE_ADMIN="admin_$(date +%s)"
ADMIN_REGISTER=$(curl -s -X POST "$BASE_URL/api/v2/admin/auth/register" \
-H "Content-Type: application/json" \
-d "{\"username\":\"$UNIQUE_ADMIN\",\"password\":\"AdminPass123!\",\"role\":\"admin\"}")
ADMIN_TOKEN=$(extract_token "$ADMIN_REGISTER")
if [ -n "$ADMIN_TOKEN" ]; then
print_success "Admin créé et authentifié"
else
print_error "Échec authentification admin"
exit 1
fi
print_test "Création livreur"
UNIQUE_LIVREUR="livreur_$(date +%s)"
ADMIN_REGISTER=$(curl -s -X POST "$BASE_URL/api/v2/admin/auth/register" \
-H "Content-Type: application/json" \
-d "{\"username\":\"$UNIQUE_LIVREUR\",\"password\":\"AdminPass123!\",\"role\":\"livreur\"}")
LIVREUR_TOKEN=$(extract_token "$ADMIN_REGISTER")
if [ -n "$LIVREUR_TOKEN" ]; then
print_success "Admin créé et authentifié"
else
print_error "Échec authentification admin"
exit 1
fi
print_test "Création cabine"
UNIQUE_CABINE="cabine_$(date +%s)"
ADMIN_REGISTER=$(curl -s -X POST "$BASE_URL/api/v2/admin/auth/register" \
-H "Content-Type: application/json" \
-d "{\"username\":\"$UNIQUE_CABINE\",\"password\":\"AdminPass123!\",\"role\":\"cabine\"}")
CABINE_TOKEN=$(extract_token "$ADMIN_REGISTER")
if [ -n "$CABINE_TOKEN" ]; then
print_success "Admin créé et authentifié"
else
print_error "Échec authentification admin"
exit 1
fi
# ============================================
# 2. CRÉATION MASSIVE DE PRODUITS
# ============================================
print_section "2. CRÉATION MASSIVE DE PRODUITS"
print_info "Création de $NUM_PRODUCTS produits..."
PREDEFINED_PRODUCTS=(
'{"name":"Zipette Borealis 1", "category":"zipette&co", "description":"Plante luminescente aux teintes bleutées inspirée des aurores boréales", "price":24.99, "stock":35.00}'
'{"name":"Zipette Titan 1", "category":"zipette&co", "description":"Plante géante fictive atteignant jusqu'\''à 2 mètres en intérieur", "price":19.99, "stock":50.00}'
'{"name":"Zipette Borealis 2", "category":"zipette&co", "description":"Plante luminescente aux teintes bleutées inspirée des aurores boréales", "price":24.99, "stock":35.00}'
'{"name":"Zipette Titan 2", "category":"zipette&co", "description":"Plante géante fictive atteignant jusqu'\''à 2 mètres en intérieur", "price":19.99, "stock":50.00}'
'{"name":"Zipette Borealis 3", "category":"zipette&co", "description":"Plante luminescente aux teintes bleutées inspirée des aurores boréales", "price":24.99, "stock":35.00}'
'{"name":"Zipette Titan 3", "category":"zipette&co", "description":"Plante géante fictive atteignant jusqu'\''à 2 mètres en intérieur", "price":19.99, "stock":50.00}'
'{"name":"Titan 4", "category":"gros&semi", "description":"Plante géante fictive atteignant jusqu'\''à 2 mètres en intérieur", "price":19.99, "stock":50.00}'
'{"name":"Borealis 4", "category":"gros&semi", "description":"Plante luminescente aux teintes bleutées inspirée des aurores boréales", "price":24.99, "stock":35.00}'
'{"name":"Titan 5", "category":"gros&semi", "description":"Plante géante fictive atteignant jusqu'\''à 2 mètres en intérieur", "price":19.99, "stock":50.00}'
'{"name":"Kush 1", "category":"weed&hash", "description":"Variété fictive aux arômes terreux et notes citronnées, culture indoor facile", "price":14.99, "stock":80.00}'
'{"name":"Haze 1", "category":"weed&hash", "description":"Plante fictive à dominance sativa, réputée pour son parfum épicé", "price":16.99, "stock":60.00}'
'{"name":"Hash Gold 1", "category":"weed&hash", "description":"Résine fictive premium à la texture souple et aux notes florales", "price":9.99, "stock":120.00}'
'{"name":"Hash Black 1", "category":"weed&hash", "description":"Résine fictive sombre, goût intense et épicé", "price":11.99, "stock":90.00}'
'{"name":"Kush 2", "category":"weed&hash", "description":"Variété fictive compacte, floraison rapide et parfum boisé", "price":14.99, "stock":75.00}'
)
for ((i=0; i<NUM_PRODUCTS && i<${#PREDEFINED_PRODUCTS[@]}; i++)); do
PRODUCT_JSON=${PREDEFINED_PRODUCTS[$i]}
PRODUCT_NAME=$(echo "$PRODUCT_JSON" | jq -r '.name')
PRODUCT_CATEGORY=$(echo "$PRODUCT_JSON" | jq -r '.category')
PRODUCT_DESC=$(echo "$PRODUCT_JSON" | jq -r '.description')
PRODUCT_PRICE=$(echo "$PRODUCT_JSON" | jq -r '.price')
PRODUCT_STOCK=$(echo "$PRODUCT_JSON" | jq -r '.stock')
print_test "Création produit $((i+1))/$NUM_PRODUCTS: $PRODUCT_NAME"
PRODUCT_RESPONSE=$(curl -s -X POST "$BASE_URL/api/v2/admin/protected/products" \
-H "Authorization: Bearer $ADMIN_TOKEN" \
-F "name=$PRODUCT_NAME" \
-F "category=$PRODUCT_CATEGORY" \
-F "description=$PRODUCT_DESC" \
-F "stock=$PRODUCT_STOCK" \
-F "prices[0][quantity]=1" \
-F "prices[0][price]=$PRODUCT_PRICE" \
-F "prices[1][quantity]=3" \
-F "prices[1][price]=$(echo "scale=2; $PRODUCT_PRICE * 3 * 0.9" | bc)" \
-F "prices[2][quantity]=5" \
-F "prices[2][price]=$(echo "scale=2; $PRODUCT_PRICE * 5 * 0.85" | bc)")
PRODUCT_ID=$(extract_id "$PRODUCT_RESPONSE")
if [ -n "$PRODUCT_ID" ]; then
PRODUCT_IDS+=("$PRODUCT_ID")
PRODUCT_DETAILS["$PRODUCT_ID"]="$PRODUCT_JSON"
print_success "Produit créé (ID: $PRODUCT_ID) - $PRODUCT_NAME - ${PRODUCT_PRICE}€"
else
print_warning "Échec création produit $PRODUCT_NAME"
echo "Response: $PRODUCT_RESPONSE"
fi
sleep 0.5
done
print_info "Total produits créés: ${#PRODUCT_IDS[@]}"