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_app_sites_package.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_app_sites_package.py · 5.1 KB · 119 lines
1"""Tests for packages/app-sites: websites published as site_pages records and
2served at /s/{site} and /s/{site}/{page} by site_sites_page.
3
4What these guard, and why it would matter:
5
6- The author's HTML is served sandboxed without allow-same-origin: a page's
7 scripts running as this server's origin could read the visitor's session
8 and act as them. This is the one claim that must never regress.
9- A private page and a missing page look the same (404): otherwise the
10 address alone tells a stranger what someone has drafted.
11- The owner sees their own unpublished pages; nobody else does.
12"""
13
14import csv
15import importlib.util
16import json
17from pathlib import Path
18
19import object_packages
20import object_records
21
22PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages"
23APP_DIR = PACKAGES_ROOT / "app-sites"
24
25
26def _page_module():
27 spec = importlib.util.spec_from_file_location("sites_page", APP_DIR / "objects" / "site" / "sites_page.py")
28 module = importlib.util.module_from_spec(spec)
29 spec.loader.exec_module(module)
30 return module
31
32
33ROWS = [
34 {"id": "1", "site": "bakery", "slug": "index", "title": "Home", "html": "<h1>Sunrise Bakery</h1>",
35 "is_public": "true", "owner_id": "dan"},
36 {"id": "2", "site": "bakery", "slug": "menu", "title": "Menu", "html": "<h1>Menu</h1>",
37 "is_public": "true", "owner_id": "dan"},
38 {"id": "3", "site": "bakery", "slug": "secret", "title": "Draft", "html": "<h1>Draft</h1>",
39 "is_public": "false", "owner_id": "dan"},
40]
41
42
43def _get(monkeypatch, request, rows=ROWS):
44 page = _page_module()
45 monkeypatch.setattr(object_records, "read_collection_records", lambda *a, **k: list(rows))
46 return page.GET(request)
47
48
49def test_manifest_ships_the_page_object_schema_rules_and_routes():
50 package = object_packages.get_package("app-sites", root=PACKAGES_ROOT)
51 assert {o["id"] for o in package["objects"]} == {"site_sites_page"}
52 assert {s["collection"] for s in package["schemas"]} == {"site_pages"}
53 with open(APP_DIR / "seed" / "site_routes.tsv", newline="") as fh:
54 routes = {r["pattern"]: r for r in csv.DictReader(fh, delimiter="\t")}
55 assert routes["/s/{site}/{page}"]["object_id"] == "site_sites_page"
56 assert routes["/s/{site}"]["object_id"] == "site_sites_page"
57 # the more specific pattern wins
58 assert int(routes["/s/{site}/{page}"]["priority"]) < int(routes["/s/{site}"]["priority"])
59
60
61def test_pages_are_served_sandboxed_without_same_origin(monkeypatch):
62 status, headers, body = _get(monkeypatch, {"site": "bakery", "page": "menu"})
63 assert status == 200 and body == "<h1>Menu</h1>"
64 csp = headers["content-security-policy"]
65 assert csp.startswith("sandbox")
66 assert "allow-same-origin" not in csp
67 assert headers["x-content-type-options"] == "nosniff"
68
69
70def test_the_site_home_goes_to_its_index_page(monkeypatch):
71 """/s/bakery redirects into the site's folder, so the index page's
72 relative links ("about.html") resolve inside the site (from /s/bakery a
73 browser would send them to /s/about.html)."""
74 status, headers, _ = _get(monkeypatch, {"site": "bakery"})
75 assert status == 302 and headers["location"] == "/s/bakery/index"
76 status, _, body = _get(monkeypatch, {"site": "bakery", "page": "index"})
77 assert status == 200 and "Sunrise Bakery" in body
78
79
80def test_builder_links_ending_in_html_resolve(monkeypatch):
81 status, _, body = _get(monkeypatch, {"site": "bakery", "page": "menu.html"})
82 assert status == 200 and body == "<h1>Menu</h1>"
83
84
85def test_a_private_page_is_a_404_like_a_missing_one(monkeypatch):
86 private = _get(monkeypatch, {"site": "bakery", "page": "secret"})
87 missing = _get(monkeypatch, {"site": "bakery", "page": "nothing-here"})
88 other = _get(monkeypatch, {"site": "bakery", "page": "secret", "_identity": {"user_id": "eve"}})
89 assert private[0] == missing[0] == other[0] == 404
90 assert private[2] == missing[2] == other[2]
91
92
93def test_the_owner_sees_their_own_draft(monkeypatch):
94 status, _, body = _get(monkeypatch, {"site": "bakery", "page": "secret", "_identity": {"user_id": "dan"}})
95 assert status == 200 and body == "<h1>Draft</h1>"
96
97
98def test_names_outside_the_pattern_never_reach_a_lookup(monkeypatch):
99 for request in ({"site": "../etc", "page": "index"}, {"site": "bakery", "page": "a/b"},
100 {"site": ""}, {"site": "Bakery!"}):
101 assert _get(monkeypatch, request)[0] == 404
102
103
104def test_without_the_collection_nothing_is_published(monkeypatch):
105 page = _page_module()
106
107 def missing(*a, **k):
108 raise FileNotFoundError("no collection")
109
110 monkeypatch.setattr(object_records, "read_collection_records", missing)
111 assert page.GET({"site": "bakery", "page": "index"})[0] == 404
112
113
114def test_rules_let_owners_write_their_own_and_everyone_read_only_public():
115 rules = json.loads((APP_DIR / "permissions" / "rules.json").read_text())["rules"]
116 owner = next(r for r in rules if r.get("collection") == "site_pages" and r["principal"] == "registered")
117 public = next(r for r in rules if r.get("collection") == "site_pages" and r["principal"] == "public")
118 assert owner["row_filter"] == {"owner_id": "$user_id"}
119 assert public["actions"] == ["read"] and public["row_filter"] == {"is_public": "true"}