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.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_app_invoices_detail_retrofit.py · 5.9 KB · 139 lines
| 1 | """Stage-6 retrofit of packages/app-invoices: the bespoke |
| 2 | site_invoice_view.py page is replaced by a seeded 59 detail view composed |
| 3 | of three GENERIC block renderers -- an editable+deletable `detail` block |
| 4 | (the invoice; the status field's own 10-flow transition guards enforce |
| 5 | legal moves through the owner edit form, so no one-click Send/Void |
| 6 | buttons are built, the same make-it-basic trade app-forum's pin/lock |
| 7 | retrofit made), a `related` block (invoice_lines by invoice_id -- lines |
| 8 | stay their own relational collection, never embedded, because they are |
| 9 | the thing receipts/notes attach to later), and a `form` block with a FK |
| 10 | locked to the page's invoice (Add Line). No hand-written money-totals |
| 11 | table either: the detail block's own /form cents formatting renders |
| 12 | subtotal/tax/total/paid/balance correctly. |
| 13 | """ |
| 14 | |
| 15 | import json |
| 16 | from pathlib import Path |
| 17 | |
| 18 | import object_packages |
| 19 | import object_permissions |
| 20 | |
| 21 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 22 | APP_INVOICES_DIR = PACKAGES_ROOT / "app-invoices" |
| 23 | |
| 24 | |
| 25 | def _seed_rows(name): |
| 26 | import csv |
| 27 | |
| 28 | with open(APP_INVOICES_DIR / "seed" / f"{name}.tsv", newline="") as fh: |
| 29 | return list(csv.DictReader(fh, delimiter="\t")) |
| 30 | |
| 31 | |
| 32 | def test_manifest_drops_bespoke_invoice_view_page_and_seeds_the_detail_view(): |
| 33 | package = object_packages.get_package("app-invoices", root=PACKAGES_ROOT) |
| 34 | # 0.6.0 deletes the bespoke /invoices list page object too -- see |
| 35 | # tests/test_app_invoices_package.py. |
| 36 | assert {obj["id"] for obj in package["objects"]} == { |
| 37 | "system_invoice_totals", |
| 38 | "system_invoice_aging", # aging + dunning runner (payments slice 2) |
| 39 | # 0.5.0 portal: /pay/{token}, the regenerate verb, and the handler |
| 40 | # that mints a token when an invoice leaves draft. |
| 41 | "site_invoice_portal", "action_regenerate_portal_link", |
| 42 | "system_invoice_portal_link", |
| 43 | # 0.8.0 time and materials: approved hours become one invoice, the |
| 44 | # last mile of the consulting billing mode. |
| 45 | "action_generate_tm_invoice", |
| 46 | # 0.9.0: invoices past their due date, counted for the home |
| 47 | # page's attention band -- a queue the calendar creates with |
| 48 | # no write happening, so nothing else announces it. |
| 49 | "system_invoice_attention", |
| 50 | } |
| 51 | assert {entry["collection"] for entry in package["seed"]} == { |
| 52 | "invoices", "invoice_lines", "views", "site_routes", |
| 53 | "notify_rules", # dunning: owner in_app ping on overdue (slice 2) |
| 54 | } |
| 55 | assert {dep["id"] for dep in package["dependencies"]} == {"app-views"} |
| 56 | |
| 57 | |
| 58 | def test_invoice_view_object_file_is_deleted(): |
| 59 | assert not (APP_INVOICES_DIR / "objects" / "site" / "invoice_view.py").exists() |
| 60 | |
| 61 | |
| 62 | def test_invoice_view_composes_detail_flat_related_and_fk_locked_form(): |
| 63 | rows = _seed_rows("views") |
| 64 | # 0.6.0 added the /invoices index view alongside this detail view, so |
| 65 | # assert on the detail row rather than a total that keeps growing. |
| 66 | rows = [r for r in rows if r["id"] == "view_invoice_detail"] |
| 67 | assert len(rows) == 1 |
| 68 | view = rows[0] |
| 69 | assert view["id"] == "view_invoice_detail" |
| 70 | assert view["route"] == "/invoices/{invoice_id}" |
| 71 | assert view["layout"] == "single" |
| 72 | blocks = json.loads(view["blocks"]) |
| 73 | kinds = [b["kind"] for b in blocks] |
| 74 | assert kinds == ["detail", "related", "form"] |
| 75 | |
| 76 | detail, related, form = blocks |
| 77 | |
| 78 | # 1. editable+deletable invoice detail |
| 79 | assert detail["collection"] == "invoices" |
| 80 | assert detail["record_id"] == "$record_id" |
| 81 | assert detail["editable"] is True and detail["deletable"] is True |
| 82 | assert detail["delete_redirect"] == "/invoices" |
| 83 | |
| 84 | # 2. FLAT invoice_lines (a plain related list by invoice_id) |
| 85 | assert related["collection"] == "invoice_lines" |
| 86 | assert related["fk_field"] == "invoice_id" |
| 87 | assert related["match"] == "$record_id" |
| 88 | assert related["title"] == "Line Items" |
| 89 | |
| 90 | # 3. add-line form with invoice_id locked to the page's invoice |
| 91 | assert form["collection"] == "invoice_lines" |
| 92 | assert form["fixed"] == {"invoice_id": "$record_id"} |
| 93 | assert form["title"] == "Add Line" |
| 94 | |
| 95 | |
| 96 | def test_permalink_route_points_to_view_render(): |
| 97 | rows = _seed_rows("site_routes") |
| 98 | # 0.5.0 appended /pay/{token}; this test is about the permalink route |
| 99 | # pointing at the generic renderer, so assert on that row rather than |
| 100 | # on a total that will keep growing. |
| 101 | rows = [r for r in rows if r["id"] == "route_invoice_detail"] |
| 102 | assert len(rows) == 1 |
| 103 | assert rows[0]["id"] == "route_invoice_detail" |
| 104 | assert rows[0]["pattern"] == "/invoices/{invoice_id}" |
| 105 | assert rows[0]["object_id"] == "site_view_render" |
| 106 | assert rows[0]["priority"] == "10" |
| 107 | |
| 108 | |
| 109 | def test_permissions_no_longer_reference_the_removed_object(): |
| 110 | payload = json.loads((APP_INVOICES_DIR / "permissions" / "rules.json").read_text()) |
| 111 | object_ids = {rule.get("object_id") for rule in payload["rules"]} |
| 112 | assert "site_invoice_view" not in object_ids |
| 113 | assert "site_invoices" in object_ids |
| 114 | |
| 115 | |
| 116 | def test_no_public_read_survives_the_retrofit(): |
| 117 | """Invoices stay owner-private: the retrofit must not have added a |
| 118 | public read rule on either collection, and the removed object's |
| 119 | public-execute rule must be gone too (already covered above).""" |
| 120 | payload = json.loads((APP_INVOICES_DIR / "permissions" / "rules.json").read_text()) |
| 121 | policy = object_permissions.policy_from_dict( |
| 122 | {"access_mode": "role_based", "rules": payload["rules"]} |
| 123 | ) |
| 124 | for collection in ("invoices", "invoice_lines"): |
| 125 | decision = object_permissions.check_permission( |
| 126 | None, object_permissions.READ, policy=policy, collection=collection, |
| 127 | record={"owner_id": "7"}, |
| 128 | ) |
| 129 | assert decision.allowed is False, f"anonymous read should be denied on {collection}" |
| 130 | |
| 131 | |
| 132 | def test_dry_run_is_safe(tmp_path): |
| 133 | object_root = tmp_path / "objects" |
| 134 | object_root.mkdir() |
| 135 | plan = object_packages.dry_run_package( |
| 136 | "app-invoices", root=PACKAGES_ROOT, base_dir=tmp_path / "data", object_roots=[object_root], |
| 137 | ) |
| 138 | assert plan["safe_to_install"] is True |
| 139 | assert plan["warnings"] == [] |