pnpm e2e:shell (db-sandbox e2e-shell) builds a Vite server on 127.0.0.1:5192 against a Supabase URL that resolves nowhere and replays its calls on the sandbox. Per-test fictitious accounts through the admin API, a clean avatar seeded: sign-in, dashboard, the photo confirmation with a slow removal, the phone drawer, FR and EN, axe. No trace kept, fixture values masked in reports.
243 lines
12 KiB
Bash
Executable File
243 lines
12 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Throwaway Supabase stack for testing migrations without touching the DEV
|
|
# stack (docs/development/SUPABASE.md §5.1). Same images as the CLI stack,
|
|
# its own Docker network, ports published on 127.0.0.1 only and distinct from
|
|
# DEV, no Docker volume (the images declare none and nothing is mounted), so
|
|
# `down` removes only the containers and network named below.
|
|
#
|
|
# pnpm db:sandbox up fresh all migrations, then the seeds (= db reset)
|
|
# pnpm db:sandbox up upgrade <version> migrations before <version>, seeds, two
|
|
# GoTrue accounts, then `migration up` (= DEV path)
|
|
# pnpm db:sandbox test-db the pgTAP files (`supabase test db`)
|
|
# pnpm db:sandbox types [--check] generated types (write, or diff with the committed file)
|
|
# pnpm db:sandbox test packages/supabase/tests/sandbox (HTTP, concurrency)
|
|
# pnpm db:sandbox e2e-shell = pnpm e2e:shell: the /app shell in Chromium (apps/app/e2e)
|
|
# pnpm db:sandbox sql psql as postgres on stdin
|
|
# pnpm db:sandbox down
|
|
#
|
|
# Secrets are generated at `up` (random password, JWT secret, keys) into a
|
|
# 600 file under $FAUNOSTEO_SANDBOX_DIR (default ~/.cache/faunosteo-db-sandbox),
|
|
# outside the repository, and never printed. Every Supabase CLI call names the
|
|
# sandbox with --db-url: none may fall back to the local DEV database.
|
|
set -euo pipefail
|
|
cd "$(dirname "$0")/.."
|
|
. scripts/dev-env.sh
|
|
|
|
PG_IMAGE=public.ecr.aws/supabase/postgres:17.11.0.004
|
|
AUTH_IMAGE=public.ecr.aws/supabase/gotrue:v2.197.0
|
|
REST_IMAGE=public.ecr.aws/supabase/postgrest:v16.4
|
|
STORAGE_IMAGE=public.ecr.aws/supabase/storage-api:v1.79.36
|
|
MAIL_IMAGE=public.ecr.aws/supabase/mailpit:v1.31.3
|
|
NET=faunosteo_dbsandbox_net
|
|
DB=faunosteo_dbsandbox_db
|
|
AUTH=faunosteo_dbsandbox_auth
|
|
REST=faunosteo_dbsandbox_rest
|
|
STORAGE=faunosteo_dbsandbox_storage
|
|
MAIL=faunosteo_dbsandbox_mail
|
|
PORT_DB=57432 PORT_AUTH=57999 PORT_REST=57300 PORT_STORAGE=57500 PORT_MAIL=57025
|
|
STATE="${FAUNOSTEO_SANDBOX_DIR:-$HOME/.cache/faunosteo-db-sandbox}"
|
|
SECRETS="$STATE/secrets.sh"
|
|
CLI=node_modules/.bin/supabase
|
|
|
|
die() { echo "db-sandbox: $*" >&2; exit 1; }
|
|
case "$(realpath -m "$STATE")" in
|
|
"$PWD" | "$PWD"/*) die "FAUNOSTEO_SANDBOX_DIR must be outside the repository" ;;
|
|
esac
|
|
# Every value must be present: an empty password would turn --db-url into an
|
|
# argument the CLI might read as absent and fall back to the DEV database.
|
|
load_secrets() {
|
|
[ -f "$SECRETS" ] || die "not running (pnpm db:sandbox up fresh)"
|
|
. "$SECRETS"
|
|
[ -n "${SB_PW:-}" ] && [ -n "${SB_JWT_SECRET:-}" ] && [ -n "${SB_ANON_KEY:-}" ] && [ -n "${SB_SERVICE_KEY:-}" ] \
|
|
|| die "secrets file incomplete (pnpm db:sandbox down, then up)"
|
|
export PGPASSWORD="$SB_PW"
|
|
}
|
|
# No password in the URL: the CLI reads PGPASSWORD, so no secret is on a command line.
|
|
db_url() { printf 'postgresql://postgres@127.0.0.1:%s/postgres?sslmode=disable' "$PORT_DB"; }
|
|
sb_psql() {
|
|
docker exec -i -e PGPASSWORD "$DB" \
|
|
psql -X -q -h 127.0.0.1 -U postgres -d postgres -v ON_ERROR_STOP=1 "$@"
|
|
}
|
|
wait_for() {
|
|
local what="$1"; shift
|
|
for _ in $(seq 1 120); do "$@" >/dev/null 2>&1 && return 0; sleep 1; done
|
|
die "timeout waiting for $what"
|
|
}
|
|
|
|
new_secrets() {
|
|
mkdir -p "$STATE"; chmod 700 "$STATE"
|
|
( umask 077; node -e '
|
|
const c = require("crypto");
|
|
const secret = c.randomBytes(32).toString("hex");
|
|
const b64 = (o) => Buffer.from(JSON.stringify(o)).toString("base64url");
|
|
const jwt = (p) => { const h = b64({ alg: "HS256", typ: "JWT" }), b = b64(p);
|
|
return h + "." + b + "." + c.createHmac("sha256", secret).update(h + "." + b).digest("base64url"); };
|
|
const exp = Math.floor(Date.now() / 1000) + 30 * 86400;
|
|
console.log("SB_PW=" + c.randomBytes(24).toString("hex"));
|
|
console.log("SB_JWT_SECRET=" + secret);
|
|
console.log("SB_ANON_KEY=" + jwt({ role: "anon", iss: "db-sandbox", exp }));
|
|
console.log("SB_SERVICE_KEY=" + jwt({ role: "service_role", iss: "db-sandbox", exp }));
|
|
' > "$SECRETS.new" && mv "$SECRETS.new" "$SECRETS" )
|
|
load_secrets
|
|
# The values reach the containers through 600 env files, never through argv.
|
|
( umask 077
|
|
printf 'POSTGRES_PASSWORD=%s\nPGPASSWORD=%s\nJWT_SECRET=%s\n' "$SB_PW" "$SB_PW" "$SB_JWT_SECRET" > "$STATE/db.env"
|
|
printf 'GOTRUE_DB_DATABASE_URL=postgresql://supabase_auth_admin:%s@%s:5432/postgres\nGOTRUE_JWT_SECRET=%s\n' \
|
|
"$SB_PW" "$DB" "$SB_JWT_SECRET" > "$STATE/auth.env"
|
|
printf 'ANON_KEY=%s\nSERVICE_KEY=%s\nAUTH_JWT_SECRET=%s\nDATABASE_URL=postgresql://supabase_storage_admin:%s@%s:5432/postgres\n' \
|
|
"$SB_ANON_KEY" "$SB_SERVICE_KEY" "$SB_JWT_SECRET" "$SB_PW" "$DB" > "$STATE/storage.env"
|
|
printf 'PGRST_DB_URI=postgresql://authenticator:%s@%s:5432/postgres\nPGRST_JWT_SECRET=%s\n' \
|
|
"$SB_PW" "$DB" "$SB_JWT_SECRET" > "$STATE/rest.env"
|
|
printf 'Authorization: Bearer %s\n' "$SB_SERVICE_KEY" > "$STATE/service-header" )
|
|
}
|
|
|
|
# The role passwords the CLI sets at db start (its /etc/postgresql.schema.sql),
|
|
# reduced to what the sandbox services use.
|
|
DB_INIT='cat <<'"'"'EOF'"'"' > /etc/postgresql.schema.sql && exec docker-entrypoint.sh postgres -D /etc/postgresql
|
|
\set pgpass `echo "$PGPASSWORD"`
|
|
\set jwt_secret `echo "$JWT_SECRET"`
|
|
ALTER DATABASE postgres SET "app.settings.jwt_secret" TO :'"'"'jwt_secret'"'"';
|
|
ALTER DATABASE postgres SET "app.settings.jwt_exp" TO '"'"'3600'"'"';
|
|
ALTER USER postgres WITH PASSWORD :'"'"'pgpass'"'"';
|
|
ALTER USER authenticator WITH PASSWORD :'"'"'pgpass'"'"';
|
|
ALTER USER supabase_auth_admin WITH PASSWORD :'"'"'pgpass'"'"';
|
|
ALTER USER supabase_storage_admin WITH PASSWORD :'"'"'pgpass'"'"';
|
|
EOF'
|
|
|
|
start_services() {
|
|
docker network create "$NET" >/dev/null
|
|
docker run -d --name "$DB" --network "$NET" -p 127.0.0.1:$PORT_DB:5432 \
|
|
--env-file "$STATE/db.env" -e POSTGRES_HOST=/var/run/postgresql -e JWT_EXP=3600 \
|
|
-e POSTGRES_INITDB_ARGS="--allow-group-access --locale-provider=icu --encoding=UTF-8 --icu-locale=en_US.UTF-8" \
|
|
--entrypoint sh "$PG_IMAGE" -c "$DB_INIT" >/dev/null
|
|
wait_for "database roles" docker exec -e PGPASSWORD "$DB" \
|
|
psql -h 127.0.0.1 -U supabase_auth_admin -d postgres -c 'select 1'
|
|
wait_for "database" docker exec -e PGPASSWORD "$DB" \
|
|
psql -h 127.0.0.1 -U postgres -d postgres -c 'select 1'
|
|
|
|
docker run -d --name "$MAIL" --network "$NET" -p 127.0.0.1:$PORT_MAIL:8025 "$MAIL_IMAGE" >/dev/null
|
|
docker run -d --name "$AUTH" --network "$NET" -p 127.0.0.1:$PORT_AUTH:9999 \
|
|
-e GOTRUE_API_HOST=0.0.0.0 -e GOTRUE_API_PORT=9999 -e API_EXTERNAL_URL=http://127.0.0.1:$PORT_AUTH \
|
|
-e GOTRUE_DB_DRIVER=postgres --env-file "$STATE/auth.env" \
|
|
-e GOTRUE_DB_MIGRATIONS_PATH=/usr/local/etc/auth/migrations \
|
|
-e GOTRUE_SITE_URL=http://127.0.0.1:5173 -e GOTRUE_DISABLE_SIGNUP=false \
|
|
-e GOTRUE_JWT_EXP=3600 \
|
|
-e GOTRUE_JWT_AUD=authenticated -e GOTRUE_JWT_DEFAULT_GROUP_NAME=authenticated \
|
|
-e GOTRUE_JWT_ADMIN_ROLES=service_role \
|
|
-e GOTRUE_EXTERNAL_EMAIL_ENABLED=true -e GOTRUE_EXTERNAL_PHONE_ENABLED=false \
|
|
-e GOTRUE_EXTERNAL_ANONYMOUS_USERS_ENABLED=false -e GOTRUE_MAILER_AUTOCONFIRM=false \
|
|
-e GOTRUE_MAILER_URLPATHS_CONFIRMATION=http://127.0.0.1:$PORT_AUTH/verify \
|
|
-e GOTRUE_SMTP_HOST="$MAIL" -e GOTRUE_SMTP_PORT=1025 \
|
|
-e GOTRUE_SMTP_ADMIN_EMAIL=admin@sandbox.local -e GOTRUE_SMTP_SENDER_NAME=sandbox \
|
|
-e GOTRUE_SMTP_MAX_FREQUENCY=1s -e GOTRUE_RATE_LIMIT_EMAIL_SENT=10000 \
|
|
-e GOTRUE_RATE_LIMIT_VERIFY=10000 -e GOTRUE_RATE_LIMIT_TOKEN_REFRESH=10000 \
|
|
-e GOTRUE_PASSWORD_MIN_LENGTH=12 "$AUTH_IMAGE" >/dev/null
|
|
wait_for "auth" curl -fsS http://127.0.0.1:$PORT_AUTH/health
|
|
docker run -d --name "$STORAGE" --network "$NET" -p 127.0.0.1:$PORT_STORAGE:5000 \
|
|
--env-file "$STATE/storage.env" \
|
|
-e FILE_SIZE_LIMIT=52428800 -e STORAGE_BACKEND=file -e FILE_STORAGE_BACKEND_PATH=/mnt \
|
|
-e TENANT_ID=stub -e STORAGE_S3_REGION=local -e GLOBAL_S3_BUCKET=stub \
|
|
-e ENABLE_IMAGE_TRANSFORMATION=false -e IMAGE_TRANSFORMATION_ENABLED=false \
|
|
-e S3_PROTOCOL_ENABLED=false -e DB_MIGRATIONS_FREEZE_AT= "$STORAGE_IMAGE" >/dev/null
|
|
wait_for "storage" curl -fsS http://127.0.0.1:$PORT_STORAGE/status
|
|
}
|
|
|
|
start_rest() {
|
|
docker run -d --name "$REST" --network "$NET" -p 127.0.0.1:$PORT_REST:3000 \
|
|
--env-file "$STATE/rest.env" \
|
|
-e PGRST_DB_SCHEMAS=public -e PGRST_DB_EXTRA_SEARCH_PATH=public,extensions \
|
|
-e PGRST_DB_ANON_ROLE=anon -e PGRST_DB_MAX_ROWS=1000 \
|
|
"$REST_IMAGE" >/dev/null
|
|
wait_for "postgrest" curl -fsS -o /dev/null http://127.0.0.1:$PORT_REST/
|
|
}
|
|
|
|
# The CLI's own migration runner, always against the sandbox URL.
|
|
migrate_up() { # workdir
|
|
"$CLI" --workdir "$1" migration up --db-url "$(db_url)"
|
|
}
|
|
|
|
seed() {
|
|
local paths
|
|
paths="$(sed -n 's/^sql_paths = \[\(.*\)\]/\1/p' supabase/config.toml | tr -d '" ' | tr ',' ' ')"
|
|
for f in $paths; do
|
|
echo " seed ${f#./}"; sb_psql --single-transaction < "supabase/${f#./}" >/dev/null
|
|
done
|
|
}
|
|
|
|
gotrue_accounts() {
|
|
curl -fsS -X POST http://127.0.0.1:$PORT_AUTH/admin/users \
|
|
-H @"$STATE/service-header" -H 'Content-Type: application/json' \
|
|
-d '{"email":"pre-confirmed@sandbox.local","password":"Sandbox-only-pw-1","email_confirm":true}' >/dev/null
|
|
curl -fsS -X POST http://127.0.0.1:$PORT_AUTH/signup -H 'Content-Type: application/json' \
|
|
-d '{"email":"pre-unconfirmed@sandbox.local","password":"Sandbox-only-pw-1"}' >/dev/null
|
|
}
|
|
|
|
cmd_up() {
|
|
local mode="${1:-}"
|
|
# Arguments checked before anything starts: a refusal leaves no container or secret.
|
|
case "$mode" in
|
|
fresh) ;;
|
|
upgrade) [[ "${2:-}" =~ ^[0-9]{14}$ ]] \
|
|
|| die "up upgrade needs the first new migration version, e.g. 20261025100000" ;;
|
|
*) die "up fresh | up upgrade <version>" ;;
|
|
esac
|
|
docker ps -a --format '{{.Names}}' | grep -q '^faunosteo_dbsandbox_' && die "already up (pnpm db:sandbox down)"
|
|
new_secrets
|
|
start_services
|
|
case "$mode" in
|
|
fresh)
|
|
migrate_up . ; seed ;;
|
|
upgrade)
|
|
local version="$2"
|
|
# A CLI workdir holding only the migrations before <version>; kept in the
|
|
# state folder and refilled at each upgrade run (the CLI writes .temp there).
|
|
local base="$STATE/upgrade-base"
|
|
mkdir -p "$base/supabase/migrations"
|
|
rm -f "$base"/supabase/migrations/*.sql
|
|
cp supabase/config.toml "$base/supabase/"
|
|
for f in supabase/migrations/*.sql; do
|
|
if [ "$(basename "$f" | cut -d_ -f1)" \< "$version" ]; then cp "$f" "$base/supabase/migrations/"; fi
|
|
done
|
|
migrate_up "$base"
|
|
seed; gotrue_accounts
|
|
echo "== before: $(sb_psql -t -A -c 'select count(*) from public.profiles') profiles"
|
|
migrate_up . ;;
|
|
esac
|
|
start_rest
|
|
echo "== sandbox up ($mode): db 127.0.0.1:$PORT_DB, auth :$PORT_AUTH, rest :$PORT_REST, mail :$PORT_MAIL"
|
|
}
|
|
|
|
cmd_down() {
|
|
docker rm -f "$REST" "$STORAGE" "$AUTH" "$MAIL" "$DB" >/dev/null 2>&1 || true
|
|
docker network rm "$NET" >/dev/null 2>&1 || true
|
|
rm -f "$SECRETS" "$STATE/db.env" "$STATE/auth.env" "$STATE/storage.env" "$STATE/rest.env" "$STATE/service-header"
|
|
echo "== sandbox removed"
|
|
}
|
|
|
|
# A test runner gets the endpoints and keys through its environment, never argv.
|
|
with_sandbox_env() (
|
|
export SANDBOX_DB_CONTAINER="$DB" SANDBOX_PG_PASSWORD="$SB_PW"
|
|
export SANDBOX_AUTH_URL="http://127.0.0.1:$PORT_AUTH" SANDBOX_REST_URL="http://127.0.0.1:$PORT_REST"
|
|
export SANDBOX_STORAGE_URL="http://127.0.0.1:$PORT_STORAGE" SANDBOX_MAIL_URL="http://127.0.0.1:$PORT_MAIL"
|
|
export SANDBOX_ANON_KEY="$SB_ANON_KEY" SANDBOX_SERVICE_KEY="$SB_SERVICE_KEY"
|
|
"$@"
|
|
)
|
|
|
|
case "${1:-}" in
|
|
up) shift; cmd_up "$@" ;;
|
|
down) cmd_down ;;
|
|
test-db) load_secrets; "$CLI" test db --db-url "$(db_url)" ;;
|
|
types)
|
|
load_secrets
|
|
out="$("$CLI" gen types typescript --db-url "$(db_url)" --schema public | node_modules/.bin/prettier --parser typescript)"
|
|
if [ "${2:-}" = --check ]; then
|
|
diff <(printf '%s\n' "$out") packages/supabase/src/database.types.ts && echo "== types match"
|
|
else
|
|
printf '%s\n' "$out" > packages/supabase/src/database.types.ts; echo "== types written"
|
|
fi ;;
|
|
test) load_secrets; with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/supabase test:sandbox ;;
|
|
e2e-shell) load_secrets; with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/app e2e:shell ;;
|
|
sql) load_secrets; sb_psql ;;
|
|
*) sed -n '2,24p' "$0"; exit 1 ;;
|
|
esac
|