Files
rf4-spotter/deploy/test-proxy-routing.sh
T
ik c6fdc969c2
CI / backend-and-migrations (push) Canceled after 0s
CI / astro-build (push) Canceled after 0s
CI / compose-e2e (push) Canceled after 0s
fix: separate admin page and API authentication
2026-09-09 07:29:39 +07:00

50 lines
2.5 KiB
Bash

#!/bin/sh
# Verify the actual production Caddy routes against an already running local stack.
# Invalid form submissions exercise Astro redirects without creating database rows.
set -eu
repo=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
cd "$repo"
web_id=$(docker compose ps -q web)
test -n "$web_id" || { echo "Start the local Compose stack first" >&2; exit 1; }
network=$(docker inspect --format '{{range $name, $_ := .NetworkSettings.Networks}}{{$name}}{{println}}{{end}}' "$web_id")
test "$(printf '%s\n' "$network" | wc -l)" -eq 1 || {
echo "Routing test requires a local web container with exactly one network" >&2; exit 1;
}
container="rf4-proxy-routing-$$"
cleanup() { docker stop "$container" >/dev/null 2>&1 || true; }
trap cleanup EXIT INT TERM
# Test-only Basic Auth hash from the Caddy documentation. No production secrets.
docker run --rm -d --name "$container" --network "$network" \
-p 127.0.0.1::8080 \
-e SITE_DOMAIN=http://localhost:8080 \
-e FILES_DOMAIN=http://localhost:8081 \
-e ACME_EMAIL=routing-test@example.com \
-e ADMIN_BASIC_USER=route-test \
-e 'ADMIN_BASIC_PASSWORD_HASH=$2a$14$Zkx19XLiW6VYouLHR5NmfOFU0z2GTNmpkT/5qqR7hx4IjWJPDhjvG' \
-v "$repo/deploy/Caddyfile:/etc/caddy/Caddyfile:ro" \
caddy:2.10.2-alpine >/dev/null
port=$(docker inspect --format '{{(index (index .NetworkSettings.Ports "8080/tcp") 0).HostPort}}' "$container")
base="http://127.0.0.1:$port"
attempt=0
until curl -fsS --max-time 2 -H 'Host: localhost:8080' "$base/health" >/dev/null 2>&1; do
attempt=$((attempt + 1))
if [ "$attempt" -ge 20 ]; then docker logs "$container" >&2; exit 1; fi
sleep 1
done
check_redirect() {
headers=$(curl -sS --max-time 15 -D - -o /dev/null \
-H 'Host: localhost:8080' -H 'Origin: http://localhost:8080' --data '' "$base$1")
if ! printf '%s\n' "$headers" | grep -Eq '^HTTP/[0-9.]+ 303 ' ||
! printf '%s\n' "$headers" | grep -Fi "location: $2" >/dev/null; then
printf 'Unexpected response for %s:\n%s\n' "$1" "$headers" >&2
exit 1
fi
}
check_redirect /api/report '/report?state=create_error'
check_redirect /api/report-screenshot '/report?state=screenshot_error&report_id='
curl -fsS --max-time 15 -H 'Host: localhost:8080' "$base/api/v1/fishes?limit=1" >/dev/null
curl -fsS --max-time 15 -H 'Host: localhost:8080' "$base/ready" >/dev/null
docker compose exec -T -e "RF4_PROXY_TEST_URL=http://$container:8080" api \
python - < "$repo/deploy/probe-admin-auth.py"
echo "Proxy routing passed: Astro form redirects, FastAPI catalog and health/readiness"