Compare commits
1
Commits
main
...
add-smoke-test
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b82a6c1d87 |
No files matched your search
Executable
+104
@@ -0,0 +1,104 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Yolkbook smoke test on an isolated copy of the stack.
|
||||||
|
#
|
||||||
|
# Usage: tests/run-smoke.sh <label> [backup.sql.gz] [--no-build]
|
||||||
|
#
|
||||||
|
# <label> name for this run. Use "baseline" before a change, anything else after
|
||||||
|
# (e.g. "after-fastapi"). Non-baseline runs are compared against the baseline.
|
||||||
|
# backup.sql.gz optional mysqldump (made with --databases) to load into the test database
|
||||||
|
# first. Without one the test runs on an empty database.
|
||||||
|
# --no-build reuse the existing yolkbook-smoke-api image instead of rebuilding ./backend
|
||||||
|
#
|
||||||
|
# Isolation: runs as compose project "yolkbook-smoke" (its own containers, network and volume),
|
||||||
|
# nginx on 127.0.0.1:18056, with throwaway credentials. It never touches the production stack
|
||||||
|
# or the real .env secrets. The stack is torn down (volume included) when the run ends.
|
||||||
|
#
|
||||||
|
# State lives in $SMOKE_DIR (default ~/.cache/yolkbook-smoke, chmod 700), outside the repo,
|
||||||
|
# because snapshots contain real data when a backup is loaded:
|
||||||
|
# test.env, saved-token.txt, snapshot-<label>.json
|
||||||
|
# Delete that directory to start fresh (new credentials, new baseline).
|
||||||
|
#
|
||||||
|
# Run the baseline and the comparison on the same day: dashboard stats depend on today's date.
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
LABEL=${1:?usage: tests/run-smoke.sh <label> [backup.sql.gz] [--no-build]}
|
||||||
|
BACKUP=""; BUILD=1
|
||||||
|
for a in "${@:2}"; do
|
||||||
|
case $a in
|
||||||
|
--no-build) BUILD=0 ;;
|
||||||
|
*) BACKUP=$a ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
REPO=$(cd "$(dirname "$0")/.." && pwd)
|
||||||
|
SMOKE_DIR=${SMOKE_DIR:-$HOME/.cache/yolkbook-smoke}
|
||||||
|
mkdir -p "$SMOKE_DIR" && chmod 700 "$SMOKE_DIR"
|
||||||
|
umask 077
|
||||||
|
|
||||||
|
if [ ! -f "$SMOKE_DIR/test.env" ]; then
|
||||||
|
cat > "$SMOKE_DIR/test.env" <<EOF
|
||||||
|
MYSQL_ROOT_PASSWORD=$(openssl rand -hex 16)
|
||||||
|
MYSQL_DATABASE=eggtracker
|
||||||
|
MYSQL_USER=eggtracker
|
||||||
|
MYSQL_PASSWORD=$(openssl rand -hex 16)
|
||||||
|
ADMIN_USERNAME=smokeadmin
|
||||||
|
ADMIN_PASSWORD=$(openssl rand -hex 12)
|
||||||
|
JWT_SECRET=$(openssl rand -hex 32)
|
||||||
|
ALLOWED_ORIGINS=http://localhost:18056
|
||||||
|
SECURE_COOKIES=false
|
||||||
|
NTFY_URL=
|
||||||
|
NTFY_TOKEN=
|
||||||
|
EOF
|
||||||
|
fi
|
||||||
|
|
||||||
|
cat > "$SMOKE_DIR/override.yml" <<EOF
|
||||||
|
services:
|
||||||
|
db:
|
||||||
|
env_file: !override [ "$SMOKE_DIR/test.env" ]
|
||||||
|
api:
|
||||||
|
env_file: !override [ "$SMOKE_DIR/test.env" ]
|
||||||
|
image: yolkbook-smoke-api
|
||||||
|
nginx:
|
||||||
|
ports: !override [ "127.0.0.1:18056:80" ]
|
||||||
|
EOF
|
||||||
|
|
||||||
|
tc() { docker compose -p yolkbook-smoke --project-directory "$REPO" --env-file "$SMOKE_DIR/test.env" \
|
||||||
|
-f "$REPO/docker-compose.yml" -f "$SMOKE_DIR/override.yml" "$@"; }
|
||||||
|
trap 'tc down -v --remove-orphans >/dev/null 2>&1 || true' EXIT
|
||||||
|
|
||||||
|
[ $BUILD = 1 ] && docker build -q -t yolkbook-smoke-api "$REPO/backend" >/dev/null
|
||||||
|
|
||||||
|
tc down -v --remove-orphans >/dev/null 2>&1 || true
|
||||||
|
tc up -d db >/dev/null 2>&1
|
||||||
|
|
||||||
|
# The healthcheck can pass during MySQL's first-time init (temporary server), so wait until
|
||||||
|
# root can log in over TCP, which only works once the real server is up.
|
||||||
|
echo "Waiting for MySQL..."
|
||||||
|
for i in $(seq 1 100); do
|
||||||
|
tc exec -T db sh -c 'mysql -h127.0.0.1 -uroot -p"$MYSQL_ROOT_PASSWORD" -e "select 1"' >/dev/null 2>&1 && break
|
||||||
|
[ "$i" = 100 ] && { echo "MySQL did not come up"; exit 1; }
|
||||||
|
sleep 3
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ -n "$BACKUP" ]; then
|
||||||
|
echo "Loading $BACKUP"
|
||||||
|
zcat -f "$BACKUP" | tc exec -T db sh -c 'mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' 2>&1 | grep -v Warning || true
|
||||||
|
fi
|
||||||
|
|
||||||
|
tc up -d >/dev/null 2>&1
|
||||||
|
for i in $(seq 1 40); do
|
||||||
|
[ "$(tc ps api --format '{{.Health}}')" = healthy ] && break
|
||||||
|
[ "$i" = 40 ] && { echo "API did not become healthy"; tc logs api | tail -30; exit 1; }
|
||||||
|
sleep 3
|
||||||
|
done
|
||||||
|
tc ps --format '{{.Service}} {{.Image}} {{.Status}}'
|
||||||
|
|
||||||
|
set -a; . "$SMOKE_DIR/test.env"; set +a
|
||||||
|
RC=0
|
||||||
|
docker run --rm --user "$(id -u):$(id -g)" --network yolkbook-smoke_backend -v "$REPO/tests:/t:ro" -v "$SMOKE_DIR:/out" \
|
||||||
|
yolkbook-smoke-api python /t/smoke.py http://nginx http://api:8000 \
|
||||||
|
"$ADMIN_USERNAME" "$ADMIN_PASSWORD" /out "$LABEL" || RC=$?
|
||||||
|
|
||||||
|
echo "--- API errors/warnings during run (impersonation and admin-action warnings are expected) ---"
|
||||||
|
tc logs api 2>&1 | grep -E -i 'error|traceback' || echo "none"
|
||||||
|
exit $RC
|
||||||
+164
@@ -0,0 +1,164 @@
|
|||||||
|
"""Yolkbook end-to-end smoke test. Normally run via tests/run-smoke.sh, not directly.
|
||||||
|
|
||||||
|
Usage: python smoke.py <nginx_url> <api_url> <admin_user> <admin_pass> <out_dir> <label>
|
||||||
|
|
||||||
|
- Static pages, CSP and login rate limiting are checked through nginx; everything else goes
|
||||||
|
straight to the API so nginx rate limits don't interfere.
|
||||||
|
- Writes <out_dir>/snapshot-<label>.json: every read endpoint's output for every user in the
|
||||||
|
database (via admin impersonation). If <label> isn't "baseline" and snapshot-baseline.json
|
||||||
|
exists, the two must match exactly.
|
||||||
|
- The first run saves a login token to <out_dir>/saved-token.txt; later runs check that token is
|
||||||
|
still accepted (catches JWT library changes that would log everyone out).
|
||||||
|
- Creates, exercises and deletes its own throwaway user; existing data is only read.
|
||||||
|
"""
|
||||||
|
import json, sys, os, secrets, base64
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
NGX, BASE, AU, AP, OUT, LABEL = sys.argv[1:7]
|
||||||
|
fails, passes = [], 0
|
||||||
|
|
||||||
|
|
||||||
|
def check(name, cond, extra=""):
|
||||||
|
global passes
|
||||||
|
if cond:
|
||||||
|
passes += 1
|
||||||
|
else:
|
||||||
|
fails.append(f"{name} {extra}")
|
||||||
|
print("FAIL", name, extra)
|
||||||
|
|
||||||
|
|
||||||
|
def client():
|
||||||
|
return httpx.Client(base_url=BASE, timeout=20)
|
||||||
|
|
||||||
|
|
||||||
|
# ── static + health + rate limit (through nginx) ──
|
||||||
|
c = httpx.Client(base_url=NGX, timeout=20)
|
||||||
|
check("health via nginx", c.get("/api/health").json() == {"status": "ok"})
|
||||||
|
check("nginx login works", c.post("/api/auth/login", json={"username": AU, "password": AP}).status_code == 200)
|
||||||
|
check("nginx csp header", "default-src 'self'" in c.get("/").headers.get("content-security-policy", ""))
|
||||||
|
rl = [c.post("/api/auth/login", json={"username": "nobody", "password": "x"}).status_code for _ in range(8)]
|
||||||
|
check("nginx login rate limit", 429 in rl, rl)
|
||||||
|
for p in ["/", "/login.html", "/log.html", "/summary.html", "/budget.html", "/flock.html", "/admin.html",
|
||||||
|
"/js/api.js", "/js/auth.js", "/js/dashboard.js", "/css/style.css", "/favicon.svg"]:
|
||||||
|
r = c.get(p)
|
||||||
|
check(f"static {p}", r.status_code == 200, r.status_code)
|
||||||
|
check("404 page", c.get("/no-such-page").status_code == 404)
|
||||||
|
c = client()
|
||||||
|
check("unauth /me", c.get("/api/auth/me").status_code == 401)
|
||||||
|
|
||||||
|
# ── admin login ──
|
||||||
|
check("bad login", c.post("/api/auth/login", json={"username": AU, "password": "wrong"}).status_code == 401)
|
||||||
|
r = c.post("/api/auth/login", json={"username": AU, "password": AP})
|
||||||
|
check("admin login", r.status_code == 200, r.text)
|
||||||
|
check("admin /me", c.get("/api/auth/me").json()["username"] == AU)
|
||||||
|
|
||||||
|
# ── old-token compatibility ──
|
||||||
|
tok_file = os.path.join(OUT, "saved-token.txt")
|
||||||
|
if os.path.exists(tok_file):
|
||||||
|
oc = client(); oc.cookies.set("token", open(tok_file).read().strip())
|
||||||
|
r = oc.get("/api/auth/me")
|
||||||
|
check("token issued by baseline build still valid", r.status_code == 200, r.status_code)
|
||||||
|
else:
|
||||||
|
open(tok_file, "w").write(c.cookies.get("token"))
|
||||||
|
|
||||||
|
# ── snapshot every user's data via impersonation ──
|
||||||
|
users = c.get("/api/admin/users").json()
|
||||||
|
snap = {}
|
||||||
|
GETS = ["/api/eggs", "/api/flock", "/api/flock/current", "/api/flock/at/2026-06-01", "/api/feed", "/api/other",
|
||||||
|
"/api/stats/dashboard", "/api/stats/monthly", "/api/stats/budget"]
|
||||||
|
for u in users:
|
||||||
|
if u["username"].startswith("smoke"):
|
||||||
|
continue
|
||||||
|
ic = client(); ic.cookies.set("token", c.cookies.get("token"))
|
||||||
|
r = ic.post(f"/api/admin/users/{u['id']}/impersonate")
|
||||||
|
check(f"impersonate {u['id']}", r.status_code == 200, r.text)
|
||||||
|
snap[u["username"]] = {g: ic.get(g).json() for g in GETS}
|
||||||
|
r = ic.post("/api/admin/unimpersonate")
|
||||||
|
check(f"unimpersonate {u['id']}", r.status_code == 200 and r.json()["user"]["username"] == AU, r.text)
|
||||||
|
json.dump(snap, open(os.path.join(OUT, f"snapshot-{LABEL}.json"), "w"), indent=1, sort_keys=True, default=str)
|
||||||
|
base_file = os.path.join(OUT, "snapshot-baseline.json")
|
||||||
|
if LABEL != "baseline" and os.path.exists(base_file):
|
||||||
|
base = json.load(open(base_file))
|
||||||
|
now = json.loads(json.dumps(snap, sort_keys=True, default=str))
|
||||||
|
diff = [f"{u} {g}" for u in set(base) | set(now) for g in GETS
|
||||||
|
if base.get(u, {}).get(g) != now.get(u, {}).get(g)]
|
||||||
|
check("data matches baseline snapshot", not diff, diff[:10])
|
||||||
|
print(f"snapshot: {len(snap)} existing user(s)")
|
||||||
|
|
||||||
|
# ── create test user and exercise CRUD ──
|
||||||
|
uname = "smoke" + secrets.token_hex(3); pw = secrets.token_hex(8)
|
||||||
|
r = c.post("/api/admin/users", json={"username": uname, "password": pw})
|
||||||
|
check("admin create user", r.status_code == 201, r.text); uid = r.json()["id"]
|
||||||
|
check("dup user 409", c.post("/api/admin/users", json={"username": uname, "password": pw}).status_code == 409)
|
||||||
|
check("short pw 422", c.post("/api/admin/users", json={"username": uname + "x", "password": "short"}).status_code == 422)
|
||||||
|
|
||||||
|
uc = client()
|
||||||
|
check("user login", uc.post("/api/auth/login", json={"username": uname, "password": pw}).status_code == 200)
|
||||||
|
check("non-admin blocked from admin", uc.get("/api/admin/users").status_code == 403)
|
||||||
|
|
||||||
|
crud = {
|
||||||
|
"/api/eggs": ({"date": "2026-11-01", "eggs": 7, "notes": "a"}, {"eggs": 9}),
|
||||||
|
"/api/flock": ({"date": "2026-10-01", "chicken_count": 6, "notes": "b"}, {"chicken_count": 8}),
|
||||||
|
"/api/feed": ({"date": "2026-11-02", "bags": "2.50", "price_per_bag": "18.99"}, {"bags": "3"}),
|
||||||
|
"/api/other": ({"date": "2026-11-03", "total": "12.34", "notes": "c"}, {"total": "20.00"}),
|
||||||
|
}
|
||||||
|
for path, (body, upd) in crud.items():
|
||||||
|
r = uc.post(path, json=body); check(f"create {path}", r.status_code == 201, r.text)
|
||||||
|
rid = r.json()["id"]
|
||||||
|
r = uc.put(f"{path}/{rid}", json=upd); check(f"update {path}", r.status_code == 200, r.text)
|
||||||
|
k, v = next(iter(upd.items()))
|
||||||
|
check(f"update value {path}", str(r.json()[k]).rstrip("0").rstrip(".") == str(v).rstrip("0").rstrip("."), r.text)
|
||||||
|
check(f"list {path}", any(x["id"] == rid for x in uc.get(path).json()))
|
||||||
|
check(f"other user can't see {path}", c.put(f"{path}/{rid}", json=upd).status_code == 404)
|
||||||
|
crud_ids = {p: uc.get(p).json()[0]["id"] for p in crud}
|
||||||
|
check("validation eggs<0", uc.post("/api/eggs", json={"date": "2026-11-01", "eggs": -1}).status_code == 422)
|
||||||
|
check("validation bad date", uc.post("/api/eggs", json={"date": "nope", "eggs": 1}).status_code == 422)
|
||||||
|
check("validation feed 0", uc.post("/api/feed", json={"date": "2026-11-01", "bags": 0, "price_per_bag": 1}).status_code == 422)
|
||||||
|
d = uc.get("/api/stats/dashboard"); check("dashboard", d.status_code == 200, d.text)
|
||||||
|
check("flock current", uc.get("/api/flock/current").json()["chicken_count"] == 8)
|
||||||
|
m = uc.get("/api/stats/monthly"); check("monthly", m.status_code == 200 and len(m.json()) >= 1, m.text)
|
||||||
|
b = uc.get("/api/stats/budget"); check("budget", b.status_code == 200, b.text)
|
||||||
|
for path, rid in crud_ids.items():
|
||||||
|
check(f"delete {path}", uc.delete(f"{path}/{rid}").status_code == 204)
|
||||||
|
check(f"delete again 404 {path}", uc.delete(f"{path}/{rid}").status_code == 404)
|
||||||
|
|
||||||
|
# timezone + password
|
||||||
|
r = uc.put("/api/auth/timezone", json={"timezone": "America/Chicago"})
|
||||||
|
check("timezone", r.status_code == 200 and r.json()["user"]["timezone"] == "America/Chicago", r.text)
|
||||||
|
check("bad timezone", uc.put("/api/auth/timezone", json={"timezone": "Mars/Base"}).status_code == 400)
|
||||||
|
check("chpw wrong current", uc.post("/api/auth/change-password", json={"current_password": "x", "new_password": "y" * 12}).status_code == 400)
|
||||||
|
pw2 = secrets.token_hex(8)
|
||||||
|
check("chpw", uc.post("/api/auth/change-password", json={"current_password": pw, "new_password": pw2}).status_code == 200)
|
||||||
|
check("login new pw", client().post("/api/auth/login", json={"username": uname, "password": pw2}).status_code == 200)
|
||||||
|
|
||||||
|
# tampered tokens
|
||||||
|
tok = uc.cookies.get("token"); h, p, s = tok.split(".")
|
||||||
|
pl = json.loads(base64.urlsafe_b64decode(p + "==")); pl["is_admin"] = True
|
||||||
|
p2 = base64.urlsafe_b64encode(json.dumps(pl).encode()).rstrip(b"=").decode()
|
||||||
|
tc = client(); tc.cookies.set("token", f"{h}.{p2}.{s}")
|
||||||
|
check("tampered token rejected", tc.get("/api/auth/me").status_code == 401)
|
||||||
|
nc = client(); nc.cookies.set("token", base64.urlsafe_b64encode(b'{"alg":"none","typ":"JWT"}').rstrip(b"=").decode() + "." + p2 + ".")
|
||||||
|
check("alg=none rejected", nc.get("/api/auth/me").status_code == 401)
|
||||||
|
|
||||||
|
# logout
|
||||||
|
check("logout", uc.post("/api/auth/logout").status_code == 200)
|
||||||
|
check("after logout 401", uc.get("/api/auth/me").status_code == 401)
|
||||||
|
|
||||||
|
# disable / enable / reset password
|
||||||
|
check("disable", c.post(f"/api/admin/users/{uid}/disable").status_code == 200)
|
||||||
|
check("disabled login blocked", client().post("/api/auth/login", json={"username": uname, "password": pw2}).status_code in (401, 403))
|
||||||
|
check("enable", c.post(f"/api/admin/users/{uid}/enable").status_code == 200)
|
||||||
|
pw3 = secrets.token_hex(8)
|
||||||
|
check("reset pw", c.post(f"/api/admin/users/{uid}/reset-password", json={"new_password": pw3}).status_code == 200)
|
||||||
|
check("login reset pw", client().post("/api/auth/login", json={"username": uname, "password": pw3}).status_code == 200)
|
||||||
|
|
||||||
|
# lockout
|
||||||
|
lc = client()
|
||||||
|
codes = [lc.post("/api/auth/login", json={"username": uname, "password": "bad"}).status_code for _ in range(5)]
|
||||||
|
check("lockout after 5", lc.post("/api/auth/login", json={"username": uname, "password": pw3}).status_code == 403, codes)
|
||||||
|
|
||||||
|
check("delete user", c.delete(f"/api/admin/users/{uid}").status_code == 204)
|
||||||
|
check("deleted user gone", all(u["id"] != uid for u in c.get("/api/admin/users").json()))
|
||||||
|
|
||||||
|
print(f"\n{LABEL}: {passes} passed, {len(fails)} failed")
|
||||||
|
sys.exit(1 if fails else 0)
|
||||||
Reference in new issue
Block a user