#!/usr/bin/env bash # Phase 1 API checks: authentication, tenant isolation (AC-06), namespace permissions, memory lifecycle, # idempotency, sessions/checkpoints (AC-09, AC-10) and persistence across a restart (AC-01). # # Needs the container from docker-compose.yml running (name "axonasp", http://localhost:8801), curl, and Git Bash/bash. # It provisions two throw-away tenants and deletes them at the end (even on failure). # bash tests/phase1-api.sh set -u BASE=${BASE:-http://localhost:8801/api/v1} PASS=0; FAIL=0; FAILED=() TENANTS=() prov() { # prov [arg1 arg2 arg3 arg4] MSYS_NO_PATHCONV=1 docker exec -u axonasp -e SERVER_WEB_ROOT=/opt/axonasp/site \ -e "PROV_ACTION=$1" -e "PROV_ARG1=${2:-}" -e "PROV_ARG2=${3:-}" -e "PROV_ARG3=${4:-}" -e "PROV_ARG4=${5:-}" \ axonasp ./axonasp-cli -r /opt/axonasp/site/scripts/provision.asp 2>&1 | tr -d '\r' } field() { sed -n "s/^$1: *\([^ ]*\).*/\1/p" | head -1; } # field from provision output has() { grep -q -- "$1" <<<"$2" && echo yes || echo no; } jget() { sed -n "s/.*\"$1\":\"\([^\"]*\)\".*/\1/p" | head -1; } # first "name":"value" in a JSON text cleanup() { for t in "${TENANTS[@]}"; do prov purge "$t" >/dev/null; done; } trap cleanup EXIT check() { # check if [ "$2" = "$3" ]; then PASS=$((PASS+1)); else FAIL=$((FAIL+1)); FAILED+=("$1 (expected $2, got $3)"); fi } code() { # code [curl args...] -> HTTP status local key=$1 m=$2 p=$3; shift 3 curl -s -o /dev/null -w '%{http_code}' -X "$m" -H "Authorization: Bearer $key" "$@" "$BASE$p" } body() { local key=$1 m=$2 p=$3; shift 3; curl -s -X "$m" -H "Authorization: Bearer $key" "$@" "$BASE$p"; } # ---- setup ----------------------------------------------------------------------------------------------- TA=$(prov tenant "test-A" | field tenant_id); TB=$(prov tenant "test-B" | field tenant_id); TENANTS=("$TA" "$TB") mk() { # mk -> sets P_ID and P_KEY local out; out=$(prov principal "$1" "$2" "$3"); P_ID=$(echo "$out" | field principal_id); P_KEY=$(echo "$out" | field api_key) } mk "$TA" writer agent; WID=$P_ID; WK=$P_KEY mk "$TA" reader agent; RID=$P_ID; RK=$P_KEY mk "$TA" other agent; OID=$P_ID; OK=$P_KEY mk "$TA" nobody agent; NID=$P_ID; NK=$P_KEY mk "$TA" sub agent; SID_=$P_ID; SK=$P_KEY mk "$TA" noinh agent; XID=$P_ID; XK=$P_KEY mk "$TB" writerB agent; BID=$P_ID; BK=$P_KEY prov grant "$WID" /proj "read,write,delete" 1 >/dev/null prov grant "$RID" /proj "read" 1 >/dev/null prov grant "$OID" /other "read,write" 1 >/dev/null prov grant "$SID_" /proj/sub "read,write" 1 >/dev/null prov grant "$XID" /proj "read,write" 0 >/dev/null prov grant "$BID" /proj "read,write,delete" 1 >/dev/null NSA=$(prov namespace "$TA" /proj | field namespace_id); NSSUB=$(prov namespace "$TA" /proj/sub | field namespace_id) NSB=$(prov namespace "$TB" /proj | field namespace_id) [ -n "$NSA" ] && [ -n "$NSB" ] && [ -n "$WK" ] || { echo "setup failed"; exit 2; } mem() { printf '{"namespace_id":"%s","type":"semantic","title":"%s","content":"%s"}' "$1" "$2" "$3"; } # ---- first request after provisioning is a write (cold-start habit) --------------------------------------- R=$(body "$WK" POST /remember -d "$(mem "$NSA" "Fact one" "The sky is blue")") M1=$(echo "$R" | jget id) check "writer remember -> 201 with id" "yes" "$([ -n "$M1" ] && echo yes || echo no)" check "remember 201" "201" "$(code "$WK" POST /remember -d "$(mem "$NSA" "Fact two" "Grass is green")")" # ---- authentication --------------------------------------------------------------------------------------- check "no auth header -> 401" "401" "$(curl -s -o /dev/null -w '%{http_code}' "$BASE/memories")" check "bad key -> 401" "401" "$(code amk_00000000000000000000000000000000 GET /memories)" check "short key -> 401" "401" "$(code abc GET /memories)" # ---- isolation between tenants --------------------------------------------------------------------------- check "B fetch A memory -> 404" "404" "$(code "$BK" GET /memories/$M1)" check "B versions of A memory -> 404" "404" "$(code "$BK" GET /memories/$M1/versions)" check "B correct A memory -> 404" "404" "$(code "$BK" PATCH /memories/$M1 -H 'If-Match: 1' -d '{"content":"hack"}')" check "B retire A memory -> 404" "404" "$(code "$BK" POST /memories/$M1/retire)" check "B forget A memory -> 404" "404" "$(code "$BK" DELETE /memories/$M1)" check "B remember into A namespace -> 404" "404" "$(code "$BK" POST /remember -d "$(mem "$NSA" x y)")" check "B list with A namespace_id -> 404" "404" "$(code "$BK" GET "/memories?namespace_id=$NSA")" check "B list sees nothing of A" '{"data":[],"next_cursor":null}' "$(body "$BK" GET /memories)" check "A memory still intact after B attempts" "200" "$(code "$WK" GET /memories/$M1)" # ---- permissions inside a tenant ------------------------------------------------------------------------- check "reader fetch -> 200" "200" "$(code "$RK" GET /memories/$M1)" check "reader list -> has data" "yes" "$(body "$RK" GET /memories | grep -q "$M1" && echo yes || echo no)" check "reader remember -> 404" "404" "$(code "$RK" POST /remember -d "$(mem "$NSA" x y)")" check "reader correct -> 404" "404" "$(code "$RK" PATCH /memories/$M1 -H 'If-Match: 1' -d '{"content":"no"}')" check "reader forget -> 404" "404" "$(code "$RK" DELETE /memories/$M1)" check "other-namespace principal fetch -> 404" "404" "$(code "$OK" GET /memories/$M1)" check "no-grant principal fetch -> 404" "404" "$(code "$NK" GET /memories/$M1)" check "no-grant principal list -> empty" '{"data":[],"next_cursor":null}' "$(body "$NK" GET /memories)" check "writer without delete grant semantics: writer has delete" "200" "$(code "$WK" POST /memories/$M1/retire)" # ---- namespace inheritance ------------------------------------------------------------------------------- check "writer (inherit) remember in /proj/sub -> 201" "201" "$(code "$WK" POST /remember -d "$(mem "$NSSUB" "Sub fact" "in sub")")" check "sub-only principal remember in /proj/sub -> 201" "201" "$(code "$SK" POST /remember -d "$(mem "$NSSUB" "Sub fact 2" "in sub too")")" check "sub-only principal remember in /proj -> 404" "404" "$(code "$SK" POST /remember -d "$(mem "$NSA" x y)")" check "non-inherit grant on /proj: remember in /proj -> 201" "201" "$(code "$XK" POST /remember -d "$(mem "$NSA" "Direct only" "direct")")" check "non-inherit grant on /proj: remember in /proj/sub -> 404" "404" "$(code "$XK" POST /remember -d "$(mem "$NSSUB" x y)")" # ---- validation / limits --------------------------------------------------------------------------------- check "unknown type -> 422" "422" "$(code "$WK" POST /remember -d "{\"namespace_id\":\"$NSA\",\"type\":\"nope\",\"title\":\"t\",\"content\":\"c\"}")" check "missing fields -> 422" "422" "$(code "$WK" POST /remember -d '{}')" check "bad json -> 400" "400" "$(code "$WK" POST /remember -d '{bad')" check "empty body -> 400" "400" "$(code "$WK" POST /remember)" check "malformed id -> 404" "404" "$(code "$WK" GET "/memories/1;DROP")" check "sql-ish namespace_id -> 404" "404" "$(code "$WK" GET "/memories?namespace_id=x'%20OR%20'1'='1")" # ---- lifecycle: correct, versions, forget ----------------------------------------------------------------- R=$(body "$WK" POST /remember -d "$(mem "$NSA" "Lifecycle" "first")"); M2=$(echo "$R" | jget id) check "correct w/o If-Match -> 428" "428" "$(code "$WK" PATCH /memories/$M2 -d '{"content":"second"}')" check "correct stale -> 412" "412" "$(code "$WK" PATCH /memories/$M2 -H 'If-Match: 5' -d '{"content":"second"}')" check "correct ok -> 200" "200" "$(code "$WK" PATCH /memories/$M2 -H 'If-Match: "1"' -d '{"content":"second"}')" check "second correct with old revision -> 412" "412" "$(code "$WK" PATCH /memories/$M2 -H 'If-Match: 1' -d '{"content":"third"}')" check "current revision is 2" "2" "$(body "$WK" GET /memories/$M2 | sed -n 's/.*"revision":\([0-9]*\).*/\1/p')" check "old revision readable" "yes" "$(body "$WK" GET "/memories/$M2?revision=1" | grep -q '"content":"first"' && echo yes || echo no)" check "versions list has both" "2" "$(body "$WK" GET /memories/$M2/versions | grep -o '"revision":' | wc -l | tr -d ' ')" check "corrected revision is unverified" "yes" "$(body "$WK" GET /memories/$M2 | grep -q '"verification_status":"unverified"' && echo yes || echo no)" check "forget by principal without delete -> 404" "404" "$(code "$XK" DELETE /memories/$M2)" check "forget -> 200" "200" "$(code "$WK" DELETE /memories/$M2)" check "fetch after forget -> 404" "404" "$(code "$WK" GET /memories/$M2)" check "versions after forget -> 404" "404" "$(code "$WK" GET /memories/$M2/versions)" # ---- duplicates and idempotency -------------------------------------------------------------------------- D1=$(body "$WK" POST /remember -d "$(mem "$NSA" "Dup" "same")" | jget id) D2=$(body "$WK" POST /remember -d "$(mem "$NSA" "Dup" "same")") check "exact duplicate returns same id" "$D1" "$(echo "$D2" | jget id)" check "exact duplicate flagged" "yes" "$(echo "$D2" | grep -q '"duplicate":true' && echo yes || echo no)" I1=$(body "$WK" POST /remember -H 'Idempotency-Key: idem-1' -d "$(mem "$NSA" "Idem" "once")" | jget id) I2=$(body "$WK" POST /remember -H 'Idempotency-Key: idem-1' -d "$(mem "$NSA" "Idem" "once")" | jget id) check "idempotent replay returns original id" "$I1" "$I2" check "idempotency key reuse with other body -> 409" "409" "$(code "$WK" POST /remember -H 'Idempotency-Key: idem-1' -d "$(mem "$NSA" "Idem" "changed")")" check "same idempotency key in another tenant is independent" "201" "$(code "$BK" POST /remember -H 'Idempotency-Key: idem-1' -d "$(mem "$NSB" "Idem" "once")")" # ---- sessions and checkpoints ---------------------------------------------------------------------------- SID=$(body "$WK" POST /sessions -d "{\"namespace_id\":\"$NSA\",\"objective\":\"Build it\"}" | jget id) check "session start" "yes" "$([ -n "$SID" ] && echo yes || echo no)" check "other tenant cannot restore session -> 404" "404" "$(code "$BK" GET /sessions/$SID/state)" check "other tenant cannot save session -> 404" "404" "$(code "$BK" PUT /sessions/$SID/state -H 'If-Match: 0' -d '{"state":{"a":1}}')" check "no-grant principal cannot restore -> 404" "404" "$(code "$NK" GET /sessions/$SID/state)" check "save without If-Match -> 428" "428" "$(code "$WK" PUT /sessions/$SID/state -d '{"state":{"a":1}}')" check "save rev 0 -> 200" "200" "$(code "$WK" PUT /sessions/$SID/state -H 'If-Match: 0' -d '{"state":{"step":1,"next":"api"}}')" check "stale save -> 412" "412" "$(code "$WK" PUT /sessions/$SID/state -H 'If-Match: 0' -d '{"state":{"step":99}}')" check "reader can restore" "yes" "$(body "$RK" GET /sessions/$SID/state | grep -q '"next":"api"' && echo yes || echo no)" check "reader cannot save -> 404" "404" "$(code "$RK" PUT /sessions/$SID/state -H 'If-Match: 1' -d '{"state":{"x":1}}')" check "different authorized agent resumes and saves -> 200" "200" "$(code "$XK" PUT /sessions/$SID/state -H 'If-Match: 1' -d '{"state":{"step":2}}')" check "restore shows latest accepted revision" "2" "$(body "$WK" GET /sessions/$SID/state | sed -n 's/.*"revision":\([0-9]*\),"state".*/\1/p')" check "state must be object -> 422" "422" "$(code "$WK" PUT /sessions/$SID/state -H 'If-Match: 2' -d '{"state":"x"}')" check "complete -> 200" "200" "$(code "$WK" POST /sessions/$SID/complete -d '{"outcome":"ok"}')" check "save after complete -> 409" "409" "$(code "$WK" PUT /sessions/$SID/state -H 'If-Match: 2' -d '{"state":{"s":3}}')" # ---- UTF-8 round trip (BMP); characters above U+FFFF are rejected (pinned runtime cannot hold them) ------- U=$'caf\xc3\xa9 \xe6\x97\xa5\xe6\x9c\xac' UJ=$(printf '{"namespace_id":"%s","type":"semantic","title":"utf8","content":"%s"}' "$NSA" "$U") UID_=$(printf '%s' "$UJ" | curl -s -X POST -H "Authorization: Bearer $WK" --data-binary @- "$BASE/remember" | jget id) check "utf-8 content round trip" "yes" "$(body "$WK" GET /memories/$UID_ | grep -qF "$U" && echo yes || echo no)" EJ=$(printf '{"namespace_id":"%s","type":"semantic","title":"emoji","content":"x %s"}' "$NSA" $'\xf0\x9f\x98\x80') check "raw emoji -> 422" "422" "$(printf '%s' "$EJ" | curl -s -o /dev/null -w '%{http_code}' -X POST -H "Authorization: Bearer $WK" --data-binary @- "$BASE/remember")" # escaped surrogate pair; the backslashes are produced at run time so no tool or shell can pre-decode them SJ=$(printf '{"namespace_id":"%s","type":"semantic","title":"emoji","content":"x %sud83d%sude00"}' "$NSA" '\' '\') check "escaped surrogate pair -> 422" "422" "$(code "$WK" POST /remember --data-binary "$SJ")" check "unsupported_character code" "yes" "$(has unsupported_character "$(body "$WK" POST /remember --data-binary "$SJ")")" # ---- revoked key, restart persistence (AC-01), cold first request ---------------------------------------- KID=$(prov key "$WID" | field api_key_id); KK=$(prov key "$WID" | field api_key) check "fresh key works" "200" "$(code "$KK" GET /memories)" KID2=$(prov key "$WID"); KEYID2=$(echo "$KID2" | field api_key_id); KEY2=$(echo "$KID2" | field api_key) prov revoke "$KEYID2" >/dev/null check "revoked key -> 401" "401" "$(code "$KEY2" GET /memories)" docker restart axonasp >/dev/null; for i in $(seq 1 20); do curl -s -o /dev/null "$BASE/health/live" && break; sleep 1; done check "after restart: write works first (cold)" "201" "$(code "$WK" POST /remember -d "$(mem "$NSA" "After restart" "cold write")")" check "after restart: memory persisted" "200" "$(code "$WK" GET /memories/$M1)" check "after restart: checkpoint persisted" "yes" "$(body "$WK" GET /sessions/$SID/state | grep -q '"step":2' && echo yes || echo no)" # ---- routing: wrong method and unknown API path ------------------------------------------------------------ R=$(curl -s -i -X DELETE "$BASE/recall") check "wrong method -> 405" "yes" "$(grep -q '^HTTP/1.1 405' <<<"$R" && echo yes || echo no)" check "405 carries Allow" "yes" "$(grep -qi '^Allow: POST' <<<"$R" && echo yes || echo no)" check "405 body is JSON error" "yes" "$(has '"code":"method_not_allowed"' "$R")" check "unknown API path -> JSON 404" "yes" "$(curl -s "$BASE/no-such-endpoint" | grep -q '"code":"not_found"' && echo yes || echo no)" check "unknown page stays 404" "404" "$(curl -s -o /dev/null -w '%{http_code}' "${BASE%/api/v1}/no-such-page")" echo "passed: $PASS failed: $FAIL" for f in "${FAILED[@]:-}"; do [ -n "$f" ] && echo " FAIL: $f"; done [ "$FAIL" -eq 0 ]