shoc-backend/scripts/smoke-elastic-beanstalk.sh
2026-07-27 19:26:07 -03:00

60 lines
2.3 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# smoke-elastic-beanstalk.sh — post-deploy smoke checks for the shoc-backend
# dev environment.
#
# Checks:
# 1. swagger.json is reachable (HTTP 200)
# 2. swagger advertises POST /api/webhooks/work-orders
# 3. swagger still advertises POST /api/Authentication/login
# 4. an unauthenticated JSON POST to the webhook returns 401 or 503 (disabled),
# never 404 or a server error other than the intentional 503
#
# Usage:
# bash scripts/smoke-elastic-beanstalk.sh [base-url]
# bash scripts/smoke-elastic-beanstalk.sh https://api.dev.seahaven.com
set -euo pipefail
BASE_URL="${1:-https://api.dev.seahaven.com}"
BASE_URL="${BASE_URL%/}"
SWAGGER_URL="$BASE_URL/swagger/v1/swagger.json"
WEBHOOK_URL="$BASE_URL/api/webhooks/work-orders"
log() { printf '\n\033[1m== %s ==\033[0m\n' "$1"; }
ok() { printf '\033[32mPASS\033[0m %s\n' "$1"; }
die() { printf '\033[31mFAIL\033[0m %s\n' "$1" >&2; exit 1; }
command -v curl >/dev/null 2>&1 || die "curl is required."
mkdir -p .artifacts/elastic-beanstalk
log "swagger reachable: $SWAGGER_URL"
swagger_http=$(curl -sS -o .artifacts/elastic-beanstalk/swagger.json \
-w '%{http_code}' --max-time 30 "$SWAGGER_URL" || true)
[[ "$swagger_http" == "200" ]] \
|| die "swagger.json returned HTTP $swagger_http (expected 200)."
swagger_file=".artifacts/elastic-beanstalk/swagger.json"
ok "swagger.json HTTP 200"
log "swagger advertises webhook and login routes"
grep -q '"/api/webhooks/work-orders"' "$swagger_file" \
|| die "swagger.json missing /api/webhooks/work-orders route."
grep -q '"/api/Authentication/login"' "$swagger_file" \
|| die "swagger.json missing /api/Authentication/login route."
ok "webhook and login routes present"
log "unauthenticated webhook POST: $WEBHOOK_URL"
webhook_http=$(curl -sS -o /dev/null -w '%{http_code}' --max-time 30 \
-X POST -H 'Content-Type: application/json' \
--data '{"smoke":true}' "$WEBHOOK_URL" || true)
case "$webhook_http" in
401) ok "webhook returned 401 (unauthorized) as expected." ;;
503) ok "webhook returned 503 (intentionally disabled) as expected." ;;
404) die "webhook returned 404 — route not wired (deployment broken)." ;;
5*) die "webhook returned HTTP $webhook_http — unexpected server error." ;;
*) die "webhook returned HTTP $webhook_http — expected 401 or 503." ;;
esac
log "smoke: all checks passed"