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

0 downloads

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
conftest.py · 7.4 KB · 181 lines
1"""Fixtures for the browser end-to-end lane: one real `uvicorn
2object_server:app` subprocess, seeded through the same public package/
3identity APIs every other test uses, driven by a real Chromium via
4pytest-playwright.
5
6Why this exists: 13 apps now share ONE generated-page pipeline (a schema,
7a seeded `views` record, site_view_render, and the shared
8window.dbbasicList/window.dbbasicForm JS). Unit/structural tests already
9cover each package's manifest and permission rules in isolation, but
10nothing exercises the pipeline the way a person actually does -- click
11the Add button, fill in fields the schema produced, watch the row show up
12without a reload. That gap is what tests/e2e/test_generated_ui.py closes;
13this file only builds the fixture (server + two identities) it drives.
14
15The server is booted ONCE per test session (module-costly: package
16install + process boot), not once per test -- pytest-playwright's own
17`page`/`context` fixtures are function-scoped and start each test with a
18brand-new, cookie-free browser context, so tests still don't leak session
19state into each other even though they share one server process and one
20data directory.
21"""
22
23from __future__ import annotations
24
25import os
26import socket
27import subprocess
28import sys
29import time
30import urllib.error
31import urllib.request
32from pathlib import Path
33from typing import Any
34
35import pytest
36
37ROOT = Path(__file__).resolve().parents[2]
38if str(ROOT) not in sys.path:
39 sys.path.insert(0, str(ROOT))
40
41import object_credentials # noqa: E402
42import object_identity # noqa: E402
43import object_packages # noqa: E402
44
45PACKAGES_ROOT = ROOT / "packages"
46
47# app-views + app-theme give the generated pipeline its renderer and JS
48# (site_view_render, /style, /list, /form, /nav); app-notes and app-tasks
49# are the two collections proving one pipeline serves many apps with zero
50# extra page code.
51PACKAGES_TO_INSTALL = ["app-views", "app-theme", "app-notes", "app-tasks"]
52
53# Long enough to clear object_credentials' MIN_PASSWORD_LENGTH by a wide
54# margin; not a security-relevant value, just a fixed test credential.
55PASSWORD = "correct horse battery staple"
56
57_SERVER_BOOT_TIMEOUT_SECONDS = 30.0
58
59
60def _free_tcp_port() -> int:
61 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
62 sock.bind(("127.0.0.1", 0))
63 return sock.getsockname()[1]
64
65
66def _wait_until_healthy(base_url: str, process: subprocess.Popen, log_path: Path) -> None:
67 deadline = time.monotonic() + _SERVER_BOOT_TIMEOUT_SECONDS
68 last_error: Exception | None = None
69 while time.monotonic() < deadline:
70 exit_code = process.poll()
71 if exit_code is not None:
72 log_text = log_path.read_text(encoding="utf-8", errors="replace") if log_path.exists() else ""
73 raise RuntimeError(
74 f"server process exited early (code {exit_code}) before answering /health:\n{log_text}"
75 )
76 try:
77 with urllib.request.urlopen(f"{base_url}/health", timeout=1) as response:
78 if response.status == 200:
79 return
80 except (urllib.error.URLError, ConnectionError, TimeoutError) as exc:
81 last_error = exc
82 time.sleep(0.1)
83 log_text = log_path.read_text(encoding="utf-8", errors="replace") if log_path.exists() else ""
84 raise RuntimeError(
85 f"server at {base_url} never answered /health within "
86 f"{_SERVER_BOOT_TIMEOUT_SECONDS}s (last error: {last_error}):\n{log_text}"
87 )
88
89
90@pytest.fixture(scope="session")
91def e2e_server(tmp_path_factory: pytest.TempPathFactory) -> dict[str, Any]:
92 """Boot one real object server for the whole e2e session.
93
94 Packages are installed through object_packages.install_package (the
95 same public write contract every package test uses) BEFORE the server
96 starts, and the two test users are created directly through
97 object_identity/object_credentials -- fixture setup, not something
98 under test. Signing in through /login IS under test; see
99 test_generated_ui.py.
100 """
101 data_dir = tmp_path_factory.mktemp("e2e-data")
102 objects_dir = tmp_path_factory.mktemp("e2e-objects")
103
104 for package_id in PACKAGES_TO_INSTALL:
105 object_packages.install_package(
106 package_id,
107 root=PACKAGES_ROOT,
108 base_dir=data_dir,
109 object_roots=[objects_dir],
110 )
111
112 users = {}
113 for label in ("alice", "bob"):
114 user_id = f"e2e_{label}"
115 email = f"{label}@e2e.test"
116 object_identity.create_user(
117 {"user_id": user_id, "email": email, "display_name": label.title()},
118 base_dir=data_dir,
119 )
120 object_credentials.set_password(user_id, PASSWORD, base_dir=data_dir)
121 users[label] = {"user_id": user_id, "email": email, "password": PASSWORD}
122
123 port = _free_tcp_port()
124 base_url = f"http://127.0.0.1:{port}"
125 log_path = data_dir / "server.log"
126
127 env = dict(os.environ)
128 env["DBBASIC_DATA_DIR"] = str(data_dir)
129 env["DBBASIC_OBJECTS_DIR"] = str(objects_dir)
130 # Password login is off by default (see object_server.PASSWORD_LOGIN_ENV);
131 # the login test drives the real /login form, so it must be reachable.
132 env["DBBASIC_ENABLE_PASSWORD_LOGIN"] = "true"
133 # Clean public URLs (/notes, /tasks -> site_routes -> site_view_render)
134 # are also feature-flagged off by default (object_server.SITE_ROUTES_ENV,
135 # _handle_site_route) -- without this every generated page 404s before
136 # site_routes is ever consulted.
137 env["DBBASIC_ENABLE_SITE_ROUTES"] = "true"
138 # Permission enforcement (row_filter, owner_scoped, etc.) is ALSO off
139 # by default -- object_server.PERMISSION_ENFORCEMENT_ENV -- and without
140 # it every /collections/... read/write instead falls back to the admin-
141 # token gate, which 403s every generated block on a server with no
142 # DBBASIC_ADMIN_TOKEN. Test 4 (owner_scoped notes) is meaningless
143 # without this actually on.
144 env["DBBASIC_ENABLE_PERMISSION_ENFORCEMENT"] = "true"
145 # object_permission_status.readiness_status refuses to actually engage
146 # enforcement (writes silently keep falling back to the admin-token
147 # gate above) unless an admin recovery token is configured -- a
148 # deliberate lockout guard, not a bug to route around. This is that
149 # token; nothing here relies on it for anything but clearing the gate.
150 env["DBBASIC_ADMIN_TOKEN"] = "e2e-test-admin-token"
151
152 with open(log_path, "wb") as log_file:
153 process = subprocess.Popen(
154 [
155 sys.executable, "-m", "uvicorn", "object_server:app",
156 "--host", "127.0.0.1", "--port", str(port),
157 ],
158 cwd=str(ROOT),
159 env=env,
160 stdout=log_file,
161 stderr=subprocess.STDOUT,
162 )
163 try:
164 _wait_until_healthy(base_url, process, log_path)
165 yield {"base_url": base_url, "users": users}
166 finally:
167 process.terminate()
168 try:
169 process.wait(timeout=10)
170 except subprocess.TimeoutExpired:
171 process.kill()
172 process.wait(timeout=10)
173
174
175@pytest.fixture(scope="session")
176def base_url(e2e_server: dict[str, Any]) -> str:
177 """Override pytest-playwright/pytest-base-url's default (None) so every
178 browser context this session creates resolves relative page.goto("/x")
179 calls against our own server -- see browser_context_args in
180 pytest_playwright, which reads this same fixture name."""
181 return e2e_server["base_url"]