askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
test_field_permission_semantics.py · 4.2 KB · 75 lines
| 1 | """The write-side field-permission semantics docs/permissions-model.md states. |
| 2 | |
| 3 | These are KNOWN LIMITS pending the owner's rule-combining design, pinned so the |
| 4 | page stays true until that design changes them (then both change together). |
| 5 | Each test is one sentence of the page's "Field permissions on writes" section. |
| 6 | """ |
| 7 | |
| 8 | import json |
| 9 | import shutil |
| 10 | from pathlib import Path |
| 11 | |
| 12 | from scripts import new_package |
| 13 | from scripts.try_package import Sandbox |
| 14 | |
| 15 | ROOT = Path(__file__).resolve().parents[1] |
| 16 | RECORDS = "/collections/tickets/records" |
| 17 | SECRET = {"name": "secret", "type": "text", |
| 18 | "permissions": {"owner": "edit", "role:auditor": "read", "default": "hidden"}} |
| 19 | |
| 20 | |
| 21 | def build(tmp_path, *, field=SECRET, rule_extra=None): |
| 22 | root = tmp_path / "packages" |
| 23 | for name in ("app-nav", "app-views"): |
| 24 | shutil.copytree(ROOT / "packages" / name, root / name) |
| 25 | package = new_package.create_package("app-tickets", packages_dir=root) |
| 26 | schema_path = package / "schemas" / "tickets.json" |
| 27 | schema = json.loads(schema_path.read_text()) |
| 28 | schema["fields"].append(field) |
| 29 | schema_path.write_text(json.dumps(schema)) |
| 30 | if rule_extra: |
| 31 | rules_path = package / "permissions" / "rules.json" |
| 32 | rules = json.loads(rules_path.read_text()) |
| 33 | rules["rules"][0].update(rule_extra) |
| 34 | rules_path.write_text(json.dumps(rules)) |
| 35 | return root |
| 36 | |
| 37 | |
| 38 | def test_only_submitted_keys_are_checked_and_create_is_checked_too(tmp_path): |
| 39 | with Sandbox(["app-tickets"], packages_dir=build(tmp_path, field={ |
| 40 | "name": "secret", "type": "text", "permissions": {"owner": "read"}})) as box: |
| 41 | box.add_user("ann") |
| 42 | status, body = box.call("ann", "POST", RECORDS, {"title": "a", "owner_id": "ann", "secret": "x"}) |
| 43 | assert (status, body["denied_fields"]) == (403, ["secret"]) # create is checked |
| 44 | status, body = box.call("ann", "POST", RECORDS, {"title": "a", "owner_id": "ann"}) |
| 45 | assert status == 201 # an unsent key is not |
| 46 | path = f"{RECORDS}/{body['record']['id']}" |
| 47 | assert box.call("ann", "PUT", path, {"title": "b"})[0] == 200 # only title was submitted |
| 48 | status, body = box.call("ann", "PUT", path, {"title": "c", "secret": "x"}) |
| 49 | assert (status, body["denied_fields"]) == (403, ["secret"]) |
| 50 | assert box.call("ann", "PUT", path, {"secret": ""})[0] == 403 # even sending it back blank |
| 51 | |
| 52 | |
| 53 | def test_the_most_restrictive_of_a_subjects_principals_wins_so_an_owner_with_a_role_loses_edit(tmp_path): |
| 54 | with Sandbox(["app-tickets"], packages_dir=build(tmp_path)) as box: |
| 55 | box.add_user("ann") |
| 56 | box.add_user("carol", roles=["auditor"]) |
| 57 | ann = box.call("ann", "POST", RECORDS, {"title": "a", "owner_id": "ann", "secret": "x"}) |
| 58 | assert ann[0] == 201 # owner only: edit |
| 59 | carol = box.call("carol", "POST", RECORDS, {"title": "c", "owner_id": "carol", "secret": "x"}) |
| 60 | assert carol[0] == 403 and carol[1]["denied_fields"] == ["secret"] # owner AND auditor: read wins |
| 61 | status, body = box.call("carol", "POST", RECORDS, {"title": "c", "owner_id": "carol"}) |
| 62 | assert status == 201 # her own rows, field unsent |
| 63 | assert box.call("carol", "PUT", f"{RECORDS}/{body['record']['id']}", {"secret": "y"})[0] == 403 |
| 64 | |
| 65 | |
| 66 | def test_an_allow_rules_denied_fields_hide_on_read_and_do_not_limit_writes(tmp_path): |
| 67 | root = build(tmp_path, field={"name": "memo", "type": "text"}, rule_extra={"denied_fields": ["memo"]}) |
| 68 | with Sandbox(["app-tickets"], packages_dir=root) as box: |
| 69 | box.add_user("ann") |
| 70 | status, body = box.call("ann", "POST", RECORDS, {"title": "a", "owner_id": "ann", "memo": "m"}) |
| 71 | assert status == 201 # written |
| 72 | assert "memo" not in body["record"] # the create answer is what a GET shows |
| 73 | path = f"{RECORDS}/{body['record']['id']}" |
| 74 | assert "memo" not in box.call("ann", "GET", path)[1]["record"] # hidden on read |
| 75 | assert box.call("ann", "PUT", path, {"memo": "m2"})[0] == 200 # still writable |