Platform-v2 step 2.99a. After 2.6 no frontend calls these any more:
- POST /api/auth/login needs `client` (manager/waiter/terminal); the no-`client`
path with v1 rules and the `legacy_manager` single-session exception are gone.
- GET /api/setup/security-config (bootstrap's `login` replaced it in 2.6b).
- POST /api/recovery/use (a recovery code works in the normal login's
password field).
- PUT /api/waiters/{id}/permission and /reset-permissions (the permission editor
saves keys with PUT /{id}/permissions); staff create/update ignore perm_* fields.
- The v1 perm_* projection: core/access/legacy.py deleted, UserOut carries only
`permissions` (no app reads the 13 booleans, waiter included).
e2e manager login sends client "manager"; smoke_routes too. Backend 281 passed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
172 lines
9.6 KiB
Python
172 lines
9.6 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
|