tests/run-smoke.sh starts an isolated copy of the stack (compose project yolkbook-smoke, nginx on 127.0.0.1:18056, throwaway credentials), optionally loads a mysqldump backup, and runs tests/smoke.py against it. The stack and its volume are removed afterwards. The test covers static pages, CSP and login rate limiting through nginx; login and lockout; CRUD and validation for eggs, flock, feed and other purchases; stats; admin user management and impersonation; password and timezone changes; logout; and rejection of tampered and alg=none tokens. The first run with a given label of "baseline" snapshots every user's list and stats output; later runs must match it exactly, and must still accept a token issued during the baseline run. State is kept in ~/.cache/yolkbook-smoke (outside the repo, since snapshots contain real data). Co-Authored-By: Claude Opus 5.5 <[email protected]>
165 lines
9.1 KiB
Python
165 lines
9.1 KiB
Python
"""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)
|