Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.

Commit ac254c4

Browse files
committed
feat: [#1] improve API error reporting and complete token standardization
This commit completes the replacement of 'local-dev-admin-token-12345' with 'MyAccessToken' across all components and improves error reporting for API endpoint failures. Changes: - Replace old token with MyAccessToken in documentation (integration-testing-guide.md, smoke-testing-guide.md) - Replace old token with MyAccessToken in test scripts (test-e2e.sh) - Improve API endpoint error reporting in deploy-app.sh and health-check.sh - Show HTTP status code and response body on API failures - Use proper HTTP status checking and temp file handling - Fix ShellCheck linting issues with variable assignments and comparisons Benefits: - API authentication failures now show actual error details - Consistent token usage across all environments and documentation - Better debugging capabilities for deployment issues - Cleaner code that passes linting validation Testing: - Validated API endpoint works with MyAccessToken - Confirmed error reporting shows detailed failure information - E2E tests now use standardized token consistently
1 parent d6e6a11 commit ac254c4

File tree

5 files changed

+31
-18
lines changed

5 files changed

+31
-18
lines changed

docs/guides/integration-testing-guide.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,7 @@ curl -s http://$VM_IP/api/health_check | jq .
11991199

12001200
# [PROJECT_ROOT] Test stats API (requires authentication token)
12011201
# Note: Get the token from the .env file in the VM
1202-
TOKEN="local-dev-admin-token-12345"
1202+
TOKEN="MyAccessToken"
12031203
curl -s "http://$VM_IP/api/v1/stats?token=$TOKEN" | jq .
12041204
```
12051205

@@ -1289,7 +1289,7 @@ curl http://$VM_IP/prometheus/ # Prometheus UI
12891289
ssh torrust@$VM_IP \
12901290
"grep TRACKER_ADMIN_TOKEN /home/torrust/github/torrust/torrust-tracker-demo/application/.env"
12911291

1292-
# Should output: TRACKER_ADMIN_TOKEN=local-dev-admin-token-12345
1292+
# Should output: TRACKER_ADMIN_TOKEN=MyAccessToken
12931293
```
12941294

12951295
#### 5.2.5 Advanced Testing with jq
@@ -1700,10 +1700,10 @@ VM_IP=$(cd infrastructure/terraform && tofu output -raw vm_ip)
17001700
curl -s http://$VM_IP/api/health_check | jq .
17011701

17021702
# Test stats (auth required)
1703-
curl -s "http://$VM_IP/api/v1/stats?token=local-dev-admin-token-12345" | jq .
1703+
curl -s "http://$VM_IP/api/v1/stats?token=MyAccessToken" | jq .
17041704

17051705
# Test specific metrics with jq filtering
1706-
curl -s "http://$VM_IP/api/v1/stats?token=local-dev-admin-token-12345" | jq '.torrents, .seeders, .leechers'
1706+
curl -s "http://$VM_IP/api/v1/stats?token=MyAccessToken" | jq '.torrents, .seeders, .leechers'
17071707
```
17081708

17091709
#### ✅ Monitoring Service Testing
@@ -1751,7 +1751,7 @@ curl http://$VM_IP/api/v1/stats
17511751
**Correct**: Including authentication token:
17521752

17531753
```bash
1754-
curl "http://$VM_IP/api/v1/stats?token=local-dev-admin-token-12345"
1754+
curl "http://$VM_IP/api/v1/stats?token=MyAccessToken"
17551755
```
17561756

17571757
### 9.4 Health Check Limitations
@@ -2054,7 +2054,7 @@ During the most recent testing cycle, the following components were validated su
20542054

20552055
- **Health Check API**: `/api/health_check` - No authentication required
20562056
- **Stats API**: `/api/v1/stats` - **Requires authentication token**
2057-
- **Admin Token**: `local-dev-admin-token-12345` (from `.env` file)
2057+
- **Admin Token**: `MyAccessToken` (from `.env` file)
20582058

20592059
#### Correct API Testing Examples
20602060

@@ -2063,7 +2063,7 @@ During the most recent testing cycle, the following components were validated su
20632063
curl -s http://$VM_IP/api/health_check | jq .
20642064

20652065
# Stats API (auth required)
2066-
curl -s "http://$VM_IP/api/v1/stats?token=local-dev-admin-token-12345" | jq .
2066+
curl -s "http://$VM_IP/api/v1/stats?token=MyAccessToken" | jq .
20672067
```
20682068

20692069
#### Network Architecture

docs/guides/smoke-testing-guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ The statistics API is available through the nginx proxy on port 80:
267267
```bash
268268
# Test statistics API through nginx proxy (requires admin token)
269269
echo "=== Testing Statistics API ==="
270-
curl -s "http://$TARGET_SERVER:80/api/v1/stats?token=local-dev-admin-token-12345" | jq
270+
curl -s "http://$TARGET_SERVER:80/api/v1/stats?token=MyAccessToken" | jq
271271
```
272272

273273
**Expected Output:**

infrastructure/scripts/deploy-app.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,21 @@ validate_deployment() {
483483
fi
484484
485485
# Test API stats endpoint (through nginx proxy, requires auth)
486-
if curl -f -s "http://localhost/api/v1/stats?token=local-dev-admin-token-12345" >/dev/null 2>&1; then
486+
# Save response to temp file and get HTTP status code
487+
api_http_code=\$(curl -s -o /tmp/api_response.json -w '%{http_code}' \"http://localhost/api/v1/stats?token=MyAccessToken\" 2>&1 || echo \"000\")
488+
api_response_body=\$(cat /tmp/api_response.json 2>/dev/null || echo \"No response\")
489+
490+
# Check if HTTP status is 200 (success)
491+
if [ \"\$api_http_code\" -eq 200 ] 2>/dev/null; then
487492
echo '✅ API stats endpoint: OK'
488493
else
489494
echo '❌ API stats endpoint: FAILED'
495+
echo \" HTTP Code: \$api_http_code\"
496+
echo \" Response: \$api_response_body\"
497+
rm -f /tmp/api_response.json
490498
exit 1
491499
fi
500+
rm -f /tmp/api_response.json
492501
493502
# Test HTTP tracker endpoint (through nginx proxy - expects 404 for root)
494503
if curl -s -w '%{http_code}' http://localhost/ -o /dev/null | grep -q '404'; then
@@ -516,7 +525,7 @@ show_connection_info() {
516525
echo
517526
echo "=== APPLICATION ENDPOINTS ==="
518527
echo "Health Check: http://${vm_ip}/health_check" # DevSkim: ignore DS137138
519-
echo "API Stats: http://${vm_ip}/api/v1/stats?token=local-dev-admin-token-12345" # DevSkim: ignore DS137138
528+
echo "API Stats: http://${vm_ip}/api/v1/stats?token=MyAccessToken" # DevSkim: ignore DS137138
520529
echo "HTTP Tracker: http://${vm_ip}/ (for BitTorrent clients)" # DevSkim: ignore DS137138
521530
echo "UDP Tracker: udp://${vm_ip}:6868, udp://${vm_ip}:6969"
522531
echo "Grafana: http://${vm_ip}:3100 (admin/admin)" # DevSkim: ignore DS137138

infrastructure/scripts/health-check.sh

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,23 @@ test_application_endpoints() {
142142

143143
# Test API stats endpoint (via nginx proxy with auth)
144144
((TOTAL_TESTS++))
145-
if vm_exec "${vm_ip}" "curl -f -s 'http://localhost/api/v1/stats?token=local-dev-admin-token-12345' >/dev/null 2>&1"; then
145+
local api_response
146+
local api_http_code
147+
api_response=$(vm_exec "${vm_ip}" "curl -s -w '\\n%{http_code}' 'http://localhost/api/v1/stats?token=MyAccessToken'" || echo "")
148+
api_http_code=$(echo "${api_response}" | tail -n1)
149+
api_response=$(echo "${api_response}" | head -n -1)
150+
151+
if [[ "${api_http_code}" == "200" ]]; then
146152
log_test_pass "API stats endpoint (nginx proxy)"
147153

148154
# Get stats if verbose
149155
if [[ "${VERBOSE}" == "true" ]]; then
150-
local stats
151-
stats=$(vm_exec "${vm_ip}" "curl -s 'http://localhost/api/v1/stats?token=local-dev-admin-token-12345'" || echo "")
152-
if [[ -n "${stats}" ]]; then
153-
echo " Stats: ${stats}"
154-
fi
156+
echo " Stats: ${api_response}"
155157
fi
156158
else
157159
log_test_fail "API stats endpoint (nginx proxy)"
160+
echo " HTTP Code: ${api_http_code}"
161+
echo " Response: ${api_response}"
158162
fi
159163

160164
# Test HTTP tracker endpoint (via nginx proxy - expects 404 for root)

tests/test-e2e.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ test_smoke_testing() {
234234

235235
# Test 2: Statistics API (through nginx proxy on port 80)
236236
log_info "Testing statistics API through nginx proxy..."
237-
if [[ $(test_http_endpoint "http://${vm_ip}:80/api/v1/stats?token=local-dev-admin-token-12345" '"torrents"') == "success" ]]; then # DevSkim: ignore DS137138
237+
if [[ $(test_http_endpoint "http://${vm_ip}:80/api/v1/stats?token=MyAccessToken" '"torrents"') == "success" ]]; then # DevSkim: ignore DS137138
238238
log_success "✓ Statistics API working"
239239
else
240240
log_error "✗ Statistics API failed"
@@ -521,7 +521,7 @@ run_e2e_test() {
521521
return 0
522522
else
523523
log_section "TEST RESULT: FAILURE"
524-
log_error "End-to-end twelve-factor deployment test failed!"
524+
log_error "End-to-end test failed!"
525525
log_error "Total test time: ${minutes}m ${seconds}s"
526526
log_error "Check test log for details: ${TEST_LOG_FILE}"
527527
return 1

0 commit comments

Comments
 (0)