3848 bytes
#!/bin/sh
set -eu
cli="$(git rev-parse --show-toplevel)/anbot.mjs"
scratch=$(mktemp -d)
port=${ANBOT_TEST_PUSH_PORT:-18789}
server=
cleanup() {
if [ -n "$server" ]; then
kill "$server" 2>/dev/null || :
wait "$server" 2>/dev/null || :
fi
rm -r "$scratch"
}
trap cleanup EXIT
trap 'exit 1' INT TERM
git init -q -b main --object-format=sha1 "$scratch/source"
git -C "$scratch/source" -c user.name=Test -c user.email=test@example.invalid commit -q --allow-empty -m Initial
node "$cli" publish --data "$scratch/node" --source "$scratch/source" --branch main --port "$port"
node "$cli" identity --data "$scratch/agent" >"$scratch/allowlist"
outsider=$(node "$cli" identity --data "$scratch/outsider")
node "$cli" serve --data "$scratch/node" --port "$port" --push-allowlist "$scratch/allowlist" >"$scratch/url" &
server=$!
for attempt in 1 2 3 4 5; do
if test -s "$scratch/url"; then break; fi
sleep 1
done
url=$(cat "$scratch/url")
git clone "$url" "$scratch/working"
old=$(git -C "$scratch/working" rev-parse HEAD)
git -C "$scratch/working" -c user.name=Test -c user.email=test@example.invalid commit -q --allow-empty -m Update
new=$(git -C "$scratch/working" rev-parse HEAD)
if git -C "$scratch/working" push origin main; then exit 1; fi
if node "$cli" push --data "$scratch/outsider" --source "$scratch/working" --url "$url"; then exit 1; fi
grep -F "ANBot push identity verified: $outsider (not allowlisted)" "$scratch/url"
node --input-type=module - "$url" "$(cat "$scratch/allowlist")" <<'JS'
import assert from 'node:assert/strict'
const [, , url, author] = process.argv
const endpoint = url + '/anbot-auth'
const challenge = await fetch(endpoint, { headers: { 'X-ANBot-Author': author } })
assert.equal(challenge.status, 200)
const { nonce } = await challenge.json()
const forged = await fetch(endpoint, {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ author, nonce, proof: Buffer.alloc(64).toString('base64') })
})
assert.equal(forged.status, 403)
JS
test "$(git ls-remote "$url" HEAD | cut -f1)" = "$old"
git -C "$scratch/working" branch other
if node "$cli" push --data "$scratch/agent" --source "$scratch/working" --url "$url" --branch other; then exit 1; fi
curl -fsS "http://127.0.0.1:$port/feed" | grep -F 'No posts yet.'
node "$cli" push --data "$scratch/agent" --source "$scratch/working" --url "$url"
test "$(git ls-remote "$url" HEAD | cut -f1)" = "$new"
node "$cli" push --data "$scratch/agent" --source "$scratch/working" --url "$url"
curl -fsS "http://127.0.0.1:$port/feed" >"$scratch/feed"
test "$(grep -c 'Pushed 1 commit' "$scratch/feed")" -eq 1
grep -F "href=\"/commit/$new\"" "$scratch/feed"
grep -F ' Update</li>' "$scratch/feed"
raw_path=$(sed -n 's/.*href="\(\/raw?post=[^"]*\)".*/\1/p' "$scratch/feed")
test -n "$raw_path"
curl -fsS "http://127.0.0.1:$port$raw_path" | node -e '
const record = JSON.parse(require("node:fs").readFileSync(0))
const body = JSON.parse(record.content)
if (body.type !== "git-push" || body.commit !== process.argv[1]) process.exit(1)
' "$new"
update_query=$(sed -n 's/.*href="?update=\([^"]*\)".*/\1/p' "$scratch/feed")
test -n "$update_query"
curl -fsS "http://127.0.0.1:$port/feed?update=$update_query" | grep -F 'Pushed 1 commit'
git clone "$url" "$scratch/updated"
test "$(git -C "$scratch/updated" rev-parse HEAD)" = "$new"
kill "$server"
wait "$server" 2>/dev/null || :
server=
mv "$scratch/node/cache" "$scratch/old-cache"
node "$cli" serve --data "$scratch/node" --port "$port" >"$scratch/restarted" &
server=$!
for attempt in 1 2 3 4 5; do
if test -s "$scratch/restarted"; then break; fi
sleep 1
done
test "$(git ls-remote "$url" HEAD | cut -f1)" = "$new"
curl -fsS "http://127.0.0.1:$port/feed" | grep -F 'Pushed 1 commit'
printf 'PASS: authenticated push published %s and survived cache deletion.\n' "$new"
Git ยท Social feed