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.
This commit is contained in:
bchanot committed 2026-11-02 00:09:02 +01:00
1 parent 57b93afae6
commit f924ad056d
6 files changed
+64 -26

No files matched your search

+21 -1
View File
@@ -20,6 +20,8 @@
# pnpm db:sandbox avatar-e2e [args] (= pnpm e2e:avatar) the avatar worker (default mode: avatars + sweep) against the
# sandbox: a gateway on 127.0.0.1:$PORT_GATEWAY, the worker
# container, then tests/e2e/avatar-flow.e2e.ts (args go to vitest)
# pnpm db:sandbox sheet-assets-e2e (= pnpm e2e:sheet-assets) the sheet-asset upload tool against the sandbox:
# a gateway on 127.0.0.1:$PORT_GATEWAY, then scripts/sheet-assets-e2e.sh
# pnpm db:sandbox sql psql as postgres on stdin
# pnpm db:sandbox down
#
@@ -356,6 +358,23 @@ cmd_avatar_e2e() {
node_modules/.bin/pnpm --filter @faunosteo/supabase test:e2e "$@"
}
# The upload tool talks to the gateway's loopback port with the typed keys in
# its environment (never argv); the runner refuses any other target. Nothing is
# started when the port is taken; the gateway is removed at exit.
cmd_sheet_assets_e2e() {
load_secrets
docker rm -f "$GATEWAY" >/dev/null 2>&1 || true
if (exec 3<>"/dev/tcp/127.0.0.1/$PORT_GATEWAY") 2>/dev/null; then
die "port 127.0.0.1:$PORT_GATEWAY is already in use: free it, nothing was started"
fi
trap 'docker rm -f "$GATEWAY" >/dev/null 2>&1 || true' EXIT
start_gateway -p 127.0.0.1:$PORT_GATEWAY:8000
[ "$(docker port "$GATEWAY" 8000/tcp)" = "127.0.0.1:$PORT_GATEWAY" ] \
|| die "the gateway is not published on 127.0.0.1:$PORT_GATEWAY only"
SUPABASE_URL="http://127.0.0.1:$PORT_GATEWAY" SUPABASE_ANON_KEY="$SB_ANON_KEY" \
SUPABASE_SERVICE_ROLE_KEY="$SB_SERVICE_KEY" bash scripts/sheet-assets-e2e.sh
}
case "${1:-}" in
up) shift; cmd_up "$@" ;;
down) cmd_down ;;
@@ -372,6 +391,7 @@ case "${1:-}" in
e2e-shell) shift; load_secrets; with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/app e2e:shell "$@" ;;
verify-e2e) shift; cmd_verify_e2e "$@" ;;
avatar-e2e) shift; cmd_avatar_e2e "$@" ;;
sheet-assets-e2e) cmd_sheet_assets_e2e ;;
sql) load_secrets; sb_psql ;;
*) sed -n '2,29p' "$0"; exit 1 ;;
*) sed -n '2,31p' "$0"; exit 1 ;;
esac
+25 -11
View File
@@ -1,8 +1,10 @@
#!/usr/bin/env bash
# `pnpm e2e:sheet-assets`: the sheet-asset upload tool against the LOCAL Supabase stack (R5.5,
# docs/development/SUPABASE.md §3.1). Needs `pnpm db:start`. Run by hand: it uploads the equine
# draft drawing to the local bucket and confirms its row (idempotent; a rerun changes nothing).
# The keys are read from `pnpm supabase status`, held in variables, never printed.
# `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
@@ -10,17 +12,29 @@
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
STATUS=$(bash scripts/supabase.sh status -o json 2>/dev/null) || { echo "stack not running (pnpm db:start)"; exit 2; }
field() { printf '%s' "$STATUS" | python3 -c "import json,sys;print(json.load(sys.stdin)['$1'])"; }
SERVICE=$(field SERVICE_ROLE_KEY)
ANON=$(field ANON_KEY)
LOCAL=http://127.0.0.1:54321
LOCAL=http://127.0.0.1:57800
# expect <name> <expected exit> <url> <key> <args...>; output kept to grep, keys never echoed.
expect() {
@@ -42,8 +56,8 @@ 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:54321" "$SERVICE" "$SEED"
expect "non-local host refused" 1 "http://example.test:54321" "$SERVICE" "$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