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_search.py · 15.8 KB · 475 lines
| 1 | """Tests for schema-driven cross-collection search matching and the endpoint.""" |
| 2 | |
| 3 | import json |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | import object_search |
| 8 | import object_server |
| 9 | |
| 10 | from test_object_server import ( |
| 11 | auth_headers, |
| 12 | enable_admin_token, |
| 13 | request, |
| 14 | save_permission_policy, |
| 15 | write_records, |
| 16 | ) |
| 17 | |
| 18 | |
| 19 | TASKS_SCHEMA = { |
| 20 | "name": "tasks", |
| 21 | "search": {"fields": ["title", "instructions"]}, |
| 22 | "views": {"list_fields": ["title", "urgency"]}, |
| 23 | } |
| 24 | |
| 25 | |
| 26 | def test_search_config_missing_returns_none(): |
| 27 | assert object_search.search_config({"name": "tasks"}) is None |
| 28 | |
| 29 | |
| 30 | def test_search_config_normalizes_fields_and_result_fields(): |
| 31 | config = object_search.search_config(TASKS_SCHEMA) |
| 32 | assert config == { |
| 33 | "fields": ["title", "instructions"], |
| 34 | "result_fields": ["id", "title", "urgency"], |
| 35 | } |
| 36 | |
| 37 | |
| 38 | def test_search_config_defaults_result_fields_to_search_fields(): |
| 39 | config = object_search.search_config({"search": {"fields": ["content"]}}) |
| 40 | assert config == {"fields": ["content"], "result_fields": ["id", "content"]} |
| 41 | |
| 42 | |
| 43 | def test_search_config_respects_explicit_result_fields(): |
| 44 | config = object_search.search_config( |
| 45 | {"search": {"fields": ["title"], "result_fields": ["id", "status"]}} |
| 46 | ) |
| 47 | assert config["result_fields"] == ["id", "status"] |
| 48 | |
| 49 | |
| 50 | @pytest.mark.parametrize( |
| 51 | "section", |
| 52 | [ |
| 53 | "title", |
| 54 | {"fields": []}, |
| 55 | {"fields": "title"}, |
| 56 | {"fields": [1]}, |
| 57 | {"fields": ["title"], "result_fields": "id"}, |
| 58 | {"fields": ["title"], "result_fields": [None]}, |
| 59 | ], |
| 60 | ) |
| 61 | def test_search_config_rejects_malformed_sections(section): |
| 62 | with pytest.raises(object_search.InvalidSearchConfigError): |
| 63 | object_search.search_config({"search": section}) |
| 64 | |
| 65 | |
| 66 | def test_terms_are_anded_and_fields_are_ored(): |
| 67 | record = {"id": "t1", "title": "Fix flywheel", "instructions": "growth loop"} |
| 68 | assert object_search.record_matches(record, "flywheel growth", ["title", "instructions"]) |
| 69 | assert object_search.record_matches(record, "FLYWHEEL", ["title", "instructions"]) |
| 70 | assert not object_search.record_matches(record, "flywheel missing", ["title", "instructions"]) |
| 71 | assert not object_search.record_matches(record, "flywheel", ["instructions"]) |
| 72 | |
| 73 | |
| 74 | def test_blank_query_matches_nothing(): |
| 75 | record = {"id": "t1", "title": "anything"} |
| 76 | assert not object_search.record_matches(record, " ", ["title"]) |
| 77 | |
| 78 | |
| 79 | def test_id_prefix_queries_match_record_ids(): |
| 80 | record = {"id": "9F3A2B10-demo", "title": "unrelated"} |
| 81 | assert object_search.looks_like_id_prefix("9f3a") |
| 82 | assert not object_search.looks_like_id_prefix("9f3") |
| 83 | assert not object_search.looks_like_id_prefix("fix flywheel") |
| 84 | assert object_search.record_matches(record, "9f3a", ["title"]) |
| 85 | assert not object_search.record_matches(record, "aaaa", ["title"]) |
| 86 | |
| 87 | |
| 88 | def test_search_records_trims_to_result_fields_and_limit(): |
| 89 | config = object_search.search_config(TASKS_SCHEMA) |
| 90 | records = [ |
| 91 | {"id": f"t{index}", "title": "flywheel", "urgency": "high", "instructions": "secret"} |
| 92 | for index in range(5) |
| 93 | ] |
| 94 | matches = object_search.search_records(records, "flywheel", config, limit=3) |
| 95 | assert len(matches) == 3 |
| 96 | assert matches[0] == {"id": "t0", "title": "flywheel", "urgency": "high"} |
| 97 | assert all("instructions" not in match for match in matches) |
| 98 | |
| 99 | |
| 100 | def test_search_records_rejects_bad_limit(): |
| 101 | config = object_search.search_config(TASKS_SCHEMA) |
| 102 | with pytest.raises(ValueError): |
| 103 | object_search.search_records([], "query", config, limit=0) |
| 104 | |
| 105 | |
| 106 | def write_schema(data_dir, name, payload): |
| 107 | schema_file = data_dir / "schemas" / f"{name}.json" |
| 108 | schema_file.parent.mkdir(parents=True, exist_ok=True) |
| 109 | schema_file.write_text(json.dumps(payload)) |
| 110 | |
| 111 | |
| 112 | def searchable_fixture(data_dir): |
| 113 | write_schema( |
| 114 | data_dir, |
| 115 | "notes", |
| 116 | {"fields": [{"name": "id"}, {"name": "content"}], "search": {"fields": ["content"]}}, |
| 117 | ) |
| 118 | write_schema( |
| 119 | data_dir, |
| 120 | "tasks", |
| 121 | { |
| 122 | "fields": [{"name": "id"}, {"name": "title"}, {"name": "urgency"}], |
| 123 | "search": {"fields": ["title"]}, |
| 124 | "views": {"list_fields": ["title", "urgency"]}, |
| 125 | }, |
| 126 | ) |
| 127 | write_records( |
| 128 | data_dir, |
| 129 | "notes", |
| 130 | "id\tcontent\towner_id\nn1\tflywheel growth loop\t7\nn2\tunrelated memo\t8\n", |
| 131 | ) |
| 132 | write_records( |
| 133 | data_dir, |
| 134 | "tasks", |
| 135 | "id\ttitle\turgency\nt1\tSpin the flywheel\thigh\nt2\tOther work\tlow\n", |
| 136 | ) |
| 137 | write_records(data_dir, "plain", "id\tname\np1\tflywheel but unsearchable\n") |
| 138 | |
| 139 | |
| 140 | def test_search_endpoint_requires_admin_token_by_default(tmp_path, monkeypatch): |
| 141 | data_dir = tmp_path / "data" |
| 142 | searchable_fixture(data_dir) |
| 143 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 144 | enable_admin_token(monkeypatch) |
| 145 | |
| 146 | status, _, payload = request("/api/search", query_string="q=flywheel") |
| 147 | assert status == 401 |
| 148 | assert "error" in payload |
| 149 | |
| 150 | status, _, payload = request( |
| 151 | "/api/search", query_string="q=flywheel", headers=auth_headers() |
| 152 | ) |
| 153 | assert status == 200 |
| 154 | assert payload["status"] == "ok" |
| 155 | |
| 156 | |
| 157 | def test_search_endpoint_matches_only_searchable_collections(tmp_path, monkeypatch): |
| 158 | data_dir = tmp_path / "data" |
| 159 | searchable_fixture(data_dir) |
| 160 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 161 | enable_admin_token(monkeypatch) |
| 162 | |
| 163 | status, _, payload = request( |
| 164 | "/api/search", query_string="q=flywheel", headers=auth_headers() |
| 165 | ) |
| 166 | |
| 167 | assert status == 200 |
| 168 | assert payload["query"] == "flywheel" |
| 169 | assert payload["results"]["notes"] == [{"id": "n1", "content": "flywheel growth loop"}] |
| 170 | assert payload["results"]["tasks"] == [ |
| 171 | {"id": "t1", "title": "Spin the flywheel", "urgency": "high"} |
| 172 | ] |
| 173 | assert "plain" not in payload["results"] |
| 174 | assert payload["total_count"] == 2 |
| 175 | |
| 176 | |
| 177 | def test_search_endpoint_requires_query(tmp_path, monkeypatch): |
| 178 | data_dir = tmp_path / "data" |
| 179 | searchable_fixture(data_dir) |
| 180 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 181 | enable_admin_token(monkeypatch) |
| 182 | |
| 183 | status, _, payload = request("/api/search", headers=auth_headers()) |
| 184 | assert status == 400 |
| 185 | assert "q" in payload["error"] |
| 186 | |
| 187 | |
| 188 | def test_search_endpoint_filters_requested_collections(tmp_path, monkeypatch): |
| 189 | data_dir = tmp_path / "data" |
| 190 | searchable_fixture(data_dir) |
| 191 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 192 | enable_admin_token(monkeypatch) |
| 193 | |
| 194 | status, _, payload = request( |
| 195 | "/api/search", |
| 196 | query_string="q=flywheel&collections=notes", |
| 197 | headers=auth_headers(), |
| 198 | ) |
| 199 | assert status == 200 |
| 200 | assert list(payload["results"]) == ["notes"] |
| 201 | assert payload["total_count"] == 1 |
| 202 | |
| 203 | |
| 204 | def test_search_endpoint_warns_on_malformed_search_config(tmp_path, monkeypatch): |
| 205 | data_dir = tmp_path / "data" |
| 206 | write_schema( |
| 207 | data_dir, |
| 208 | "broken", |
| 209 | {"fields": [{"name": "id"}], "search": {"fields": []}}, |
| 210 | ) |
| 211 | write_records(data_dir, "broken", "id\nb1\n") |
| 212 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 213 | enable_admin_token(monkeypatch) |
| 214 | |
| 215 | status, _, payload = request( |
| 216 | "/api/search", query_string="q=anything", headers=auth_headers() |
| 217 | ) |
| 218 | assert status == 200 |
| 219 | assert payload["results"] == {} |
| 220 | assert payload["warnings"] == ["broken: Schema search.fields must be a non-empty list"] |
| 221 | |
| 222 | |
| 223 | def test_search_endpoint_public_row_filter_serves_anonymous(tmp_path, monkeypatch): |
| 224 | data_dir = tmp_path / "data" |
| 225 | write_schema( |
| 226 | data_dir, |
| 227 | "notes", |
| 228 | { |
| 229 | "fields": [ |
| 230 | {"name": "id"}, |
| 231 | {"name": "content"}, |
| 232 | {"name": "is_public", "type": "boolean", "default": "false"}, |
| 233 | ], |
| 234 | "search": {"fields": ["content"]}, |
| 235 | }, |
| 236 | ) |
| 237 | write_records( |
| 238 | data_dir, |
| 239 | "notes", |
| 240 | "id\tcontent\tis_public\towner_id\n" |
| 241 | "n1\tshared flywheel note\ttrue\t7\n" |
| 242 | "n2\tprivate flywheel note\tfalse\t7\n", |
| 243 | ) |
| 244 | save_permission_policy( |
| 245 | data_dir, |
| 246 | { |
| 247 | "access_mode": "role_based", |
| 248 | "rules": [ |
| 249 | { |
| 250 | "effect": "allow", |
| 251 | "principal": "registered", |
| 252 | "actions": ["create", "read", "update", "delete"], |
| 253 | "collection": "notes", |
| 254 | "row_filter": {"owner_id": "$user_id"}, |
| 255 | "reason": "owners manage their own notes", |
| 256 | }, |
| 257 | { |
| 258 | "effect": "allow", |
| 259 | "principal": "public", |
| 260 | "actions": ["read"], |
| 261 | "collection": "notes", |
| 262 | "row_filter": {"is_public": "true"}, |
| 263 | "reason": "public notes are readable by anyone", |
| 264 | }, |
| 265 | ], |
| 266 | }, |
| 267 | ) |
| 268 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 269 | monkeypatch.setenv(object_server.PERMISSION_ENFORCEMENT_ENV, "true") |
| 270 | monkeypatch.setenv(object_server.PERMISSION_TRUST_HEADERS_ENV, "true") |
| 271 | enable_admin_token(monkeypatch) |
| 272 | |
| 273 | anonymous_status, _, anonymous = request("/api/search", query_string="q=flywheel") |
| 274 | assert anonymous_status == 200 |
| 275 | assert [note["id"] for note in anonymous["results"]["notes"]] == ["n1"] |
| 276 | |
| 277 | owner_status, _, owner = request( |
| 278 | "/api/search", |
| 279 | query_string="q=flywheel", |
| 280 | headers=[("x-dbbasic-user-id", "7"), ("x-dbbasic-roles", "member")], |
| 281 | ) |
| 282 | assert owner_status == 200 |
| 283 | assert [note["id"] for note in owner["results"]["notes"]] == ["n1", "n2"] |
| 284 | |
| 285 | private_record_status, _, _ = request("/collections/notes/records/n2") |
| 286 | assert private_record_status == 403 |
| 287 | public_record_status, _, public_record = request("/collections/notes/records/n1") |
| 288 | assert public_record_status == 200 |
| 289 | assert public_record["record"]["content"] == "shared flywheel note" |
| 290 | |
| 291 | |
| 292 | def test_project_access_grants_shared_visibility(tmp_path, monkeypatch): |
| 293 | data_dir = tmp_path / "data" |
| 294 | write_schema( |
| 295 | data_dir, |
| 296 | "notes", |
| 297 | { |
| 298 | "fields": [{"name": "id"}, {"name": "content"}, {"name": "project_id"}], |
| 299 | "search": {"fields": ["content"]}, |
| 300 | }, |
| 301 | ) |
| 302 | write_records( |
| 303 | data_dir, |
| 304 | "notes", |
| 305 | "id\tcontent\tproject_id\towner_id\n" |
| 306 | "n1\tshared flywheel plan\tp1\t7\n" |
| 307 | "n2\tprivate flywheel plan\tp9\t7\n", |
| 308 | ) |
| 309 | write_records( |
| 310 | data_dir, |
| 311 | "project_access", |
| 312 | "id\tproject_id\tuser_id\tpermission\n" |
| 313 | "g1\tp1\t8\tread\n", |
| 314 | ) |
| 315 | save_permission_policy( |
| 316 | data_dir, |
| 317 | { |
| 318 | "access_mode": "role_based", |
| 319 | "rules": [ |
| 320 | { |
| 321 | "effect": "allow", |
| 322 | "principal": "registered", |
| 323 | "actions": ["create", "read", "update", "delete"], |
| 324 | "collection": "notes", |
| 325 | "row_filter": {"owner_id": "$user_id"}, |
| 326 | "reason": "owners manage their own notes", |
| 327 | }, |
| 328 | { |
| 329 | "effect": "allow", |
| 330 | "principal": "registered", |
| 331 | "actions": ["read"], |
| 332 | "collection": "notes", |
| 333 | "row_filter": {"project_id": "$accessible_projects"}, |
| 334 | "reason": "notes in shared projects are readable", |
| 335 | }, |
| 336 | ], |
| 337 | }, |
| 338 | ) |
| 339 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 340 | monkeypatch.setenv(object_server.PERMISSION_ENFORCEMENT_ENV, "true") |
| 341 | monkeypatch.setenv(object_server.PERMISSION_TRUST_HEADERS_ENV, "true") |
| 342 | enable_admin_token(monkeypatch) |
| 343 | |
| 344 | granted_headers = [("x-dbbasic-user-id", "8"), ("x-dbbasic-roles", "member")] |
| 345 | |
| 346 | status, _, listed = request("/collections/notes/records", headers=granted_headers) |
| 347 | assert status == 200 |
| 348 | assert [note["id"] for note in listed["records"]] == ["n1"] |
| 349 | |
| 350 | status, _, found = request( |
| 351 | "/api/search", query_string="q=flywheel", headers=granted_headers |
| 352 | ) |
| 353 | assert status == 200 |
| 354 | assert [note["id"] for note in found["results"]["notes"]] == ["n1"] |
| 355 | |
| 356 | status, _, _ = request("/collections/notes/records/n2", headers=granted_headers) |
| 357 | assert status == 403 |
| 358 | |
| 359 | ungranted_headers = [("x-dbbasic-user-id", "9"), ("x-dbbasic-roles", "member")] |
| 360 | status, _, found = request( |
| 361 | "/api/search", query_string="q=flywheel", headers=ungranted_headers |
| 362 | ) |
| 363 | assert status == 200 |
| 364 | assert found["results"]["notes"] == [] |
| 365 | |
| 366 | |
| 367 | def test_project_owners_manage_grants_self_serve(tmp_path, monkeypatch): |
| 368 | data_dir = tmp_path / "data" |
| 369 | write_schema( |
| 370 | data_dir, |
| 371 | "project_access", |
| 372 | { |
| 373 | "fields": [ |
| 374 | {"name": "id"}, |
| 375 | {"name": "project_id", "required": True}, |
| 376 | {"name": "user_id", "required": True}, |
| 377 | ], |
| 378 | }, |
| 379 | ) |
| 380 | write_records(data_dir, "projects", "id\tname\towner_id\np1\tAlpha\t7\np9\tOther\t9\n") |
| 381 | write_records(data_dir, "project_access", "id\tproject_id\tuser_id\n") |
| 382 | save_permission_policy( |
| 383 | data_dir, |
| 384 | { |
| 385 | "access_mode": "role_based", |
| 386 | "rules": [ |
| 387 | { |
| 388 | "effect": "allow", |
| 389 | "principal": "registered", |
| 390 | "actions": ["create", "read", "delete"], |
| 391 | "collection": "project_access", |
| 392 | "row_filter": {"project_id": "$owned_projects"}, |
| 393 | "reason": "owners share their own projects", |
| 394 | } |
| 395 | ], |
| 396 | }, |
| 397 | ) |
| 398 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 399 | monkeypatch.setenv(object_server.PERMISSION_ENFORCEMENT_ENV, "true") |
| 400 | monkeypatch.setenv(object_server.PERMISSION_TRUST_HEADERS_ENV, "true") |
| 401 | enable_admin_token(monkeypatch) |
| 402 | |
| 403 | owner_headers = [("x-dbbasic-user-id", "7"), ("x-dbbasic-roles", "member")] |
| 404 | |
| 405 | status, _, granted = request( |
| 406 | "/collections/project_access/records", |
| 407 | method="POST", |
| 408 | body=json.dumps({"id": "g1", "project_id": "p1", "user_id": "8"}).encode(), |
| 409 | headers=owner_headers + [("content-type", "application/json")], |
| 410 | ) |
| 411 | assert status == 201, granted |
| 412 | |
| 413 | status, _, foreign = request( |
| 414 | "/collections/project_access/records", |
| 415 | method="POST", |
| 416 | body=json.dumps({"id": "g2", "project_id": "p9", "user_id": "7"}).encode(), |
| 417 | headers=owner_headers + [("content-type", "application/json")], |
| 418 | ) |
| 419 | assert status == 403, foreign |
| 420 | |
| 421 | status, _, revoked = request( |
| 422 | "/collections/project_access/records/g1", |
| 423 | method="DELETE", |
| 424 | headers=owner_headers, |
| 425 | ) |
| 426 | assert status == 200, revoked |
| 427 | |
| 428 | |
| 429 | def test_search_endpoint_enforcement_applies_row_filters_and_skips_denied( |
| 430 | tmp_path, monkeypatch |
| 431 | ): |
| 432 | data_dir = tmp_path / "data" |
| 433 | searchable_fixture(data_dir) |
| 434 | save_permission_policy( |
| 435 | data_dir, |
| 436 | { |
| 437 | "access_mode": "role_based", |
| 438 | "rules": [ |
| 439 | { |
| 440 | "effect": "allow", |
| 441 | "principal": "role:member", |
| 442 | "actions": ["read"], |
| 443 | "collection": "notes", |
| 444 | "row_filter": {"owner_id": "$user_id"}, |
| 445 | "reason": "members read their own notes", |
| 446 | } |
| 447 | ], |
| 448 | }, |
| 449 | ) |
| 450 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 451 | monkeypatch.setenv(object_server.PERMISSION_ENFORCEMENT_ENV, "true") |
| 452 | monkeypatch.setenv(object_server.PERMISSION_TRUST_HEADERS_ENV, "true") |
| 453 | enable_admin_token(monkeypatch) |
| 454 | |
| 455 | status, _, payload = request( |
| 456 | "/api/search", |
| 457 | query_string="q=flywheel", |
| 458 | headers=[("x-dbbasic-user-id", "7"), ("x-dbbasic-roles", "member")], |
| 459 | ) |
| 460 | |
| 461 | assert status == 200 |
| 462 | assert payload["results"] == { |
| 463 | "notes": [{"id": "n1", "content": "flywheel growth loop"}] |
| 464 | } |
| 465 | assert "tasks" not in payload["results"] |
| 466 | assert payload["total_count"] == 1 |
| 467 | |
| 468 | status, _, payload = request( |
| 469 | "/api/search", |
| 470 | query_string="q=unrelated", |
| 471 | headers=[("x-dbbasic-user-id", "7"), ("x-dbbasic-roles", "member")], |
| 472 | ) |
| 473 | assert status == 200 |
| 474 | assert payload["results"]["notes"] == [] |
| 475 | assert payload["total_count"] == 0 |