68 lines
2.1 KiB
Bash
Executable File
68 lines
2.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
|
|
SCRIPT="$ROOT_DIR/scripts/acceptance/verify_user_key_self_service.sh"
|
|
|
|
fail() {
|
|
echo "FAIL: $*" >&2
|
|
exit 1
|
|
}
|
|
|
|
assert_contains() {
|
|
local haystack="$1"
|
|
local needle="$2"
|
|
if [[ "$haystack" != *"$needle"* ]]; then
|
|
fail "expected to find [$needle] in [$haystack]"
|
|
fi
|
|
}
|
|
|
|
[[ -f "$SCRIPT" ]] || fail "missing $SCRIPT"
|
|
|
|
help_output="$(bash "$SCRIPT" --help)"
|
|
assert_contains "$help_output" "verify_user_key_self_service.sh"
|
|
assert_contains "$help_output" "--env-check"
|
|
assert_contains "$help_output" "--run"
|
|
assert_contains "$help_output" "USER_CHAT_BASE"
|
|
|
|
tmpdir="$(mktemp -d)"
|
|
trap 'rm -rf "$tmpdir"' EXIT
|
|
fakebin="$tmpdir/bin"
|
|
mkdir -p "$fakebin"
|
|
cat > "$fakebin/curl" <<'EOF'
|
|
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
url="${@: -1}"
|
|
case "$url" in
|
|
https://crm.example.com/healthz)
|
|
printf 'ok'
|
|
;;
|
|
https://chat.example.com/healthz)
|
|
printf '<!doctype html><html lang="zh-CN"><head><title>Sub2API</title></head><body>ok</body></html>'
|
|
;;
|
|
*)
|
|
printf '{}'
|
|
;;
|
|
esac
|
|
EOF
|
|
chmod +x "$fakebin/curl"
|
|
|
|
env_output="$(PATH="$fakebin:$PATH" ARTIFACT_DIR="$tmpdir/artifacts" CRM_BASE="https://crm.example.com" USER_CHAT_BASE="https://chat.example.com" bash "$SCRIPT" --env-check)"
|
|
assert_contains "$env_output" "CRM healthz=ok"
|
|
assert_contains "$env_output" "env summary"
|
|
[[ -f "$tmpdir/artifacts/00-env.json" ]] || fail "missing env summary json"
|
|
summary_text="$(cat "$tmpdir/artifacts/00-env.json")"
|
|
assert_contains "$summary_text" '"crm_health": "ok"'
|
|
assert_contains "$summary_text" '"user_chat_health": "<!doctype html><html lang=\"zh-CN\"'
|
|
|
|
set +e
|
|
missing_auth_output="$(PATH="$fakebin:$PATH" ARTIFACT_DIR="$tmpdir/run-no-auth" CRM_BASE="https://crm.example.com" USER_CHAT_BASE="https://chat.example.com" bash "$SCRIPT" --run 2>&1)"
|
|
missing_auth_status=$?
|
|
set -e
|
|
if [[ $missing_auth_status -eq 0 ]]; then
|
|
fail "expected --run without auth to fail"
|
|
fi
|
|
assert_contains "$missing_auth_output" "set USER_SUBJECT_ID or USER_AUTH_TOKEN"
|
|
|
|
echo "PASS: user key self-service script regression checks"
|