Files
EventHubBack/test/scripts/test_search_api.sh
2026-04-20 21:04:16 +03:00

393 lines
14 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"; }
log_warning() { echo -e "${YELLOW}[WARNING]${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"
}
url_encode() {
echo -n "$1" | sed 's/ /%20/g;s/,/%2C/g'
}
echo "============================================================"
echo " EVENTHUB SEARCH 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="search_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")
if [ -z "$OWNER_TOKEN" ]; then
log_error "Failed to create owner"
exit 1
fi
log_success "Owner created: $OWNER_EMAIL"
echo ""
log_info "============================================================"
log_info "STEP 2: Create calendar with tags"
log_info "============================================================"
log_info "Creating calendar..."
response=$(http_post "$BASE_URL/v1/calendars" \
"{\"title\":\"Tech Events Calendar\",\"description\":\"Calendar for technology events and workshops\",\"tags\":[\"tech\",\"programming\",\"workshop\"]}" "$OWNER_TOKEN")
CALENDAR_ID=$(extract_json "$response" "id")
log_success "Calendar created with tags: $CALENDAR_ID"
# Добавляем теги через обновление
http_put "$BASE_URL/v1/calendars/$CALENDAR_ID" "{\"tags\":[\"tech\",\"programming\",\"workshop\"]}" "$OWNER_TOKEN" > /dev/null
log_success "Calendar created with tags: $CALENDAR_ID"
echo ""
log_info "============================================================"
log_info "STEP 3: Create events with different properties"
log_info "============================================================"
# Функция для создания события
create_event() {
local title=$1
local description=$2
local start_time=$3
local tags=$4
local lat=$5
local lon=$6
local address=$7
local location_json="null"
if [ -n "$lat" ] && [ -n "$lon" ]; then
location_json="{\"address\":\"$address\",\"lat\":$lat,\"lon\":$lon}"
fi
local tags_json="[]"
if [ -n "$tags" ]; then
tags_json="$tags"
fi
local data="{\"title\":\"$title\",\"description\":\"$description\",\"start_time\":\"$start_time\",\"duration\":60,\"tags\":$tags_json"
if [ "$location_json" != "null" ]; then
data="$data,\"location\":$location_json"
fi
data="$data}"
response=$(http_post "$BASE_URL/v1/calendars/$CALENDAR_ID/events" "$data" "$OWNER_TOKEN")
local event_id=$(extract_json "$response" "id")
echo "$event_id"
}
log_info "Creating Python Workshop event..."
WORKSHOP_ID=$(create_event "Python Workshop" "Learn Python programming basics" "2026-06-01T10:00:00Z" \
"[\"python\",\"workshop\",\"programming\"]" "55.7558" "37.6173" "Moscow, Russia")
log_success "Created: $WORKSHOP_ID"
log_info "Creating JavaScript Conference event..."
JS_ID=$(create_event "JavaScript Conference" "Annual JS conference for developers" "2026-06-15T09:00:00Z" \
"[\"javascript\",\"conference\",\"web\"]" "55.7558" "37.6173" "Moscow, Russia")
log_success "Created: $JS_ID"
log_info "Creating Yoga Class event (no tags)..."
YOGA_ID=$(create_event "Yoga Class" "Morning yoga session" "2026-06-10T08:00:00Z" \
"" "" "" "")
log_success "Created: $YOGA_ID"
log_info "Creating Tech Meetup in another city..."
MEETUP_ID=$(create_event "Tech Meetup" "Networking for tech professionals" "2026-06-20T18:00:00Z" \
"[\"networking\",\"tech\"]" "59.9343" "30.3351" "Saint Petersburg, Russia")
log_success "Created: $MEETUP_ID"
log_info "Creating past event..."
PAST_ID=$(create_event "Past Event" "This event already happened" "2020-01-01T10:00:00Z" \
"[\"past\"]" "" "" "")
log_success "Created: $PAST_ID"
echo ""
log_info "============================================================"
log_info "TEST 1: Search by text query"
log_info "============================================================"
log_info "Searching for 'Python'..."
response=$(http_get "$BASE_URL/v1/search?type=event&q=Python" "$OWNER_TOKEN")
if echo "$response" | grep -q "Python Workshop"; then
log_success "Found Python Workshop"
else
log_error "Python Workshop not found"
fi
log_info "Searching for 'conference'..."
response=$(http_get "$BASE_URL/v1/search?type=event&q=conference" "$OWNER_TOKEN")
if echo "$response" | grep -q "JavaScript Conference"; then
log_success "Found JavaScript Conference"
else
log_error "JavaScript Conference not found"
fi
echo ""
log_info "============================================================"
log_info "TEST 2: Search by tags"
log_info "============================================================"
log_info "Searching for events with tag 'python'..."
response=$(http_get "$BASE_URL/v1/search?type=event&tags=python" "$OWNER_TOKEN")
if echo "$response" | grep -q "Python Workshop"; then
log_success "Found Python Workshop by tag"
else
log_error "Python Workshop not found by tag"
fi
log_info "Searching for multiple tags 'tech,workshop'..."
response=$(http_get "$BASE_URL/v1/search?type=event&tags=tech,workshop" "$OWNER_TOKEN")
log_success "Multiple tag search completed"
log_info "Searching for tag 'yoga' (should be empty)..."
response=$(http_get "$BASE_URL/v1/search?type=event&tags=yoga" "$OWNER_TOKEN")
if echo "$response" | grep -q '"total":0'; then
log_success "Yoga tag correctly returned no results (no tags on event)"
else
log_warning "Yoga event has no tags, but might appear in results"
fi
echo ""
log_info "============================================================"
log_info "TEST 3: Search by date range"
log_info "============================================================"
log_info "Searching events in June 2026..."
FROM="2026-06-01T00:00:00Z"
TO="2026-06-30T23:59:59Z"
response=$(http_get "$BASE_URL/v1/search?type=event&from=$FROM&to=$TO" "$OWNER_TOKEN")
if echo "$response" | grep -q "Python Workshop"; then
log_success "Found June events"
else
log_error "June events not found"
fi
log_info "Searching past events only..."
FROM="2019-01-01T00:00:00Z"
TO="2021-01-01T00:00:00Z"
response=$(http_get "$BASE_URL/v1/search?type=event&from=$FROM&to=$TO" "$OWNER_TOKEN")
if echo "$response" | grep -q "Past Event"; then
log_success "Found past event"
else
log_error "Past event not found"
fi
echo ""
log_info "============================================================"
log_info "TEST 4: Geo-location search"
log_info "============================================================"
log_info "Searching events within 5km of Moscow center..."
response=$(http_get "$BASE_URL/v1/search?type=event&lat=55.7558&lon=37.6173&radius=5" "$OWNER_TOKEN")
if echo "$response" | grep -q "Python Workshop"; then
log_success "Found Moscow events"
else
log_error "Moscow events not found"
fi
log_info "Searching events within 1km of Moscow (should find fewer)..."
response=$(http_get "$BASE_URL/v1/search?type=event&lat=55.7558&lon=37.6173&radius=1" "$OWNER_TOKEN")
log_success "Radius search completed"
log_info "Searching events in Saint Petersburg..."
response=$(http_get "$BASE_URL/v1/search?type=event&lat=59.9343&lon=30.3351&radius=10" "$OWNER_TOKEN")
if echo "$response" | grep -q "Tech Meetup"; then
log_success "Found Saint Petersburg event"
else
log_error "Saint Petersburg event not found"
fi
echo ""
log_info "============================================================"
log_info "TEST 5: Combined search"
log_info "============================================================"
log_info "Search: text 'Python' + tag 'workshop'..."
response=$(http_get "$BASE_URL/v1/search?type=event&q=Python&tags=workshop" "$OWNER_TOKEN")
if echo "$response" | grep -q "Python Workshop"; then
log_success "Combined text+tag search successful"
else
log_error "Combined search failed"
fi
log_info "Search: tag 'javascript' + date range..."
FROM="2026-06-01T00:00:00Z"
TO="2026-06-30T23:59:59Z"
response=$(http_get "$BASE_URL/v1/search?type=event&tags=javascript&from=$FROM&to=$TO" "$OWNER_TOKEN")
if echo "$response" | grep -q "JavaScript Conference"; then
log_success "Combined tag+date search successful"
else
log_error "Combined search failed"
fi
echo ""
log_info "============================================================"
log_info "TEST 6: Pagination"
log_info "============================================================"
log_info "Search with limit=2..."
response=$(http_get "$BASE_URL/v1/search?type=event&limit=2" "$OWNER_TOKEN")
COUNT=$(echo "$response" | grep -o "\"id\"" | wc -l)
if [ "$COUNT" -le 2 ]; then
log_success "Pagination limit works (got $COUNT results)"
else
log_error "Pagination limit failed (got $COUNT results)"
fi
log_info "Search with offset=2..."
response=$(http_get "$BASE_URL/v1/search?type=event&limit=2&offset=2" "$OWNER_TOKEN")
log_success "Pagination offset works"
echo ""
log_info "============================================================"
log_info "TEST 7: Sorting"
log_info "============================================================"
log_info "Sort by start_time ascending..."
response=$(http_get "$BASE_URL/v1/search?type=event&sort=start_time&order=asc" "$OWNER_TOKEN")
log_success "Sort ascending completed"
log_info "Sort by start_time descending..."
response=$(http_get "$BASE_URL/v1/search?type=event&sort=start_time&order=desc" "$OWNER_TOKEN")
log_success "Sort descending completed"
echo ""
log_info "============================================================"
log_info "TEST 8: Calendar search"
log_info "============================================================"
log_info "Searching calendars by text..."
response=$(http_get "$BASE_URL/v1/search?type=calendar&q=Tech" "$OWNER_TOKEN")
if echo "$response" | grep -q "Tech Events Calendar"; then
log_success "Found calendar by text"
else
log_error "Calendar not found by text"
fi
log_info "Searching calendars by tag..."
response=$(http_get "$BASE_URL/v1/search?type=calendar&tags=tech" "$OWNER_TOKEN")
if echo "$response" | grep -q "Tech Events Calendar"; then
log_success "Found calendar by tag"
else
log_error "Calendar not found by tag"
fi
echo ""
log_info "============================================================"
log_info "TEST 9: Search all (events + calendars)"
log_info "============================================================"
log_info "Searching all (no type specified)..."
response=$(http_get "$BASE_URL/v1/search?q=Tech" "$OWNER_TOKEN")
if echo "$response" | grep -q "events" && echo "$response" | grep -q "calendars"; then
log_success "All search returned both events and calendars"
else
log_warning "All search may not have returned both types"
fi
echo ""
log_info "============================================================"
log_info "TEST 10: Empty search results"
log_info "============================================================"
log_info "Searching for non-existent text..."
response=$(http_get "$BASE_URL/v1/search?type=event&q=nonexistenttext12345" "$OWNER_TOKEN")
if echo "$response" | grep -q '"total":0'; then
log_success "Empty search handled correctly"
else
log_error "Empty search not handled correctly"
fi
echo ""
log_info "============================================================"
log_info "TEST 11: Commercial calendar visibility"
log_info "============================================================"
log_info "Creating commercial calendar..."
response=$(http_post "$BASE_URL/v1/calendars" \
"{\"title\":\"Public Commercial Calendar\",\"type\":\"commercial\"}" "$OWNER_TOKEN")
COMMERCIAL_ID=$(extract_json "$response" "id")
log_success "Commercial calendar created: $COMMERCIAL_ID"
log_info "Creating event in commercial calendar..."
response=$(http_post "$BASE_URL/v1/calendars/$COMMERCIAL_ID/events" \
"{\"title\":\"Public Event\",\"start_time\":\"2026-06-01T10:00:00Z\",\"duration\":60}" "$OWNER_TOKEN")
log_success "Public event created"
log_info "Creating another user to test visibility..."
OTHER_EMAIL="search_other_$(date +%s)@example.com"
response=$(http_post "$BASE_URL/v1/register" "{\"email\":\"$OTHER_EMAIL\",\"password\":\"test123\"}" "")
OTHER_TOKEN=$(extract_json "$response" "token")
log_info "Other user searching for public event..."
response=$(http_get "$BASE_URL/v1/search?type=event&q=Public" "$OTHER_TOKEN")
if echo "$response" | grep -q "Public Event"; then
log_success "Other user can see public event in commercial calendar"
else
log_error "Other user cannot see public event"
fi
echo ""
echo "============================================================"
log_success "SEARCH API TESTS COMPLETED!"
echo "============================================================"
echo ""
echo "Summary of created resources:"
echo " Owner: $OWNER_EMAIL"
echo " Calendar: $CALENDAR_ID"
echo " Commercial Calendar: $COMMERCIAL_ID"
echo " Events created: 5"
echo ""