#!/usr/bin/env bats # crosscode demo (lib/manage.sh): argument checks, then backup → demo data as the site's user → # reload, in that order, and nothing after a step that fails. setup() { export CROSSCODE_NO_UPDATE=1 CC="$BATS_TEST_DIRNAME/../crosscode" TMP="$BATS_TEST_TMPDIR" CALLS="$TMP/calls" : > "$CALLS" # shellcheck source=../lib/common.sh source "$BATS_TEST_DIRNAME/../lib/common.sh" # shellcheck source=../lib/manage.sh source "$BATS_TEST_DIRNAME/../lib/manage.sh" # A church with Events, and Events' hooks recording what they'd do. export CROSSCODE_HOME="$TMP/home" mkdir -p "$CROSSCODE_HOME/products" "$TMP/sites/hope/events/apps/events/backend/scripts" touch "$TMP/sites/hope/events/apps/events/backend/scripts/set-demo.js" cat > "$CROSSCODE_HOME/products/events.sh" <> "$CALLS"; [ -z "\${FAIL_BACKUP:-}" ]; } events_reload() { echo "reload \$2" >> "$CALLS"; } events_health_url() { echo http://127.0.0.1:20001/health; } events_expected_version() { echo 2.4.1; } EOF require_root() { :; } load_church() { [ "$1" = hope ] || die "There's no church $1 here."; } church_has_product() { [ "$1" = events ]; } pvar() { case "$2" in DIR) echo "$TMP/sites/hope/events" ;; USER) echo cc-hope ;; esac; } checkout_ref() { echo events@2.4.1; } wait_for_version() { echo "wait $2" >> "$CALLS"; } run_as() { local user="$1" dir="$2"; shift 2; echo "run $user ${dir#"$TMP"/} $*" >> "$CALLS"; [ -z "${FAIL_SEED:-}" ]; } } @test "the profile and admin email are checked before anything else" { CROSSCODE_HOME= run bash "$CC" demo hope [ "$status" -eq 1 ] [[ "$output" == *"--profile: one of small events ministry multicampus"* ]] CROSSCODE_HOME= run bash "$CC" demo hope --profile everything [ "$status" -eq 1 ] CROSSCODE_HOME= run bash "$CC" demo hope --profile small --admin-email 'x; rm -rf /' [ "$status" -eq 1 ] [[ "$output" == *"not an email address"* ]] CROSSCODE_HOME= run bash "$CC" help demo [[ "$output" == *"crosscode demo --profile"* ]] } @test "backup, then the demo as the site's user (no prompt), then a reload" { run cmd_demo hope --profile ministry [ "$status" -eq 0 ] [ "$(cat "$CALLS")" = "backup cc-hope run cc-hope sites/hope/events/apps/events/backend env node scripts/set-demo.js --profile=ministry --yes reload cc-hope wait 2.4.1" ] } @test "the admin to keep goes in DEMO_ADMIN_EMAIL (older releases only read that)" { run cmd_demo hope --profile small --admin-email jane@hope.test [ "$status" -eq 0 ] grep -qx 'run cc-hope sites/hope/events/apps/events/backend env DEMO_ADMIN_EMAIL=jane@hope.test node scripts/set-demo.js --profile=small --yes' "$CALLS" } @test "a failed backup stops it before the data is touched" { FAIL_BACKUP=1 run cmd_demo hope --profile small [ "$status" -eq 1 ] [[ "$output" == *"The backup failed"* ]] [ "$(cat "$CALLS")" = "backup cc-hope" ] } @test "a failed demo isn't followed by a reload" { FAIL_SEED=1 run cmd_demo hope --profile small [ "$status" -eq 1 ] ! grep -q reload "$CALLS" } @test "a release without the demo script is refused" { rm "$TMP/sites/hope/events/apps/events/backend/scripts/set-demo.js" run cmd_demo hope --profile small [ "$status" -eq 1 ] [[ "$output" == *"has no demo data"* ]] [ ! -s "$CALLS" ] }