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.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_private_package_overlay.py · 3.7 KB · 86 lines
| 1 | """Closed-source package overlay (packages-private/). |
| 2 | |
| 3 | The server searches a private package root ahead of the open `packages/` dir, |
| 4 | so a proprietary package (e.g. Mailcow email hosting) is discovered, listed, |
| 5 | and resolved through the same flow as an open one -- and shadows an open |
| 6 | package that shares its id. See packages-private/README.md. |
| 7 | """ |
| 8 | |
| 9 | import json |
| 10 | |
| 11 | import object_server |
| 12 | |
| 13 | |
| 14 | def _write_pkg(root, package_id, *, name): |
| 15 | pkg_dir = root / package_id |
| 16 | pkg_dir.mkdir(parents=True) |
| 17 | (pkg_dir / "dbbasic-package.json").write_text(json.dumps({ |
| 18 | "id": package_id, |
| 19 | "name": name, |
| 20 | "version": "0.1.0", |
| 21 | "compatibility": {"dbbasic_object_server": ">=0.1.0"}, |
| 22 | "objects": [], "schemas": [], "permissions": [], "seed": [], "migrations": [], |
| 23 | })) |
| 24 | return pkg_dir |
| 25 | |
| 26 | |
| 27 | def _point_env(monkeypatch, public, private): |
| 28 | monkeypatch.setenv(object_server.PACKAGES_DIR_ENV, str(public)) |
| 29 | monkeypatch.setenv(object_server.PRIVATE_PACKAGES_DIR_ENV, str(private)) |
| 30 | |
| 31 | |
| 32 | def test_private_root_included_only_when_it_exists(tmp_path, monkeypatch): |
| 33 | public = tmp_path / "packages"; public.mkdir() |
| 34 | private = tmp_path / "packages-private" # does NOT exist yet |
| 35 | _point_env(monkeypatch, public, private) |
| 36 | # absent private root -> behaves exactly like the open-only checkout |
| 37 | assert object_server._package_roots() == [str(public)] |
| 38 | private.mkdir() |
| 39 | assert object_server._package_roots() == [str(private), str(public)] |
| 40 | |
| 41 | |
| 42 | def test_list_merges_both_roots(tmp_path, monkeypatch): |
| 43 | public = tmp_path / "packages"; public.mkdir() |
| 44 | private = tmp_path / "packages-private"; private.mkdir() |
| 45 | _point_env(monkeypatch, public, private) |
| 46 | _write_pkg(public, "app-open-only", name="Open Only") |
| 47 | _write_pkg(private, "app-mailcow", name="Mailcow Hosting") |
| 48 | |
| 49 | ids = {p["id"] for p in object_server._list_all_packages()} |
| 50 | assert ids == {"app-open-only", "app-mailcow"} |
| 51 | |
| 52 | |
| 53 | def test_private_shadows_open_on_id_collision(tmp_path, monkeypatch): |
| 54 | public = tmp_path / "packages"; public.mkdir() |
| 55 | private = tmp_path / "packages-private"; private.mkdir() |
| 56 | _point_env(monkeypatch, public, private) |
| 57 | _write_pkg(public, "app-email", name="Open Email Adapter") |
| 58 | _write_pkg(private, "app-email", name="Private Email Override") |
| 59 | |
| 60 | # private wins in both the merged listing and single-id resolution |
| 61 | listed = {p["id"]: p["name"] for p in object_server._list_all_packages()} |
| 62 | assert listed["app-email"] == "Private Email Override" |
| 63 | assert object_server._root_for_package("app-email") == str(private) |
| 64 | |
| 65 | |
| 66 | def test_private_root_defaults_to_sibling_of_packages_dir(tmp_path, monkeypatch): |
| 67 | # With only the packages dir overridden, the private overlay must derive as |
| 68 | # a SIBLING of it -- never a fixed cwd-relative path -- so a hermetic test |
| 69 | # can't accidentally pick up a developer's real packages-private/ contents. |
| 70 | pkgs = tmp_path / "pkgs"; pkgs.mkdir() |
| 71 | monkeypatch.setenv(object_server.PACKAGES_DIR_ENV, str(pkgs)) |
| 72 | monkeypatch.delenv(object_server.PRIVATE_PACKAGES_DIR_ENV, raising=False) |
| 73 | assert object_server._private_packages_dir() == str(tmp_path / "packages-private") |
| 74 | # the sibling doesn't exist -> overlay empty -> only the open root is searched |
| 75 | assert object_server._package_roots() == [str(pkgs)] |
| 76 | |
| 77 | |
| 78 | def test_resolve_falls_back_to_open_root_for_unknown_id(tmp_path, monkeypatch): |
| 79 | public = tmp_path / "packages"; public.mkdir() |
| 80 | private = tmp_path / "packages-private"; private.mkdir() |
| 81 | _point_env(monkeypatch, public, private) |
| 82 | _write_pkg(public, "app-open-only", name="Open Only") |
| 83 | |
| 84 | assert object_server._root_for_package("app-open-only") == str(public) |
| 85 | # unknown id -> open root, so downstream raises the normal not-found |
| 86 | assert object_server._root_for_package("app-nope") == str(public) |