boxes-api/test2.bash

175 lines
4.7 KiB
Bash
Raw Normal View History

2024-10-05 01:10:35 +00:00
#!/bin/bash
# API base URL
2024-10-05 03:03:50 +00:00
API_BASE_URL="http://10.0.0.66:8080"
2024-10-05 01:10:35 +00:00
# Login credentials
USERNAME="boxuser"
PASSWORD="boxuser"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
# Function to make an authenticated request
function authenticated_request() {
local method=$1
local endpoint=$2
local data=$3
# Get a new JWT token
TOKEN=$(curl -s -X POST -H "Content-Type: application/json" \
-d "{\"username\":\"$USERNAME\", \"password\":\"$PASSWORD\"}" \
"$API_BASE_URL/login" | jq -r '.token')
# Make the authenticated request
curl -s -X $method -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d "$data" \
"$API_BASE_URL$endpoint"
}
# --- Test Cases ---
# 1. Login
echo
echo
echo "Testing /login..."
response=$(curl -s -X POST -H "Content-Type: application/json" \
-d "{\"username\":\"$USERNAME\", \"password\":\"$PASSWORD\"}" \
"$API_BASE_URL/login")
if [[ $(echo "$response" | jq -r '.token') != "null" ]]; then
echo -e " /login: ${GREEN}PASS${NC}"
else
echo -e " /login: ${RED}FAIL${NC} (Invalid response)"
echo "$response"
fi
# 2. Get Boxes
echo
echo
echo "Testing /boxes (GET)..."
response=$(authenticated_request "GET" "/boxes" "")
if [[ $(echo "$response" | jq -r '. | length') -ge 0 ]]; then
echo -e " /boxes (GET): ${GREEN}PASS${NC}"
else
echo -e " /boxes (GET): ${RED}FAIL${NC} (Invalid response)"
echo "$response"
fi
# 3. Create Box
echo
echo
echo "Testing /boxes (POST)..."
response=$(authenticated_request "POST" "/boxes" "{\"name\":\"Test Box\"}")
echo $response | jq '.'
if [[ $(echo "$response" | jq -r '.name') == "Test Box" ]]; then
echo -e " /boxes (POST): ${GREEN}PASS${NC}"
echo $response
BOX_ID=$(echo "$response" | jq -r '.id') # Extract and save the box ID
echo $BOX_ID | jq .
else
echo -e " /boxes (POST): ${RED}FAIL${NC} (Invalid response)"
echo "$response"
fi
# 5. Create Item (Assuming a box with ID $BOX_ID exists)
echo
echo
echo "Testing /items (POST)..."
echo $BOX_ID
response=$(authenticated_request "POST" "/items" "{\"name\":\"Test Item\", \"description\":\"Test Description\", \"box_id\":$BOX_ID}")
if [[ $(echo "$response" | jq -r '.name') == "Test Item" ]]; then
echo -e " /items (POST): ${GREEN}PASS${NC}"
ITEM_ID=$(echo "$response" | jq -r '.id') # Extract and save the item ID
echo $response
else
echo -e " /items (POST): ${RED}FAIL${NC} (Invalid response)"
echo "$response"
fi
# 6. Get Items
echo
echo
echo "Testing /items (GET)..."
response=$(authenticated_request "GET" "/items" "")
if [[ $(echo "$response" | jq -r '. | length') -ge 0 ]]; then
echo -e " /items (GET): ${GREEN}PASS${NC}"
else
echo -e " /items (GET): ${RED}FAIL${NC} (Invalid response)"
echo "$response"
fi
# 7. Get Item by ID (Using the saved ITEM_ID)
echo
echo
echo "Testing /items/{id} (GET)..."
response=$(authenticated_request "GET" "/items/$ITEM_ID" "")
echo $response | jq .
if [[ $(echo "$response" | jq -r '.ID') == "$ITEM_ID" ]]; then
echo -e " /items/{id} (GET): ${GREEN}PASS${NC}"
else
echo -e " /items/{id} (GET): ${RED}FAIL${NC} (Invalid response)"
echo "$response"
fi
# 8. Get Items in Box (Using the saved BOX_ID)
echo
echo
echo "Testing /items/{id}/items (GET)..."
response=$(authenticated_request "GET" "/items/$BOX_ID/items" "")
if [[ $(echo "$response" | jq -r '. | length') -ge 1 ]]; then # Expecting at least one item
echo -e " /items/{id}/items (GET): ${GREEN}PASS${NC}"
else
echo -e " /items/{id}/items (GET): ${RED}FAIL${NC} (Invalid response)"
echo "$response"
fi
# 9. Update Item (Using the saved ITEM_ID)
echo
echo
echo "Testing /items/{id} (PUT)..."
response=$(authenticated_request "PUT" "/items/$ITEM_ID" "{\"name\":\"Updated Item\", \"description\":\"Updated Description\"}")
if [[ $(echo "$response" | jq -r '.name') == "Updated Item" ]]; then
echo -e " /items/{id} (PUT): ${GREEN}PASS${NC}"
else
echo -e " /items/{id} (PUT): ${RED}FAIL${NC} (Invalid response)"
echo "$response"
fi
# 10. Delete Item (Using the saved ITEM_ID)
echo
echo
echo "Testing /items/{id} (DELETE)..."
response=$(authenticated_request "DELETE" "/items/$ITEM_ID" "")
if [[ "$response" == "" ]]; then # Expecting 204 No Content (empty response)
echo -e " /items/{id} (DELETE): ${GREEN}PASS${NC}"
else
echo -e " /items/{id} (DELETE): ${RED}FAIL${NC} (Invalid response)"
echo "$response"
fi
# 4. Delete Box (Using the saved BOX_ID)
echo
echo
echo "Testing /boxes/{id} (DELETE)..."
response=$(authenticated_request "DELETE" "/boxes/$BOX_ID" "")
if [[ "$response" == "" ]]; then # Expecting 204 No Content (empty response)
echo -e " /boxes/{id} (DELETE): ${GREEN}PASS${NC}"
else
echo -e " /boxes/{id} (DELETE): ${RED}FAIL${NC} (Invalid response)"
echo "$response"
fi
echo "Tests completed."