#!/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/\"$//" } extract_json_number() { echo "$1" | grep -o "\"$2\":[0-9.]*" | head -1 | sed "s/\"$2\"://" } 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 REVIEWS 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 "============================================================" # Админ (создаётся первым) ADMIN_EMAIL="admin_$(date +%s)@example.com" ADMIN_PASSWORD="admin123" log_info "Creating admin user (first user)..." response=$(http_post "$BASE_URL/v1/register" "{\"email\":\"$ADMIN_EMAIL\",\"password\":\"$ADMIN_PASSWORD\"}" "") ADMIN_TOKEN=$(extract_json "$response" "token") ADMIN_ID=$(extract_json "$response" "id") log_success "Admin created: $ADMIN_EMAIL" # Владелец календаря OWNER_EMAIL="review_owner_$(date +%s)@example.com" OWNER_PASSWORD="owner123" log_info "Creating calendar owner..." response=$(http_post "$BASE_URL/v1/register" "{\"email\":\"$OWNER_EMAIL\",\"password\":\"$OWNER_PASSWORD\"}" "") OWNER_TOKEN=$(extract_json "$response" "token") OWNER_ID=$(extract_json "$response" "id") log_success "Owner created: $OWNER_EMAIL" # Участник 1 PARTICIPANT1_EMAIL="review_p1_$(date +%s)@example.com" PARTICIPANT1_PASSWORD="p1_123" log_info "Creating participant 1..." response=$(http_post "$BASE_URL/v1/register" "{\"email\":\"$PARTICIPANT1_EMAIL\",\"password\":\"$PARTICIPANT1_PASSWORD\"}" "") PARTICIPANT1_TOKEN=$(extract_json "$response" "token") PARTICIPANT1_ID=$(extract_json "$response" "id") log_success "Participant 1 created" # Участник 2 PARTICIPANT2_EMAIL="review_p2_$(date +%s)@example.com" PARTICIPANT2_PASSWORD="p2_123" log_info "Creating participant 2..." response=$(http_post "$BASE_URL/v1/register" "{\"email\":\"$PARTICIPANT2_EMAIL\",\"password\":\"$PARTICIPANT2_PASSWORD\"}" "") PARTICIPANT2_TOKEN=$(extract_json "$response" "token") PARTICIPANT2_ID=$(extract_json "$response" "id") log_success "Participant 2 created" # Сторонний пользователь (без бронирований) OTHER_EMAIL="review_other_$(date +%s)@example.com" OTHER_PASSWORD="other123" log_info "Creating other user (no bookings)..." response=$(http_post "$BASE_URL/v1/register" "{\"email\":\"$OTHER_EMAIL\",\"password\":\"$OTHER_PASSWORD\"}" "") OTHER_TOKEN=$(extract_json "$response" "token") OTHER_ID=$(extract_json "$response" "id") log_success "Other user created" echo "" log_info "============================================================" log_info "STEP 2: Create calendar and events" log_info "============================================================" log_info "Creating calendar..." response=$(http_post "$BASE_URL/v1/calendars" \ "{\"title\":\"Review Test Calendar\",\"description\":\"Calendar for review tests\"}" "$OWNER_TOKEN") CALENDAR_ID=$(extract_json "$response" "id") log_success "Calendar created: $CALENDAR_ID" log_info "Creating event..." EVENT_START="2026-06-01T10:00:00Z" response=$(http_post "$BASE_URL/v1/calendars/$CALENDAR_ID/events" \ "{\"title\":\"Test Event\",\"start_time\":\"$EVENT_START\",\"duration\":60,\"capacity\":10}" "$OWNER_TOKEN") EVENT_ID=$(extract_json "$response" "id") log_success "Event created: $EVENT_ID" log_info "Creating second event..." response=$(http_post "$BASE_URL/v1/calendars/$CALENDAR_ID/events" \ "{\"title\":\"Test Event 2\",\"start_time\":\"$EVENT_START\",\"duration\":60}" "$OWNER_TOKEN") EVENT2_ID=$(extract_json "$response" "id") log_success "Second event created: $EVENT2_ID" echo "" log_info "============================================================" log_info "STEP 3: Create bookings" log_info "============================================================" log_info "Participant 1 booking event..." response=$(http_post "$BASE_URL/v1/events/$EVENT_ID/bookings" "" "$PARTICIPANT1_TOKEN") BOOKING1_ID=$(extract_json "$response" "id") log_success "Booking created: $BOOKING1_ID" log_info "Owner confirming participant 1 booking..." response=$(http_put "$BASE_URL/v1/bookings/$BOOKING1_ID" "{\"action\":\"confirm\"}" "$OWNER_TOKEN") log_success "Booking confirmed" log_info "Participant 2 booking event..." response=$(http_post "$BASE_URL/v1/events/$EVENT_ID/bookings" "" "$PARTICIPANT2_TOKEN") BOOKING2_ID=$(extract_json "$response" "id") log_success "Booking created: $BOOKING2_ID" log_info "Owner confirming participant 2 booking..." response=$(http_put "$BASE_URL/v1/bookings/$BOOKING2_ID" "{\"action\":\"confirm\"}" "$OWNER_TOKEN") log_success "Booking confirmed" echo "" log_info "============================================================" log_info "TEST 1: Create review for event (participant)" log_info "============================================================" log_info "Participant 1 creating review..." response=$(http_post "$BASE_URL/v1/reviews" \ "{\"target_type\":\"event\",\"target_id\":\"$EVENT_ID\",\"rating\":5,\"comment\":\"Excellent event!\"}" "$PARTICIPANT1_TOKEN") REVIEW1_ID=$(extract_json "$response" "id") if [ -n "$REVIEW1_ID" ]; then log_success "Review created: $REVIEW1_ID" else log_error "Failed to create review: $response" fi echo "" log_info "============================================================" log_info "TEST 2: Create review for event (second participant)" log_info "============================================================" log_info "Participant 2 creating review..." response=$(http_post "$BASE_URL/v1/reviews" \ "{\"target_type\":\"event\",\"target_id\":\"$EVENT_ID\",\"rating\":3,\"comment\":\"It was okay\"}" "$PARTICIPANT2_TOKEN") REVIEW2_ID=$(extract_json "$response" "id") if [ -n "$REVIEW2_ID" ]; then log_success "Review created: $REVIEW2_ID" else log_error "Failed to create review: $response" fi echo "" log_info "============================================================" log_info "TEST 3: Cannot review twice" log_info "============================================================" log_info "Participant 1 trying to review again..." response=$(http_post "$BASE_URL/v1/reviews" \ "{\"target_type\":\"event\",\"target_id\":\"$EVENT_ID\",\"rating\":4,\"comment\":\"Trying again\"}" "$PARTICIPANT1_TOKEN") if echo "$response" | grep -q "Already reviewed"; then log_success "Duplicate review correctly rejected" else log_error "Duplicate review not rejected: $response" fi echo "" log_info "============================================================" log_info "TEST 4: Cannot review without booking" log_info "============================================================" log_info "Other user trying to review event..." response=$(http_post "$BASE_URL/v1/reviews" \ "{\"target_type\":\"event\",\"target_id\":\"$EVENT_ID\",\"rating\":5,\"comment\":\"Wasn't there\"}" "$OTHER_TOKEN") if echo "$response" | grep -q "Cannot review"; then log_success "Review without booking correctly rejected" else log_error "Review without booking not rejected: $response" fi echo "" log_info "============================================================" log_info "TEST 5: Create review for calendar" log_info "============================================================" log_info "Other user creating calendar review..." response=$(http_post "$BASE_URL/v1/reviews" \ "{\"target_type\":\"calendar\",\"target_id\":\"$CALENDAR_ID\",\"rating\":4,\"comment\":\"Nice calendar!\"}" "$OTHER_TOKEN") CALENDAR_REVIEW_ID=$(extract_json "$response" "id") if [ -n "$CALENDAR_REVIEW_ID" ]; then log_success "Calendar review created: $CALENDAR_REVIEW_ID" else log_error "Failed to create calendar review: $response" fi echo "" log_info "============================================================" log_info "TEST 6: Get reviews for event" log_info "============================================================" log_info "Getting reviews for event..." response=$(http_get "$BASE_URL/v1/reviews?target_type=event&target_id=$EVENT_ID" "$PARTICIPANT1_TOKEN") REVIEW_COUNT=$(echo "$response" | grep -o "\"id\"" | wc -l) if [ "$REVIEW_COUNT" -eq 2 ]; then log_success "Found $REVIEW_COUNT reviews for event" else log_error "Expected 2 reviews, found $REVIEW_COUNT" fi sleep 1 echo "" log_info "============================================================" log_info "TEST 7: Check event rating updated" log_info "============================================================" log_info "Checking event rating..." response=$(http_get "$BASE_URL/v1/events/$EVENT_ID" "$OWNER_TOKEN") RATING_AVG=$(extract_json_number "$response" "rating_avg") RATING_COUNT=$(extract_json_number "$response" "rating_count") if [ "$RATING_AVG" = "4.0" ] && [ "$RATING_COUNT" = "2" ]; then log_success "Event rating updated: $RATING_AVG ($RATING_COUNT reviews)" else log_error "Event rating incorrect: avg=$RATING_AVG, count=$RATING_COUNT" fi echo "" log_info "============================================================" log_info "TEST 8: Update own review" log_info "============================================================" log_info "Participant 1 updating review..." response=$(http_put "$BASE_URL/v1/reviews/$REVIEW1_ID" \ "{\"rating\":4,\"comment\":\"Updated: Very good!\"}" "$PARTICIPANT1_TOKEN") if echo "$response" | grep -q "\"id\""; then log_success "Review updated" else log_error "Review update failed: $response" fi sleep 1 log_info "Checking event rating after update..." response=$(http_get "$BASE_URL/v1/events/$EVENT_ID" "$OWNER_TOKEN") NEW_RATING_AVG=$(extract_json_number "$response" "rating_avg") if [ "$NEW_RATING_AVG" = "3.5" ]; then log_success "Event rating updated to $NEW_RATING_AVG" else log_error "Event rating incorrect: $NEW_RATING_AVG (expected 3.5)" fi echo "" log_info "============================================================" log_info "TEST 9: Cannot update others review" log_info "============================================================" log_info "Participant 2 trying to update participant 1 review..." response=$(http_put "$BASE_URL/v1/reviews/$REVIEW1_ID" \ "{\"rating\":1,\"comment\":\"Hacked!\"}" "$PARTICIPANT2_TOKEN") if echo "$response" | grep -q "Access denied"; then log_success "Update others review correctly rejected" else log_error "Update others review not rejected: $response" fi echo "" log_info "============================================================" log_info "TEST 10: Get user reviews" log_info "============================================================" log_info "Getting participant 1 reviews..." response=$(http_get "$BASE_URL/v1/user/reviews" "$PARTICIPANT1_TOKEN") USER_REVIEW_COUNT=$(echo "$response" | grep -o "\"id\"" | wc -l) if [ "$USER_REVIEW_COUNT" -ge 1 ]; then log_success "Found $USER_REVIEW_COUNT reviews for user" else log_error "User reviews not found" fi echo "" log_info "============================================================" log_info "TEST 11: Admin hides review" log_info "============================================================" log_info "Admin hiding review $REVIEW2_ID..." response=$(http_put "$BASE_URL/v1/admin/reviews/$REVIEW2_ID" \ "{\"action\":\"hide\"}" "$ADMIN_TOKEN") HIDDEN_STATUS=$(extract_json "$response" "status") if [ "$HIDDEN_STATUS" = "hidden" ]; then log_success "Review hidden by admin" else log_error "Failed to hide review: $response" fi log_info "Participant 1 getting event reviews (hidden should not appear)..." response=$(http_get "$BASE_URL/v1/reviews?target_type=event&target_id=$EVENT_ID" "$PARTICIPANT1_TOKEN") VISIBLE_COUNT=$(echo "$response" | grep -o "\"id\"" | wc -l) if [ "$VISIBLE_COUNT" -eq 1 ]; then log_success "Only 1 review visible (hidden filtered out)" else log_error "Expected 1 visible review, found $VISIBLE_COUNT" fi log_info "Admin getting event reviews (should see all)..." response=$(http_get "$BASE_URL/v1/reviews?target_type=event&target_id=$EVENT_ID" "$ADMIN_TOKEN") ADMIN_COUNT=$(echo "$response" | grep -o "\"id\"" | wc -l) if [ "$ADMIN_COUNT" -eq 2 ]; then log_success "Admin sees all 2 reviews" else log_error "Admin should see 2 reviews, found $ADMIN_COUNT" fi echo "" log_info "============================================================" log_info "TEST 12: Admin unhides review" log_info "============================================================" log_info "Admin unhiding review..." response=$(http_put "$BASE_URL/v1/admin/reviews/$REVIEW2_ID" \ "{\"action\":\"unhide\"}" "$ADMIN_TOKEN") UNHIDDEN_STATUS=$(extract_json "$response" "status") if [ "$UNHIDDEN_STATUS" = "visible" ]; then log_success "Review unhidden by admin" else log_error "Failed to unhide review: $response" fi echo "" log_info "============================================================" log_info "TEST 13: Delete own review" log_info "============================================================" log_info "Participant 1 deleting review..." response=$(http_delete "$BASE_URL/v1/reviews/$REVIEW1_ID" "$PARTICIPANT1_TOKEN") if echo "$response" | grep -q "deleted"; then log_success "Review deleted" else log_error "Failed to delete review: $response" fi sleep 1 log_info "Checking event rating after deletion..." response=$(http_get "$BASE_URL/v1/events/$EVENT_ID" "$OWNER_TOKEN") FINAL_RATING_AVG=$(extract_json_number "$response" "rating_avg") FINAL_RATING_COUNT=$(extract_json_number "$response" "rating_count") if [ "$FINAL_RATING_AVG" = "3.0" ] && [ "$FINAL_RATING_COUNT" = "1" ]; then log_success "Event rating updated: $FINAL_RATING_AVG ($FINAL_RATING_COUNT review)" else log_error "Event rating incorrect: avg=$FINAL_RATING_AVG, count=$FINAL_RATING_COUNT" fi echo "" log_info "============================================================" log_info "TEST 14: Get deleted review (should fail)" log_info "============================================================" log_info "Trying to get deleted review..." response=$(http_get "$BASE_URL/v1/reviews/$REVIEW1_ID" "$PARTICIPANT1_TOKEN") if echo "$response" | grep -q "not found"; then log_success "Deleted review not found" else log_error "Deleted review still accessible: $response" fi echo "" log_info "============================================================" log_info "TEST 15: Calendar rating updated" log_info "============================================================" log_info "Checking calendar rating..." response=$(http_get "$BASE_URL/v1/calendars/$CALENDAR_ID" "$OWNER_TOKEN") CAL_RATING_AVG=$(extract_json_number "$response" "rating_avg") CAL_RATING_COUNT=$(extract_json_number "$response" "rating_count") if [ "$CAL_RATING_AVG" = "4.0" ] && [ "$CAL_RATING_COUNT" = "1" ]; then log_success "Calendar rating: $CAL_RATING_AVG ($CAL_RATING_COUNT review)" else log_error "Calendar rating incorrect: avg=$CAL_RATING_AVG, count=$CAL_RATING_COUNT" fi echo "" echo "============================================================" log_success "REVIEWS API TESTS COMPLETED!" echo "============================================================" echo "" echo "Summary of created resources:" echo " Admin: $ADMIN_EMAIL" echo " Owner: $OWNER_EMAIL" echo " Participant 1: $PARTICIPANT1_EMAIL" echo " Participant 2: $PARTICIPANT2_EMAIL" echo " Calendar: $CALENDAR_ID" echo " Event: $EVENT_ID" echo ""