#!/bin/bash # simplegit HTTP API end-to-end tests. # # Assumes the backend is already listening at $BASE (default :8091). Start it # in another terminal, e.g.: # go run ./cmd -root /tmp/sgtest -http :8091 -ssh :2222 # then run: # ./tests/api_e2e.sh # # Each run uses a random username/repo suffix so it can be re-run against the # same DB without "already exists" collisions. BASE="${BASE:-http://localhost:8091/api/mgmt}" SUFFIX="${RANDOM:-$$}" USER="tester$SUFFIX" REPO="repo$SUFFIX" # A fixed throwaway ed25519 public key; the fingerprint is what matters, not # whether the matching private key exists (these tests only exercise the API). KEY="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHSPxVEqkYiI+9Z9QPgwdMEw5cjTvaO8RefSq1ccxOjw $USER" pass=0; fail=0 # check desc expected actual check() { if [ "$2" = "$3" ]; then echo " PASS: $1"; pass=$((pass+1)) else echo " FAIL: $1 (expected [$2], got [$3])"; fail=$((fail+1)); fi } # code ...curl-args... -> HTTP status code code() { curl -s -o /dev/null -w "%{http_code}" "$@"; } echo "## auth" REG=$(curl -s -X POST "$BASE/auth/register" -H 'Content-Type: application/json' -d "{\"username\":\"$USER\",\"password\":\"pass\"}") TOKEN=$(echo "$REG" | grep -o '"token":"[^"]*"' | sed 's/"token":"//;s/"//') [ -n "$TOKEN" ] && check "register returns token" "ok" "ok" || check "register returns token" "ok" "empty" check "login wrong creds -> 401" "401" "$(code -X POST "$BASE/auth/login" -H 'Content-Type: application/json' -d '{"username":"x","password":"x"}')" check "GET /me no token -> 401" "401" "$(code "$BASE/me")" ME=$(curl -s "$BASE/me" -H "Authorization: Bearer $TOKEN") echo "$ME" | grep -q "\"$USER\"" && check "GET /me returns user" "ok" "ok" || check "GET /me returns user" "ok" "fail" echo "## ssh keys" check "GET /me/keys empty" '{"keys":[]}' "$(curl -s "$BASE/me/keys" -H "Authorization: Bearer $TOKEN")" ADD=$(curl -s -X POST "$BASE/me/keys" -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' -d "{\"public_key\":\"$KEY\",\"comment\":\"laptop\"}") KID=$(echo "$ADD" | grep -o '"id":[0-9]*' | head -1 | grep -o '[0-9]*$') [ -n "$KID" ] && check "add key returns id" "ok" "ok" || check "add key returns id" "ok" "empty" curl -s "$BASE/me/keys" -H "Authorization: Bearer $TOKEN" | grep -q "\"id\":$KID" && check "key appears in list" "ok" "ok" || check "key appears in list" "ok" "fail" check "delete key -> 204" "204" "$(code -X DELETE "$BASE/me/keys/$KID" -H "Authorization: Bearer $TOKEN")" check "GET /me/keys empty after delete" '{"keys":[]}' "$(curl -s "$BASE/me/keys" -H "Authorization: Bearer $TOKEN")" echo "## repos" CR=$(curl -s -X POST "$BASE/repos" -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' -d "{\"name\":\"$REPO\",\"default_branch\":\"main\"}") echo "$CR" | grep -q "\"$USER/$REPO.git\"" && check "create repo" "ok" "ok" || check "create repo" "ok" "fail" echo "$CR" | grep -q '"default_branch":"main"' && check "default_branch is main" "ok" "ok" || check "default_branch is main" "ok" "fail" check "duplicate repo -> 400" "400" "$(code -X POST "$BASE/repos" -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' -d "{\"name\":\"$REPO\"}")" check "create no name -> 400" "400" "$(code -X POST "$BASE/repos" -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' -d '{"name":""}')" check "create no token -> 401" "401" "$(code -X POST "$BASE/repos" -H 'Content-Type: application/json' -d '{"name":"x"}')" curl -s "$BASE/repos" -H "Authorization: Bearer $TOKEN" | grep -q "\"$USER/$REPO.git\"" && check "repo appears in list" "ok" "ok" || check "repo appears in list" "ok" "fail" echo echo "## $pass passed, $fail failed" [ "$fail" -eq 0 ] || exit 1