feat(backend): sign people out after N days without use (auth.idle_days, default 14)

Platform-v2 step 2.99d, owner decision 2026-11-14.
- New core setting auth.idle_days (int, default 14, 0 = never). A manager, waiter or
  paired-device session unused for that long ends with reason "idle" on its next
  request (sessions.validate) or when someone opens "Signed in now"
  (sessions.end_idle in GET /api/auth/sessions). Support keeps its own 4 h expiry.
- Manager: Settings -> Sign-in & security -> "Sign people out after" (7/14/30/60/90
  days / Never; a value set elsewhere is kept); "Signed in now" names the reason.
  Texts en/el.
- Tests: backend 284 (+ 14-day default, list drops idle ones, setting incl. 0);
  manager e2e checks the default and saving 30. The support-access check that
  failed once in ~8 runs now saves a screenshot and the page's alert text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-14 13:38:00 +02:00
co-authored by Claude Opus 5.5
parent cc4e76019b
commit f1f20f7bba
8 changed files with 119 additions and 6 deletions
+48
View File
@@ -169,3 +169,51 @@ def test_session_survives_cache_loss_like_a_restart(client, make_user):
h = _bearer(client.post("/api/auth/login", json={"client": "manager", "username": m.username, "password": TEST_PASSWORD}))
sessions.clear_cache()
assert client.get("/api/auth/me", headers=h).status_code == 200
# ── idle sessions (plan 2.99d, owner decision 2026-11-14) ────────────────────
def _age(db, days, **flt):
from datetime import timedelta
from core import sessions
from core.timeutil import utcnow
from models.session import LoginSession
for row in db.query(LoginSession).filter_by(**flt).all():
row.last_seen_at = utcnow() - timedelta(days=days)
db.commit()
sessions.clear_cache()
def test_idle_session_ends_after_14_days_by_default(client, db, make_user):
w = make_user(role="waiter")
wh = _bearer(client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "pin": TEST_PIN}))
_age(db, 13, user_id=w.id)
assert client.get("/api/auth/me", headers=wh).status_code == 200 # 13 days: still in
_age(db, 15, user_id=w.id)
r = client.get("/api/auth/me", headers=wh)
assert r.status_code == 401 and r.json()["error"]["code"] == "auth.session_ended"
from models.session import LoginSession
db.expire_all()
assert db.query(LoginSession).filter_by(user_id=w.id).one().end_reason == "idle"
def test_signed_in_now_drops_idle_sessions(client, db, make_user, login):
owner = make_user(role="owner")
w = make_user(role="waiter")
client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "pin": TEST_PIN})
oh = login(owner)
_age(db, 20, user_id=w.id)
assert all(s["user_id"] != w.id for s in client.get("/api/auth/sessions", headers=oh).json())
ended = client.get("/api/auth/sessions?include_ended=true", headers=oh).json()
assert next(s for s in ended if s["user_id"] == w.id)["end_reason"] == "idle"
def test_idle_days_setting_and_zero_means_never(client, db, make_user, admin_h):
w = make_user(role="waiter")
wh = _bearer(client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "pin": TEST_PIN}))
_set(client, admin_h, "auth.idle_days", "0")
_age(db, 400, user_id=w.id)
assert client.get("/api/auth/me", headers=wh).status_code == 200
_set(client, admin_h, "auth.idle_days", "3")
_age(db, 4, user_id=w.id)
assert client.get("/api/auth/me", headers=wh).status_code == 401