#!/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 } echo "============================================================" echo " EVENTHUB SUBSCRIPTION 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 "============================================================" # Пользователь 1 (будет использовать пробный период через commercial календарь) USER1_EMAIL="sub_user1_$(date +%s)@example.com" USER1_PASSWORD="user123" log_info "Creating user 1..." response=$(http_post "$BASE_URL/v1/register" "{\"email\":\"$USER1_EMAIL\",\"password\":\"$USER1_PASSWORD\"}" "") USER1_TOKEN=$(extract_json "$response" "token") USER1_ID=$(extract_json "$response" "id") log_success "User 1 created" # Пользователь 2 (для проверки, что пробный период используется один раз) USER2_EMAIL="sub_user2_$(date +%s)@example.com" USER2_PASSWORD="user123" log_info "Creating user 2..." response=$(http_post "$BASE_URL/v1/register" "{\"email\":\"$USER2_EMAIL\",\"password\":\"$USER2_PASSWORD\"}" "") USER2_TOKEN=$(extract_json "$response" "token") USER2_ID=$(extract_json "$response" "id") log_success "User 2 created" echo "" log_info "============================================================" log_info "TEST 1: Get subscription (free)" log_info "============================================================" response=$(http_get "$BASE_URL/v1/subscription" "$USER1_TOKEN") if echo "$response" | grep -q "free"; then log_success "User has free subscription" else log_error "Expected free subscription: $response" fi echo "" log_info "============================================================" log_info "TEST 2: Create personal calendar (should work with free)" log_info "============================================================" response=$(http_post "$BASE_URL/v1/calendars" "{\"title\":\"Personal Calendar\",\"type\":\"personal\"}" "$USER1_TOKEN") PERSONAL_CALENDAR_ID=$(extract_json "$response" "id") if [ -n "$PERSONAL_CALENDAR_ID" ]; then log_success "Personal calendar created (free subscription allows this)" else log_error "Failed to create personal calendar: $response" fi echo "" log_info "============================================================" log_info "TEST 3: Create commercial calendar (auto-activates trial)" log_info "============================================================" log_info "User 1 creating commercial calendar (should auto-start trial)..." response=$(http_post "$BASE_URL/v1/calendars" "{\"title\":\"Commercial Calendar\",\"type\":\"commercial\"}" "$USER1_TOKEN") COMMERCIAL_CALENDAR_ID=$(extract_json "$response" "id") if [ -n "$COMMERCIAL_CALENDAR_ID" ]; then log_success "Commercial calendar created - trial auto-activated" else log_error "Failed to create commercial calendar: $response" fi echo "" log_info "============================================================" log_info "TEST 4: Get subscription (should be trial now)" log_info "============================================================" response=$(http_get "$BASE_URL/v1/subscription" "$USER1_TOKEN") if echo "$response" | grep -q "trial"; then log_success "User now has trial subscription" else log_error "Expected trial subscription: $response" fi echo "" log_info "============================================================" log_info "TEST 5: Create second commercial calendar (should still work)" log_info "============================================================" response=$(http_post "$BASE_URL/v1/calendars" "{\"title\":\"Second Commercial\",\"type\":\"commercial\"}" "$USER1_TOKEN") SECOND_COMMERCIAL_ID=$(extract_json "$response" "id") if [ -n "$SECOND_COMMERCIAL_ID" ]; then log_success "Second commercial calendar created" else log_error "Failed to create second commercial calendar: $response" fi echo "" log_info "============================================================" log_info "TEST 6: User 2 creates commercial calendar (gets trial)" log_info "============================================================" log_info "User 2 creating commercial calendar..." response=$(http_post "$BASE_URL/v1/calendars" "{\"title\":\"User2 Commercial\",\"type\":\"commercial\"}" "$USER2_TOKEN") USER2_CALENDAR_ID=$(extract_json "$response" "id") if [ -n "$USER2_CALENDAR_ID" ]; then log_success "User 2 commercial calendar created - trial activated" else log_error "User 2 failed: $response" fi response=$(http_get "$BASE_URL/v1/subscription" "$USER2_TOKEN") if echo "$response" | grep -q "trial"; then log_success "User 2 has trial subscription" else log_error "User 2 subscription: $response" fi echo "" log_info "============================================================" log_info "TEST 7: Activate paid subscription" log_info "============================================================" log_info "User 1 activating paid subscription..." response=$(http_post "$BASE_URL/v1/subscription" "{\"action\":\"activate\",\"plan\":\"monthly\",\"payment_info\":{\"card\":\"4242424242424242\"}}" "$USER1_TOKEN") if echo "$response" | grep -q "monthly"; then log_success "Paid subscription activated" else log_error "Failed to activate: $response" fi echo "" log_info "============================================================" log_info "TEST 8: Get subscription (should be active paid)" log_info "============================================================" response=$(http_get "$BASE_URL/v1/subscription" "$USER1_TOKEN") if echo "$response" | grep -q "active" && echo "$response" | grep -q "monthly"; then log_success "User has active paid subscription" else log_error "Expected active paid subscription: $response" fi echo "" log_info "============================================================" log_info "TEST 9: User 3 (free) tries to use already consumed trial" log_info "============================================================" # Создаём пользователя 3, который сначала использует trial, потом отменяет подписку USER3_EMAIL="sub_user3_$(date +%s)@example.com" USER3_PASSWORD="user123" log_info "Creating user 3..." response=$(http_post "$BASE_URL/v1/register" "{\"email\":\"$USER3_EMAIL\",\"password\":\"$USER3_PASSWORD\"}" "") USER3_TOKEN=$(extract_json "$response" "token") log_success "User 3 created" log_info "User 3 creating commercial calendar (uses trial)..." response=$(http_post "$BASE_URL/v1/calendars" "{\"title\":\"User3 Commercial\",\"type\":\"commercial\"}" "$USER3_TOKEN") USER3_CALENDAR_ID=$(extract_json "$response" "id") log_success "Commercial calendar created" log_info "Simulating trial expiration (requires admin or time travel - skipped)" echo "" echo "============================================================" log_success "SUBSCRIPTION API TESTS COMPLETED!" echo "============================================================" echo "" echo "Summary:" echo " User 1: $USER1_EMAIL (trial -> paid)" echo " User 2: $USER2_EMAIL (trial)" echo " User 3: $USER3_EMAIL (trial)" echo ""