174 lines
5.3 KiB
Bash
Raw Normal View History

2025-01-23 20:16:58 +02:00
#!/bin/bash
# Configuration
API_URL="http://localhost:8000"
TEST_ALARM_NAME="Test Alarm"
TEST_ALARM_TIME="08:30:00"
TEST_AUDIO_FILE="/tmp/test.mp3"
# Color codes for output
GREEN='\033[0;32m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Helper function to print status messages
print_status() {
echo -e "${BLUE}=== $1 ===${NC}"
}
# Helper function to check test results
check_result() {
if [ $? -eq 0 ]; then
echo -e "${GREEN}$1${NC}"
else
echo -e "${RED}$1${NC}"
exit 1
fi
}
# 1. Test server connectivity
print_status "Testing server connectivity"
curl -s "$API_URL" > /dev/null
check_result "Server connectivity check"
# 2. Get initial state
print_status "Getting initial state"
INITIAL_STATE=$(curl -s -X GET "$API_URL")
echo "$INITIAL_STATE" | jq .
check_result "Retrieved initial state"
# 3. Add a new alarm (POST)
print_status "Adding new alarm"
POST_DATA=$(jq -n \
--arg name "$TEST_ALARM_NAME" \
--arg time "$TEST_ALARM_TIME" \
'{
"name": $name,
"time": $time,
"repeat_rule": {
"type": "weekly",
"days_of_week": ["monday", "wednesday", "friday"]
},
"enabled": true,
"snooze": {
"enabled": true,
"duration": 5,
"max_count": 3
},
"metadata": {
"volume": 75,
"notes": "Test alarm with full configuration"
}
}'
)
ADD_RESPONSE=$(curl -s -X POST -H "Content-Type: application/json" -d "$POST_DATA" "$API_URL")
NEW_ALARM_ID=$(echo "$ADD_RESPONSE" | jq -r '.data.id')
if [[ "$NEW_ALARM_ID" == "null" || -z "$NEW_ALARM_ID" ]]; then
echo -e "${RED}Failed to add alarm${NC}"
echo "Response: $ADD_RESPONSE"
exit 1
fi
echo -e "${GREEN}Added alarm with ID: $NEW_ALARM_ID${NC}"
# 4. Test duplicate alarm detection
print_status "Testing duplicate alarm detection"
DUPLICATE_RESPONSE=$(curl -s -X POST -H "Content-Type: application/json" -d "$POST_DATA" "$API_URL")
if [[ $(echo "$DUPLICATE_RESPONSE" | jq -r '.error') == *"Duplicate alarm detected"* ]]; then
echo -e "${GREEN}✓ Duplicate detection working${NC}"
else
echo -e "${RED}✗ Duplicate detection failed${NC}"
echo "Response: $DUPLICATE_RESPONSE"
fi
# 5. Update the alarm (PUT)
print_status "Updating alarm"
UPDATE_DATA=$(jq -n \
--arg name "$TEST_ALARM_NAME Updated" \
--arg time "$TEST_ALARM_TIME" \
--argjson id "$NEW_ALARM_ID" \
'{
"id": $id,
"name": $name,
"time": $time,
"repeat_rule": {
"type": "daily"
},
"enabled": true,
"snooze": {
"enabled": false,
"duration": 10,
"max_count": 2
},
"metadata": {
"volume": 90,
"notes": "Updated test alarm"
}
}'
)
UPDATE_RESPONSE=$(curl -s -X PUT -H "Content-Type: application/json" -d "$UPDATE_DATA" "$API_URL")
if [[ $(echo "$UPDATE_RESPONSE" | jq -r '.data.message') == "Alarm updated successfully" ]]; then
echo -e "${GREEN}✓ Alarm update successful${NC}"
else
echo -e "${RED}✗ Alarm update failed${NC}"
echo "Response: $UPDATE_RESPONSE"
fi
# 6. Verify the update
print_status "Verifying update"
UPDATED_STATE=$(curl -s -X GET "$API_URL")
UPDATED_ALARM=$(echo "$UPDATED_STATE" | jq -r ".data[] | select(.id == $NEW_ALARM_ID)")
if [[ $(echo "$UPDATED_ALARM" | jq -r '.name') == "$TEST_ALARM_NAME Updated" ]]; then
echo -e "${GREEN}✓ Update verification successful${NC}"
else
echo -e "${RED}✗ Update verification failed${NC}"
echo "Current alarm state: $UPDATED_ALARM"
fi
# 7. Test invalid inputs
print_status "Testing invalid inputs"
# Test invalid time format
INVALID_TIME_DATA=$(echo "$POST_DATA" | jq '. + {"time": "25:00:00"}')
INVALID_TIME_RESPONSE=$(curl -s -X POST -H "Content-Type: application/json" -d "$INVALID_TIME_DATA" "$API_URL")
if [[ $(echo "$INVALID_TIME_RESPONSE" | jq -r '.error') == *"Invalid alarm configuration"* ]]; then
echo -e "${GREEN}✓ Invalid time format detection working${NC}"
else
echo -e "${RED}✗ Invalid time format detection failed${NC}"
fi
# Test invalid repeat rule
INVALID_REPEAT_DATA=$(echo "$POST_DATA" | jq '.repeat_rule.type = "monthly"')
INVALID_REPEAT_RESPONSE=$(curl -s -X POST -H "Content-Type: application/json" -d "$INVALID_REPEAT_DATA" "$API_URL")
if [[ $(echo "$INVALID_REPEAT_RESPONSE" | jq -r '.error') == *"Invalid alarm configuration"* ]]; then
echo -e "${GREEN}✓ Invalid repeat rule detection working${NC}"
else
echo -e "${RED}✗ Invalid repeat rule detection failed${NC}"
fi
# 8. Delete the test alarm
print_status "Deleting test alarm"
DELETE_RESPONSE=$(curl -s -X DELETE -H "Content-Type: application/json" -d "{\"id\":$NEW_ALARM_ID}" "$API_URL")
if [[ $(echo "$DELETE_RESPONSE" | jq -r '.data.message') == "Alarm removed successfully" ]]; then
echo -e "${GREEN}✓ Alarm deletion successful${NC}"
else
echo -e "${RED}✗ Alarm deletion failed${NC}"
echo "Response: $DELETE_RESPONSE"
fi
# 9. Verify deletion
print_status "Verifying deletion"
FINAL_STATE=$(curl -s -X GET "$API_URL")
if [[ $(echo "$FINAL_STATE" | jq ".data[] | select(.id == $NEW_ALARM_ID)") == "" ]]; then
echo -e "${GREEN}✓ Deletion verification successful${NC}"
else
echo -e "${RED}✗ Deletion verification failed${NC}"
echo "Current state: $FINAL_STATE"
fi
print_status "Test suite completed!"