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_object_namespace_symlinks.py · 4.1 KB · 108 lines
| 1 | """Symlink containment in object source listing and lookup. |
| 2 | |
| 3 | The listing resolves each root once per call and each file on its own, and |
| 4 | the lookup skips missing candidates before resolving them. These tests pin |
| 5 | that a source file reached through a link that leaves its root is still |
| 6 | refused, including a link swapped in after an earlier call had answered, |
| 7 | and that a root changed through the environment is honoured on the next call. |
| 8 | """ |
| 9 | |
| 10 | import os |
| 11 | from pathlib import Path |
| 12 | |
| 13 | import object_namespace |
| 14 | |
| 15 | |
| 16 | def write_object(path: Path) -> Path: |
| 17 | path.parent.mkdir(parents=True, exist_ok=True) |
| 18 | path.write_text("def GET(request):\n return {'status': 'ok'}\n") |
| 19 | return path |
| 20 | |
| 21 | |
| 22 | def listed_ids(roots): |
| 23 | return [source.object_id for source in object_namespace.iter_object_sources(roots)] |
| 24 | |
| 25 | |
| 26 | def test_file_link_out_of_root_is_not_listed_or_resolved(tmp_path): |
| 27 | root = tmp_path / "objects" |
| 28 | write_object(root / "basics" / "counter.py") |
| 29 | outside = write_object(tmp_path / "elsewhere" / "evil.py") |
| 30 | (root / "basics" / "evil.py").symlink_to(outside) |
| 31 | |
| 32 | assert listed_ids([root]) == ["basics_counter"] |
| 33 | assert object_namespace.resolve_object_id("basics_evil", [root]) is None |
| 34 | |
| 35 | |
| 36 | def test_link_swapped_in_after_first_answer_is_still_refused(tmp_path): |
| 37 | root = tmp_path / "objects" |
| 38 | write_object(root / "basics" / "counter.py") |
| 39 | target = root / "basics" / "evil.py" |
| 40 | write_object(target) |
| 41 | |
| 42 | assert listed_ids([root]) == ["basics_counter", "basics_evil"] |
| 43 | assert object_namespace.resolve_object_id("basics_evil", [root]) == target |
| 44 | |
| 45 | outside = write_object(tmp_path / "elsewhere" / "evil.py") |
| 46 | target.unlink() |
| 47 | target.symlink_to(outside) |
| 48 | |
| 49 | assert listed_ids([root]) == ["basics_counter"] |
| 50 | assert object_namespace.resolve_object_id("basics_evil", [root]) is None |
| 51 | |
| 52 | |
| 53 | def test_directory_link_out_of_root_is_not_listed_or_resolved(tmp_path): |
| 54 | root = tmp_path / "objects" |
| 55 | write_object(root / "basics" / "counter.py") |
| 56 | write_object(tmp_path / "elsewhere" / "evil.py") |
| 57 | (root / "loot").symlink_to(tmp_path / "elsewhere", target_is_directory=True) |
| 58 | |
| 59 | assert object_namespace.resolve_object_id("loot_evil", [root]) is None |
| 60 | assert "loot_evil" not in listed_ids([root]) |
| 61 | |
| 62 | |
| 63 | def test_root_reached_through_a_link_still_lists_its_own_files(tmp_path): |
| 64 | real_root = tmp_path / "real_objects" |
| 65 | write_object(real_root / "basics" / "counter.py") |
| 66 | linked_root = tmp_path / "objects" |
| 67 | linked_root.symlink_to(real_root, target_is_directory=True) |
| 68 | |
| 69 | assert listed_ids([linked_root]) == ["basics_counter"] |
| 70 | assert ( |
| 71 | object_namespace.resolve_object_id("basics_counter", [linked_root]) |
| 72 | == linked_root / "basics" / "counter.py" |
| 73 | ) |
| 74 | |
| 75 | |
| 76 | def test_root_changed_through_environment_is_used_on_next_call(tmp_path, monkeypatch): |
| 77 | first = tmp_path / "first" |
| 78 | second = tmp_path / "second" |
| 79 | write_object(first / "basics" / "counter.py") |
| 80 | write_object(second / "basics" / "timer.py") |
| 81 | |
| 82 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(first)) |
| 83 | monkeypatch.delenv("DBBASIC_OVERRIDES_DIR", raising=False) |
| 84 | assert listed_ids(None) == ["basics_counter"] |
| 85 | assert object_namespace.resolve_object_id("basics_counter") == first / "basics" / "counter.py" |
| 86 | |
| 87 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(second)) |
| 88 | assert listed_ids(None) == ["basics_timer"] |
| 89 | assert object_namespace.resolve_object_id("basics_counter") is None |
| 90 | assert object_namespace.resolve_object_id("basics_timer") == second / "basics" / "timer.py" |
| 91 | |
| 92 | |
| 93 | def test_root_link_retargeted_between_calls_follows_the_new_target(tmp_path): |
| 94 | first = tmp_path / "first" |
| 95 | second = tmp_path / "second" |
| 96 | write_object(first / "basics" / "counter.py") |
| 97 | write_object(second / "basics" / "timer.py") |
| 98 | root = tmp_path / "objects" |
| 99 | root.symlink_to(first, target_is_directory=True) |
| 100 | |
| 101 | assert listed_ids([root]) == ["basics_counter"] |
| 102 | |
| 103 | root.unlink() |
| 104 | os.symlink(second, root, target_is_directory=True) |
| 105 | |
| 106 | assert listed_ids([root]) == ["basics_timer"] |
| 107 | assert object_namespace.resolve_object_id("basics_counter", [root]) is None |
| 108 | assert object_namespace.resolve_object_id("basics_timer", [root]) == root / "basics" / "timer.py" |