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_permissions.py · 25.8 KB · 711 lines
| 1 | from datetime import datetime, timezone |
| 2 | |
| 3 | import object_permissions as permissions |
| 4 | |
| 5 | |
| 6 | def test_public_access_allows_read_and_execute_only(): |
| 7 | policy = permissions.PermissionPolicy(access_mode="public") |
| 8 | |
| 9 | read = permissions.check_permission(None, permissions.READ, policy=policy, collection="articles") |
| 10 | execute = permissions.check_permission( |
| 11 | permissions.PermissionSubject.anonymous(), |
| 12 | permissions.EXECUTE, |
| 13 | policy=policy, |
| 14 | object_id="site_home", |
| 15 | ) |
| 16 | update = permissions.check_permission(None, permissions.UPDATE, policy=policy, collection="articles") |
| 17 | |
| 18 | assert read.allowed is True |
| 19 | assert execute.allowed is True |
| 20 | assert update.allowed is False |
| 21 | assert update.reason == "public access is read-only" |
| 22 | |
| 23 | |
| 24 | def test_registered_access_requires_authenticated_subject(): |
| 25 | policy = permissions.PermissionPolicy(access_mode="registered") |
| 26 | |
| 27 | anonymous = permissions.check_permission(None, permissions.READ, policy=policy, collection="forum") |
| 28 | signed_in = permissions.check_permission( |
| 29 | permissions.PermissionSubject(user_id="42"), |
| 30 | permissions.READ, |
| 31 | policy=policy, |
| 32 | collection="forum", |
| 33 | ) |
| 34 | |
| 35 | assert anonymous.allowed is False |
| 36 | assert anonymous.reason == "registered user required" |
| 37 | assert anonymous.code == "authentication_required" |
| 38 | assert anonymous.http_status == 401 |
| 39 | assert signed_in.allowed is True |
| 40 | assert signed_in.reason == "registered access" |
| 41 | |
| 42 | |
| 43 | def test_admin_role_allows_all_actions_from_subject_or_policy_assignment(): |
| 44 | policy = permissions.PermissionPolicy( |
| 45 | access_mode="role_based", |
| 46 | user_roles={"42": ("admin",)}, |
| 47 | ) |
| 48 | |
| 49 | decision = permissions.check_permission( |
| 50 | permissions.PermissionSubject(user_id="42"), |
| 51 | permissions.DELETE, |
| 52 | policy=policy, |
| 53 | collection="invoices", |
| 54 | ) |
| 55 | |
| 56 | assert decision.allowed is True |
| 57 | assert decision.reason == "admin role" |
| 58 | |
| 59 | |
| 60 | def test_old_user_object_owner_convention_still_works_as_fallback(): |
| 61 | owner = permissions.PermissionSubject(user_id="7") |
| 62 | other_user = permissions.PermissionSubject(user_id="8") |
| 63 | |
| 64 | owner_decision = permissions.check_permission(owner, permissions.SOURCE, object_id="u_7_report") |
| 65 | other_decision = permissions.check_permission(other_user, permissions.SOURCE, object_id="u_7_report") |
| 66 | |
| 67 | assert owner_decision.allowed is True |
| 68 | assert owner_decision.reason == "object owner" |
| 69 | assert other_decision.allowed is False |
| 70 | |
| 71 | |
| 72 | def test_system_object_fallback_allows_public_read_execute_but_not_source(): |
| 73 | assert permissions.check_permission(None, permissions.READ, object_id="site_home").allowed is True |
| 74 | assert permissions.check_permission(None, permissions.EXECUTE, object_id="site_home").allowed is True |
| 75 | |
| 76 | source = permissions.check_permission(None, permissions.SOURCE, object_id="site_home") |
| 77 | |
| 78 | assert source.allowed is False |
| 79 | |
| 80 | |
| 81 | def test_role_rule_with_row_filter_returns_filter_when_no_record_is_supplied(): |
| 82 | policy = permissions.PermissionPolicy( |
| 83 | access_mode="role_based", |
| 84 | rules=( |
| 85 | permissions.PermissionRule.allow( |
| 86 | "role:sales", |
| 87 | [permissions.READ], |
| 88 | collection="contacts", |
| 89 | row_filter={"owner_id": "$user_id"}, |
| 90 | reason="sales reps only see own contacts", |
| 91 | ), |
| 92 | ), |
| 93 | ) |
| 94 | |
| 95 | decision = permissions.check_permission( |
| 96 | permissions.PermissionSubject(user_id="7", roles=("sales",)), |
| 97 | permissions.READ, |
| 98 | policy=policy, |
| 99 | collection="contacts", |
| 100 | ) |
| 101 | |
| 102 | assert decision.allowed is True |
| 103 | assert decision.row_filter == {"owner_id": "$user_id"} |
| 104 | assert decision.reason == "sales reps only see own contacts" |
| 105 | |
| 106 | |
| 107 | def test_role_rule_with_row_filter_checks_record_when_supplied(): |
| 108 | policy = permissions.PermissionPolicy( |
| 109 | access_mode="role_based", |
| 110 | rules=( |
| 111 | permissions.PermissionRule.allow( |
| 112 | "role:sales", |
| 113 | [permissions.READ], |
| 114 | collection="contacts", |
| 115 | row_filter={"owner_id": "$user_id"}, |
| 116 | ), |
| 117 | ), |
| 118 | ) |
| 119 | subject = permissions.PermissionSubject(user_id="7", roles=("sales",)) |
| 120 | |
| 121 | own_contact = permissions.check_permission( |
| 122 | subject, |
| 123 | permissions.READ, |
| 124 | policy=policy, |
| 125 | collection="contacts", |
| 126 | record={"owner_id": "7", "name": "Alice"}, |
| 127 | ) |
| 128 | other_contact = permissions.check_permission( |
| 129 | subject, |
| 130 | permissions.READ, |
| 131 | policy=policy, |
| 132 | collection="contacts", |
| 133 | record={"owner_id": "8", "name": "Bob"}, |
| 134 | ) |
| 135 | |
| 136 | assert own_contact.allowed is True |
| 137 | assert other_contact.allowed is False |
| 138 | |
| 139 | |
| 140 | def test_accessible_projects_filter_matches_by_membership(): |
| 141 | policy = permissions.PermissionPolicy( |
| 142 | access_mode="role_based", |
| 143 | rules=( |
| 144 | permissions.PermissionRule.allow( |
| 145 | "registered", |
| 146 | [permissions.READ], |
| 147 | collection="notes", |
| 148 | row_filter={"project_id": "$accessible_projects"}, |
| 149 | ), |
| 150 | ), |
| 151 | ) |
| 152 | granted = permissions.PermissionSubject(user_id="8", project_ids=("p1", "p2")) |
| 153 | ungranted = permissions.PermissionSubject(user_id="9") |
| 154 | shared_note = {"id": "n1", "project_id": "p1", "owner_id": "7"} |
| 155 | other_note = {"id": "n2", "project_id": "p9", "owner_id": "7"} |
| 156 | unfiled_note = {"id": "n3", "project_id": "", "owner_id": "7"} |
| 157 | |
| 158 | assert permissions.check_permission( |
| 159 | granted, permissions.READ, policy=policy, collection="notes", record=shared_note |
| 160 | ).allowed is True |
| 161 | assert permissions.check_permission( |
| 162 | granted, permissions.READ, policy=policy, collection="notes", record=other_note |
| 163 | ).allowed is False |
| 164 | assert permissions.check_permission( |
| 165 | granted, permissions.READ, policy=policy, collection="notes", record=unfiled_note |
| 166 | ).allowed is False |
| 167 | assert permissions.check_permission( |
| 168 | ungranted, permissions.READ, policy=policy, collection="notes", record=shared_note |
| 169 | ).allowed is False |
| 170 | |
| 171 | |
| 172 | def test_shared_records_filter_is_collection_scoped(): |
| 173 | """capabilities.shareable: `{id: $shared_records}` grants read to a record |
| 174 | explicitly shared with the subject -- and ONLY in the collection it was |
| 175 | shared in. The collection is threaded into filter resolution so a share on |
| 176 | a task never leaks a same-id row in another collection, and an unshared |
| 177 | user (or a share on a different record) never matches. |
| 178 | """ |
| 179 | policy = permissions.PermissionPolicy( |
| 180 | access_mode="role_based", |
| 181 | rules=( |
| 182 | permissions.PermissionRule.allow( |
| 183 | "registered", [permissions.READ], collection="tasks", |
| 184 | row_filter={"id": "$shared_records"}, |
| 185 | ), |
| 186 | ), |
| 187 | ) |
| 188 | # user "8" was granted task "t1" (and only that). Same id "t1" also exists |
| 189 | # as a record in another collection they were NOT granted. |
| 190 | granted = permissions.PermissionSubject(user_id="8").with_shares({"tasks": ["t1"]}) |
| 191 | ungranted = permissions.PermissionSubject(user_id="9") |
| 192 | shared_task = {"id": "t1", "owner_id": "7"} |
| 193 | other_task = {"id": "t2", "owner_id": "7"} |
| 194 | |
| 195 | # grantee reads the shared task |
| 196 | assert permissions.check_permission( |
| 197 | granted, permissions.READ, policy=policy, collection="tasks", record=shared_task |
| 198 | ).allowed is True |
| 199 | # but not a different task |
| 200 | assert permissions.check_permission( |
| 201 | granted, permissions.READ, policy=policy, collection="tasks", record=other_task |
| 202 | ).allowed is False |
| 203 | # and the SAME rule/id in a DIFFERENT collection must not match (collection |
| 204 | # scoping): the grant was for tasks, so an id-t1 row in "invoices" is denied |
| 205 | invoice_policy = permissions.PermissionPolicy( |
| 206 | access_mode="role_based", |
| 207 | rules=( |
| 208 | permissions.PermissionRule.allow( |
| 209 | "registered", [permissions.READ], collection="invoices", |
| 210 | row_filter={"id": "$shared_records"}, |
| 211 | ), |
| 212 | ), |
| 213 | ) |
| 214 | assert permissions.check_permission( |
| 215 | granted, permissions.READ, policy=invoice_policy, collection="invoices", |
| 216 | record={"id": "t1", "owner_id": "7"}, |
| 217 | ).allowed is False |
| 218 | # a user with no grant never matches |
| 219 | assert permissions.check_permission( |
| 220 | ungranted, permissions.READ, policy=policy, collection="tasks", record=shared_task |
| 221 | ).allowed is False |
| 222 | |
| 223 | |
| 224 | def test_subject_from_dict_reads_project_ids(): |
| 225 | subject = permissions.subject_from_dict( |
| 226 | {"user_id": "8", "project_ids": ["p1", "p2"], "owned_project_ids": ["p9"]} |
| 227 | ) |
| 228 | assert subject.project_ids == ("p1", "p2") |
| 229 | assert subject.owned_project_ids == ("p9",) |
| 230 | enriched = subject.with_projects(["p3"], ["p4"]) |
| 231 | assert enriched.project_ids == ("p3",) |
| 232 | assert enriched.owned_project_ids == ("p4",) |
| 233 | |
| 234 | |
| 235 | def test_writable_projects_filter_matches_write_grants_only(): |
| 236 | policy = permissions.PermissionPolicy( |
| 237 | access_mode="role_based", |
| 238 | rules=( |
| 239 | permissions.PermissionRule.allow( |
| 240 | "registered", |
| 241 | [permissions.UPDATE], |
| 242 | collection="notes", |
| 243 | row_filter={"project_id": "$writable_projects"}, |
| 244 | ), |
| 245 | ), |
| 246 | ) |
| 247 | writer = permissions.PermissionSubject(user_id="8", writable_project_ids=("p1",)) |
| 248 | reader_only = permissions.PermissionSubject(user_id="9", project_ids=("p1",)) |
| 249 | note = {"id": "n1", "project_id": "p1"} |
| 250 | |
| 251 | assert permissions.check_permission( |
| 252 | writer, permissions.UPDATE, policy=policy, collection="notes", record=note |
| 253 | ).allowed is True |
| 254 | assert permissions.check_permission( |
| 255 | reader_only, permissions.UPDATE, policy=policy, collection="notes", record=note |
| 256 | ).allowed is False |
| 257 | |
| 258 | |
| 259 | def test_subject_from_dict_reads_writable_project_ids(): |
| 260 | subject = permissions.subject_from_dict( |
| 261 | {"user_id": "8", "writable_project_ids": ["p1"]} |
| 262 | ) |
| 263 | assert subject.writable_project_ids == ("p1",) |
| 264 | enriched = subject.with_projects(["p3"], ["p4"], ["p5"]) |
| 265 | assert enriched.project_ids == ("p3",) |
| 266 | assert enriched.owned_project_ids == ("p4",) |
| 267 | assert enriched.writable_project_ids == ("p5",) |
| 268 | |
| 269 | |
| 270 | def test_record_matches_filter_public_wrapper_shares_row_filter_semantics(): |
| 271 | """Transition guards reuse this for their ``when`` clauses (see |
| 272 | object_records._validate_field_transitions), so it must match the same |
| 273 | $-variable resolution and empty-string posture row filters use.""" |
| 274 | subject = permissions.PermissionSubject(user_id="7") |
| 275 | |
| 276 | assert permissions.record_matches_filter( |
| 277 | {"owner_id": "7"}, {"owner_id": "$user_id"}, subject |
| 278 | ) is True |
| 279 | assert permissions.record_matches_filter( |
| 280 | {"owner_id": "8"}, {"owner_id": "$user_id"}, subject |
| 281 | ) is False |
| 282 | assert permissions.record_matches_filter( |
| 283 | {"owner_id": ""}, {"owner_id": "$user_id"}, subject |
| 284 | ) is False |
| 285 | assert permissions.record_matches_filter( |
| 286 | {"status": "open"}, {"status": "open"}, subject |
| 287 | ) is True |
| 288 | |
| 289 | |
| 290 | def test_owned_projects_filter_gates_grant_writes(): |
| 291 | policy = permissions.PermissionPolicy( |
| 292 | access_mode="role_based", |
| 293 | rules=( |
| 294 | permissions.PermissionRule.allow( |
| 295 | "registered", |
| 296 | [permissions.CREATE, permissions.DELETE], |
| 297 | collection="project_access", |
| 298 | row_filter={"project_id": "$owned_projects"}, |
| 299 | ), |
| 300 | ), |
| 301 | ) |
| 302 | owner = permissions.PermissionSubject(user_id="7", owned_project_ids=("p1",)) |
| 303 | |
| 304 | own_grant = {"id": "g1", "project_id": "p1", "user_id": "8"} |
| 305 | foreign_grant = {"id": "g2", "project_id": "p9", "user_id": "7"} |
| 306 | |
| 307 | assert permissions.check_permission( |
| 308 | owner, permissions.CREATE, policy=policy, collection="project_access", record=own_grant |
| 309 | ).allowed is True |
| 310 | assert permissions.check_permission( |
| 311 | owner, permissions.CREATE, policy=policy, collection="project_access", record=foreign_grant |
| 312 | ).allowed is False |
| 313 | |
| 314 | |
| 315 | def test_customer_employee_account_rule_models_tenant_shared_access(): |
| 316 | policy = permissions.PermissionPolicy( |
| 317 | access_mode="role_based", |
| 318 | rules=( |
| 319 | permissions.PermissionRule.allow( |
| 320 | "account:customer-acme", |
| 321 | [permissions.READ], |
| 322 | collection="invoices", |
| 323 | row_filter={"customer_account_id": "$account_id"}, |
| 324 | fields=["invoice_id", "status", "total"], |
| 325 | denied_fields=["internal_notes"], |
| 326 | ), |
| 327 | ), |
| 328 | ) |
| 329 | subject = permissions.PermissionSubject( |
| 330 | user_id="employee-1", |
| 331 | account_id="customer-acme", |
| 332 | roles=("customer_employee",), |
| 333 | ) |
| 334 | |
| 335 | decision = permissions.check_permission( |
| 336 | subject, |
| 337 | permissions.READ, |
| 338 | policy=policy, |
| 339 | collection="invoices", |
| 340 | record={"customer_account_id": "customer-acme", "total": 120}, |
| 341 | ) |
| 342 | |
| 343 | assert decision.allowed is True |
| 344 | assert decision.fields == frozenset({"invoice_id", "status", "total"}) |
| 345 | assert decision.denied_fields == frozenset({"internal_notes"}) |
| 346 | |
| 347 | |
| 348 | def test_user_share_rule_allows_specific_user_without_owner_role(): |
| 349 | policy = permissions.PermissionPolicy( |
| 350 | access_mode="role_based", |
| 351 | rules=( |
| 352 | permissions.PermissionRule.allow( |
| 353 | "user:99", |
| 354 | [permissions.READ, permissions.UPDATE], |
| 355 | object_id="u_7_shared_calculator", |
| 356 | reason="shared by owner", |
| 357 | ), |
| 358 | ), |
| 359 | ) |
| 360 | |
| 361 | decision = permissions.check_permission( |
| 362 | permissions.PermissionSubject(user_id="99"), |
| 363 | permissions.UPDATE, |
| 364 | policy=policy, |
| 365 | object_id="u_7_shared_calculator", |
| 366 | ) |
| 367 | |
| 368 | assert decision.allowed is True |
| 369 | assert decision.reason == "shared by owner" |
| 370 | |
| 371 | |
| 372 | def test_explicit_deny_overrides_role_allow(): |
| 373 | policy = permissions.PermissionPolicy( |
| 374 | access_mode="role_based", |
| 375 | rules=( |
| 376 | permissions.PermissionRule.deny( |
| 377 | "role:support", |
| 378 | [permissions.DELETE], |
| 379 | collection="tickets", |
| 380 | reason="support cannot delete tickets", |
| 381 | ), |
| 382 | permissions.PermissionRule.allow( |
| 383 | "role:support", |
| 384 | [permissions.READ, permissions.UPDATE, permissions.DELETE], |
| 385 | collection="tickets", |
| 386 | ), |
| 387 | ), |
| 388 | ) |
| 389 | |
| 390 | decision = permissions.check_permission( |
| 391 | permissions.PermissionSubject(user_id="3", roles=("support",)), |
| 392 | permissions.DELETE, |
| 393 | policy=policy, |
| 394 | collection="tickets", |
| 395 | ) |
| 396 | |
| 397 | assert decision.allowed is False |
| 398 | assert decision.reason == "support cannot delete tickets" |
| 399 | |
| 400 | |
| 401 | def test_subscription_mode_allows_subscribed_users_to_read(): |
| 402 | policy = permissions.PermissionPolicy(access_mode="subscription") |
| 403 | subscriber = permissions.PermissionSubject(user_id="42", subscriptions=("pro",)) |
| 404 | |
| 405 | read = permissions.check_permission(subscriber, permissions.READ, policy=policy, collection="reports") |
| 406 | write = permissions.check_permission( |
| 407 | subscriber, |
| 408 | permissions.UPDATE, |
| 409 | policy=policy, |
| 410 | collection="reports", |
| 411 | ) |
| 412 | |
| 413 | assert read.allowed is True |
| 414 | assert write.allowed is False |
| 415 | assert write.reason == "subscription access is read-only" |
| 416 | assert write.http_status == 403 |
| 417 | |
| 418 | |
| 419 | def test_subscription_mode_returns_payment_required_for_missing_entitlement(): |
| 420 | policy = permissions.PermissionPolicy(access_mode="subscription") |
| 421 | |
| 422 | decision = permissions.check_permission( |
| 423 | permissions.PermissionSubject(user_id="42"), |
| 424 | permissions.READ, |
| 425 | policy=policy, |
| 426 | collection="reports", |
| 427 | ) |
| 428 | |
| 429 | assert decision.allowed is False |
| 430 | assert decision.code == "payment_required" |
| 431 | assert decision.http_status == 402 |
| 432 | |
| 433 | |
| 434 | def test_subscription_rule_can_target_a_specific_plan(): |
| 435 | policy = permissions.PermissionPolicy( |
| 436 | access_mode="role_based", |
| 437 | rules=( |
| 438 | permissions.PermissionRule.allow( |
| 439 | "subscription:pro", |
| 440 | [permissions.READ], |
| 441 | collection="premium_reports", |
| 442 | reason="active pro subscription", |
| 443 | ), |
| 444 | ), |
| 445 | ) |
| 446 | |
| 447 | free_user = permissions.PermissionSubject(user_id="1", subscriptions=("free",)) |
| 448 | pro_user = permissions.PermissionSubject(user_id="2", subscriptions=("pro",)) |
| 449 | |
| 450 | assert ( |
| 451 | permissions.check_permission( |
| 452 | free_user, |
| 453 | permissions.READ, |
| 454 | policy=policy, |
| 455 | collection="premium_reports", |
| 456 | ).allowed |
| 457 | is False |
| 458 | ) |
| 459 | |
| 460 | decision = permissions.check_permission( |
| 461 | pro_user, |
| 462 | permissions.READ, |
| 463 | policy=policy, |
| 464 | collection="premium_reports", |
| 465 | ) |
| 466 | |
| 467 | assert decision.allowed is True |
| 468 | assert decision.reason == "active pro subscription" |
| 469 | |
| 470 | |
| 471 | def test_time_boxed_rule_models_temporary_pay_per_view_access(): |
| 472 | policy = permissions.PermissionPolicy( |
| 473 | access_mode="role_based", |
| 474 | rules=( |
| 475 | permissions.PermissionRule.allow( |
| 476 | "user:42", |
| 477 | [permissions.READ], |
| 478 | object_id="reports_market_snapshot", |
| 479 | valid_from="2026-06-01T00:00:00Z", |
| 480 | expires_at="2026-07-01T00:00:00Z", |
| 481 | reason="temporary paid access", |
| 482 | ), |
| 483 | ), |
| 484 | ) |
| 485 | subject = permissions.PermissionSubject(user_id="42") |
| 486 | |
| 487 | active = permissions.check_permission( |
| 488 | subject, |
| 489 | permissions.READ, |
| 490 | policy=policy, |
| 491 | object_id="reports_market_snapshot", |
| 492 | now=datetime(2026, 6, 15, tzinfo=timezone.utc), |
| 493 | ) |
| 494 | expired = permissions.check_permission( |
| 495 | subject, |
| 496 | permissions.READ, |
| 497 | policy=policy, |
| 498 | object_id="reports_market_snapshot", |
| 499 | now=datetime(2026, 7, 1, tzinfo=timezone.utc), |
| 500 | ) |
| 501 | |
| 502 | assert active.allowed is True |
| 503 | assert active.reason == "temporary paid access" |
| 504 | assert expired.allowed is False |
| 505 | |
| 506 | |
| 507 | def test_policy_serialization_round_trips_json_shape(): |
| 508 | payload = { |
| 509 | "access_mode": "role_based", |
| 510 | "roles": {"sales": {"label": "Sales"}}, |
| 511 | "user_roles": {"7": ["sales"]}, |
| 512 | "rules": [ |
| 513 | { |
| 514 | "effect": "allow", |
| 515 | "principal": "role:sales", |
| 516 | "actions": ["read"], |
| 517 | "collection": "contacts", |
| 518 | "row_filter": {"owner_id": "$user_id"}, |
| 519 | "fields": ["email", "name"], |
| 520 | "denied_fields": ["internal_notes"], |
| 521 | "reason": "sales reps only see own contacts", |
| 522 | } |
| 523 | ], |
| 524 | "admin_roles": ["admin"], |
| 525 | } |
| 526 | |
| 527 | policy = permissions.policy_from_dict(payload) |
| 528 | serialized = permissions.policy_to_dict(policy) |
| 529 | |
| 530 | assert serialized == { |
| 531 | "access_mode": "role_based", |
| 532 | "roles": {"sales": {"label": "Sales"}}, |
| 533 | "user_roles": {"7": ["sales"]}, |
| 534 | "rules": [ |
| 535 | { |
| 536 | "effect": "allow", |
| 537 | "actions": ["read"], |
| 538 | "principal": "role:sales", |
| 539 | "collection": "contacts", |
| 540 | "row_filter": {"owner_id": "$user_id"}, |
| 541 | "fields": ["email", "name"], |
| 542 | "denied_fields": ["internal_notes"], |
| 543 | "reason": "sales reps only see own contacts", |
| 544 | } |
| 545 | ], |
| 546 | "admin_roles": ["admin"], |
| 547 | } |
| 548 | |
| 549 | |
| 550 | def test_subject_and_decision_serialization_match_scroll_shape(): |
| 551 | subject = permissions.subject_from_dict( |
| 552 | { |
| 553 | "user_id": 42, |
| 554 | "account_id": "customer-acme", |
| 555 | "roles": ["customer_employee"], |
| 556 | "subscriptions": ["pro"], |
| 557 | } |
| 558 | ) |
| 559 | decision = permissions.PermissionDecision.allow( |
| 560 | "active pro subscription", |
| 561 | row_filter={"customer_account_id": "$account_id"}, |
| 562 | fields=["invoice_id", "total"], |
| 563 | denied_fields=["internal_notes"], |
| 564 | ) |
| 565 | |
| 566 | assert subject.user_id == "42" |
| 567 | assert subject.account_id == "customer-acme" |
| 568 | assert subject.roles == ("customer_employee",) |
| 569 | assert permissions.decision_to_dict(decision) == { |
| 570 | "allowed": True, |
| 571 | "reason": "active pro subscription", |
| 572 | "code": "allowed", |
| 573 | "http_status": 200, |
| 574 | "row_filter": {"customer_account_id": "$account_id"}, |
| 575 | "fields": ["invoice_id", "total"], |
| 576 | "denied_fields": ["internal_notes"], |
| 577 | } |
| 578 | |
| 579 | |
| 580 | def test_policy_from_dict_rejects_unknown_access_mode(): |
| 581 | try: |
| 582 | permissions.policy_from_dict({"access_mode": "unknown"}) |
| 583 | except ValueError as exc: |
| 584 | assert "Permission access_mode must be one of:" in str(exc) |
| 585 | else: |
| 586 | raise AssertionError("Expected unknown access mode to fail") |
| 587 | |
| 588 | |
| 589 | # --- 58 query/filter operators (record_matches_filter extension) ---------- |
| 590 | |
| 591 | |
| 592 | def test_filter_condition_builds_op_value_dict(): |
| 593 | assert permissions.filter_condition("gte", "5") == {"op": "gte", "value": "5"} |
| 594 | |
| 595 | |
| 596 | def test_record_matches_filter_eq_is_still_the_bare_literal_default(): |
| 597 | subject = permissions.PermissionSubject.anonymous() |
| 598 | assert permissions.record_matches_filter({"status": "hot"}, {"status": "hot"}, subject) is True |
| 599 | assert permissions.record_matches_filter({"status": "cold"}, {"status": "hot"}, subject) is False |
| 600 | # Explicit eq condition matches the bare-literal shorthand exactly. |
| 601 | condition = permissions.filter_condition("eq", "hot") |
| 602 | assert permissions.record_matches_filter({"status": "hot"}, {"status": condition}, subject) is True |
| 603 | |
| 604 | |
| 605 | def test_record_matches_filter_ne(): |
| 606 | subject = permissions.PermissionSubject.anonymous() |
| 607 | condition = permissions.filter_condition("ne", "hot") |
| 608 | assert permissions.record_matches_filter({"status": "cold"}, {"status": condition}, subject) is True |
| 609 | assert permissions.record_matches_filter({"status": "hot"}, {"status": condition}, subject) is False |
| 610 | # A record missing the field entirely is "not hot" too. |
| 611 | assert permissions.record_matches_filter({}, {"status": condition}, subject) is True |
| 612 | |
| 613 | |
| 614 | def test_record_matches_filter_in(): |
| 615 | subject = permissions.PermissionSubject.anonymous() |
| 616 | condition = permissions.filter_condition("in", ("open", "assigned")) |
| 617 | assert permissions.record_matches_filter({"status": "open"}, {"status": condition}, subject) is True |
| 618 | assert permissions.record_matches_filter({"status": "assigned"}, {"status": condition}, subject) is True |
| 619 | assert permissions.record_matches_filter({"status": "closed"}, {"status": condition}, subject) is False |
| 620 | assert permissions.record_matches_filter({}, {"status": condition}, subject) is False |
| 621 | |
| 622 | |
| 623 | def test_record_matches_filter_ordered_ops_numeric(): |
| 624 | subject = permissions.PermissionSubject.anonymous() |
| 625 | record = {"total": "150"} |
| 626 | assert permissions.record_matches_filter( |
| 627 | record, {"total": permissions.filter_condition("gte", "100")}, subject |
| 628 | ) is True |
| 629 | assert permissions.record_matches_filter( |
| 630 | record, {"total": permissions.filter_condition("gt", "150")}, subject |
| 631 | ) is False |
| 632 | assert permissions.record_matches_filter( |
| 633 | record, {"total": permissions.filter_condition("lte", "150")}, subject |
| 634 | ) is True |
| 635 | assert permissions.record_matches_filter( |
| 636 | record, {"total": permissions.filter_condition("lt", "150")}, subject |
| 637 | ) is False |
| 638 | # Numeric comparison, not string comparison: "9" > "10" as strings but |
| 639 | # not as numbers. |
| 640 | assert permissions.record_matches_filter( |
| 641 | {"total": "9"}, {"total": permissions.filter_condition("gt", "10")}, subject |
| 642 | ) is False |
| 643 | |
| 644 | |
| 645 | def test_record_matches_filter_ordered_ops_dates(): |
| 646 | subject = permissions.PermissionSubject.anonymous() |
| 647 | record = {"created_at": "2026-07-15"} |
| 648 | assert permissions.record_matches_filter( |
| 649 | record, {"created_at": permissions.filter_condition("gte", "2026-07-01")}, subject |
| 650 | ) is True |
| 651 | assert permissions.record_matches_filter( |
| 652 | record, {"created_at": permissions.filter_condition("lte", "2026-07-31")}, subject |
| 653 | ) is True |
| 654 | assert permissions.record_matches_filter( |
| 655 | record, {"created_at": permissions.filter_condition("lt", "2026-07-01")}, subject |
| 656 | ) is False |
| 657 | |
| 658 | |
| 659 | def test_record_matches_filter_ordered_op_on_missing_field_never_matches(): |
| 660 | subject = permissions.PermissionSubject.anonymous() |
| 661 | assert permissions.record_matches_filter( |
| 662 | {}, {"total": permissions.filter_condition("gte", "1")}, subject |
| 663 | ) is False |
| 664 | |
| 665 | |
| 666 | def test_record_matches_filter_multiple_conditions_on_one_field_are_anded(): |
| 667 | """A date range (created_at.gte=X&created_at.lte=Y) is two conditions on |
| 668 | the SAME field; both must hold -- this is why the normalized filter |
| 669 | shape is a LIST of conditions per field, not a single value.""" |
| 670 | subject = permissions.PermissionSubject.anonymous() |
| 671 | row_filter = { |
| 672 | "created_at": [ |
| 673 | permissions.filter_condition("gte", "2026-07-01"), |
| 674 | permissions.filter_condition("lte", "2026-07-31"), |
| 675 | ] |
| 676 | } |
| 677 | assert permissions.record_matches_filter({"created_at": "2026-07-15"}, row_filter, subject) is True |
| 678 | assert permissions.record_matches_filter({"created_at": "2026-08-01"}, row_filter, subject) is False |
| 679 | assert permissions.record_matches_filter({"created_at": "2026-06-30"}, row_filter, subject) is False |
| 680 | |
| 681 | |
| 682 | def test_record_matches_filter_unknown_operator_raises(): |
| 683 | subject = permissions.PermissionSubject.anonymous() |
| 684 | condition = {"op": "like", "value": "x"} |
| 685 | try: |
| 686 | permissions.record_matches_filter({"name": "x"}, {"name": condition}, subject) |
| 687 | except ValueError as exc: |
| 688 | assert "like" in str(exc) |
| 689 | else: |
| 690 | raise AssertionError("Expected an unknown operator to raise") |
| 691 | |
| 692 | |
| 693 | def test_record_matches_filter_preserves_existing_dollar_var_row_filter_behavior(): |
| 694 | """Extending the matcher for 58 must not change row-filter/transition- |
| 695 | guard semantics: $-variable resolution, tuple membership from |
| 696 | $accessible_projects-style variables, and empty-string non-matching |
| 697 | all still behave exactly as before.""" |
| 698 | subject = permissions.PermissionSubject(user_id="7", project_ids=("p1", "p2")) |
| 699 | |
| 700 | assert permissions.record_matches_filter( |
| 701 | {"owner_id": "7"}, {"owner_id": "$user_id"}, subject |
| 702 | ) is True |
| 703 | assert permissions.record_matches_filter( |
| 704 | {"owner_id": ""}, {"owner_id": "$user_id"}, subject |
| 705 | ) is False |
| 706 | assert permissions.record_matches_filter( |
| 707 | {"project_id": "p1"}, {"project_id": "$accessible_projects"}, subject |
| 708 | ) is True |
| 709 | assert permissions.record_matches_filter( |
| 710 | {"project_id": "p9"}, {"project_id": "$accessible_projects"}, subject |
| 711 | ) is False |