Files
mtusi2026lab2/scripts/test-expiry.sh
T

47 lines
1.6 KiB
Bash
Executable File

#!/bin/sh
set -eu
ROOT="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)"
cd "$ROOT"
BASE="${BASE_URL:-http://127.0.0.1:8080}"
echo "=== restart api with SESSION_TTL_SECONDS=2 ==="
SESSION_TTL_SECONDS=2 docker compose --env-file .env up -d --no-deps --force-recreate api >/dev/null
i=0
while [ "$i" -lt 40 ]; do
if curl -sf "$BASE/health/ready" >/dev/null 2>&1; then
break
fi
i=$((i + 1))
sleep 0.5
done
USER="exp_$(date +%s)"
PASS="T3st-$(openssl rand -hex 8)"
curl -sf -X POST "$BASE/api/v1/users" \
-H 'Content-Type: application/json' \
-d "{\"username\":\"$USER\",\"display_name\":\"Expiry\",\"password\":\"$PASS\"}" >/tmp/exp_user.json
CRED=$(printf '%s:%s' "$USER" "$PASS" | base64 -w0 2>/dev/null || printf '%s:%s' "$USER" "$PASS" | base64)
SESS=$(curl -sf -X POST "$BASE/api/v1/auth/sessions" -H "Authorization: Basic $CRED")
TOKEN=$(printf '%s' "$SESS" | python3 -c 'import sys,json; print(json.load(sys.stdin)["token"])')
code=$(curl -sS -o /tmp/exp_me1.json -w '%{http_code}' -H "Authorization: Bearer $TOKEN" "$BASE/api/v1/users/me")
echo "immediate /users/me -> $code (expect 200)"
test "$code" = "200"
echo "sleep 3 for expiry"
sleep 3
code=$(curl -sS -o /tmp/exp_me2.json -w '%{http_code}' -H "Authorization: Bearer $TOKEN" "$BASE/api/v1/users/me" || true)
echo "after ttl /users/me -> $code (expect 401)"
test "$code" = "401"
echo "=== restore SESSION_TTL_SECONDS=3600 ==="
SESSION_TTL_SECONDS=3600 docker compose --env-file .env up -d --no-deps --force-recreate api >/dev/null
i=0
while [ "$i" -lt 40 ]; do
if curl -sf "$BASE/health/ready" >/dev/null 2>&1; then
break
fi
i=$((i + 1))
sleep 0.5
done
echo "PASS: session expiry"