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_object_site_routes.py · 11.2 KB · 287 lines
| 1 | """Tests for clean-URL site routing.""" |
| 2 | |
| 3 | import json |
| 4 | |
| 5 | import object_site_routes |
| 6 | import object_server |
| 7 | |
| 8 | from test_object_server import ( |
| 9 | ANONYMOUS_IDENTITY, |
| 10 | auth_headers, |
| 11 | enable_admin_token, |
| 12 | raw_request, |
| 13 | request, |
| 14 | write_records, |
| 15 | write_source, |
| 16 | ) |
| 17 | |
| 18 | |
| 19 | def enable_site_routes(monkeypatch, tmp_path): |
| 20 | root = tmp_path / "objects" |
| 21 | data_dir = tmp_path / "data" |
| 22 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(root)) |
| 23 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 24 | monkeypatch.setenv(object_server.SITE_ROUTES_ENV, "true") |
| 25 | return root, data_dir |
| 26 | |
| 27 | |
| 28 | def test_convention_object_id_mapping(): |
| 29 | assert object_site_routes.convention_object_id("/") == "site_home" |
| 30 | assert object_site_routes.convention_object_id("/about") == "site_about" |
| 31 | assert object_site_routes.convention_object_id("/docs/install") == "site_docs_install" |
| 32 | assert object_site_routes.convention_object_id("/getting-started") == "site_getting_started" |
| 33 | assert object_site_routes.convention_object_id("/has space") is None |
| 34 | assert object_site_routes.convention_object_id("/../etc") is None |
| 35 | assert object_site_routes.convention_object_id("/favicon.ico") is None |
| 36 | |
| 37 | |
| 38 | def test_pattern_matching_with_uuid_params(): |
| 39 | records = [ |
| 40 | {"pattern": "/articles/{article_id:uuid}", "object_id": "articles_view"}, |
| 41 | {"pattern": "/blog/{slug}", "object_id": "blog_post"}, |
| 42 | {"pattern": "/blog/archive", "object_id": "blog_archive", "priority": 1}, |
| 43 | ] |
| 44 | |
| 45 | uuid_value = "123e4567-e89b-42d3-a456-426614174000" |
| 46 | assert object_site_routes.match_records(f"/articles/{uuid_value}", records) == ( |
| 47 | "articles_view", |
| 48 | {"article_id": uuid_value}, |
| 49 | ) |
| 50 | assert object_site_routes.match_records("/articles/not-a-uuid", records) is None |
| 51 | assert object_site_routes.match_records("/blog/hello-world", records) == ( |
| 52 | "blog_post", |
| 53 | {"slug": "hello-world"}, |
| 54 | ) |
| 55 | assert object_site_routes.match_records("/blog/archive", records) == ( |
| 56 | "blog_archive", |
| 57 | {}, |
| 58 | ) |
| 59 | assert object_site_routes.match_records("/nope", records) is None |
| 60 | |
| 61 | |
| 62 | def test_site_routes_disabled_by_default(tmp_path, monkeypatch): |
| 63 | root = tmp_path / "objects" |
| 64 | write_source(root / "site" / "about.py", "def GET(request):\n return {'page': 'about'}\n") |
| 65 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(root)) |
| 66 | monkeypatch.delenv(object_server.SITE_ROUTES_ENV, raising=False) |
| 67 | |
| 68 | status, _, payload = request("/about") |
| 69 | |
| 70 | assert status == 404 |
| 71 | assert payload == {"status": "error", "error": "Not found"} |
| 72 | |
| 73 | |
| 74 | def test_convention_routes_serve_objects(tmp_path, monkeypatch): |
| 75 | root, _ = enable_site_routes(monkeypatch, tmp_path) |
| 76 | write_source(root / "site" / "home.py", "def GET(request):\n return {'page': 'home'}\n") |
| 77 | write_source(root / "site" / "about.py", "def GET(request):\n return {'page': 'about'}\n") |
| 78 | write_source( |
| 79 | root / "site" / "docs" / "install.py", |
| 80 | "def GET(request):\n return {'page': 'install', 'v': request.get('v')}\n", |
| 81 | ) |
| 82 | |
| 83 | home_status, _, home = request("/") |
| 84 | about_status, _, about = request("/about") |
| 85 | nested_status, _, nested = request("/docs/install", query_string="v=2") |
| 86 | |
| 87 | assert home_status == 200 and home == {"page": "home"} |
| 88 | assert about_status == 200 and about == {"page": "about"} |
| 89 | assert nested_status == 200 and nested == {"page": "install", "v": "2"} |
| 90 | |
| 91 | |
| 92 | def test_route_table_patterns_execute_with_params(tmp_path, monkeypatch): |
| 93 | root, data_dir = enable_site_routes(monkeypatch, tmp_path) |
| 94 | write_source( |
| 95 | root / "articles" / "view.py", |
| 96 | "def GET(request):\n return {'article_id': request['article_id']}\n", |
| 97 | ) |
| 98 | uuid_value = "123e4567-e89b-42d3-a456-426614174000" |
| 99 | write_records( |
| 100 | data_dir, |
| 101 | "site_routes", |
| 102 | "id\tpattern\tobject_id\tpriority\n" |
| 103 | "r1\t/articles/{article_id:uuid}\tarticles_view\t10\n", |
| 104 | ) |
| 105 | |
| 106 | status, _, payload = request(f"/articles/{uuid_value}") |
| 107 | miss_status, _, _ = request("/articles/not-a-uuid") |
| 108 | |
| 109 | assert status == 200 |
| 110 | assert payload == {"article_id": uuid_value} |
| 111 | assert miss_status == 404 |
| 112 | |
| 113 | |
| 114 | def test_site_404_object_handles_misses(tmp_path, monkeypatch): |
| 115 | root, _ = enable_site_routes(monkeypatch, tmp_path) |
| 116 | write_source( |
| 117 | root / "site" / "404.py", |
| 118 | "def GET(request):\n" |
| 119 | " return {'content_type': 'text/html', 'status_code': 404,\n" |
| 120 | " 'body': 'missing: ' + request.get('path', '')}\n", |
| 121 | ) |
| 122 | |
| 123 | status, headers, body = raw_request("/no/such/page") |
| 124 | |
| 125 | assert status == 404 |
| 126 | assert b"missing: /no/such/page" in body |
| 127 | |
| 128 | |
| 129 | def test_form_post_to_routed_page(tmp_path, monkeypatch): |
| 130 | root, _ = enable_site_routes(monkeypatch, tmp_path) |
| 131 | write_source( |
| 132 | root / "site" / "contact.py", |
| 133 | "def POST(request):\n" |
| 134 | " return {'received': request.get('email'), 'user': request['_identity']['user_id']}\n", |
| 135 | ) |
| 136 | |
| 137 | status, _, payload = request( |
| 138 | "/contact", |
| 139 | method="POST", |
| 140 | body=b"email=dan%40example.com", |
| 141 | headers=[("content-type", "application/x-www-form-urlencoded")], |
| 142 | ) |
| 143 | |
| 144 | assert status == 200 |
| 145 | assert payload == {"received": "dan@example.com", "user": None} |
| 146 | |
| 147 | |
| 148 | def test_reserved_routes_are_never_shadowed(tmp_path, monkeypatch): |
| 149 | root, _ = enable_site_routes(monkeypatch, tmp_path) |
| 150 | write_source(root / "site" / "health.py", "def GET(request):\n return {'fake': True}\n") |
| 151 | write_source(root / "site" / "login.py", "def GET(request):\n return {'fake': True}\n") |
| 152 | |
| 153 | health_status, _, health = request("/health") |
| 154 | login_status, _, login_body = raw_request("/login") |
| 155 | |
| 156 | assert health_status == 200 |
| 157 | assert health == {"status": "ok"} |
| 158 | assert login_status == 403 |
| 159 | assert b"Password login is disabled" in login_body |
| 160 | |
| 161 | |
| 162 | def test_normalize_host_and_resolve_host(): |
| 163 | records = [ |
| 164 | {"id": "h1", "host": "DBBasic.com:443", "prefix": "db", "home_object": "db_start"}, |
| 165 | {"id": "h2", "host": "q9.is", "prefix": "q9"}, |
| 166 | {"id": "bad", "host": "evil.example", "prefix": "Bad-Prefix!"}, |
| 167 | ] |
| 168 | |
| 169 | assert object_site_routes.normalize_host("DBBasic.com:8443") == "dbbasic.com" |
| 170 | db = object_site_routes.resolve_host("dbbasic.com", records) |
| 171 | q9 = object_site_routes.resolve_host("Q9.IS:443", records) |
| 172 | unknown = object_site_routes.resolve_host("other.example", records) |
| 173 | invalid = object_site_routes.resolve_host("evil.example", records) |
| 174 | |
| 175 | assert db == {"host": "dbbasic.com", "prefix": "db", "home": "db_start", "not_found": "db_404"} |
| 176 | assert q9["prefix"] == "q9" and q9["home"] == "q9_home" and q9["not_found"] == "q9_404" |
| 177 | assert unknown["prefix"] == "site" and unknown["home"] == "site_home" |
| 178 | assert invalid["prefix"] == "site" # bad prefix records are ignored |
| 179 | |
| 180 | |
| 181 | def test_multi_domain_conventions_serve_separate_sites(tmp_path, monkeypatch): |
| 182 | root, data_dir = enable_site_routes(monkeypatch, tmp_path) |
| 183 | write_source(root / "site" / "home.py", "def GET(request):\n return {'site': 'default'}\n") |
| 184 | write_source(root / "db" / "home.py", "def GET(request):\n return {'site': 'dbbasic'}\n") |
| 185 | write_source(root / "db" / "pricing.py", "def GET(request):\n return {'page': 'pricing'}\n") |
| 186 | write_source(root / "q9" / "home.py", "def GET(request):\n return {'site': 'q9'}\n") |
| 187 | write_records( |
| 188 | data_dir, |
| 189 | "site_hosts", |
| 190 | "id\thost\tprefix\thome_object\tnot_found_object\n" |
| 191 | "h1\tdbbasic.com\tdb\t\t\n" |
| 192 | "h2\tq9.is\tq9\t\t\n", |
| 193 | ) |
| 194 | |
| 195 | default_status, _, default_home = request("/") |
| 196 | db_status, _, db_home = request("/", headers=[("host", "dbbasic.com")]) |
| 197 | db_page_status, _, db_page = request("/pricing", headers=[("host", "DBBASIC.COM:443")]) |
| 198 | q9_status, _, q9_home = request("/", headers=[("host", "q9.is")]) |
| 199 | cross_status, _, _ = request("/pricing", headers=[("host", "q9.is")]) |
| 200 | |
| 201 | assert default_status == 200 and default_home == {"site": "default"} |
| 202 | assert db_status == 200 and db_home == {"site": "dbbasic"} |
| 203 | assert db_page_status == 200 and db_page == {"page": "pricing"} |
| 204 | assert q9_status == 200 and q9_home == {"site": "q9"} |
| 205 | assert cross_status == 404 # q9.is has no pricing page; sites do not leak |
| 206 | |
| 207 | |
| 208 | def test_host_scoped_route_records(tmp_path, monkeypatch): |
| 209 | root, data_dir = enable_site_routes(monkeypatch, tmp_path) |
| 210 | write_source(root / "db" / "article.py", "def GET(request):\n return {'site': 'db', 'slug': request['slug']}\n") |
| 211 | write_source(root / "q9" / "article.py", "def GET(request):\n return {'site': 'q9', 'slug': request['slug']}\n") |
| 212 | write_records( |
| 213 | data_dir, |
| 214 | "site_hosts", |
| 215 | "id\thost\tprefix\nh1\tdbbasic.com\tdb\nh2\tq9.is\tq9\n", |
| 216 | ) |
| 217 | write_records( |
| 218 | data_dir, |
| 219 | "site_routes", |
| 220 | "id\tpattern\tobject_id\thost\tpriority\n" |
| 221 | "r1\t/blog/{slug}\tdb_article\tdbbasic.com\t10\n" |
| 222 | "r2\t/blog/{slug}\tq9_article\tq9.is\t10\n", |
| 223 | ) |
| 224 | |
| 225 | db_status, _, db_payload = request("/blog/hello", headers=[("host", "dbbasic.com")]) |
| 226 | q9_status, _, q9_payload = request("/blog/hello", headers=[("host", "q9.is")]) |
| 227 | |
| 228 | assert db_status == 200 and db_payload == {"site": "db", "slug": "hello"} |
| 229 | assert q9_status == 200 and q9_payload == {"site": "q9", "slug": "hello"} |
| 230 | |
| 231 | |
| 232 | def test_host_specific_404_with_generic_fallback(tmp_path, monkeypatch): |
| 233 | root, data_dir = enable_site_routes(monkeypatch, tmp_path) |
| 234 | write_source( |
| 235 | root / "db" / "404.py", |
| 236 | "def GET(request):\n" |
| 237 | " return {'content_type': 'text/html', 'status_code': 404, 'body': 'db missing'}\n", |
| 238 | ) |
| 239 | write_source( |
| 240 | root / "site" / "404.py", |
| 241 | "def GET(request):\n" |
| 242 | " return {'content_type': 'text/html', 'status_code': 404, 'body': 'generic missing'}\n", |
| 243 | ) |
| 244 | write_records( |
| 245 | data_dir, |
| 246 | "site_hosts", |
| 247 | "id\thost\tprefix\nh1\tdbbasic.com\tdb\nh2\tq9.is\tq9\n", |
| 248 | ) |
| 249 | |
| 250 | db_status, _, db_body = raw_request("/nope", headers=[("host", "dbbasic.com")]) |
| 251 | q9_status, _, q9_body = raw_request("/nope", headers=[("host", "q9.is")]) |
| 252 | |
| 253 | assert db_status == 404 and b"db missing" in db_body |
| 254 | assert q9_status == 404 and b"generic missing" in q9_body # q9 has no q9_404; generic serves |
| 255 | |
| 256 | |
| 257 | def test_site_routes_respect_permission_enforcement(tmp_path, monkeypatch): |
| 258 | from test_object_server import save_permission_policy |
| 259 | |
| 260 | root, data_dir = enable_site_routes(monkeypatch, tmp_path) |
| 261 | write_source(root / "site" / "public.py", "def GET(request):\n return {'ok': True}\n") |
| 262 | write_source(root / "site" / "secret.py", "def GET(request):\n return {'ok': True}\n") |
| 263 | save_permission_policy( |
| 264 | data_dir, |
| 265 | { |
| 266 | "access_mode": "role_based", |
| 267 | "rules": [ |
| 268 | { |
| 269 | "effect": "allow", |
| 270 | "principal": "public", |
| 271 | "actions": ["execute"], |
| 272 | "object_id": "site_public", |
| 273 | } |
| 274 | ], |
| 275 | }, |
| 276 | ) |
| 277 | monkeypatch.setenv(object_server.PERMISSION_ENFORCEMENT_ENV, "true") |
| 278 | monkeypatch.setenv(object_server.PERMISSION_TRUST_HEADERS_ENV, "true") |
| 279 | enable_admin_token(monkeypatch) |
| 280 | |
| 281 | public_status, _, public_payload = request("/public") |
| 282 | secret_status, _, secret_payload = request("/secret") |
| 283 | |
| 284 | assert public_status == 200 |
| 285 | assert public_payload == {"ok": True} |
| 286 | assert secret_status == 403 |
| 287 | assert secret_payload["code"] == "forbidden" |