Files
api-sessions/test-api.sh
2026-01-23 01:03:09 +01:00

313 líneas
8.6 KiB
Bash
Archivo Ejecutable

#!/bin/sh
# API Testing Script for Product CRUD Operations
# This script tests all endpoints of the API with JWT authentication
BASE_URL="http://localhost:8080"
TOKEN=""
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Print colored output
print_header() {
echo "${BLUE}================================================${NC}"
echo "${BLUE}$1${NC}"
echo "${BLUE}================================================${NC}"
}
print_success() {
echo "${GREEN}$1${NC}"
}
print_error() {
echo "${RED}$1${NC}"
}
print_info() {
echo "${YELLOW}$1${NC}"
}
# Function to pretty print JSON
print_json() {
if command -v python3 >/dev/null 2>&1; then
echo "$1" | python3 -m json.tool 2>/dev/null || echo "$1"
else
echo "$1"
fi
}
# Test 1: Login and get JWT token
test_login() {
print_header "TEST 1: LOGIN - Get JWT Token"
print_info "Attempting to login with username: admin, password: admin123"
RESPONSE=$(curl -s -X POST "$BASE_URL/api/auth/login" \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"admin123"}')
echo "Response:"
print_json "$RESPONSE"
TOKEN=$(echo "$RESPONSE" | grep -o '"token":"[^"]*' | cut -d'"' -f4)
if [ -n "$TOKEN" ]; then
print_success "Login successful! Token obtained."
echo "Token: $TOKEN"
else
print_error "Login failed! No token received."
exit 1
fi
echo ""
}
# Test 2: Check authentication status
test_auth_status() {
print_header "TEST 2: CHECK AUTHENTICATION STATUS"
RESPONSE=$(curl -s -X GET "$BASE_URL/api/auth/status" \
-H "Authorization: Bearer $TOKEN")
echo "Response:"
print_json "$RESPONSE"
print_success "Authentication status checked"
echo ""
}
# Test 3: Get all products
test_get_all_products() {
print_header "TEST 3: GET ALL PRODUCTS"
RESPONSE=$(curl -s -X GET "$BASE_URL/api/products" \
-H "Authorization: Bearer $TOKEN")
echo "Response:"
print_json "$RESPONSE"
print_success "Retrieved all products"
echo ""
}
# Test 4: Get product by ID
test_get_product_by_id() {
print_header "TEST 4: GET PRODUCT BY ID (ID=1)"
RESPONSE=$(curl -s -X GET "$BASE_URL/api/products/1" \
-H "Authorization: Bearer $TOKEN")
echo "Response:"
print_json "$RESPONSE"
print_success "Retrieved product with ID 1"
echo ""
}
# Test 5: Search products by name
test_search_products() {
print_header "TEST 5: SEARCH PRODUCTS (name=laptop)"
RESPONSE=$(curl -s -X GET "$BASE_URL/api/products/search?name=laptop" \
-H "Authorization: Bearer $TOKEN")
echo "Response:"
print_json "$RESPONSE"
print_success "Search completed"
echo ""
}
# Test 6: Get products by category
test_get_by_category() {
print_header "TEST 6: GET PRODUCTS BY CATEGORY (Electronics)"
RESPONSE=$(curl -s -X GET "$BASE_URL/api/products/category/Electronics" \
-H "Authorization: Bearer $TOKEN")
echo "Response:"
print_json "$RESPONSE"
print_success "Retrieved products in Electronics category"
echo ""
}
# Test 7: Create a new product
test_create_product() {
print_header "TEST 7: CREATE NEW PRODUCT"
print_info "Creating product: Gaming Console PlayStation 5"
RESPONSE=$(curl -s -X POST "$BASE_URL/api/products" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "Gaming Console PlayStation 5",
"description": "Next-gen gaming console with 4K support",
"price": 499.99,
"stock": 10,
"category": "Gaming"
}')
echo "Response:"
print_json "$RESPONSE"
print_success "Product created successfully"
echo ""
}
# Test 8: Update a product
test_update_product() {
print_header "TEST 8: UPDATE PRODUCT (ID=1)"
print_info "Updating product ID 1: Changing price and stock"
RESPONSE=$(curl -s -X PUT "$BASE_URL/api/products/1" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "Laptop Dell XPS 15 (Updated)",
"description": "High-performance laptop with 32GB RAM and 1TB SSD - UPDATED!",
"price": 1499.99,
"stock": 20,
"category": "Electronics"
}')
echo "Response:"
print_json "$RESPONSE"
print_success "Product updated successfully"
echo ""
}
# Test 9: Get updated product to verify changes
test_verify_update() {
print_header "TEST 9: VERIFY UPDATE (Get Product ID=1)"
RESPONSE=$(curl -s -X GET "$BASE_URL/api/products/1" \
-H "Authorization: Bearer $TOKEN")
echo "Response:"
print_json "$RESPONSE"
print_success "Verified product update"
echo ""
}
# Test 10: Get all products after creation
test_get_all_after_create() {
print_header "TEST 10: GET ALL PRODUCTS (After Create)"
RESPONSE=$(curl -s -X GET "$BASE_URL/api/products" \
-H "Authorization: Bearer $TOKEN")
echo "Response:"
print_json "$RESPONSE"
print_success "Retrieved all products (should see new product)"
echo ""
}
# Test 11: Delete a product
test_delete_product() {
print_header "TEST 11: DELETE PRODUCT (ID=9)"
print_info "Deleting the newly created product (ID=9)"
RESPONSE=$(curl -s -X DELETE "$BASE_URL/api/products/9" \
-H "Authorization: Bearer $TOKEN")
echo "Response:"
echo "$RESPONSE"
print_success "Product deleted successfully"
echo ""
}
# Test 12: Verify deletion
test_verify_deletion() {
print_header "TEST 12: VERIFY DELETION (Try to get deleted product ID=9)"
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" -X GET "$BASE_URL/api/products/9" \
-H "Authorization: Bearer $TOKEN")
echo "HTTP Status Code: $HTTP_CODE"
if [ "$HTTP_CODE" = "404" ]; then
print_success "Product successfully deleted (404 Not Found)"
else
print_error "Product may still exist (Status: $HTTP_CODE)"
fi
echo ""
}
# Test 13: Get all products after deletion
test_get_all_final() {
print_header "TEST 13: GET ALL PRODUCTS (Final State)"
RESPONSE=$(curl -s -X GET "$BASE_URL/api/products" \
-H "Authorization: Bearer $TOKEN")
echo "Response:"
print_json "$RESPONSE"
print_success "Retrieved final product list"
echo ""
}
# Test 14: Test authentication failure (without token)
test_unauthorized_access() {
print_header "TEST 14: TEST UNAUTHORIZED ACCESS (No Token)"
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" -X GET "$BASE_URL/api/products")
echo "HTTP Status Code: $HTTP_CODE"
if [ "$HTTP_CODE" = "401" ] || [ "$HTTP_CODE" = "403" ]; then
print_success "Access correctly denied without token (Status: $HTTP_CODE)"
else
print_error "Unexpected status code: $HTTP_CODE"
fi
echo ""
}
# Main execution
main() {
echo "${GREEN}"
echo "╔═══════════════════════════════════════════════════════════╗"
echo "║ API SESSIONS - CRUD TEST SUITE ║"
echo "║ Testing JWT Authentication & Product API ║"
echo "╚═══════════════════════════════════════════════════════════╝"
echo "${NC}"
echo ""
print_info "Starting API tests..."
print_info "Base URL: $BASE_URL"
echo ""
# Run all tests
test_login
test_auth_status
test_get_all_products
test_get_product_by_id
test_search_products
test_get_by_category
test_create_product
test_update_product
test_verify_update
test_get_all_after_create
test_delete_product
test_verify_deletion
test_get_all_final
test_unauthorized_access
print_header "ALL TESTS COMPLETED!"
echo "${GREEN}All CRUD operations have been tested successfully!${NC}"
echo ""
echo "${YELLOW}Summary of tested operations:${NC}"
echo " ✓ Authentication (Login)"
echo " ✓ Authorization (JWT Token)"
echo " ✓ CREATE - New product"
echo " ✓ READ - All products, by ID, search, by category"
echo " ✓ UPDATE - Existing product"
echo " ✓ DELETE - Product removal"
echo " ✓ Security - Unauthorized access protection"
echo ""
}
# Run main function
main