Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

dbbasic-object-server/tests/test_collection_path_check.py
Install on DigitalOcean Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 18 minutes ago History
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
3The check is "root/<collection>/records.tsv resolves inside root". With a
4validated one-component name only a symlink at the collection directory or
5at the records file can make it fail, so the fast path proves "no link
6there" and otherwise runs the old resolve check unchanged. Each case here is
7compared with that old check, written out as the oracle.
8"""
9
10import os
11from pathlib import Path
12
13import pytest
14
15import object_collections
16import object_records
17
18
19def 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
30def 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
37def 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
44def 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
56def 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
64def 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
70def 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
79def 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
88def 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
94def 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
100def 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
111def 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)