AkurAI Build
Menu

Public repository / 57bd5b278e2d4096a4ce6e5d1127a4e06f16b066

AkurAI-Framework / tests/test_validate.sh

Latest change 6dcfea4319fde02bfdcdbd5c872f5ee323b2cf65 - Checkpoint WIP + add AkurAI Build CI/CD pipeline by Ólafur Búi Ólafsson

#!/usr/bin/env bash
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
TMP="$(mktemp -d)"
cleanup() {
  status=$?
  if [ "$status" -ne 0 ]; then
    cat "$TMP/pass.out" "$TMP/fail.out" "$TMP/curl.log" 2>/dev/null || true
  fi
  rm -rf "$TMP" /tmp/validator-contract_cookies
}
trap cleanup EXIT
mkdir -p "$TMP/bin"

cat > "$TMP/bin/systemctl" <<'EOF'
#!/usr/bin/env bash
[ "$1" = is-active ]
EOF

cat > "$TMP/bin/curl" <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
printf '%s\n' "$*" >> "${FAKE_CURL_LOG:?}"
url="${*: -1}"
outfile=""
write_format=""
cookie_out=""
have_data=0
method=GET
args=("$@")
for ((i=0; i<${#args[@]}; i++)); do
  case "${args[$i]}" in
    -o) outfile="${args[$((i+1))]}" ;;
    -w) write_format="${args[$((i+1))]}" ;;
    -c) cookie_out="${args[$((i+1))]}" ;;
    -d) have_data=1 ;;
    -X) method="${args[$((i+1))]}" ;;
  esac
done
[ -z "$cookie_out" ] || printf 'session=test\n' > "$cookie_out"
emit() { [ "$outfile" = /dev/null ] || printf '%s' "$1"; }
case "$url" in
  */api/health)
    if [ "${FAKE_BAD_HEALTH:-0}" = 1 ]; then emit '{"app":"wrong","status":"wat"}';
    else emit '{"app":"validator-contract","version":"1.0.0","status":"ok"}'; fi
    ;;
  */login)
    if [ "$have_data" = 1 ]; then emit $'HTTP/1.1 303 See Other\r\nSet-Cookie: session=test; HttpOnly\r\n\r\n';
    else emit '<form>login password session</form>'; fi
    ;;
  */account)
    [ -f /tmp/validator-contract_cookies ] || exit 22
    emit '<h1>Account</h1>'
    ;;
  */logout)
    [ "$method" = POST ] || exit 22
    if [ -n "$write_format" ]; then printf '303'; fi
    ;;
  */) emit '<title>AkurAI Framework</title>' ;;
  *) exit 22 ;;
esac
EOF
chmod 755 "$TMP/bin/systemctl" "$TMP/bin/curl"

export PATH="$TMP/bin:$PATH" FAKE_CURL_LOG="$TMP/curl.log"
DOMAIN=validator.test PORT=9999 APP_NAME=validator-contract "$ROOT/scripts/validate.sh" > "$TMP/pass.out"
grep -F 'Pass: 10' "$TMP/pass.out" >/dev/null
grep -F 'Fail: 0' "$TMP/pass.out" >/dev/null
grep -F 'https://validator.test/account' "$TMP/curl.log" >/dev/null
grep -F -- '-X POST' "$TMP/curl.log" | grep -F 'https://validator.test/logout' >/dev/null

set +e
FAKE_BAD_HEALTH=1 DOMAIN=validator.test PORT=9999 APP_NAME=validator-contract \
  "$ROOT/scripts/validate.sh" > "$TMP/fail.out"
status=$?
set -e
[ "$status" -ne 0 ]
grep -F 'health JSON schema invalid' "$TMP/fail.out" >/dev/null
grep -F "health app='wrong'" "$TMP/fail.out" >/dev/null
grep -F 'Pass:' "$TMP/fail.out" >/dev/null
grep -F 'Fail:' "$TMP/fail.out" >/dev/null

printf 'validator contract tests: PASS\n'