#!/bin/bash # API base URL API_BASE_URL="http://localhost:8080" # Login credentials USERNAME="boxuser" PASSWORD="boxuser" # 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 "\033[32m /login: PASS\033[0m" # Green PASS else echo -e "\033[31m /login: FAIL (Invalid response)\033[0m" # Red FAIL 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 "\033[32m /boxes (GET): PASS\033[0m" # Green PASS else echo -e "\033[31m /boxes (GET): FAIL (Invalid response)\033[0m" # Red FAIL 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 "\033[32m /boxes (POST): PASS\033[0m" # Green PASS echo $response BOX_ID=$(echo "$response" | jq -r '.id') # Extract and save the box ID echo $BOX_ID | jq . else echo -e "\033[31m /boxes (POST): FAIL (Invalid response)\033[0m" # Red FAIL 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 "\033[32m /items (POST): PASS\033[0m" # Green PASS ITEM_ID=$(echo "$response" | jq -r '.id') # Extract and save the item ID echo $response else echo -e "\033[31m /items (POST): FAIL (Invalid response)\033[0m" # Red FAIL 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 "\033[32m /items (GET): PASS\033[0m" # Green PASS else echo -e "\033[31m /items (GET): FAIL (Invalid response)\033[0m" # Red FAIL 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 "\033[32m /items/{id} (GET): PASS\033[0m" # Green PASS else echo -e "\033[31m /items/{id} (GET): FAIL (Invalid response)\033[0m" # Red FAIL 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 "\033[32m /items/{id}/items (GET): PASS\033[0m" # Green PASS else echo -e "\033[31m /items/{id}/items (GET): FAIL (Invalid response)\033[0m" # Red FAIL 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 "\033[32m /items/{id} (PUT): PASS\033[0m" # Green PASS else echo -e "\033[31m /items/{id} (PUT): FAIL (Invalid response)\033[0m" # Red FAIL 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 "\033[32m /items/{id} (DELETE): PASS\033[0m" # Green PASS else echo -e "\033[31m /items/{id} (DELETE): FAIL (Invalid response)\033[0m" # Red FAIL 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 "\033[32m /boxes/{id} (DELETE): PASS\033[0m" # Green PASS else echo -e "\033[31m /boxes/{id} (DELETE): FAIL (Invalid response)\033[0m" # Red FAIL echo "$response" fi # --- Add more test cases for other endpoints --- # Example for GET /items/{id} # echo "Testing /items/{id} (GET)..." # response=$(authenticated_request "GET" "/items/1" "") # # ... (Add assertions based on the expected response) echo "Tests completed."