Repository navigation
Expand file tree
/
Copy pathsmoke-test.sh
More file actions
executable file
·220 lines (176 loc) · 7.38 KB
/
Copy pathsmoke-test.sh
File metadata and controls
executable file
·220 lines (176 loc) · 7.38 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/bin/bash
# scripts/smoke-test.sh
# Full-stack smoke test script for xConfess
# Usage: ./scripts/smoke-test.sh [--verbose]
# Comprehensive validation of all critical endpoints
set -e
# Configuration
BACKEND_URL="${BACKEND_URL:-http://localhost:5000}"
FRONTEND_URL="${FRONTEND_URL:-http://localhost:3000}"
VERBOSE="${1:-}"
RESULTS_FILE="smoke-test-results.txt"
REQUESTS_LOG="smoke-test-requests.log"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Test counters
TESTS_RUN=0
TESTS_PASSED=0
TESTS_FAILED=0
# Logging functions
log_section() {
echo -e "\n${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}$1${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
}
log_test() {
echo -e "${YELLOW}Testing: $1${NC}"
}
log_pass() {
echo -e "${GREEN}✓ PASS${NC}: $1"
((TESTS_PASSED++))
}
log_fail() {
echo -e "${RED}✗ FAIL${NC}: $1"
((TESTS_FAILED++))
}
log_request() {
echo "[$1] $2" >> "$REQUESTS_LOG"
}
# HTTP test function
test_http_endpoint() {
local method=$1
local endpoint=$2
local expected_code=$3
local data=$4
local description=$5
local headers=${6:-"-H 'Content-Type: application/json'"}
((TESTS_RUN++))
log_test "$description"
local url="${BACKEND_URL}${endpoint}"
log_request "$method" "$url"
if [ -n "$data" ]; then
response=$(curl -s -w "\n%{http_code}" -X "$method" "$url" $headers -d "$data" 2>/dev/null)
else
response=$(curl -s -w "\n%{http_code}" -X "$method" "$url" $headers 2>/dev/null)
fi
local http_code=$(echo "$response" | tail -n1)
local body=$(echo "$response" | sed '$d')
if [ -z "$http_code" ]; then
log_fail "$description (no response)"
return 1
fi
if [[ "$expected_code" == *"$http_code"* ]]; then
log_pass "$description (HTTP $http_code)"
[ -n "$VERBOSE" ] && echo "Response: $body"
return 0
else
log_fail "$description (HTTP $http_code, expected $expected_code)"
[ -n "$VERBOSE" ] && echo "Response: $body"
return 1
fi
}
# Initialize results file
echo "xConfess Smoke Test Results" > "$RESULTS_FILE"
echo "Generated: $(date)" >> "$RESULTS_FILE"
echo "Backend: $BACKEND_URL" >> "$RESULTS_FILE"
echo "Frontend: $FRONTEND_URL" >> "$RESULTS_FILE"
echo "========================================" >> "$RESULTS_FILE"
# Initialize requests log
echo "# xConfess Smoke Test API Requests Log" > "$REQUESTS_LOG"
echo "# Generated: $(date)" >> "$REQUESTS_LOG"
echo "# Format: [METHOD] URL" >> "$REQUESTS_LOG"
echo -e "${GREEN}xConfess Smoke Test Suite${NC}"
echo "Backend: $BACKEND_URL"
echo "Frontend: $FRONTEND_URL"
echo ""
# ============================================================================
# SECTION 1: BACKEND HEALTH CHECKS
# ============================================================================
log_section "SECTION 1: BACKEND HEALTH & READINESS"
test_http_endpoint "GET" "/" "200" "" "Root endpoint (GET /)" "-H 'Content-Type: application/json'"
test_http_endpoint "GET" "/health/live" "200" "" "Liveness probe (GET /health/live)" "-H 'Content-Type: application/json'"
test_http_endpoint "GET" "/health/ready" "200 503" "" "Readiness probe (GET /health/ready)" "-H 'Content-Type: application/json'"
# ============================================================================
# SECTION 2: CONFESSION API ENDPOINTS
# ============================================================================
log_section "SECTION 2: CONFESSION API ENDPOINTS"
test_http_endpoint "GET" "/confessions?page=1&limit=10" "200" "" "List confessions (unauthenticated)" "-H 'Content-Type: application/json'"
# Create a test confession
test_confession_data='{"message":"Test confession from smoke test suite","gender":"female","tags":["test"]}'
test_http_endpoint "POST" "/confessions" "201 400" "$test_confession_data" "Create confession" "-H 'Content-Type: application/json'"
# ============================================================================
# SECTION 3: REPORT ENDPOINT
# ============================================================================
log_section "SECTION 3: REPORT SUBMISSION"
# First, get a confession ID to report
confession_id=$(curl -s "${BACKEND_URL}/confessions?limit=1" | jq -r '.data[0].id // "test-id-123"' 2>/dev/null)
test_report_data="{\"confessionId\":\"${confession_id}\",\"type\":\"offensive\",\"reason\":\"Test report from smoke test\"}"
test_http_endpoint "POST" "/reports" "201 400" "$test_report_data" "Submit report (unauthenticated)" "-H 'Content-Type: application/json'"
# ============================================================================
# SECTION 4: ADMIN ENDPOINTS (SHOULD FAIL UNAUTHENTICATED)
# ============================================================================
log_section "SECTION 4: ADMIN ENDPOINTS (AUTHENTICATION REQUIRED)"
test_http_endpoint "GET" "/diagnostics/notifications" "401 403" "" "Admin diagnostics endpoint without auth (should fail)" "-H 'Content-Type: application/json'"
# ============================================================================
# SECTION 5: FRONTEND HEALTH CHECK
# ============================================================================
log_section "SECTION 5: FRONTEND AVAILABILITY"
((TESTS_RUN++))
log_test "Frontend homepage availability (GET /)"
frontend_response=$(curl -s -w "\n%{http_code}" "${FRONTEND_URL}/" 2>/dev/null)
frontend_code=$(echo "$frontend_response" | tail -n1)
frontend_body=$(echo "$frontend_response" | sed '$d')
if [ "$frontend_code" = "200" ] || [ "$frontend_code" = "404" ]; then
log_pass "Frontend is responsive (HTTP $frontend_code)"
elif [ -z "$frontend_code" ]; then
log_fail "Frontend is not responding (connection refused)"
else
log_fail "Frontend returned HTTP $frontend_code"
fi
# ============================================================================
# RESULTS SUMMARY
# ============================================================================
log_section "TEST RESULTS SUMMARY"
echo "Total Tests Run: $TESTS_RUN"
echo "Tests Passed: ${GREEN}$TESTS_PASSED${NC}"
echo "Tests Failed: ${RED}$TESTS_FAILED${NC}"
pass_rate=$((TESTS_PASSED * 100 / TESTS_RUN))
echo "Pass Rate: ${YELLOW}${pass_rate}%${NC}"
# Write summary to results file
echo "" >> "$RESULTS_FILE"
echo "Total Tests: $TESTS_RUN" >> "$RESULTS_FILE"
echo "Passed: $TESTS_PASSED" >> "$RESULTS_FILE"
echo "Failed: $TESTS_FAILED" >> "$RESULTS_FILE"
echo "Pass Rate: ${pass_rate}%" >> "$RESULTS_FILE"
# Determine exit code
if [ $TESTS_FAILED -gt 0 ]; then
echo -e "\n${RED}Some tests failed. Review logs for details.${NC}"
echo "Results saved to: $RESULTS_FILE"
echo "Requests logged to: $REQUESTS_LOG"
exit 1
else
echo -e "\n${GREEN}All tests passed!${NC}"
echo "Results saved to: $RESULTS_FILE"
echo "Requests logged to: $REQUESTS_LOG"
exit 0
fi
if echo "$content" | grep -qi '<html'; then
pass_msg "Root route returns HTML"
else
fail_msg "Root route did not return HTML"
fi
else
fail_msg "Root route — unreachable"
fi
echo ""
echo "============================================"
echo " Results: $pass passed, $fail failed"
echo "============================================"
if [ "$fail" -gt 0 ]; then
exit 1
fi
exit 0