265 lines
9.0 KiB
Bash
265 lines
9.0 KiB
Bash
#!/bin/bash
|
|
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
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 BOOKING API TEST SCRIPT"
|
|
echo "============================================================"
|
|
echo ""
|
|
|
|
log_info "Checking if server is running..."
|
|
if ! curl -s "$BASE_URL/health" | grep -q "ok"; then
|
|
log_error "Server is not running"
|
|
exit 1
|
|
fi
|
|
log_success "Server is running"
|
|
|
|
echo ""
|
|
log_info "============================================================"
|
|
log_info "STEP 1: Create test users"
|
|
log_info "============================================================"
|
|
|
|
OWNER_EMAIL="owner_test@example.com"
|
|
OWNER_PASSWORD="owner123"
|
|
PARTICIPANT_EMAIL="participant_test@example.com"
|
|
PARTICIPANT_PASSWORD="participant123"
|
|
|
|
# Пробуем зарегистрировать владельца
|
|
log_info "Creating calendar owner..."
|
|
response=$(http_post "$BASE_URL/v1/register" "{\"email\":\"$OWNER_EMAIL\",\"password\":\"$OWNER_PASSWORD\"}" "")
|
|
|
|
if echo "$response" | grep -q "token"; then
|
|
OWNER_TOKEN=$(extract_json "$response" "token")
|
|
OWNER_ID=$(extract_json "$response" "id")
|
|
log_success "Owner registered: $OWNER_EMAIL"
|
|
else
|
|
log_info "Owner exists, trying login..."
|
|
response=$(http_post "$BASE_URL/v1/login" "{\"email\":\"$OWNER_EMAIL\",\"password\":\"$OWNER_PASSWORD\"}" "")
|
|
OWNER_TOKEN=$(extract_json "$response" "token")
|
|
OWNER_ID=$(extract_json "$response" "id")
|
|
fi
|
|
|
|
if [ -z "$OWNER_TOKEN" ]; then
|
|
log_error "Failed to get owner token"
|
|
echo "$response"
|
|
exit 1
|
|
fi
|
|
log_success "Owner ready (ID: $OWNER_ID)"
|
|
|
|
# Пробуем зарегистрировать участника
|
|
log_info "Creating participant..."
|
|
response=$(http_post "$BASE_URL/v1/register" "{\"email\":\"$PARTICIPANT_EMAIL\",\"password\":\"$PARTICIPANT_PASSWORD\"}" "")
|
|
|
|
if echo "$response" | grep -q "token"; then
|
|
PARTICIPANT_TOKEN=$(extract_json "$response" "token")
|
|
PARTICIPANT_ID=$(extract_json "$response" "id")
|
|
log_success "Participant registered: $PARTICIPANT_EMAIL"
|
|
else
|
|
log_info "Participant exists, trying login..."
|
|
response=$(http_post "$BASE_URL/v1/login" "{\"email\":\"$PARTICIPANT_EMAIL\",\"password\":\"$PARTICIPANT_PASSWORD\"}" "")
|
|
PARTICIPANT_TOKEN=$(extract_json "$response" "token")
|
|
PARTICIPANT_ID=$(extract_json "$response" "id")
|
|
fi
|
|
|
|
if [ -z "$PARTICIPANT_TOKEN" ]; then
|
|
log_error "Failed to get participant token"
|
|
echo "$response"
|
|
exit 1
|
|
fi
|
|
log_success "Participant ready (ID: $PARTICIPANT_ID)"
|
|
|
|
echo ""
|
|
log_info "============================================================"
|
|
log_info "STEP 2: Create calendars"
|
|
log_info "============================================================"
|
|
|
|
log_info "Creating AUTO calendar..."
|
|
response=$(http_post "$BASE_URL/v1/calendars" "{\"title\":\"Auto Calendar\",\"confirmation\":\"auto\"}" "$OWNER_TOKEN")
|
|
AUTO_CALENDAR_ID=$(extract_json "$response" "id")
|
|
log_success "Auto calendar: $AUTO_CALENDAR_ID"
|
|
|
|
log_info "Creating MANUAL calendar..."
|
|
response=$(http_post "$BASE_URL/v1/calendars" "{\"title\":\"Manual Calendar\",\"confirmation\":\"manual\"}" "$OWNER_TOKEN")
|
|
MANUAL_CALENDAR_ID=$(extract_json "$response" "id")
|
|
log_success "Manual calendar: $MANUAL_CALENDAR_ID"
|
|
|
|
echo ""
|
|
log_info "============================================================"
|
|
log_info "STEP 3: Create events"
|
|
log_info "============================================================"
|
|
|
|
EVENT_START="2026-05-01T10:00:00Z"
|
|
|
|
log_info "Creating event in AUTO calendar..."
|
|
response=$(http_post "$BASE_URL/v1/calendars/$AUTO_CALENDAR_ID/events" \
|
|
"{\"title\":\"Auto Event\",\"start_time\":\"$EVENT_START\",\"duration\":60,\"capacity\":10}" "$OWNER_TOKEN")
|
|
AUTO_EVENT_ID=$(extract_json "$response" "id")
|
|
log_success "Auto event: $AUTO_EVENT_ID"
|
|
|
|
log_info "Creating event in MANUAL calendar..."
|
|
response=$(http_post "$BASE_URL/v1/calendars/$MANUAL_CALENDAR_ID/events" \
|
|
"{\"title\":\"Manual Event\",\"start_time\":\"$EVENT_START\",\"duration\":60,\"capacity\":10}" "$OWNER_TOKEN")
|
|
MANUAL_EVENT_ID=$(extract_json "$response" "id")
|
|
log_success "Manual event: $MANUAL_EVENT_ID"
|
|
|
|
echo ""
|
|
log_info "============================================================"
|
|
log_info "STEP 4: Test AUTO confirmation"
|
|
log_info "============================================================"
|
|
|
|
log_info "Participant booking AUTO event..."
|
|
response=$(http_post "$BASE_URL/v1/events/$AUTO_EVENT_ID/bookings" "" "$PARTICIPANT_TOKEN")
|
|
echo "Response: $response"
|
|
AUTO_BOOKING_STATUS=$(extract_json "$response" "status")
|
|
|
|
if [ "$AUTO_BOOKING_STATUS" = "confirmed" ]; then
|
|
log_success "Auto-booking confirmed immediately"
|
|
else
|
|
log_error "Auto-booking status: $AUTO_BOOKING_STATUS"
|
|
fi
|
|
|
|
# Сохраняем ID авто-бронирования
|
|
AUTO_BOOKING_ID=$(extract_json "$response" "id")
|
|
|
|
echo ""
|
|
log_info "============================================================"
|
|
log_info "STEP 5: Test MANUAL confirmation"
|
|
log_info "============================================================"
|
|
|
|
log_info "Participant booking MANUAL event..."
|
|
response=$(http_post "$BASE_URL/v1/events/$MANUAL_EVENT_ID/bookings" "" "$PARTICIPANT_TOKEN")
|
|
MANUAL_BOOKING_ID=$(extract_json "$response" "id")
|
|
MANUAL_BOOKING_STATUS=$(extract_json "$response" "status")
|
|
|
|
if [ "$MANUAL_BOOKING_STATUS" = "pending" ]; then
|
|
log_success "Manual-booking is pending: $MANUAL_BOOKING_ID"
|
|
else
|
|
log_error "Manual-booking status: $MANUAL_BOOKING_STATUS"
|
|
fi
|
|
|
|
log_info "Owner confirming booking..."
|
|
response=$(http_put "$BASE_URL/v1/bookings/$MANUAL_BOOKING_ID" "{\"action\":\"confirm\"}" "$OWNER_TOKEN")
|
|
CONFIRMED_STATUS=$(extract_json "$response" "status")
|
|
|
|
if [ "$CONFIRMED_STATUS" = "confirmed" ]; then
|
|
log_success "Booking confirmed by owner"
|
|
else
|
|
log_error "Confirmation failed"
|
|
fi
|
|
|
|
echo ""
|
|
log_info "============================================================"
|
|
log_info "STEP 6: Test booking lists"
|
|
log_info "============================================================"
|
|
|
|
log_info "Owner viewing event bookings..."
|
|
response=$(http_get "$BASE_URL/v1/events/$MANUAL_EVENT_ID/bookings" "$OWNER_TOKEN")
|
|
echo "Response: $response"
|
|
|
|
log_info "Participant viewing their bookings..."
|
|
response=$(http_get "$BASE_URL/v1/user/bookings" "$PARTICIPANT_TOKEN")
|
|
echo "Response: $response"
|
|
|
|
echo ""
|
|
log_info "============================================================"
|
|
log_info "STEP 7: Test booking cancellation"
|
|
log_info "============================================================"
|
|
|
|
# Используем первое бронирование для отмены
|
|
if [ -n "$AUTO_BOOKING_ID" ]; then
|
|
CANCEL_BOOKING_ID="$AUTO_BOOKING_ID"
|
|
log_info "Using auto-booking for cancellation: $CANCEL_BOOKING_ID"
|
|
else
|
|
# Создаём новое событие для теста отмены
|
|
log_info "Creating new event for cancellation test..."
|
|
response=$(http_post "$BASE_URL/v1/calendars/$MANUAL_CALENDAR_ID/events" \
|
|
"{\"title\":\"Cancel Test Event\",\"start_time\":\"$EVENT_START\",\"duration\":60,\"capacity\":10}" "$OWNER_TOKEN")
|
|
CANCEL_EVENT_ID=$(extract_json "$response" "id")
|
|
log_info "Event created: $CANCEL_EVENT_ID"
|
|
|
|
log_info "Creating booking to cancel..."
|
|
response=$(http_post "$BASE_URL/v1/events/$CANCEL_EVENT_ID/bookings" "" "$PARTICIPANT_TOKEN")
|
|
CANCEL_BOOKING_ID=$(extract_json "$response" "id")
|
|
log_info "Created: $CANCEL_BOOKING_ID"
|
|
fi
|
|
|
|
if [ -n "$CANCEL_BOOKING_ID" ]; then
|
|
log_info "Cancelling booking $CANCEL_BOOKING_ID..."
|
|
response=$(http_delete "$BASE_URL/v1/bookings/$CANCEL_BOOKING_ID" "$PARTICIPANT_TOKEN")
|
|
CANCELLED_STATUS=$(extract_json "$response" "status")
|
|
|
|
if [ "$CANCELLED_STATUS" = "cancelled" ]; then
|
|
log_success "Booking cancelled"
|
|
else
|
|
log_error "Cancellation failed: $response"
|
|
fi
|
|
else
|
|
log_error "No booking to cancel"
|
|
fi
|
|
|
|
echo ""
|
|
echo "============================================================"
|
|
log_success "TESTS COMPLETED!"
|
|
echo "============================================================" |