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_collection_path_check.py · 4.2 KB · 115 lines
| 1 | """collection_records_file refuses exactly the paths it refused before, with two lstats instead of two resolves. |
| 2 | |
| 3 | The check is "root/<collection>/records.tsv resolves inside root". With a |
| 4 | validated one-component name only a symlink at the collection directory or |
| 5 | at the records file can make it fail, so the fast path proves "no link |
| 6 | there" and otherwise runs the old resolve check unchanged. Each case here is |
| 7 | compared with that old check, written out as the oracle. |
| 8 | """ |
| 9 | |
| 10 | import os |
| 11 | from pathlib import Path |
| 12 | |
| 13 | import pytest |
| 14 | |
| 15 | import object_collections |
| 16 | import object_records |
| 17 | |
| 18 | |
| 19 | def old_check(collection: str, base_dir: Path) -> Path: |
| 20 | """The check as it was: both sides resolved, then compared.""" |
| 21 | root = Path(base_dir) / object_records.COLLECTIONS_DIR |
| 22 | path = root / collection / object_records.RECORDS_FILE |
| 23 | try: |
| 24 | path.resolve(strict=False).relative_to(root.resolve(strict=False)) |
| 25 | except ValueError as exc: |
| 26 | raise object_collections.InvalidCollectionNameError(collection) from exc |
| 27 | return path |
| 28 | |
| 29 | |
| 30 | def outcome(fn, collection, base_dir): |
| 31 | try: |
| 32 | return ("ok", fn(collection, base_dir)) |
| 33 | except object_collections.InvalidCollectionNameError: |
| 34 | return ("refused", None) |
| 35 | |
| 36 | |
| 37 | def same_as_before(collection, base_dir): |
| 38 | new = outcome(lambda c, b: object_records.collection_records_file(c, base_dir=b), collection, base_dir) |
| 39 | assert new == outcome(old_check, collection, base_dir) |
| 40 | return new[0] |
| 41 | |
| 42 | |
| 43 | @pytest.fixture |
| 44 | def layout(tmp_path): |
| 45 | base = tmp_path / "data" |
| 46 | root = base / "collections" |
| 47 | (root / "plain").mkdir(parents=True) |
| 48 | (root / "plain" / "records.tsv").write_text("id\n1\n") |
| 49 | (root / "empty_dir").mkdir() |
| 50 | outside = tmp_path / "outside" |
| 51 | outside.mkdir() |
| 52 | (outside / "records.tsv").write_text("id\nsecret\n") |
| 53 | return base, root, outside |
| 54 | |
| 55 | |
| 56 | def test_ordinary_present_and_missing_collections_pass(layout): |
| 57 | base, _, _ = layout |
| 58 | assert same_as_before("plain", base) == "ok" |
| 59 | assert same_as_before("empty_dir", base) == "ok" |
| 60 | assert same_as_before("never_created", base) == "ok" |
| 61 | assert same_as_before("anything", base / "no_such_data_dir") == "ok" |
| 62 | |
| 63 | |
| 64 | def test_a_collection_directory_linked_outside_is_refused(layout): |
| 65 | base, root, outside = layout |
| 66 | os.symlink(outside, root / "escape") |
| 67 | assert same_as_before("escape", base) == "refused" |
| 68 | |
| 69 | |
| 70 | def test_a_records_file_linked_outside_is_refused(layout): |
| 71 | base, root, outside = layout |
| 72 | (root / "leaky").mkdir() |
| 73 | os.symlink(outside / "records.tsv", root / "leaky" / "records.tsv") |
| 74 | assert same_as_before("leaky", base) == "refused" |
| 75 | os.symlink(outside / "missing.tsv", root / "empty_dir" / "records.tsv") |
| 76 | assert same_as_before("empty_dir", base) == "refused", "a dangling link outside too" |
| 77 | |
| 78 | |
| 79 | def test_links_that_stay_inside_still_pass(layout): |
| 80 | base, root, _ = layout |
| 81 | os.symlink(root / "plain", root / "alias") |
| 82 | assert same_as_before("alias", base) == "ok" |
| 83 | (root / "inner").mkdir() |
| 84 | os.symlink(root / "plain" / "records.tsv", root / "inner" / "records.tsv") |
| 85 | assert same_as_before("inner", base) == "ok" |
| 86 | |
| 87 | |
| 88 | def test_a_linked_data_directory_is_judged_the_same_on_both_sides(layout, tmp_path): |
| 89 | base, _, _ = layout |
| 90 | os.symlink(base, tmp_path / "data_link") |
| 91 | assert same_as_before("plain", tmp_path / "data_link") == "ok" |
| 92 | |
| 93 | |
| 94 | def test_a_collection_name_that_is_a_file_takes_the_old_path(layout): |
| 95 | base, root, _ = layout |
| 96 | (root / "afile").write_text("not a directory") |
| 97 | assert same_as_before("afile", base) == "ok" |
| 98 | |
| 99 | |
| 100 | def test_a_link_made_after_a_pass_is_refused_on_the_next_call(layout): |
| 101 | """Nothing is remembered between calls.""" |
| 102 | base, root, outside = layout |
| 103 | (root / "later").mkdir() |
| 104 | assert same_as_before("later", base) == "ok" |
| 105 | os.symlink(outside / "records.tsv", root / "later" / "records.tsv") |
| 106 | assert same_as_before("later", base) == "refused" |
| 107 | with pytest.raises(object_collections.InvalidCollectionNameError): |
| 108 | object_records.read_collection_records("later", base_dir=base, roots=[]) |
| 109 | |
| 110 | |
| 111 | def test_invalid_names_are_refused_before_any_lookup(layout): |
| 112 | base, _, _ = layout |
| 113 | for name in ("..", ".", "a/b", "", "x" * 65, "a.b"): |
| 114 | with pytest.raises(object_collections.InvalidCollectionNameError): |
| 115 | object_records.collection_records_file(name, base_dir=base) |