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_package_install_reuse_restore_point.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, 19 minutes ago History
test_package_install_reuse_restore_point.py · 5.1 KB · 162 lines
1"""Package install may name an existing backup instead of writing a new one."""
2
3import json
4
5import pytest
6
7from test_object_server import (
8 auth_headers,
9 enable_admin_token,
10 request,
11 write_package,
12 write_source,
13)
14
15
16@pytest.fixture
17def env(tmp_path, monkeypatch):
18 packages_root = tmp_path / "packages"
19 data_dir = tmp_path / "data"
20 object_root = tmp_path / "objects"
21 write_source(object_root / "site" / "home.py", "def GET(request): return {'before': True}\n")
22 write_package(
23 packages_root,
24 "hello-world",
25 {
26 "id": "hello-world",
27 "name": "Hello World",
28 "version": "0.1.0",
29 "objects": [{"id": "hello_world", "path": "objects/hello/world.py"}],
30 },
31 files=(("objects/hello/world.py", "def GET(request): return {'new': True}\n"),),
32 )
33 monkeypatch.setenv("DBBASIC_PACKAGES_DIR", str(packages_root))
34 monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir))
35 monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(object_root))
36 monkeypatch.setenv("DBBASIC_ENABLE_PACKAGE_INSTALLS", "true")
37 monkeypatch.setenv("DBBASIC_ENABLE_PACKAGE_RESTORE", "true")
38 monkeypatch.delenv("DBBASIC_BACKUPS_DIR", raising=False)
39 enable_admin_token(monkeypatch)
40 return data_dir, object_root
41
42
43def _install(body):
44 return request(
45 "/packages/hello-world/install",
46 method="POST",
47 body=json.dumps(body).encode(),
48 headers=auth_headers(),
49 )
50
51
52def _backup_names(data_dir):
53 directory = data_dir / "backups"
54 return sorted(p.name for p in directory.glob("*.tar.gz")) if directory.is_dir() else []
55
56
57def _make_backup():
58 status, _, payload = request("/admin/backups", method="POST", headers=auth_headers())
59 assert status == 201
60 return payload["backup"]["id"]
61
62
63def _no_changes():
64 _, _, history = request("/packages/hello-world/changes", headers=auth_headers())
65 return history["changes"] == []
66
67
68def test_install_reuses_named_backup_and_restore_uses_it(env):
69 data_dir, object_root = env
70 backup_id = _make_backup()
71 assert _backup_names(data_dir) == [backup_id]
72
73 status, _, result = _install({"allow_replace": True, "restore_point": backup_id})
74
75 assert status == 201
76 assert _backup_names(data_dir) == [backup_id] # nothing new written
77 restore_point = result["restore_point"]
78 assert restore_point["path"] == str(data_dir / "backups" / backup_id)
79 assert set(restore_point) == {
80 "path", "format_version", "created_at", "files", "bytes", "warnings",
81 }
82 assert (object_root / "hello" / "world.py").is_file()
83
84 change = result["changes"]["installed"]
85 assert change["details"]["restore_point"] == restore_point
86
87 status, _, restored = request(
88 "/packages/hello-world/restore",
89 method="POST",
90 body=json.dumps(
91 {"change_id": change["change_id"], "confirm": "restore-runtime"}
92 ).encode(),
93 headers=auth_headers(),
94 )
95 assert status == 200
96 assert restored["restore_point"]["path"] == restore_point["path"]
97 assert not (object_root / "hello" / "world.py").exists()
98 assert (object_root / "site" / "home.py").is_file()
99
100
101def test_install_without_field_still_writes_its_own_restore_point(env):
102 data_dir, _ = env
103 status, _, result = _install({})
104 assert status == 201
105 names = _backup_names(data_dir)
106 assert len(names) == 1 and names[0].endswith("-package-hello-world.tar.gz")
107 assert result["restore_point"]["path"].endswith(names[0])
108
109
110def test_missing_backup_is_refused_before_anything_is_written(env):
111 data_dir, object_root = env
112 status, _, payload = _install({"restore_point": "20990101T000000Z-manual.tar.gz"})
113 assert status == 409
114 assert payload["status"] == "error"
115 assert not (object_root / "hello" / "world.py").exists()
116 assert _backup_names(data_dir) == []
117 assert _no_changes()
118
119
120def test_unreadable_archive_is_refused(env):
121 data_dir, object_root = env
122 (data_dir / "backups").mkdir(parents=True)
123 (data_dir / "backups" / "junk.tar.gz").write_bytes(b"not a tarball")
124 status, _, _ = _install({"restore_point": "junk.tar.gz"})
125 assert status == 409
126 assert not (object_root / "hello" / "world.py").exists()
127 assert _no_changes()
128
129
130@pytest.mark.parametrize(
131 "value",
132 [
133 "../x.tar.gz",
134 "a/b.tar.gz",
135 "..\\x.tar.gz",
136 "/etc/passwd",
137 "not-an-archive.txt",
138 "",
139 " ",
140 7,
141 True,
142 ["a.tar.gz"],
143 None,
144 ],
145)
146def test_malformed_restore_point_is_a_400_and_writes_nothing(env, value):
147 data_dir, object_root = env
148 status, _, payload = _install({"restore_point": value})
149 assert status == 400
150 assert payload["status"] == "error"
151 assert not (object_root / "hello" / "world.py").exists()
152 assert _backup_names(data_dir) == []
153 assert _no_changes()
154
155
156def test_install_with_restore_point_still_requires_admin(env):
157 status, _, _ = request(
158 "/packages/hello-world/install",
159 method="POST",
160 body=json.dumps({"restore_point": "x.tar.gz"}).encode(),
161 )
162 assert status in (401, 403)