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_action_owner_checks.py · 8.2 KB · 174 lines
| 1 | """Do apply_count, render_pdf and generate_tm_invoice check who is asking? |
| 2 | |
| 3 | A reviewer suspected, without verifying, that these three actions had no |
| 4 | owner or permission check (2026-11-28). Each is driven here end to end |
| 5 | through the real ASGI app with permission enforcement ON and real sessions |
| 6 | (the Site fixture from test_findings_access_control), as a signed-in user |
| 7 | who does NOT own the records the request names. |
| 8 | |
| 9 | Outcome, verified before the fixes: |
| 10 | |
| 11 | - action_apply_count: CONFIRMED. Any signed-in user could post a count |
| 12 | against anybody's product at anybody's location; it wrote a stock move |
| 13 | that changed the owner's derived stock, and the answer told the caller |
| 14 | the owner's on-hand quantity. |
| 15 | - action_render_pdf: CONFIRMED for {kind, source}. Any signed-in user could |
| 16 | name another user's invoice and receive its full rendered HTML (customer, |
| 17 | lines, amounts) in the 409 body. The {token} path is unchanged: the token |
| 18 | is the bearer capability the public /d/{token} page already honours. |
| 19 | - action_generate_tm_invoice: REFUTED. The object itself checks nothing, |
| 20 | but its only execute grant is `role:manager`, and the server refuses |
| 21 | everybody else before the object runs; a manager may already read and |
| 22 | update every time log and expense, which is all the action does to them. |
| 23 | Pinned here so that loosening the grant fails a test. |
| 24 | """ |
| 25 | |
| 26 | from __future__ import annotations |
| 27 | |
| 28 | import json |
| 29 | |
| 30 | import object_records |
| 31 | import test_findings_access_control as fac |
| 32 | |
| 33 | make_site = fac.make_site # the fixture |
| 34 | |
| 35 | |
| 36 | def _json(response): |
| 37 | return json.loads(response.body.decode("utf-8")) |
| 38 | |
| 39 | |
| 40 | # --- apply_count -------------------------------------------------------------- |
| 41 | |
| 42 | def _catalog(make_site): |
| 43 | site = make_site("app-catalog") |
| 44 | dan = site.user("dan") |
| 45 | eve = site.user("eve") |
| 46 | site.create("products", {"id": "p-dan", "name": "Mug", "sku": "MUG", |
| 47 | "product_type": "physical", "cost_cents": "250", |
| 48 | "price_cents": "1000", "currency": "USD", |
| 49 | "is_active": "true", "owner_id": "dan"}) |
| 50 | site.create("locations", {"id": "loc-dan", "name": "Dan's shelf", |
| 51 | "location_type": "warehouse", "owner_id": "dan"}) |
| 52 | site.create("locations", {"id": "loc-supplier", "name": "Supplier", |
| 53 | "location_type": "supplier", "owner_id": "dan"}) |
| 54 | site.create("stock_moves", {"id": "m-in", "product_id": "p-dan", |
| 55 | "from_location_id": "loc-supplier", |
| 56 | "to_location_id": "loc-dan", "quantity": "10", |
| 57 | "unit_cost_cents": "250", "reason": "purchase", |
| 58 | "occurred_at": "2026-06-01", "owner_id": "dan"}) |
| 59 | return site, dan, eve |
| 60 | |
| 61 | |
| 62 | def test_apply_count_refuses_someone_elses_product(make_site): |
| 63 | site, dan, eve = _catalog(make_site) |
| 64 | response = site.execute("action_apply_count", |
| 65 | {"product_id": "p-dan", "location_id": "loc-dan", |
| 66 | "counted_qty": "0"}, eve) |
| 67 | body = _json(response) |
| 68 | moves = site.rows("stock_moves") |
| 69 | assert [m["id"] for m in moves] == ["m-in"], ( |
| 70 | f"eve's count wrote {len(moves) - 1} move(s) into dan's stock: {body}") |
| 71 | assert "on_hand_before" not in json.dumps(body), body |
| 72 | assert body.get("status") == 404 or response.status == 404, body |
| 73 | |
| 74 | |
| 75 | def test_apply_count_refuses_someone_elses_location(make_site): |
| 76 | site, dan, eve = _catalog(make_site) |
| 77 | site.create("products", {"id": "p-eve", "name": "Cup", "sku": "CUP", |
| 78 | "product_type": "physical", "cost_cents": "100", |
| 79 | "price_cents": "500", "currency": "USD", |
| 80 | "is_active": "true", "owner_id": "eve"}) |
| 81 | response = site.execute("action_apply_count", |
| 82 | {"product_id": "p-eve", "location_id": "loc-dan", |
| 83 | "counted_qty": "5"}, eve) |
| 84 | body = _json(response) |
| 85 | assert [m["id"] for m in site.rows("stock_moves")] == ["m-in"], body |
| 86 | assert body.get("status") == 404, body |
| 87 | |
| 88 | |
| 89 | def test_apply_count_still_works_for_the_owner(make_site): |
| 90 | site, dan, eve = _catalog(make_site) |
| 91 | response = site.execute("action_apply_count", |
| 92 | {"product_id": "p-dan", "location_id": "loc-dan", |
| 93 | "counted_qty": "9", "counted_on": "2026-06-02"}, dan) |
| 94 | body = _json(response) |
| 95 | assert body.get("status") == 200 and body["variance"] == "-1", body |
| 96 | assert len(site.rows("stock_moves")) == 2 |
| 97 | |
| 98 | |
| 99 | # --- render_pdf --------------------------------------------------------------- |
| 100 | |
| 101 | def _invoices(make_site): |
| 102 | site = make_site("app-settings", "app-documents", "app-invoices") |
| 103 | dan = site.user("dan") |
| 104 | eve = site.user("eve") |
| 105 | site.create("app_settings", {"id": "s-biz", "key": "business.name", |
| 106 | "value": "Dan's Shop"}) |
| 107 | site.create("invoices", {"id": "inv-dan", "number": "INV-7001", |
| 108 | "customer_name": "Secret Customer Ltd", |
| 109 | "status": "sent", "issue_date": "2026-06-01", |
| 110 | "due_date": "2026-06-15", "currency": "USD", |
| 111 | "subtotal_cents": "123400", "total_cents": "123400", |
| 112 | "owner_id": "dan"}) |
| 113 | site.create("invoice_lines", {"id": "il-1", "invoice_id": "inv-dan", |
| 114 | "description": "Confidential retainer", |
| 115 | "quantity": "1", "unit_price_cents": "123400", |
| 116 | "line_total_cents": "123400", "owner_id": "dan"}) |
| 117 | return site, dan, eve |
| 118 | |
| 119 | |
| 120 | def test_render_pdf_does_not_render_someone_elses_invoice(make_site): |
| 121 | site, dan, eve = _invoices(make_site) |
| 122 | response = site.execute("action_render_pdf", |
| 123 | {"kind": "invoice", "source": "invoices/inv-dan"}, eve) |
| 124 | text = response.body.decode("utf-8", "replace") |
| 125 | assert "Secret Customer" not in text and "Confidential retainer" not in text, ( |
| 126 | f"eve received dan's invoice: {text[:300]}") |
| 127 | assert _json(response).get("status") == 404 |
| 128 | |
| 129 | |
| 130 | def test_render_pdf_still_renders_the_owners_invoice(make_site): |
| 131 | site, dan, eve = _invoices(make_site) |
| 132 | body = _json(site.execute("action_render_pdf", |
| 133 | {"kind": "invoice", "source": "invoices/inv-dan"}, dan)) |
| 134 | assert body.get("status") == 409 # no engine: the honest refusal |
| 135 | assert "Secret Customer" in body["html"] and "Confidential retainer" in body["html"] |
| 136 | |
| 137 | |
| 138 | # --- generate_tm_invoice ------------------------------------------------------ |
| 139 | |
| 140 | def _tm(make_site): |
| 141 | site = make_site("app-invoices", "app-timers", "app-projects") |
| 142 | site.create("projects", {"id": "proj-acme", "name": "Acme", "owner_id": "boss"}) |
| 143 | site.create("time_logs", {"id": "t1", "project_id": "proj-acme", |
| 144 | "started_at": "2026-06-15T09:00:00Z", |
| 145 | "ended_at": "2026-06-15T10:00:00Z", |
| 146 | "is_running": "false", "billable": "true", |
| 147 | "status": "approved", "duration_seconds": "3600", |
| 148 | "hourly_rate_cents": "15000", "amount_cents": "15000", |
| 149 | "owner_id": "dana"}) |
| 150 | return site |
| 151 | |
| 152 | |
| 153 | def test_generate_tm_invoice_is_refused_to_a_user_who_is_not_a_manager(make_site): |
| 154 | site = _tm(make_site) |
| 155 | eve = site.user("eve") |
| 156 | dana = site.user("dana") # owns the hours, still not a biller |
| 157 | for who in (eve, dana): |
| 158 | response = site.execute("action_generate_tm_invoice", |
| 159 | {"project_id": "proj-acme", |
| 160 | "through_date": "2026-06-30"}, who) |
| 161 | assert response.status == 403, response.body[:200] |
| 162 | assert site.rows("invoices") == [] |
| 163 | assert site.row("time_logs", "t1")["status"] == "approved" |
| 164 | |
| 165 | |
| 166 | def test_generate_tm_invoice_runs_for_a_manager(make_site): |
| 167 | site = _tm(make_site) |
| 168 | boss = site.user("boss", roles=["manager"]) |
| 169 | response = site.execute("action_generate_tm_invoice", |
| 170 | {"project_id": "proj-acme", |
| 171 | "through_date": "2026-06-30"}, boss) |
| 172 | assert response.status == 200, response.body[:300] |
| 173 | assert _json(response).get("invoiced") == 1 |
| 174 | assert len(site.rows("invoices")) == 1 |