добавлены автоматические тесты аутентификации и отказов
This commit is contained in:
Executable
+168
@@ -0,0 +1,168 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
BASE_URL="http://localhost:8080"
|
||||
TOKEN_FILE=$(mktemp)
|
||||
|
||||
restore_services() {
|
||||
echo "Restoring PostgreSQL and Redis..."
|
||||
docker compose up -d postgres redis
|
||||
|
||||
echo "Waiting for API readiness..."
|
||||
for attempt in $(seq 1 60); do
|
||||
if curl --silent --fail \
|
||||
"$BASE_URL/health/ready" > /dev/null; then
|
||||
rm -f "$TOKEN_FILE"
|
||||
echo "Services restored."
|
||||
return
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
|
||||
rm -f "$TOKEN_FILE"
|
||||
echo "WARNING: API did not become ready after recovery." >&2
|
||||
}
|
||||
|
||||
trap restore_services EXIT
|
||||
|
||||
check_status() {
|
||||
local expected="$1"
|
||||
local path="$2"
|
||||
local authorization="${3:-}"
|
||||
|
||||
local status
|
||||
|
||||
if [[ -n "$authorization" ]]; then
|
||||
status=$(curl --silent --output /dev/null \
|
||||
--max-time 12 \
|
||||
--write-out "%{http_code}" \
|
||||
-H "Authorization: Bearer $authorization" \
|
||||
"$BASE_URL$path")
|
||||
else
|
||||
status=$(curl --silent --output /dev/null \
|
||||
--max-time 12 \
|
||||
--write-out "%{http_code}" \
|
||||
"$BASE_URL$path")
|
||||
fi
|
||||
|
||||
if [[ "$status" != "$expected" ]]; then
|
||||
echo "FAIL: $path expected $expected, got $status" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "PASS: $path returned $status"
|
||||
}
|
||||
|
||||
echo "Creating a test user and session..."
|
||||
|
||||
python - "$TOKEN_FILE" <<'PY'
|
||||
import base64
|
||||
import json
|
||||
import sys
|
||||
import urllib.request
|
||||
import uuid
|
||||
|
||||
base = "http://localhost:8080"
|
||||
username = "failure_" + uuid.uuid4().hex[:16]
|
||||
password = "correct-password-123"
|
||||
|
||||
def post(path, data=None, authorization=None):
|
||||
headers = {}
|
||||
body = None
|
||||
|
||||
if data is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
body = json.dumps(data).encode("utf-8")
|
||||
|
||||
if authorization:
|
||||
headers["Authorization"] = authorization
|
||||
|
||||
request = urllib.request.Request(
|
||||
base + path,
|
||||
data=body,
|
||||
headers=headers,
|
||||
method="POST",
|
||||
)
|
||||
|
||||
with urllib.request.urlopen(request, timeout=10) as response:
|
||||
assert response.status == 201
|
||||
return json.load(response)
|
||||
|
||||
post("/api/v1/users", {
|
||||
"username": username,
|
||||
"display_name": "Failure Test User",
|
||||
"password": password,
|
||||
})
|
||||
|
||||
credentials = base64.b64encode(
|
||||
f"{username}:{password}".encode()
|
||||
).decode()
|
||||
|
||||
result = post(
|
||||
"/api/v1/auth/sessions",
|
||||
authorization=f"Basic {credentials}",
|
||||
)
|
||||
|
||||
with open(sys.argv[1], "w", encoding="utf-8") as file:
|
||||
file.write(result["token"])
|
||||
|
||||
print("Test session created.")
|
||||
PY
|
||||
|
||||
TOKEN=$(cat "$TOKEN_FILE")
|
||||
|
||||
echo
|
||||
echo "Checking baseline..."
|
||||
check_status 200 "/health/live"
|
||||
check_status 200 "/health/ready"
|
||||
check_status 200 "/api/v1/users/me" "$TOKEN"
|
||||
|
||||
echo
|
||||
echo "=== Redis failure ==="
|
||||
|
||||
docker compose stop redis
|
||||
|
||||
check_status 200 "/health/live"
|
||||
check_status 503 "/health/ready"
|
||||
check_status 503 "/api/v1/users/me" "$TOKEN"
|
||||
|
||||
echo "Restarting Redis..."
|
||||
docker compose start redis
|
||||
|
||||
for attempt in $(seq 1 60); do
|
||||
if curl --silent --fail \
|
||||
"$BASE_URL/health/ready" > /dev/null; then
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
|
||||
check_status 200 "/health/ready"
|
||||
check_status 200 "/api/v1/users/me" "$TOKEN"
|
||||
|
||||
echo
|
||||
echo "=== PostgreSQL failure ==="
|
||||
|
||||
docker compose stop postgres
|
||||
|
||||
check_status 200 "/health/live"
|
||||
check_status 503 "/health/ready"
|
||||
check_status 503 "/api/v1/users/me" "$TOKEN"
|
||||
|
||||
echo "Restarting PostgreSQL..."
|
||||
docker compose start postgres
|
||||
|
||||
for attempt in $(seq 1 60); do
|
||||
if curl --silent --fail \
|
||||
"$BASE_URL/health/ready" > /dev/null; then
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
|
||||
check_status 200 "/health/ready"
|
||||
check_status 200 "/api/v1/users/me" "$TOKEN"
|
||||
|
||||
echo
|
||||
echo "PASS: dependency failure and recovery tests completed."
|
||||
Executable
+51
@@ -0,0 +1,51 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
restore_api() {
|
||||
echo "Restoring API with SESSION_TTL_SECONDS=3600..."
|
||||
|
||||
SESSION_TTL_SECONDS=3600 \
|
||||
docker compose up -d --force-recreate --no-deps api
|
||||
}
|
||||
|
||||
trap restore_api EXIT
|
||||
|
||||
echo "Starting API with SESSION_TTL_SECONDS=2..."
|
||||
|
||||
SESSION_TTL_SECONDS=2 \
|
||||
docker compose up -d --force-recreate --no-deps api
|
||||
|
||||
echo "Checking API configuration..."
|
||||
|
||||
actual_ttl=$(docker compose exec -T api printenv SESSION_TTL_SECONDS)
|
||||
|
||||
if [[ "$actual_ttl" != "2" ]]; then
|
||||
echo "Expected SESSION_TTL_SECONDS=2, got $actual_ttl" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Waiting for API readiness..."
|
||||
|
||||
ready=false
|
||||
|
||||
for attempt in $(seq 1 30); do
|
||||
if curl --silent --fail \
|
||||
http://localhost:8080/health/ready > /dev/null; then
|
||||
ready=true
|
||||
break
|
||||
fi
|
||||
|
||||
sleep 1
|
||||
done
|
||||
|
||||
if [[ "$ready" != "true" ]]; then
|
||||
echo "API did not become ready." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Running short TTL test..."
|
||||
|
||||
python tests_student/test_short_ttl.py
|
||||
|
||||
echo "PASS: short session TTL test completed."
|
||||
Reference in New Issue
Block a user