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_object_collections.py · 7.4 KB · 225 lines
| 1 | from pathlib import Path |
| 2 | |
| 3 | import pytest |
| 4 | |
| 5 | import object_collections |
| 6 | import object_logs |
| 7 | import object_permission_store |
| 8 | import object_permissions |
| 9 | |
| 10 | |
| 11 | def write_source(path: Path, content: str = "def GET(request):\n return {}\n") -> Path: |
| 12 | path.parent.mkdir(parents=True, exist_ok=True) |
| 13 | path.write_text(content) |
| 14 | return path |
| 15 | |
| 16 | |
| 17 | def write_state(data_dir: Path, object_id: str) -> None: |
| 18 | state_file = data_dir / "state" / object_id / "state.tsv" |
| 19 | state_file.parent.mkdir(parents=True, exist_ok=True) |
| 20 | state_file.write_text("count\t2\n") |
| 21 | |
| 22 | |
| 23 | def write_file(data_dir: Path, object_id: str, filename: str, content: bytes = b"hello") -> None: |
| 24 | file_path = data_dir / "files" / object_id / filename |
| 25 | file_path.parent.mkdir(parents=True, exist_ok=True) |
| 26 | file_path.write_bytes(content) |
| 27 | |
| 28 | |
| 29 | def write_records(data_dir: Path, collection: str, content: str = "id\tname\nr1\tAda\n") -> None: |
| 30 | records_file = data_dir / "collections" / collection / "records.tsv" |
| 31 | records_file.parent.mkdir(parents=True, exist_ok=True) |
| 32 | records_file.write_text(content) |
| 33 | |
| 34 | |
| 35 | def save_policy(data_dir: Path, policy: object_permissions.PermissionPolicy) -> None: |
| 36 | object_permission_store.save_policy(policy, base_dir=data_dir) |
| 37 | |
| 38 | |
| 39 | def by_name(collections: list[dict]) -> dict[str, dict]: |
| 40 | return {item["name"]: item for item in collections} |
| 41 | |
| 42 | |
| 43 | def test_list_collections_derives_summaries_from_sources_state_logs_files_and_policy(tmp_path): |
| 44 | root = tmp_path / "objects" |
| 45 | data_dir = tmp_path / "data" |
| 46 | write_source(root / "site" / "home.py") |
| 47 | write_source(root / "site" / "about.py") |
| 48 | write_source(root / "users" / "42" / "deals.py") |
| 49 | write_source(root / "users" / "42" / "deals_report.py") |
| 50 | write_state(data_dir, "site_home") |
| 51 | object_logs.append_object_log("site_home", "INFO", "served", base_dir=data_dir) |
| 52 | write_file(data_dir, "site_home", "card.txt") |
| 53 | write_records(data_dir, "site") |
| 54 | save_policy( |
| 55 | data_dir, |
| 56 | object_permissions.PermissionPolicy( |
| 57 | access_mode="role_based", |
| 58 | rules=( |
| 59 | object_permissions.PermissionRule.allow( |
| 60 | "role:admin", |
| 61 | {object_permissions.READ, object_permissions.EXECUTE}, |
| 62 | collection="site", |
| 63 | ), |
| 64 | object_permissions.PermissionRule.deny( |
| 65 | "public", |
| 66 | {object_permissions.UPDATE}, |
| 67 | collection="site", |
| 68 | ), |
| 69 | object_permissions.PermissionRule.allow( |
| 70 | "subscription:pro", |
| 71 | {object_permissions.READ}, |
| 72 | collection="deals", |
| 73 | ), |
| 74 | ), |
| 75 | ), |
| 76 | ) |
| 77 | |
| 78 | collections = by_name( |
| 79 | object_collections.list_collections(base_dir=data_dir, roots=[root]) |
| 80 | ) |
| 81 | |
| 82 | assert sorted(collections) == ["deals", "site"] |
| 83 | assert collections["site"] == { |
| 84 | "name": "site", |
| 85 | "object_count": 2, |
| 86 | "file_count": 1, |
| 87 | "state_object_count": 1, |
| 88 | "log_object_count": 1, |
| 89 | "has_records": True, |
| 90 | "has_schema": False, |
| 91 | "owners": ["system"], |
| 92 | "kinds": {"system": 2}, |
| 93 | "permission": { |
| 94 | "access_mode": "role_based", |
| 95 | "rule_count": 2, |
| 96 | "allow_count": 1, |
| 97 | "deny_count": 1, |
| 98 | "actions": ["execute", "read", "update"], |
| 99 | "principals": ["public", "role:admin"], |
| 100 | }, |
| 101 | } |
| 102 | assert collections["deals"]["object_count"] == 2 |
| 103 | assert collections["deals"]["owners"] == ["42"] |
| 104 | assert collections["deals"]["kinds"] == {"user": 2} |
| 105 | assert collections["deals"]["permission"]["principals"] == ["subscription:pro"] |
| 106 | |
| 107 | |
| 108 | def test_get_collection_includes_object_details(tmp_path): |
| 109 | root = tmp_path / "objects" |
| 110 | data_dir = tmp_path / "data" |
| 111 | write_source(root / "apps" / "widget_counter.py") |
| 112 | write_state(data_dir, "apps_widget_counter") |
| 113 | write_file(data_dir, "apps_widget_counter", "nested/widget.json", b"{}") |
| 114 | |
| 115 | collection = object_collections.get_collection("apps", base_dir=data_dir, roots=[root]) |
| 116 | |
| 117 | assert collection["name"] == "apps" |
| 118 | assert collection["object_count"] == 1 |
| 119 | assert collection["has_records"] is False |
| 120 | assert collection["objects"] == [ |
| 121 | { |
| 122 | "object_id": "apps_widget_counter", |
| 123 | "path": "apps/widget_counter.py", |
| 124 | "owner": "system", |
| 125 | "kind": "system", |
| 126 | "state_count": 1, |
| 127 | "has_logs": False, |
| 128 | "file_count": 1, |
| 129 | } |
| 130 | ] |
| 131 | |
| 132 | |
| 133 | def test_permission_only_collection_is_listed(tmp_path): |
| 134 | data_dir = tmp_path / "data" |
| 135 | root = tmp_path / "objects" |
| 136 | save_policy( |
| 137 | data_dir, |
| 138 | object_permissions.PermissionPolicy( |
| 139 | rules=( |
| 140 | object_permissions.PermissionRule.allow( |
| 141 | "role:billing", |
| 142 | {object_permissions.READ}, |
| 143 | collection="invoices", |
| 144 | ), |
| 145 | ), |
| 146 | ), |
| 147 | ) |
| 148 | |
| 149 | collections = object_collections.list_collections(base_dir=data_dir, roots=[root]) |
| 150 | |
| 151 | assert collections == [ |
| 152 | { |
| 153 | "name": "invoices", |
| 154 | "object_count": 0, |
| 155 | "file_count": 0, |
| 156 | "state_object_count": 0, |
| 157 | "log_object_count": 0, |
| 158 | "has_records": False, |
| 159 | "has_schema": False, |
| 160 | "owners": [], |
| 161 | "kinds": {}, |
| 162 | "permission": { |
| 163 | "access_mode": "role_based", |
| 164 | "rule_count": 1, |
| 165 | "allow_count": 1, |
| 166 | "deny_count": 0, |
| 167 | "actions": ["read"], |
| 168 | "principals": ["role:billing"], |
| 169 | }, |
| 170 | } |
| 171 | ] |
| 172 | |
| 173 | |
| 174 | def test_record_only_collection_is_listed(tmp_path): |
| 175 | data_dir = tmp_path / "data" |
| 176 | root = tmp_path / "objects" |
| 177 | write_records(data_dir, "contacts") |
| 178 | |
| 179 | collections = object_collections.list_collections(base_dir=data_dir, roots=[root]) |
| 180 | |
| 181 | assert collections == [ |
| 182 | { |
| 183 | "name": "contacts", |
| 184 | "object_count": 0, |
| 185 | "file_count": 0, |
| 186 | "state_object_count": 0, |
| 187 | "log_object_count": 0, |
| 188 | "has_records": True, |
| 189 | "has_schema": False, |
| 190 | "owners": [], |
| 191 | "kinds": {}, |
| 192 | "permission": { |
| 193 | "access_mode": "role_based", |
| 194 | "rule_count": 0, |
| 195 | "allow_count": 0, |
| 196 | "deny_count": 0, |
| 197 | "actions": [], |
| 198 | "principals": [], |
| 199 | }, |
| 200 | } |
| 201 | ] |
| 202 | |
| 203 | |
| 204 | def test_get_collection_rejects_unsafe_names(tmp_path): |
| 205 | with pytest.raises(object_collections.InvalidCollectionNameError): |
| 206 | object_collections.get_collection("../bad", base_dir=tmp_path / "data", roots=[]) |
| 207 | |
| 208 | |
| 209 | def test_get_collection_rejects_missing_collections(tmp_path): |
| 210 | with pytest.raises(object_collections.CollectionNotFoundError): |
| 211 | object_collections.get_collection("missing", base_dir=tmp_path / "data", roots=[]) |
| 212 | |
| 213 | |
| 214 | def test_schema_defined_collections_are_listed(tmp_path): |
| 215 | schema_file = tmp_path / "schemas" / "site_routes.json" |
| 216 | schema_file.parent.mkdir(parents=True) |
| 217 | schema_file.write_text('{"fields": [{"name": "pattern"}, {"name": "object_id"}]}') |
| 218 | |
| 219 | collections = object_collections.list_collections(base_dir=tmp_path, roots=[tmp_path / "objects"]) |
| 220 | by_name = {item["name"]: item for item in collections} |
| 221 | |
| 222 | assert "site_routes" in by_name |
| 223 | assert by_name["site_routes"]["has_schema"] is True |
| 224 | assert by_name["site_routes"]["has_records"] is False |
| 225 | assert by_name["site_routes"]["object_count"] == 0 |