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

0 downloads

dbbasic-object-server/tests/test_object_files.py
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.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
test_object_files.py · 5.6 KB · 172 lines
1from pathlib import Path
2
3import pytest
4
5import object_files
6from object_versions import InvalidObjectIdError
7
8
9def write_file(path: Path, content: bytes):
10 path.parent.mkdir(parents=True, exist_ok=True)
11 path.write_bytes(content)
12 return path
13
14
15def test_list_object_files_returns_metadata_for_nested_files(tmp_path):
16 data_dir = tmp_path / "data"
17 write_file(data_dir / "files" / "site_home" / "image.png", b"png")
18 write_file(data_dir / "files" / "site_home" / "nested" / "note.txt", b"hello")
19
20 files = object_files.list_object_files("site_home", base_dir=data_dir)
21
22 assert [item["name"] for item in files] == ["image.png", "nested/note.txt"]
23 assert files[0]["size"] == 3
24 assert isinstance(files[0]["modified"], float)
25
26
27def test_list_object_files_returns_empty_for_missing_directory(tmp_path):
28 assert object_files.list_object_files("site_home", base_dir=tmp_path / "data") == []
29
30
31def test_list_object_files_skips_symlink_outside_object_directory(tmp_path):
32 data_dir = tmp_path / "data"
33 outside = write_file(tmp_path / "outside.txt", b"secret")
34 link = data_dir / "files" / "site_home" / "link.txt"
35 link.parent.mkdir(parents=True, exist_ok=True)
36 link.symlink_to(outside)
37
38 assert object_files.list_object_files("site_home", base_dir=data_dir) == []
39
40
41def test_read_object_file_returns_bytes_and_metadata(tmp_path):
42 data_dir = tmp_path / "data"
43 write_file(data_dir / "files" / "site_home" / "report.txt", b"hello")
44
45 content, metadata = object_files.read_object_file(
46 "site_home",
47 "report.txt",
48 base_dir=data_dir,
49 )
50
51 assert content == b"hello"
52 assert metadata["name"] == "report.txt"
53 assert metadata["size"] == 5
54
55
56@pytest.mark.parametrize(
57 "filename",
58 ["", "../secret.txt", "nested/../../secret.txt", "/etc/passwd", "bad\x00name"],
59)
60def test_read_object_file_rejects_unsafe_filenames(tmp_path, filename):
61 with pytest.raises(object_files.InvalidObjectFilenameError):
62 object_files.read_object_file("site_home", filename, base_dir=tmp_path / "data")
63
64
65def test_read_object_file_rejects_invalid_object_id(tmp_path):
66 with pytest.raises(InvalidObjectIdError):
67 object_files.read_object_file("../bad", "file.txt", base_dir=tmp_path / "data")
68
69
70def test_read_object_file_blocks_symlink_outside_object_directory(tmp_path):
71 data_dir = tmp_path / "data"
72 outside = write_file(tmp_path / "outside.txt", b"secret")
73 link = data_dir / "files" / "site_home" / "link.txt"
74 link.parent.mkdir(parents=True, exist_ok=True)
75 link.symlink_to(outside)
76
77 with pytest.raises(object_files.InvalidObjectFilenameError):
78 object_files.read_object_file("site_home", "link.txt", base_dir=data_dir)
79
80
81def test_read_object_file_raises_for_missing_file(tmp_path):
82 with pytest.raises(object_files.ObjectFileNotFoundError):
83 object_files.read_object_file("site_home", "missing.txt", base_dir=tmp_path / "data")
84
85
86def test_write_object_file_creates_nested_file_and_metadata(tmp_path):
87 data_dir = tmp_path / "data"
88
89 metadata = object_files.write_object_file(
90 "site_home",
91 "assets/report.txt",
92 b"hello",
93 base_dir=data_dir,
94 )
95
96 assert metadata["name"] == "assets/report.txt"
97 assert metadata["size"] == 5
98 assert (data_dir / "files" / "site_home" / "assets" / "report.txt").read_bytes() == b"hello"
99
100
101def test_write_object_file_rejects_duplicate_without_overwrite(tmp_path):
102 data_dir = tmp_path / "data"
103 write_file(data_dir / "files" / "site_home" / "assets" / "report.txt", b"old")
104
105 with pytest.raises(object_files.ObjectFileExistsError):
106 object_files.write_object_file(
107 "site_home",
108 "assets/report.txt",
109 b"new",
110 base_dir=data_dir,
111 )
112
113 assert (data_dir / "files" / "site_home" / "assets" / "report.txt").read_bytes() == b"old"
114
115
116def test_write_object_file_overwrites_when_requested(tmp_path):
117 data_dir = tmp_path / "data"
118 write_file(data_dir / "files" / "site_home" / "assets" / "report.txt", b"old")
119
120 metadata = object_files.write_object_file(
121 "site_home",
122 "assets/report.txt",
123 b"new",
124 base_dir=data_dir,
125 overwrite=True,
126 )
127
128 assert metadata["name"] == "assets/report.txt"
129 assert metadata["size"] == 3
130 assert (data_dir / "files" / "site_home" / "assets" / "report.txt").read_bytes() == b"new"
131
132
133def test_write_object_file_enforces_max_bytes(tmp_path):
134 with pytest.raises(object_files.ObjectFileTooLargeError):
135 object_files.write_object_file(
136 "site_home",
137 "report.txt",
138 b"hello",
139 base_dir=tmp_path / "data",
140 max_bytes=4,
141 )
142
143
144def test_write_object_file_rejects_unsafe_filename(tmp_path):
145 with pytest.raises(object_files.InvalidObjectFilenameError):
146 object_files.write_object_file(
147 "site_home",
148 "../secret.txt",
149 b"secret",
150 base_dir=tmp_path / "data",
151 )
152
153
154def test_delete_object_file_removes_file_and_prunes_empty_dirs(tmp_path):
155 data_dir = tmp_path / "data"
156 file_path = write_file(data_dir / "files" / "site_home" / "assets" / "report.txt", b"old")
157
158 metadata = object_files.delete_object_file(
159 "site_home",
160 "assets/report.txt",
161 base_dir=data_dir,
162 )
163
164 assert metadata["name"] == "assets/report.txt"
165 assert metadata["size"] == 3
166 assert not file_path.exists()
167 assert not file_path.parent.exists()
168
169
170def test_delete_object_file_raises_for_missing_file(tmp_path):
171 with pytest.raises(object_files.ObjectFileNotFoundError):
172 object_files.delete_object_file("site_home", "missing.txt", base_dir=tmp_path / "data")