-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-apis.sh
More file actions
executable file
·194 lines (162 loc) · 5.05 KB
/
test-apis.sh
File metadata and controls
executable file
·194 lines (162 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/bash
# BookSpace - API Testing Script
# Quick script to test all API endpoints
set -e
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m'
print_header() {
echo -e "${BLUE}================================${NC}"
echo -e "${BLUE}$1${NC}"
echo -e "${BLUE}================================${NC}"
}
print_success() {
echo -e "${GREEN}✓ $1${NC}"
}
print_error() {
echo -e "${RED}✗ $1${NC}"
}
print_info() {
echo -e "${YELLOW}ℹ $1${NC}"
}
# Base URL
GATEWAY_URL="http://localhost:3000"
echo ""
print_header "BookSpace API Testing Script"
echo ""
# Test 1: Health checks
print_header "1. Testing Health Endpoints"
echo ""
services=(
"3000:Gateway"
"3001:Auth"
"3002:Grafana"
"3003:User"
"3004:Document"
"3005:Upload"
"3006:Search"
)
for service in "${services[@]}"; do
IFS=':' read -r port name <<< "$service"
if curl -s -f -o /dev/null "http://localhost:$port/health" 2>/dev/null; then
print_success "$name service (port $port) is healthy"
else
# Grafana health check is different
if [ "$name" == "Grafana" ]; then
if curl -s -f -o /dev/null "http://localhost:$port/api/health" 2>/dev/null; then
print_success "$name service (port $port) is healthy"
else
print_error "$name service (port $port) is not responding"
fi
else
print_error "$name service (port $port) is not responding"
fi
fi
done
echo ""
print_header "2. Testing Authentication Flow"
echo ""
# Generate random email to avoid conflicts
RANDOM_EMAIL="test_$(date +%s)@example.com"
# Register
print_info "Registering user: $RANDOM_EMAIL"
REGISTER_RESPONSE=$(curl -s -X POST "$GATEWAY_URL/v1/auth/signup" \
-H "Content-Type: application/json" \
-d "{
\"email\": \"$RANDOM_EMAIL\",
\"password\": \"SecurePass123!\",
\"name\": \"Test User\"
}" 2>/dev/null || echo '{"error": true}')
if echo "$REGISTER_RESPONSE" | grep -q "error"; then
print_error "Registration failed"
echo "Response: $REGISTER_RESPONSE"
else
print_success "User registered successfully"
fi
echo ""
# Login
print_info "Logging in..."
LOGIN_RESPONSE=$(curl -s -X POST "$GATEWAY_URL/v1/auth/login" \
-H "Content-Type: application/json" \
-d "{
\"email\": \"$RANDOM_EMAIL\",
\"password\": \"SecurePass123!\"
}" 2>/dev/null || echo '{"error": true}')
if echo "$LOGIN_RESPONSE" | grep -q "accessToken"; then
print_success "Login successful"
ACCESS_TOKEN=$(echo "$LOGIN_RESPONSE" | grep -o '"accessToken":"[^"]*' | cut -d'"' -f4)
print_info "Access token obtained"
else
print_error "Login failed"
echo "Response: $LOGIN_RESPONSE"
ACCESS_TOKEN=""
fi
echo ""
# Test protected endpoint
if [ -n "$ACCESS_TOKEN" ]; then
print_header "3. Testing Protected Endpoints"
echo ""
print_info "Fetching user profile..."
PROFILE_RESPONSE=$(curl -s "$GATEWAY_URL/v1/users/me" \
-H "Authorization: Bearer $ACCESS_TOKEN" 2>/dev/null || echo '{"error": true}')
if echo "$PROFILE_RESPONSE" | grep -q "$RANDOM_EMAIL"; then
print_success "Profile fetched successfully"
else
print_error "Failed to fetch profile"
fi
echo ""
print_info "Creating a document..."
DOC_RESPONSE=$(curl -s -X POST "$GATEWAY_URL/v1/docs" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"title": "Test Document",
"content": "This is a test document created by the API test script",
"tags": ["test", "automated"]
}' 2>/dev/null || echo '{"error": true}')
if echo "$DOC_RESPONSE" | grep -q "Test Document"; then
print_success "Document created successfully"
else
print_error "Failed to create document"
fi
echo ""
print_info "Searching documents..."
SEARCH_RESPONSE=$(curl -s "$GATEWAY_URL/v1/search?q=test" \
-H "Authorization: Bearer $ACCESS_TOKEN" 2>/dev/null || echo '{"error": true}')
if echo "$SEARCH_RESPONSE" | grep -q "results"; then
print_success "Search completed successfully"
else
print_error "Search failed"
fi
fi
echo ""
print_header "4. Testing Infrastructure Services"
echo ""
# Test Redis
if redis-cli ping 2>/dev/null | grep -q "PONG"; then
print_success "Redis is responding"
else
print_error "Redis is not responding"
fi
# Test Elasticsearch
if curl -s -f -o /dev/null "http://localhost:9200/_cluster/health" 2>/dev/null; then
print_success "Elasticsearch is responding"
else
print_error "Elasticsearch is not responding"
fi
# Test RabbitMQ
if curl -s -f -o /dev/null -u "bookspace:bookspace_dev_password" "http://localhost:15672/api/overview" 2>/dev/null; then
print_success "RabbitMQ is responding"
else
print_error "RabbitMQ is not responding"
fi
echo ""
print_header "Testing Complete!"
echo ""
print_info "Management UIs:"
echo " • RabbitMQ: http://localhost:15672"
echo " • Grafana: http://localhost:3002"
echo " • Prometheus: http://localhost:9091"
echo ""