Files
faunosteo/scripts/sheet-assets-e2e.sh
T
bchanot f924ad056d test(supabase): sheet-assets e2e on the sandbox only
pnpm e2e:sheet-assets is now an alias of pnpm db:sandbox sheet-assets-e2e:
gateway on 127.0.0.1:57800 (occupied port refused), the runner refuses any
URL but the sandbox gateway and any key not issued by the sandbox, before
building or any request; no DEV URL, port or key remains.
2026-11-02 00:09:02 +01:00

67 lines
3.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# `pnpm e2e:sheet-assets` (= `pnpm db:sandbox sheet-assets-e2e`): the sheet-asset upload tool against the
# throwaway SANDBOX stack (docs/development/SUPABASE.md §3.1, §5.1), never DEV. It uploads the equine draft
# drawing to the sandbox bucket and confirms its row (idempotent; a rerun changes nothing).
# The runner (scripts/db-sandbox.sh) starts the gateway and passes SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY
# and SUPABASE_ANON_KEY in the environment; this script refuses anything else before building or any
# request, and never prints a key.
#
# Checks: --check writes nothing; a first run uploads and confirms; a rerun has nothing to do;
# the anon key and a wrong key are refused; a destination that is not a local stack is refused
# before any request; an unreachable local port is a network error (exit 3).
set -uo pipefail
cd "$(dirname "$0")/.."
SANDBOX_URL=http://127.0.0.1:57800
SERVICE=${SUPABASE_SERVICE_ROLE_KEY:-}
ANON=${SUPABASE_ANON_KEY:-}
# A key is accepted only when its JWT payload names the sandbox issuer; nothing is printed.
sandbox_key() { printf '%s' "$1" | python3 -I -c '
import base64, json, sys
try:
p = sys.stdin.read().split(".")[1]
sys.exit(0 if json.loads(base64.urlsafe_b64decode(p + "=" * (-len(p) % 4))).get("iss") == "db-sandbox" else 1)
except Exception:
sys.exit(1)'; }
if [ "${SUPABASE_URL:-}" != "$SANDBOX_URL" ] || ! sandbox_key "$SERVICE" || ! sandbox_key "$ANON"; then
echo "sheet-assets-e2e: sandbox only, run it through pnpm e2e:sheet-assets" >&2
exit 2
fi
. scripts/rust-env.sh
SEED=supabase/seed/anatomical-sheets/equine/draft
BIN=target/release/upload-sheet-assets
FAIL=0
env -u SUPABASE_SERVICE_ROLE_KEY cargo build --release --locked -q -p faunosteo-core --features upload --bin upload-sheet-assets || exit 2
LOCAL=http://127.0.0.1:57800
# expect <name> <expected exit> <url> <key> <args...>; output kept to grep, keys never echoed.
expect() {
local name=$1 want=$2 url=$3 key=$4; shift 4
local out rc
# Keys go through the environment of the binary and grep's pattern file, never on an argv.
out=$(unset FAUNOSTEO_DEV_HOST INIT_CWD; SUPABASE_URL="$url" SUPABASE_SERVICE_ROLE_KEY="$key" "$BIN" "$@" 2>&1); rc=$?
LAST=$out
if [ "$rc" -eq "$want" ] && ! grep -qF -f <(printf '%s\n' "$key" "$SERVICE" "$ANON") <<<"$out"; then
echo "PASS $name (exit $rc)"
else
echo "FAIL $name (exit $rc, wanted $want)"; FAIL=1
fi
}
expect "check writes nothing" 0 "$LOCAL" "$SERVICE" --check "$SEED"
expect "first run" 0 "$LOCAL" "$SERVICE" "$SEED"
expect "rerun" 0 "$LOCAL" "$SERVICE" "$SEED"
grep -q "nothing to do" <<<"$LAST" && echo "PASS rerun has nothing to do" || { echo "FAIL rerun did work"; FAIL=1; }
expect "anon key refused" 1 "$LOCAL" "$ANON" "$SEED"
expect "wrong key refused" 1 "$LOCAL" "not-a-valid-key" "$SEED"
expect "https refused" 1 "https://127.0.0.1:57800" "$SERVICE" "$SEED"
expect "non-local host refused" 1 "http://example.test:57800" "$SERVICE" "$SEED"
expect "no port refused" 1 "http://127.0.0.1" "$SERVICE" "$SEED"
expect "unreachable port" 3 "http://127.0.0.1:9" "$SERVICE" "$SEED"
expect "folder outside the seed" 1 "$LOCAL" "$SERVICE" --check crates/core/src
[ "$FAIL" -eq 0 ] && echo "e2e:sheet-assets OK" || echo "e2e:sheet-assets FAILED"
exit "$FAIL"