#!/usr/bin/env bats # Reading and changing .env values for the Hub (lib/envedit.cjs, `crosscode env`, contracts 1.12.0): # secrets are never printed, edits keep the rest of the file, secrets and generated values are refused. setup() { TMP="$BATS_TEST_TMPDIR" JS="$BATS_TEST_DIRNAME/../lib/envedit.cjs" APP="$TMP/app" mkdir -p "$APP/backend" "$APP/frontend" cat > "$TMP/template.json" <<'EOF' { "schema": "crosscode.env-template/1", "product": "events", "files": { "backend/.env": [ { "name": "NODE_ENV", "layer": "generated", "required": true }, { "name": "DATABASE_URL", "layer": "generated", "required": true }, { "name": "JWT_SECRET", "layer": "generated", "required": true }, { "name": "FRONTEND_URL", "layer": "install", "from": "https://{domain}", "required": true, "description": "The site's address" }, { "name": "TRUST_PROXY", "layer": "server", "required": true }, { "name": "SENTRY_DSN", "layer": "defaults" } ], "frontend/.env.production": [ { "name": "NEXT_PUBLIC_API_URL", "layer": "install", "from": "https://{domain}", "required": true } ] } } EOF cat > "$APP/backend/.env" <<'EOF' # Written by crosscode NODE_ENV=production DATABASE_URL=postgresql://hope:s3cr3t@localhost:5432/hope JWT_SECRET=abcdef123456 FRONTEND_URL=https://events.hope.co.za TRUST_PROXY=loopback SMTP_PASS="pa ss" ORG_NAME="Hope Church" EOF chmod 640 "$APP/backend/.env" printf 'NEXT_PUBLIC_API_URL=https://events.hope.co.za\n' > "$APP/frontend/.env.production" } edit() { node "$JS" "$@" --template "$TMP/template.json" --dir "$APP"; } @test "read: values in file order, secrets only as set or not, template values the file lacks" { run edit read --product events [ "$status" -eq 0 ] [[ "$output" != *"s3cr3t"* && "$output" != *"abcdef"* && "$output" != *"pa ss"* ]] node -e ' const r = JSON.parse(process.argv[1]); const be = r.files.find(f => f.file === "backend/.env"); const v = Object.fromEntries(be.variables.map(x => [x.name, x])); const ok = r.product === "events" && be.exists && v.FRONTEND_URL.value === "https://events.hope.co.za" && v.FRONTEND_URL.editable && v.FRONTEND_URL.description === "The site'"'"'s address" && v.ORG_NAME.value === "Hope Church" && v.ORG_NAME.layer === null && v.ORG_NAME.editable && v.DATABASE_URL.value === null && v.DATABASE_URL.secret && v.DATABASE_URL.set && !v.DATABASE_URL.editable && v.SMTP_PASS.value === null && v.SMTP_PASS.secret && v.NODE_ENV.value === "production" && !v.NODE_ENV.editable && v.SENTRY_DSN.set === false && v.SENTRY_DSN.secret && r.files.find(f => f.file === "frontend/.env.production").variables[0].value === "https://events.hope.co.za"; process.exit(ok ? 0 : 1);' "$output" } @test "set: changes lines where they are, appends new ones, removes, keeps the mode and a copy" { run edit set --file backend/.env --backup "$TMP/copy" --set FRONTEND_URL=https://new.hope.co.za --set ORG_NAME="Hope & Co" --set NEW_FLAG=false --unset TRUST_PROXY [ "$status" -eq 0 ] [[ "$output" == *'"changed":["FRONTEND_URL","ORG_NAME"]'* && "$output" == *'"added":["NEW_FLAG"]'* && "$output" == *'"removed":["TRUST_PROXY"]'* ]] [ "$(sed -n 5p "$APP/backend/.env")" = "FRONTEND_URL=https://new.hope.co.za" ] grep -qx 'ORG_NAME="Hope & Co"' "$APP/backend/.env" grep -qx 'NEW_FLAG=false' "$APP/backend/.env" ! grep -q '^TRUST_PROXY=' "$APP/backend/.env" grep -qx 'JWT_SECRET=abcdef123456' "$APP/backend/.env" [ "$(uname -s)" != Linux ] || [ "$(stat -c %a "$APP/backend/.env")" = 640 ] grep -qx 'FRONTEND_URL=https://events.hope.co.za' "$TMP/copy" } @test "set: refuses secrets, generated values and bad values, changing nothing" { cp "$APP/backend/.env" "$TMP/before" run edit set --file backend/.env --set JWT_SECRET=x [ "$status" -eq 3 ] run edit set --file backend/.env --set NODE_ENV=development [ "$status" -eq 3 ] run edit set --file backend/.env --unset SMTP_PASS [ "$status" -eq 3 ] run edit set --file backend/.env --set TRUST_PROXY=TRUE [ "$status" -eq 4 ] run edit set --file ../.env --set A=1 [ "$status" -eq 2 ] cmp "$TMP/before" "$APP/backend/.env" } @test "restore: puts the copy back" { edit set --file backend/.env --backup "$TMP/copy" --set FRONTEND_URL=https://new.hope.co.za node "$JS" restore --file "$APP/backend/.env" --backup "$TMP/copy" grep -qx 'FRONTEND_URL=https://events.hope.co.za' "$APP/backend/.env" }