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>
220 lines
12 KiB
Python
220 lines
12 KiB
Python
"""Sessions + login modes (plan step 1.6, APPS_PLATFORM D-8, D-9, §6.2, §6.4)."""
|
|
import jwt
|
|
import pytest
|
|
|
|
from conftest import TEST_PASSWORD, TEST_PIN
|
|
|
|
|
|
def _set(client, admin_h, key, value):
|
|
r = client.put(f"/api/settings/{key}", headers=admin_h, json={"value": value})
|
|
assert r.status_code == 200, r.text
|
|
|
|
|
|
def _bearer(r):
|
|
assert r.status_code == 200, r.text
|
|
return {"Authorization": f"Bearer {r.json()['access_token']}"}
|
|
|
|
|
|
@pytest.fixture
|
|
def admin_h(make_user, login):
|
|
return login(make_user(role="superadmin"))
|
|
|
|
|
|
# ── manager logins ───────────────────────────────────────────────────────────
|
|
|
|
def test_login_needs_client(client, make_user): # the v1 no-`client` path is gone (2.99)
|
|
m = make_user(role="store_manager")
|
|
r = client.post("/api/auth/login", json={"username": m.username, "password": TEST_PASSWORD})
|
|
assert r.status_code == 422 and r.json()["error"]["code"].startswith("validation.")
|
|
assert client.post("/api/auth/login", json={"client": "manager", "username": m.username, "password": TEST_PASSWORD}).status_code == 200
|
|
|
|
|
|
def test_manager_password_mode_refuses_pin(client, make_user, admin_h):
|
|
m = make_user(role="store_manager")
|
|
r = client.post("/api/auth/login", json={"client": "manager", "username": m.username, "pin": TEST_PIN})
|
|
assert r.status_code == 403 and r.json()["error"]["code"] == "auth.login_mode_not_allowed"
|
|
assert client.post("/api/auth/login", json={"client": "manager", "username": m.username,
|
|
"password": TEST_PASSWORD}).status_code == 200
|
|
_set(client, admin_h, "auth.manager_login_mode", "pin")
|
|
assert client.post("/api/auth/login", json={"client": "manager", "username": m.username, "pin": TEST_PIN}).status_code == 200
|
|
|
|
|
|
def test_manager_none_mode_is_gone(client, admin_h): # D-8
|
|
r = client.put("/api/settings/auth.manager_login_mode", headers=admin_h, json={"value": "none"})
|
|
assert r.status_code == 422 and r.json()["error"]["code"] == "validation.setting_value"
|
|
assert client.post("/api/auth/login-no-auth", json={"username": "x"}).status_code == 404
|
|
|
|
|
|
def test_wrong_password_and_wrong_app(client, make_user):
|
|
m = make_user(role="store_manager")
|
|
r = client.post("/api/auth/login", json={"client": "manager", "username": m.username, "password": "nope"})
|
|
assert r.status_code == 401 and r.json()["error"]["code"] == "auth.invalid_credentials"
|
|
w = make_user(role="waiter")
|
|
r = client.post("/api/auth/login", json={"client": "manager", "username": w.username, "password": TEST_PASSWORD})
|
|
assert r.status_code == 403 and r.json()["error"] == {"code": "auth.client_not_allowed", "params": {"client": "manager"},
|
|
"message": "This account can't use the manager app."}
|
|
|
|
|
|
# ── waiter logins ────────────────────────────────────────────────────────────
|
|
|
|
def test_waiter_pin_by_id_and_deprecated_endpoint(client, make_user):
|
|
w = make_user(role="waiter")
|
|
assert client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "pin": TEST_PIN}).status_code == 200
|
|
assert client.post("/api/auth/login-by-id", json={"waiter_id": w.id, "pin": TEST_PIN}).status_code == 200
|
|
chef = make_user(role="chef")
|
|
r = client.post("/api/auth/login", json={"client": "waiter", "user_id": chef.id, "pin": TEST_PIN})
|
|
assert r.json()["error"]["code"] == "auth.client_not_allowed"
|
|
|
|
|
|
def test_waiter_modes(client, make_user, admin_h):
|
|
w = make_user(role="waiter")
|
|
_set(client, admin_h, "auth.waiter_login_mode", "password")
|
|
r = client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "pin": TEST_PIN})
|
|
assert r.json()["error"]["code"] == "auth.login_mode_not_allowed"
|
|
assert client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "password": TEST_PASSWORD}).status_code == 200
|
|
|
|
_set(client, admin_h, "auth.waiter_login_mode", "tap")
|
|
r = client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id})
|
|
assert r.status_code == 403 and r.json()["error"] == {"code": "auth.mode_needs_device", "params": {"mode": "tap"},
|
|
"message": "Logging in by tap only works on a paired device."}
|
|
|
|
_set(client, admin_h, "auth.waiter_login_mode", "pin")
|
|
_set(client, admin_h, "auth.waiter_require_username", "true")
|
|
r = client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "pin": TEST_PIN})
|
|
assert r.json()["error"]["code"] == "auth.username_required"
|
|
assert client.post("/api/auth/login", json={"client": "waiter", "username": w.username, "pin": TEST_PIN}).status_code == 200
|
|
|
|
|
|
# ── sessions ─────────────────────────────────────────────────────────────────
|
|
|
|
def test_token_carries_sid_and_logout_ends_it(client, make_user):
|
|
m = make_user(role="store_manager")
|
|
h = _bearer(client.post("/api/auth/login", json={"client": "manager", "username": m.username, "password": TEST_PASSWORD}))
|
|
payload = jwt.decode(h["Authorization"][7:], options={"verify_signature": False})
|
|
assert len(payload["sid"]) == 32
|
|
assert client.get("/api/auth/me", headers=h).status_code == 200
|
|
assert client.post("/api/auth/logout", headers=h).status_code == 200
|
|
r = client.get("/api/auth/me", headers=h)
|
|
assert r.status_code == 401 and r.json()["error"]["code"] == "auth.session_ended"
|
|
|
|
|
|
def test_end_old_ends_the_first_phone(client, make_user, admin_h): # "done when"
|
|
_set(client, admin_h, "auth.single_session_waiter", "end_old")
|
|
w = make_user(role="waiter")
|
|
phone1 = _bearer(client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "pin": TEST_PIN}))
|
|
phone2 = _bearer(client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "pin": TEST_PIN}))
|
|
r = client.get("/api/auth/me", headers=phone1)
|
|
assert r.status_code == 401 and r.json()["error"]["code"] == "auth.session_ended"
|
|
assert client.get("/api/auth/me", headers=phone2).status_code == 200
|
|
|
|
|
|
def test_refuse_new(client, make_user, admin_h):
|
|
_set(client, admin_h, "auth.single_session_waiter", "refuse_new")
|
|
w = make_user(role="waiter")
|
|
assert client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "pin": TEST_PIN}).status_code == 200
|
|
r = client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "pin": TEST_PIN})
|
|
assert r.status_code == 409 and r.json()["error"]["code"] == "auth.session_exists"
|
|
|
|
|
|
def test_single_session_is_per_client(client, make_user, admin_h):
|
|
_set(client, admin_h, "auth.single_session_waiter", "end_old")
|
|
owner = make_user(role="owner") # owners can use both apps
|
|
mgr = _bearer(client.post("/api/auth/login", json={"client": "manager", "username": owner.username, "password": TEST_PASSWORD}))
|
|
client.post("/api/auth/login", json={"client": "waiter", "user_id": owner.id, "pin": TEST_PIN})
|
|
assert client.get("/api/auth/me", headers=mgr).status_code == 200
|
|
|
|
|
|
def test_manager_lists_and_ends_sessions(client, make_user, login):
|
|
owner = make_user(role="owner")
|
|
w = make_user(role="waiter")
|
|
wh = _bearer(client.post("/api/auth/login", json={"client": "waiter", "user_id": w.id, "pin": TEST_PIN}))
|
|
oh = login(owner)
|
|
rows = client.get("/api/auth/sessions", headers=oh).json()
|
|
mine = next(s for s in rows if s["user_id"] == w.id)
|
|
assert mine["client"] == "waiter" and mine["created_at"].endswith("Z")
|
|
assert client.post(f"/api/auth/sessions/{mine['id']}/end", headers=oh).status_code == 200
|
|
assert client.get("/api/auth/me", headers=wh).json()["error"]["code"] == "auth.session_ended"
|
|
assert client.get("/api/auth/sessions", headers=login(w, "waiter")).status_code == 403
|
|
|
|
|
|
def test_blocking_a_user_ends_their_sessions(client, make_user, login):
|
|
owner = make_user(role="owner")
|
|
w = make_user(role="waiter")
|
|
wh = login(w, "waiter")
|
|
assert client.put(f"/api/waiters/{w.id}/block", headers=login(owner)).json()["is_active"] is False
|
|
assert client.get("/api/auth/me", headers=wh).status_code == 401
|
|
|
|
|
|
def test_refresh_keeps_the_session(client, make_user):
|
|
m = make_user(role="store_manager")
|
|
h = _bearer(client.post("/api/auth/login", json={"client": "manager", "username": m.username, "password": TEST_PASSWORD}))
|
|
new = _bearer(client.post("/api/auth/refresh", headers=h))
|
|
sid = lambda hh: jwt.decode(hh["Authorization"][7:], options={"verify_signature": False})["sid"] # noqa: E731
|
|
assert sid(new) == sid(h)
|
|
|
|
|
|
def test_token_without_session_is_refused(client, make_user):
|
|
from config import settings
|
|
m = make_user(role="store_manager")
|
|
forged = jwt.encode({"sub": str(m.id), "role": m.role, "exp": 9999999999}, settings.SECRET_KEY, algorithm="HS256")
|
|
r = client.get("/api/auth/me", headers={"Authorization": f"Bearer {forged}"})
|
|
assert r.status_code == 401 and r.json()["error"]["code"] == "auth.session_ended"
|
|
|
|
|
|
def test_session_survives_cache_loss_like_a_restart(client, make_user):
|
|
"""The session lives in the DB: dropping every in-memory cache (what a restart does)
|
|
keeps the token valid. The real restart is checked in the plan notes."""
|
|
from core import sessions
|
|
m = make_user(role="store_manager")
|
|
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
|