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_object_pointer_grants.py · 18.7 KB · 398 lines
| 1 | """Objects that write a request's pointer below the record API ask its question. |
| 2 | |
| 3 | Two shipped objects copied an id from the request onto a row whose schema |
| 4 | declares ``relation.requires``, without asking whether the caller may act |
| 5 | on what it names. The record API asks (object_access.relation_refusal); |
| 6 | these objects write through object_records, below it. |
| 7 | |
| 8 | Measured before the fix, with enforcement on and off, as any registered |
| 9 | user: |
| 10 | |
| 11 | - ``POST /timers/start {"task_id": <another user's private task>}`` |
| 12 | (site_timer_actions) answered 200 and stored a running time_log pointing |
| 13 | at that task; a task id that does not exist answered 500 with a |
| 14 | traceback, so the pair told the caller which task ids exist. |
| 15 | - ``action_confirm_scan {"project_id": <another user's project>}`` put a |
| 16 | draft expense on that project (billable by default, which is what |
| 17 | generate_tm_invoice collects). And it carried ``entity_id`` from the |
| 18 | scan: a scan written before action_scan_ingest checked that pointer |
| 19 | keeps whatever it was given, so confirming it planted the expense in |
| 20 | another user's books. |
| 21 | |
| 22 | Neither could ask ``object_access.Access.relation_refusal`` then, because |
| 23 | Access did not resolve project or record-share grants: a project member or |
| 24 | a person a task is shared with, both allowed by the record API, would have |
| 25 | been refused. Access now resolves them with the server's own code, so the |
| 26 | objects ask the one question, before anything is written: another user's |
| 27 | record and a missing one get the record API's one 400, never a 500, and a |
| 28 | member and a sharee are still allowed. |
| 29 | """ |
| 30 | |
| 31 | from __future__ import annotations |
| 32 | |
| 33 | import pytest |
| 34 | from test_findings_access_control import _json, _text, make_site # noqa: F401 |
| 35 | |
| 36 | import object_records |
| 37 | import object_schemas |
| 38 | |
| 39 | |
| 40 | def _missing(field, target, value): |
| 41 | return f"Record field '{field}' references a missing record: {target}/{value}" |
| 42 | |
| 43 | |
| 44 | def _users(site, *names): |
| 45 | return {name: site.user(name) for name in names} |
| 46 | |
| 47 | |
| 48 | def _grants(site, tasks=True): |
| 49 | """alice owns a project, a task in it and a private task; carol is a |
| 50 | member of the project, dave has the private task shared with him.""" |
| 51 | site.create("projects", {"id": "p-alice", "name": "Alice's", "owner_id": "alice"}) |
| 52 | site.create("project_access", {"project_id": "p-alice", "user_id": "carol", |
| 53 | "permission": "read"}) |
| 54 | if not tasks: |
| 55 | return |
| 56 | site.create("tasks", {"id": "t-proj", "title": "In the project", "owner_id": "alice", |
| 57 | "project_id": "p-alice"}) |
| 58 | site.create("tasks", {"id": "t-alice", "title": "Private", "owner_id": "alice"}) |
| 59 | site.create("record_shares", {"collection": "tasks", "record_id": "t-alice", |
| 60 | "user_id": "dave", "permission": "read", |
| 61 | "granted_by": "alice"}) |
| 62 | |
| 63 | |
| 64 | def _off(monkeypatch, mode): |
| 65 | if mode == "off": |
| 66 | monkeypatch.delenv("DBBASIC_ENABLE_PERMISSION_ENFORCEMENT") |
| 67 | |
| 68 | |
| 69 | # --- site_timer_actions: time_logs.task_id requires read ------------------------------------- |
| 70 | |
| 71 | def _timer_box(make_site, monkeypatch, mode): |
| 72 | site = make_site("app-timers", "app-projects", "app-share") |
| 73 | users = _users(site, "alice", "bob", "carol", "dave") |
| 74 | _grants(site) |
| 75 | _off(monkeypatch, mode) |
| 76 | return site, users |
| 77 | |
| 78 | |
| 79 | def _start(site, headers, task_id): |
| 80 | return site.execute("site_timer_actions", {"action": "start", "task_id": task_id}, headers) |
| 81 | |
| 82 | |
| 83 | @pytest.mark.parametrize("mode", ["enforced", "off"]) |
| 84 | def test_a_timer_cannot_point_at_a_task_the_caller_may_not_read(make_site, monkeypatch, mode): |
| 85 | site, users = _timer_box(make_site, monkeypatch, mode) |
| 86 | own = _start(site, users["bob"], "") # a running timer the refusals must not stop |
| 87 | assert own.status == 200, _text(own) |
| 88 | |
| 89 | hidden = _start(site, users["bob"], "t-alice") |
| 90 | missing = _start(site, users["bob"], "t-nobody") |
| 91 | |
| 92 | assert hidden.status == 400, _text(hidden) |
| 93 | assert missing.status == 400, _text(missing) |
| 94 | assert _json(hidden)["error"] == _missing("task_id", "tasks", "t-alice") |
| 95 | assert _json(missing)["error"] == _missing("task_id", "tasks", "t-nobody") |
| 96 | assert set(_json(hidden)) == set(_json(missing)) |
| 97 | assert "Traceback" not in _text(missing) |
| 98 | rows = site.rows("time_logs") |
| 99 | assert [row["task_id"] for row in rows] == [""] |
| 100 | assert rows[0]["is_running"] == "true" # refused before anything was written |
| 101 | |
| 102 | |
| 103 | def test_the_timer_answers_what_the_record_api_answers(make_site, monkeypatch): |
| 104 | site, users = _timer_box(make_site, monkeypatch, "enforced") |
| 105 | for task_id in ("t-alice", "t-nobody"): |
| 106 | api = site.call("/collections/time_logs/records", "POST", |
| 107 | {"task_id": task_id, "owner_id": "bob"}, users["bob"]) |
| 108 | action = _start(site, users["bob"], task_id) |
| 109 | assert (api.status, _json(api)["error"]) == (action.status, _json(action)["error"]) |
| 110 | |
| 111 | |
| 112 | @pytest.mark.parametrize("mode", ["enforced", "off"]) |
| 113 | @pytest.mark.parametrize("who,task_id", [ |
| 114 | ("alice", "t-alice"), # owner |
| 115 | ("carol", "t-proj"), # project member |
| 116 | ("dave", "t-alice"), # sharee |
| 117 | ]) |
| 118 | def test_whoever_may_read_the_task_may_still_time_it(make_site, monkeypatch, mode, who, task_id): |
| 119 | site, users = _timer_box(make_site, monkeypatch, mode) |
| 120 | response = _start(site, users[who], task_id) |
| 121 | assert response.status == 200, _text(response) |
| 122 | row = _json(response)["time_log"] |
| 123 | assert (row["owner_id"], row["task_id"], row["is_running"]) == (who, task_id, "true") |
| 124 | |
| 125 | |
| 126 | def test_an_admin_may_time_any_task(make_site, monkeypatch): |
| 127 | site, _users_ = _timer_box(make_site, monkeypatch, "enforced") |
| 128 | admin = site.user("root", roles=["admin"]) |
| 129 | assert _start(site, admin, "t-alice").status == 200 |
| 130 | |
| 131 | |
| 132 | # --- action_confirm_scan: expenses.project_id requires read, entity_id update ---------------- |
| 133 | |
| 134 | def _intake_box(make_site, monkeypatch, mode): |
| 135 | site = make_site("app-intake", "app-finance", "app-entities", "app-projects") |
| 136 | users = _users(site, "alice", "bob", "carol") |
| 137 | _grants(site, tasks=False) |
| 138 | site.create("entities", {"id": "e-alice", "name": "Alice Books", "owner_id": "alice"}) |
| 139 | site.create("entities", {"id": "e-bob", "name": "Bob Books", "owner_id": "bob"}) |
| 140 | _off(monkeypatch, mode) |
| 141 | return site, users |
| 142 | |
| 143 | |
| 144 | def _extracted_scan(site, headers, text="CAFE\nTOTAL 8.99\n", entity_id=""): |
| 145 | response = site.execute("action_scan_ingest", {"text": text, "entity_id": entity_id}, headers) |
| 146 | assert response.status == 200, _text(response) |
| 147 | assert site.execute("system_scan_processor", {"engine": "text"}, site.admin).status == 200 |
| 148 | return _json(response)["scan_id"] |
| 149 | |
| 150 | |
| 151 | def _confirm(site, headers, scan_id, **fields): |
| 152 | return site.execute("action_confirm_scan", |
| 153 | {"scan_id": scan_id, "amount_cents": "899", **fields}, headers) |
| 154 | |
| 155 | |
| 156 | @pytest.mark.parametrize("mode", ["enforced", "off"]) |
| 157 | def test_confirm_cannot_put_an_expense_on_a_project_the_caller_may_not_read( |
| 158 | make_site, monkeypatch, mode): |
| 159 | site, users = _intake_box(make_site, monkeypatch, mode) |
| 160 | scan_id = _extracted_scan(site, users["bob"]) |
| 161 | |
| 162 | hidden = _confirm(site, users["bob"], scan_id, project_id="p-alice") |
| 163 | missing = _confirm(site, users["bob"], scan_id, project_id="p-nobody") |
| 164 | preview = _confirm(site, users["bob"], scan_id, project_id="p-alice", preview=True) |
| 165 | |
| 166 | for response, value in ((hidden, "p-alice"), (missing, "p-nobody"), (preview, "p-alice")): |
| 167 | assert response.status == 400, _text(response) |
| 168 | assert _json(response)["error"] == _missing("project_id", "projects", value) |
| 169 | assert "Traceback" not in _text(response) |
| 170 | assert site.rows("expenses") == [] |
| 171 | assert site.row("scans", scan_id)["status"] == "extracted" |
| 172 | |
| 173 | |
| 174 | def test_confirm_answers_what_the_record_api_answers(make_site, monkeypatch): |
| 175 | site, users = _intake_box(make_site, monkeypatch, "enforced") |
| 176 | scan_id = _extracted_scan(site, users["bob"]) |
| 177 | for project_id in ("p-alice", "p-nobody"): |
| 178 | api = site.call("/collections/expenses/records", "POST", |
| 179 | {"description": "x", "amount_cents": "1", "incurred_on": "2026-12-01", |
| 180 | "project_id": project_id}, |
| 181 | users["bob"]) |
| 182 | action = _confirm(site, users["bob"], scan_id, project_id=project_id) |
| 183 | assert (api.status, _json(api)["error"]) == (action.status, _json(action)["error"]) |
| 184 | |
| 185 | |
| 186 | @pytest.mark.parametrize("mode", ["enforced", "off"]) |
| 187 | def test_confirm_rechecks_the_entity_a_scan_was_stored_with(make_site, monkeypatch, mode): |
| 188 | """A scan written before ingest checked entity_id (or by any writer |
| 189 | below the record API) keeps the pointer it was given.""" |
| 190 | site, users = _intake_box(make_site, monkeypatch, mode) |
| 191 | scan_id = _extracted_scan(site, users["bob"]) |
| 192 | object_records.update_collection_record("scans", scan_id, {"entity_id": "e-alice"}, |
| 193 | base_dir=site.data) |
| 194 | |
| 195 | response = _confirm(site, users["bob"], scan_id) |
| 196 | assert response.status == 400, _text(response) |
| 197 | assert _json(response)["error"] == _missing("entity_id", "entities", "e-alice") |
| 198 | assert site.rows("expenses") == [] |
| 199 | |
| 200 | |
| 201 | @pytest.mark.parametrize("mode", ["enforced", "off"]) |
| 202 | @pytest.mark.parametrize("who,project_id", [ |
| 203 | ("alice", "p-alice"), # owner |
| 204 | ("carol", "p-alice"), # project member |
| 205 | ("bob", ""), # no project |
| 206 | ]) |
| 207 | def test_whoever_may_read_the_project_may_still_put_an_expense_on_it( |
| 208 | make_site, monkeypatch, mode, who, project_id): |
| 209 | site, users = _intake_box(make_site, monkeypatch, mode) |
| 210 | scan_id = _extracted_scan(site, users[who], entity_id="e-bob" if who == "bob" else "") |
| 211 | response = _confirm(site, users[who], scan_id, project_id=project_id) |
| 212 | assert response.status == 200, _text(response) |
| 213 | expense = site.row("expenses", _json(response)["expense_id"]) |
| 214 | assert (expense["owner_id"], expense["project_id"]) == (who, project_id) |
| 215 | |
| 216 | |
| 217 | # --- action_generate_tm_invoice: owner_id and customer_id from the body ---------------------- |
| 218 | # |
| 219 | # Measured before: a manager's run with owner_id naming another user made |
| 220 | # that user the owner of the client invoice and its lines (200), with no |
| 221 | # owner_id the first billed entry's owner got it, and customer_id named any |
| 222 | # contact, a hidden and a missing one alike accepted. |
| 223 | |
| 224 | def _tm_box(make_site): |
| 225 | site = make_site("app-invoices", "app-timers", "app-projects", "app-contacts") |
| 226 | users = _users(site, "dana", "eve") |
| 227 | users["boss"] = site.user("boss", roles=["manager"]) |
| 228 | site.create("projects", {"id": "proj-acme", "name": "Acme", "owner_id": "boss"}) |
| 229 | site.create("contacts", {"id": "c-boss", "first_name": "Acme", "owner_id": "boss"}) |
| 230 | site.create("contacts", {"id": "c-eve", "first_name": "Friend", "owner_id": "eve"}) |
| 231 | site.create("time_logs", {"id": "t1", "project_id": "proj-acme", |
| 232 | "started_at": "2026-06-15T09:00:00Z", |
| 233 | "ended_at": "2026-06-15T10:00:00Z", |
| 234 | "is_running": "false", "billable": "true", |
| 235 | "status": "approved", "duration_seconds": "3600", |
| 236 | "hourly_rate_cents": "15000", "amount_cents": "15000", |
| 237 | "owner_id": "dana"}) |
| 238 | return site, users |
| 239 | |
| 240 | |
| 241 | def _bill(site, headers, **fields): |
| 242 | return site.execute("action_generate_tm_invoice", |
| 243 | {"project_id": "proj-acme", "through_date": "2026-06-30", **fields}, |
| 244 | headers) |
| 245 | |
| 246 | |
| 247 | def test_a_managers_tm_invoice_is_the_managers(make_site): |
| 248 | site, users = _tm_box(make_site) |
| 249 | response = _bill(site, users["boss"], customer_id="c-boss") |
| 250 | assert response.status == 200, _text(response) |
| 251 | (invoice,) = site.rows("invoices") |
| 252 | assert (invoice["owner_id"], invoice["customer_id"]) == ("boss", "c-boss") |
| 253 | assert {line["owner_id"] for line in site.rows("invoice_lines")} == {"boss"} |
| 254 | |
| 255 | |
| 256 | def test_a_manager_cannot_hand_the_invoice_to_somebody_else(make_site): |
| 257 | site, users = _tm_box(make_site) |
| 258 | response = _bill(site, users["boss"], owner_id="eve") |
| 259 | assert response.status == 403, _text(response) |
| 260 | assert _json(response)["field"] == "owner_id" |
| 261 | assert site.rows("invoices") == [] and site.rows("invoice_lines") == [] |
| 262 | assert site.row("time_logs", "t1")["status"] == "approved" |
| 263 | |
| 264 | |
| 265 | def test_a_manager_cannot_bill_a_contact_they_may_not_read(make_site): |
| 266 | site, users = _tm_box(make_site) |
| 267 | hidden = _bill(site, users["boss"], customer_id="c-eve") |
| 268 | missing = _bill(site, users["boss"], customer_id="c-nobody") |
| 269 | for response, value in ((hidden, "c-eve"), (missing, "c-nobody")): |
| 270 | assert response.status == 400, _text(response) |
| 271 | assert _json(response)["error"] == _missing("customer_id", "contacts", value) |
| 272 | assert site.rows("invoices") == [] |
| 273 | assert site.row("time_logs", "t1")["status"] == "approved" |
| 274 | |
| 275 | |
| 276 | def test_an_admin_may_raise_the_invoice_for_anybody(make_site): |
| 277 | site, _users_ = _tm_box(make_site) |
| 278 | response = _bill(site, site.admin, owner_id="eve", customer_id="c-eve") |
| 279 | assert response.status == 200, _text(response) |
| 280 | (invoice,) = site.rows("invoices") |
| 281 | assert (invoice["owner_id"], invoice["customer_id"]) == ("eve", "c-eve") |
| 282 | |
| 283 | |
| 284 | # --- the rule: staff raise from hours belongs to the staff member who raised it -------------- |
| 285 | |
| 286 | def _raised(make_site): |
| 287 | """boss (manager) bills dana's hour; dana is neither raiser nor manager.""" |
| 288 | site, users = _tm_box(make_site) |
| 289 | assert _bill(site, users["boss"]).status == 200 |
| 290 | (invoice,) = site.rows("invoices") |
| 291 | (line,) = site.rows("invoice_lines") |
| 292 | return site, users, invoice, line |
| 293 | |
| 294 | |
| 295 | def test_an_employee_whose_hours_were_billed_cannot_see_or_change_the_invoice(make_site): |
| 296 | site, users, invoice, line = _raised(make_site) |
| 297 | dana = users["dana"] |
| 298 | assert invoice["owner_id"] == "boss" |
| 299 | for path in (f"/collections/invoices/records/{invoice['id']}", |
| 300 | f"/collections/invoice_lines/records/{line['id']}"): |
| 301 | assert site.call(path, headers=dana).status in (403, 404) |
| 302 | assert site.call(path, method="PUT", headers=dana, |
| 303 | payload={"notes": "mine now"}).status in (403, 404) |
| 304 | listing = site.call("/collections/invoices/records", headers=dana) |
| 305 | if listing.status == 200: |
| 306 | assert invoice["id"] not in listing.body.decode() |
| 307 | assert site.row("invoices", invoice["id"])["notes"] == invoice["notes"] |
| 308 | assert site.row("invoice_lines", line["id"])["owner_id"] == "boss" |
| 309 | |
| 310 | |
| 311 | def test_an_admin_may_name_another_owner_for_the_invoice_and_its_lines(make_site): |
| 312 | site, _users_ = _tm_box(make_site) |
| 313 | assert _bill(site, site.admin, owner_id="eve").status == 200 |
| 314 | (invoice,) = site.rows("invoices") |
| 315 | assert invoice["owner_id"] == "eve" |
| 316 | assert {line["owner_id"] for line in site.rows("invoice_lines")} == {"eve"} |
| 317 | |
| 318 | |
| 319 | def test_the_raising_manager_may_regenerate_the_pay_link(make_site): |
| 320 | site, users, invoice, _line = _raised(make_site) |
| 321 | response = site.execute("action_regenerate_portal_link", |
| 322 | {"invoice_id": invoice["id"]}, users["boss"]) |
| 323 | assert response.status == 200, _text(response) |
| 324 | assert site.row("invoices", invoice["id"])["portal_token"] |
| 325 | # the billed employee is not the owner and may not |
| 326 | refused = site.execute("action_regenerate_portal_link", |
| 327 | {"invoice_id": invoice["id"]}, users["dana"]) |
| 328 | assert refused.status == 403, _text(refused) |
| 329 | |
| 330 | |
| 331 | |
| 332 | # --- only an admin moves an invoice, a line or a credit to another owner --------------------- |
| 333 | # |
| 334 | # Measured before: the billing manager's unconditional update grant let a PUT |
| 335 | # of {"owner_id": "eve"} hand any client's invoice (or one of its lines or |
| 336 | # credits) to eve, 200, with the lines left on the old owner. The record API |
| 337 | # now asks an owner change what a create naming that owner asks: an admin |
| 338 | # role or an on_behalf grant, and app-invoices grants on_behalf to nobody. |
| 339 | |
| 340 | def _credited(make_site): |
| 341 | site, users, invoice, line = _raised(make_site) |
| 342 | site.create("invoices", {"id": "cn-1", "number": "CN-1", "doc_type": "credit_note", |
| 343 | "customer_name": "Acme", "owner_id": "boss"}) |
| 344 | site.create("invoice_credits", {"id": "cr-1", "credit_note_id": "cn-1", |
| 345 | "invoice_id": invoice["id"], "amount_cents": "100", |
| 346 | "applied_on": "2026-07-01", "owner_id": "boss"}) |
| 347 | return site, users, {"invoices": invoice["id"], "invoice_lines": line["id"], |
| 348 | "invoice_credits": "cr-1"} |
| 349 | |
| 350 | |
| 351 | def _record_path(collection, record_id): |
| 352 | return f"/collections/{collection}/records/{record_id}" |
| 353 | |
| 354 | |
| 355 | @pytest.mark.parametrize("collection", ["invoices", "invoice_lines", "invoice_credits"]) |
| 356 | def test_a_manager_cannot_move_an_invoice_its_lines_or_credits_to_another_owner( |
| 357 | make_site, collection): |
| 358 | site, users, ids = _credited(make_site) |
| 359 | before = site.row(collection, ids[collection]) |
| 360 | for new_owner in ("eve", "dana"): |
| 361 | response = site.call(_record_path(collection, ids[collection]), method="PUT", |
| 362 | headers=users["boss"], payload={"owner_id": new_owner}) |
| 363 | assert response.status == 403, _text(response) |
| 364 | assert _json(response)["field"] == "owner_id" |
| 365 | assert site.row(collection, ids[collection]) == before |
| 366 | |
| 367 | |
| 368 | @pytest.mark.parametrize("collection", ["invoices", "invoice_lines", "invoice_credits"]) |
| 369 | def test_an_admin_may_move_an_invoice_its_lines_or_credits(make_site, collection): |
| 370 | site, users, ids = _credited(make_site) |
| 371 | olive = site.user("olive", roles=["admin"]) |
| 372 | for headers, new_owner in ((olive, "eve"), (site.admin, "dana")): |
| 373 | response = site.call(_record_path(collection, ids[collection]), method="PUT", |
| 374 | headers=headers, payload={"owner_id": new_owner}) |
| 375 | assert response.status == 200, _text(response) |
| 376 | assert site.row(collection, ids[collection])["owner_id"] == new_owner |
| 377 | |
| 378 | |
| 379 | def test_a_manager_may_put_the_whole_invoice_back_with_its_owner_unchanged(make_site): |
| 380 | """Honest clients send back the record they read: an unchanged owner_id |
| 381 | is no change of owner.""" |
| 382 | site, users, ids = _credited(make_site) |
| 383 | path = _record_path("invoices", ids["invoices"]) |
| 384 | response = site.call(path, headers=users["boss"]) |
| 385 | assert response.status == 200, _text(response) |
| 386 | record = _json(response)["record"] |
| 387 | # A form leaves out what the server stamps (read-only and computed). |
| 388 | schema = object_schemas.get_schema("invoices", base_dir=site.data) |
| 389 | read_only = {field["name"] for field in schema["fields"] |
| 390 | if field.get("read_only") or field.get("computed") |
| 391 | or field.get("type") == "computed"} |
| 392 | body = {key: value for key, value in record.items() |
| 393 | if key not in read_only and not key.startswith("_")} |
| 394 | body["notes"] = "corrected" |
| 395 | response = site.call(path, method="PUT", headers=users["boss"], payload=body) |
| 396 | assert response.status == 200, _text(response) |
| 397 | stored = site.row("invoices", ids["invoices"]) |
| 398 | assert (stored["owner_id"], stored["notes"]) == ("boss", "corrected") |