Files
EventHubBack/test/scripts/test_event_api.sh

212 lines
6.8 KiB
Bash

#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'
BASE_URL="http://localhost:8080"
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
extract_json() {
echo "$1" | grep -o "\"$2\":\"[^\"]*\"" | head -1 | sed "s/\"$2\":\"//;s/\"$//"
}
http_post() {
local url=$1
local data=$2
local token=$3
if [ -n "$token" ]; then
curl -s -X POST "$url" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $token" \
-d "$data"
else
curl -s -X POST "$url" \
-H "Content-Type: application/json" \
-d "$data"
fi
}
http_get() {
local url=$1
local token=$2
if [ -n "$token" ]; then
curl -s -X GET "$url" \
-H "Authorization: Bearer $token"
else
curl -s -X GET "$url"
fi
}
http_put() {
local url=$1
local data=$2
local token=$3
curl -s -X PUT "$url" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $token" \
-d "$data"
}
http_delete() {
local url=$1; local token=$2
curl -s -X DELETE "$url" -H "Authorization: Bearer $token"
}
echo "============================================================"
echo " EVENTHUB EVENT API TEST SCRIPT"
echo "============================================================"
echo ""
log_info "Setting up test users and calendar..."
OWNER_EMAIL="event_owner_$(date +%s)@example.com"
OWNER_PASS="owner123"
response=$(http_post "$BASE_URL/v1/register" "{\"email\":\"$OWNER_EMAIL\",\"password\":\"$OWNER_PASS\"}" "")
OWNER_TOKEN=$(extract_json "$response" "token")
OWNER_ID=$(extract_json "$response" "id")
log_success "Owner created"
response=$(http_post "$BASE_URL/v1/calendars" "{\"title\":\"Test Calendar\"}" "$OWNER_TOKEN")
CALENDAR_ID=$(extract_json "$response" "id")
log_success "Calendar created: $CALENDAR_ID"
echo ""
log_info "============================================================"
log_info "TEST 1: Create single event"
log_info "============================================================"
EVENT_START="2026-06-01T10:00:00Z"
response=$(http_post "$BASE_URL/v1/calendars/$CALENDAR_ID/events" \
"{\"title\":\"Single Event\",\"start_time\":\"$EVENT_START\",\"duration\":60}" "$OWNER_TOKEN")
EVENT_ID=$(extract_json "$response" "id")
if [ -n "$EVENT_ID" ]; then
log_success "Single event created: $EVENT_ID"
else
log_error "Event creation failed: $response"
exit 1
fi
echo ""
log_info "============================================================"
log_info "TEST 2: Create event with capacity"
log_info "============================================================"
response=$(http_post "$BASE_URL/v1/calendars/$CALENDAR_ID/events" \
"{\"title\":\"Capacity Event\",\"start_time\":\"$EVENT_START\",\"duration\":60,\"capacity\":10}" "$OWNER_TOKEN")
CAPACITY_EVENT_ID=$(extract_json "$response" "id")
log_success "Event with capacity created: $CAPACITY_EVENT_ID"
echo ""
log_info "============================================================"
log_info "TEST 3: Create event in past (should fail)"
log_info "============================================================"
PAST_START="2020-01-01T10:00:00Z"
response=$(http_post "$BASE_URL/v1/calendars/$CALENDAR_ID/events" \
"{\"title\":\"Past Event\",\"start_time\":\"$PAST_START\",\"duration\":60}" "$OWNER_TOKEN")
if echo "$response" | grep -q "past"; then
log_success "Past event correctly rejected"
else
log_error "Past event not rejected: $response"
fi
echo ""
log_info "============================================================"
log_info "TEST 4: List events"
log_info "============================================================"
response=$(http_get "$BASE_URL/v1/calendars/$CALENDAR_ID/events" "$OWNER_TOKEN")
COUNT=$(echo "$response" | grep -o "\"id\"" | wc -l)
log_success "Found $COUNT events"
echo ""
log_info "============================================================"
log_info "TEST 5: Get event by ID"
log_info "============================================================"
response=$(http_get "$BASE_URL/v1/events/$EVENT_ID" "$OWNER_TOKEN")
if echo "$response" | grep -q "Single Event"; then
log_success "Event retrieved successfully"
else
log_error "Event retrieval failed: $response"
fi
echo ""
log_info "============================================================"
log_info "TEST 6: Update event"
log_info "============================================================"
response=$(http_put "$BASE_URL/v1/events/$EVENT_ID" "{\"title\":\"Updated Event\"}" "$OWNER_TOKEN")
if echo "$response" | grep -q "Updated Event"; then
log_success "Event updated"
else
log_error "Event update failed: $response"
fi
echo ""
log_info "============================================================"
log_info "TEST 7: Delete event"
log_info "============================================================"
response=$(http_delete "$BASE_URL/v1/events/$EVENT_ID" "$OWNER_TOKEN")
if echo "$response" | grep -q "deleted"; then
log_success "Event deleted"
else
log_error "Event deletion failed: $response"
fi
echo ""
log_info "============================================================"
log_info "TEST 8: Get deleted event (should fail)"
log_info "============================================================"
response=$(http_get "$BASE_URL/v1/events/$EVENT_ID" "$OWNER_TOKEN")
if echo "$response" | grep -q "not found"; then
log_success "Deleted event not found"
else
log_error "Deleted event still accessible: $response"
fi
echo ""
log_info "============================================================"
log_info "TEST 9: Create recurring event"
log_info "============================================================"
response=$(http_post "$BASE_URL/v1/calendars/$CALENDAR_ID/events" \
"{\"title\":\"Weekly Meeting\",\"start_time\":\"$EVENT_START\",\"duration\":60,\"recurrence\":{\"freq\":\"WEEKLY\",\"interval\":1}}" "$OWNER_TOKEN")
RECURRING_ID=$(extract_json "$response" "id")
if [ -n "$RECURRING_ID" ]; then
log_success "Recurring event created: $RECURRING_ID"
else
log_error "Recurring event creation failed: $response"
fi
echo ""
log_info "============================================================"
log_info "TEST 10: Get occurrences"
log_info "============================================================"
FROM="2026-06-01T00:00:00Z"
TO="2026-06-30T00:00:00Z"
response=$(http_get "$BASE_URL/v1/events/$RECURRING_ID/occurrences?from=$FROM&to=$TO" "$OWNER_TOKEN")
if [ -n "$response" ] && [ "$response" != "[]" ]; then
log_success "Occurrences retrieved"
else
log_error "Occurrences retrieval failed: $response"
fi
echo ""
echo "============================================================"
log_success "EVENT API TESTS COMPLETED!"
echo "============================================================"