refactor(backend): remove the v1 auth and permission leftovers no frontend uses
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>
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
"""Permission guard (D-7, plan step 1.5): nothing reads v1 perm_* attributes any more.
|
||||
|
||||
Permissions are keys checked with core.access (holds/can/require). The only file that
|
||||
knows the 13 v1 booleans is core/access/legacy.py (temporary projection for the current
|
||||
frontends). The UserOut schema declares them as output fields, which isn't a read.
|
||||
Permissions are keys checked with core.access (holds/can/require). The v1 projection
|
||||
(core/access/legacy.py) is gone since step 2.99; only migrations still name the columns.
|
||||
"""
|
||||
import ast
|
||||
from pathlib import Path
|
||||
|
||||
BACKEND = Path(__file__).resolve().parents[2]
|
||||
ALLOWED = {"core/access/legacy.py"}
|
||||
ALLOWED: set[str] = set()
|
||||
|
||||
|
||||
def test_no_perm_attribute_reads():
|
||||
|
||||
@@ -4,7 +4,6 @@ from types import SimpleNamespace
|
||||
import pytest
|
||||
|
||||
from core.access.check import AccessContext, can, check, holds
|
||||
from core.access.legacy import LEGACY_MAP, project
|
||||
from core.access.templates import role_template
|
||||
from core.errors import XeniaError
|
||||
from core.modules import entitlements as E
|
||||
@@ -54,7 +53,7 @@ def test_unknown_key_is_a_programming_error(db):
|
||||
check(AccessContext(user=_user(role="superadmin")), "no.such.key", db)
|
||||
|
||||
|
||||
# ── templates + legacy projection ────────────────────────────────────────────
|
||||
# ── templates ────────────────────────────────────────────
|
||||
|
||||
def test_role_templates_match_v1_defaults():
|
||||
assert "core.settings.manage" in role_template("owner")
|
||||
@@ -65,14 +64,6 @@ def test_role_templates_match_v1_defaults():
|
||||
assert role_template("custom") == set()
|
||||
|
||||
|
||||
def test_legacy_projection_round_trip():
|
||||
u = _user(keys=role_template("store_manager"))
|
||||
p = project(u)
|
||||
assert p["perm_access_dashboard"] and p["perm_view_reports"] and not p["perm_manage_settings"]
|
||||
assert set(p) == set(LEGACY_MAP)
|
||||
assert all(project(_user(role="superadmin")).values())
|
||||
|
||||
|
||||
# ── API ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
def test_waiter_is_denied_manager_endpoint_with_code(client, make_user, login):
|
||||
@@ -82,11 +73,11 @@ def test_waiter_is_denied_manager_endpoint_with_code(client, make_user, login):
|
||||
"message": "You don't have permission to do this (core.staff.manage)."}
|
||||
|
||||
|
||||
def test_userout_carries_keys_and_legacy_booleans(client, make_user, login):
|
||||
def test_userout_carries_keys_only(client, make_user, login): # the v1 booleans are gone (2.99)
|
||||
w = make_user(role="waiter")
|
||||
me = client.get("/api/auth/me", headers=login(w)).json()
|
||||
assert "waiter_app.access" in me["permissions"]
|
||||
assert me["perm_access_waiter_app"] is True and me["perm_access_dashboard"] is False
|
||||
assert not any(k.startswith("perm_") for k in me)
|
||||
|
||||
|
||||
def test_staff_permission_api(client, make_user, login):
|
||||
@@ -107,9 +98,6 @@ def test_staff_permission_api(client, make_user, login):
|
||||
bad = client.put(f"/api/waiters/{w.id}/permissions", headers=h, json={"keys": ["nope.key"]})
|
||||
assert bad.status_code == 422 and bad.json()["error"]["code"] == "access.unknown_key"
|
||||
|
||||
reset = client.put(f"/api/waiters/{w.id}/reset-permissions", headers=h)
|
||||
assert reset.status_code == 200 and set(reset.json()["permissions"]) == role_template("waiter")
|
||||
|
||||
|
||||
def test_cannot_grant_keys_you_dont_hold(client, make_user, login):
|
||||
sm = make_user(role="staff_manager") # can manage staff, can't change settings
|
||||
@@ -119,16 +107,15 @@ def test_cannot_grant_keys_you_dont_hold(client, make_user, login):
|
||||
assert r.status_code == 403 and r.json()["error"]["code"] == "access.cannot_grant"
|
||||
|
||||
|
||||
def test_legacy_toggle_still_works_for_current_manager_ui(client, make_user, login):
|
||||
def test_v1_permission_writes_are_gone(client, make_user, login): # 2.99: keys only, via PUT /{id}/permissions
|
||||
owner = make_user(role="owner")
|
||||
w = make_user(role="waiter")
|
||||
r = client.put(f"/api/waiters/{w.id}/permission", headers=login(owner),
|
||||
json={"permission": "perm_view_reports", "value": True})
|
||||
assert r.status_code == 200
|
||||
keys = client.get(f"/api/waiters/{w.id}/permissions", headers=login(owner)).json()["keys"]
|
||||
assert {"reports.view", "history.view"} <= set(keys)
|
||||
upd = client.put(f"/api/waiters/{w.id}", headers=login(owner), json={"perm_view_reports": False, "nickname": "N"})
|
||||
assert upd.status_code == 200 and upd.json()["perm_view_reports"] is False and upd.json()["nickname"] == "N"
|
||||
h = login(owner)
|
||||
assert client.put(f"/api/waiters/{w.id}/permission", headers=h, json={"permission": "reports.view", "value": True}).status_code in (404, 405)
|
||||
assert client.put(f"/api/waiters/{w.id}/reset-permissions", headers=h).status_code in (404, 405)
|
||||
upd = client.put(f"/api/waiters/{w.id}", headers=h, json={"perm_view_reports": True, "nickname": "N"})
|
||||
assert upd.status_code == 200 and upd.json()["nickname"] == "N"
|
||||
assert "reports.view" not in client.get(f"/api/waiters/{w.id}/permissions", headers=h).json()["keys"]
|
||||
|
||||
|
||||
def test_created_staff_get_role_template(client, make_user, login):
|
||||
@@ -155,6 +142,11 @@ def test_0005_copies_flags_to_keys(tmp_path):
|
||||
from alembic import command
|
||||
from sqlalchemy import create_engine, text
|
||||
from core.schema import alembic_config, upgrade_to_head
|
||||
import importlib.util
|
||||
spec = importlib.util.spec_from_file_location("m0005", __import__("pathlib").Path(__file__).resolve().parents[1] / "migrations/versions/0005_permission_keys.py")
|
||||
m0005 = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(m0005)
|
||||
LEGACY_MAP = m0005.FROZEN_MAP # the v1 columns as they were (core/access/legacy.py is gone)
|
||||
|
||||
eng = create_engine(f"sqlite:///{(tmp_path / 'm.db').as_posix()}")
|
||||
with eng.begin() as conn:
|
||||
|
||||
@@ -162,11 +162,10 @@ def test_expired_site_can_log_in_and_see_license_page_but_not_order(client, make
|
||||
from conftest import TEST_PASSWORD
|
||||
E.set_current(E.compute(None, False, False, NOW, False, problem="expired"), notify=False)
|
||||
m = make_user(role="store_manager")
|
||||
r = client.post("/api/auth/login", json={"username": m.username, "password": TEST_PASSWORD})
|
||||
r = client.post("/api/auth/login", json={"client": "manager", "username": m.username, "password": TEST_PASSWORD})
|
||||
assert r.status_code == 200
|
||||
h = {"Authorization": f"Bearer {r.json()['access_token']}"}
|
||||
assert client.get("/api/system/status", headers=h).status_code == 200
|
||||
assert client.get("/api/setup/security-config").status_code == 200
|
||||
boot = client.get("/api/platform/bootstrap", headers=h).json()
|
||||
assert boot["license"] == {"mode": "free", "reason": "expired"}
|
||||
assert "ordering" not in {x["key"] for x in boot["modules"]}
|
||||
|
||||
@@ -4,7 +4,7 @@ from conftest import TEST_PASSWORD
|
||||
|
||||
def test_manager_logs_in_and_reads_me(client, make_user):
|
||||
m = make_user(role="store_manager")
|
||||
r = client.post("/api/auth/login", json={"username": m.username, "password": TEST_PASSWORD})
|
||||
r = client.post("/api/auth/login", json={"client": "manager", "username": m.username, "password": TEST_PASSWORD})
|
||||
assert r.status_code == 200, r.text
|
||||
token = r.json()["access_token"]
|
||||
me = client.get("/api/auth/me", headers={"Authorization": f"Bearer {token}"})
|
||||
@@ -14,7 +14,7 @@ def test_manager_logs_in_and_reads_me(client, make_user):
|
||||
|
||||
def test_wrong_password_is_rejected(client, make_user):
|
||||
m = make_user(role="store_manager")
|
||||
r = client.post("/api/auth/login", json={"username": m.username, "password": "nope"})
|
||||
r = client.post("/api/auth/login", json={"client": "manager", "username": m.username, "password": "nope"})
|
||||
assert r.status_code == 401
|
||||
|
||||
|
||||
|
||||
@@ -22,10 +22,11 @@ def admin_h(make_user, login):
|
||||
|
||||
# ── manager logins ───────────────────────────────────────────────────────────
|
||||
|
||||
def test_current_manager_ui_login_keeps_v1_rules(client, make_user):
|
||||
def test_login_needs_client(client, make_user): # the v1 no-`client` path is gone (2.99)
|
||||
m = make_user(role="store_manager")
|
||||
assert client.post("/api/auth/login", json={"username": m.username, "password": TEST_PASSWORD}).status_code == 200
|
||||
assert client.post("/api/auth/login", json={"username": m.username, "pin": TEST_PIN}).status_code == 200 # lock screen
|
||||
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):
|
||||
@@ -46,10 +47,10 @@ def test_manager_none_mode_is_gone(client, admin_h): # D-8
|
||||
|
||||
def test_wrong_password_and_wrong_app(client, make_user):
|
||||
m = make_user(role="store_manager")
|
||||
r = client.post("/api/auth/login", json={"username": m.username, "password": "nope"})
|
||||
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={"username": w.username, "password": TEST_PASSWORD})
|
||||
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."}
|
||||
|
||||
@@ -88,7 +89,7 @@ def test_waiter_modes(client, make_user, admin_h):
|
||||
|
||||
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={"username": m.username, "password": TEST_PASSWORD}))
|
||||
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
|
||||
@@ -118,7 +119,7 @@ def test_refuse_new(client, make_user, admin_h):
|
||||
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={"username": owner.username, "password": TEST_PASSWORD}))
|
||||
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
|
||||
|
||||
@@ -146,7 +147,7 @@ def test_blocking_a_user_ends_their_sessions(client, make_user, login):
|
||||
|
||||
def test_refresh_keeps_the_session(client, make_user):
|
||||
m = make_user(role="store_manager")
|
||||
h = _bearer(client.post("/api/auth/login", json={"username": m.username, "password": TEST_PASSWORD}))
|
||||
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)
|
||||
@@ -165,6 +166,6 @@ def test_session_survives_cache_loss_like_a_restart(client, make_user):
|
||||
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={"username": m.username, "password": TEST_PASSWORD}))
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user