git clone https://garage44.eu/jeroen/midas.git.. /tests/e2e/hosted.sh
#!/usr/bin/env bash
# A project the hub hosts, end to end on a scratch hub (src/hosted.rs): made
# through the API, cloned over public HTTP, pushed through its hooks with an
# agent's role — a branch yes, main only from the landing with a pass — read
# on its pages and as an archive, and a private one not found.
#
# cargo build && tests/e2e/hosted.sh [target/debug/midas]
#
# The ssh login (`midas hub git --key`) only sets MIDAS_GIT_ROLE and runs
# git receive-pack; here a local push sets the role itself.
set -euo pipefail
midas=$(realpath "${1:-target/debug/midas}")
e2e=$(mktemp -d)
trap 'kill "${hub_pid:-}" 2>/dev/null; rm -rf "$e2e"' EXIT
fail() { echo "hosted e2e: $*" >&2; [ -f "$e2e/hub.log" ] && tail -20 "$e2e/hub.log" >&2; exit 1; }
port=$((40000 + RANDOM % 20000))
hub=http://127.0.0.1:$port
key=$(head -c 24 /dev/urandom | od -An -tx1 | tr -d ' \n')
export MIDAS_STATE=$e2e/state MIDAS_HUB_CONF=$e2e/hub.conf
mkdir -p "$MIDAS_STATE"
printf '{"listen": "127.0.0.1:%s", "forgejo_ssh": "", "forgejo_api": "", "repos": []}\n' "$port" > "$MIDAS_HUB_CONF"
printf '{"e2e": {"key": "%s"}}\n' "$key" > "$e2e/callers.json"
MIDAS_CALLERS=$e2e/callers.json "$midas" hub serve --listen "127.0.0.1:$port" > "$e2e/hub.log" 2>&1 &
hub_pid=$!
for _ in $(seq 50); do curl -fs "$hub/v1/health" >/dev/null 2>&1 && break; sleep 0.2; done
curl -fs "$hub/v1/health" >/dev/null || fail "the hub did not come up"
git_id=(-c user.name=e2e -c user.email=e2e@e2e)
echo "== made through the API"
MIDAS_HUB=$hub MIDAS_HUB_KEY=$key MIDAS_OWNER=s-e2e HOME=$e2e "$midas" project create demo --public --gate true \
--description "An e2e project" > "$e2e/made.json" || fail "project create"
repo=$(sed -n 's/.*"repo": "\(.*\)".*/\1/p' "$e2e/made.json")
[ "$repo" = midas/demo ] || fail "not in the workspace's namespace: $(cat "$e2e/made.json")"
MIDAS_HUB=$hub MIDAS_HUB_KEY=$key HOME=$e2e "$midas" project create hidden --description "private" >/dev/null || fail "private create"
bare=$MIDAS_STATE/git/$repo.git
echo " $repo at $bare"
echo "== cloned over public HTTP, without a login"
git clone -q "$hub/git/$repo.git" "$e2e/clone" || fail "public clone"
grep -q "An e2e project" "$e2e/clone/README.md" || fail "README not cloned"
if git clone -q "$hub/${repo%/*}/hidden.git" "$e2e/nope" 2>/dev/null; then fail "a private repository cloned"; fi
echo "== an agent pushes a branch"
cd "$e2e/clone"
echo "more" >> README.md
git "${git_id[@]}" commit -qam "more"
head=$(git rev-parse HEAD)
export MIDAS_GIT_ROLE=agent MIDAS_GIT_KEY=e2e
git push -q "$bare" HEAD:refs/heads/session/s-e2e || fail "branch push refused"
echo "== main: refused without a landing, a turn and a pass"
if git push -q "$bare" HEAD:refs/heads/main 2> "$e2e/err"; then fail "main moved without a landing"; fi
grep -q "moved by a landing" "$e2e/err" || fail "unexpected refusal: $(cat "$e2e/err")"
MIDAS_HUB=$hub MIDAS_HUB_KEY=$key MIDAS_OWNER=s-e2e HOME=$e2e "$midas" land take --purpose e2e >/dev/null 2>&1 || fail "land take"
if git push -q -o midas-owner=s-e2e "$bare" HEAD:refs/heads/main 2> "$e2e/err"; then fail "main moved with no pass"; fi
grep -q "no gate pass" "$e2e/err" || fail "unexpected refusal: $(cat "$e2e/err")"
MIDAS_HUB=$hub MIDAS_HUB_KEY=$key MIDAS_OWNER=s-e2e HOME=$e2e "$midas" verified --sha "$head" --node e2e >/dev/null 2>&1 || fail "verified"
git push -q -o midas-owner=s-e2e "$bare" HEAD:refs/heads/main || fail "the landing's push refused"
[ "$(git -C "$bare" rev-parse main)" = "$head" ] || fail "main is not the landed head"
echo "== read on its pages and as an archive, at Forgejo's paths"
curl -fs "$hub/$repo" | grep -q "An e2e project" || fail "home page"
curl -fs "$hub/$repo/log" | grep -q "more" || fail "log page"
curl -fs "$hub/$repo/commit/$head" | grep -q "+more" || fail "commit page"
curl -fs "$hub/$repo/raw/README.md" | grep -q "^more" || fail "raw file"
curl -fs "$hub/$repo/archive/main.tar.gz" | tar tz | grep -q "demo/README.md" || fail "archive"
loc=$(curl -s -o /dev/null -w '%{redirect_url}' "$hub/$repo/src/branch/main/README.md")
[ "$loc" = "$hub/$repo/tree/README.md?rev=main" ] || fail "a Forgejo path went to $loc"
loc=$(curl -s -o /dev/null -w '%{redirect_url}' "$hub/git/$repo/log")
[ "$loc" = "$hub/$repo/log" ] || fail "an old /git/ page went to $loc"
git clone -q "$hub/$repo.git" "$e2e/clone2" || fail "a clone at the root path"
for p in "$hub/${repo%/*}/hidden" "$hub/${repo%/*}/hidden.git/info/refs?service=git-upload-pack" "$hub/git/${repo%/*}/hidden"; do
code=$(curl -sL -o /dev/null -w '%{http_code}' "$p")
[ "$code" = 404 ] || fail "a private repository answered $code at $p"
done
code=$(curl -s -o /dev/null -w '%{http_code}' -X POST -H 'Content-Type: application/vnd.git-lfs+json' \
--data '{"operation":"upload","objects":[]}' "$hub/$repo.git/info/lfs/objects/batch")
[ "$code" = 403 ] || fail "a public LFS upload answered $code"
curl -fs -X POST -H 'Content-Type: application/vnd.git-lfs+json' --data '{"operation":"download","objects":[{"oid":"'"$(printf %064d 0)"'","size":1}]}' \
"$hub/$repo.git/info/lfs/objects/batch" | grep -q '"code":404' || fail "a public LFS batch"
echo "== the front page, to a visitor through the proxy"
curl -fs -H 'X-Forwarded-For: 203.0.113.9' -H 'X-Forwarded-Proto: https' "$hub/" | grep -q "href=\"/$repo\"" || fail "front page"
if curl -fs -H 'X-Forwarded-For: 203.0.113.9' -H 'X-Forwarded-Proto: https' "$hub/" | grep -q hidden; then fail "a private one on the front page"; fi
MIDAS_HUB=$hub MIDAS_HUB_KEY=$key HOME=$e2e "$midas" project log "$repo" | grep -q "more" || fail "project log"
echo "hosted e2e: all passed"