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_privacy.py · 29.0 KB · 649 lines
| 1 | """The tests that keep a generated privacy policy true. |
| 2 | |
| 3 | A privacy policy is the one document in a system that nothing checks. It |
| 4 | is written once, by a template, and the software changes weekly; nothing |
| 5 | connects the two, so nothing notices when they part company. Generating |
| 6 | the page from the configuration removes most of that drift by |
| 7 | construction -- but only most of it, because the generator itself is code |
| 8 | somebody can forget to update. |
| 9 | |
| 10 | So the first test in this file is a SWEEP, and it is the one that matters: |
| 11 | it walks every source file on the box looking for anything that sets a |
| 12 | cookie, and fails unless every cookie name it finds is disclosed in the |
| 13 | policy's table, naming the undisclosed cookie in the failure message. A |
| 14 | new cookie added anywhere in this repo breaks the build until somebody |
| 15 | writes down what it is for and whether it is strictly necessary. That is a |
| 16 | property no amount of care in the policy module can give you, because the |
| 17 | thing it guards against happens in a file the policy module has never |
| 18 | heard of. |
| 19 | |
| 20 | The rest hold the fold honest in the directions it could go wrong: a |
| 21 | retention it states that the box does not keep, a section for an app that |
| 22 | is not installed, a sub-processor nobody configured, a policy nobody |
| 23 | signed, and an export that returns somebody else's rows. |
| 24 | """ |
| 25 | |
| 26 | import importlib.util |
| 27 | import json |
| 28 | import pathlib |
| 29 | import re |
| 30 | |
| 31 | import pytest |
| 32 | |
| 33 | from conftest import stage_collection |
| 34 | |
| 35 | import object_analytics |
| 36 | import object_execution |
| 37 | import object_records |
| 38 | import python_object_runtime |
| 39 | |
| 40 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 41 | PACKAGES = REPO_ROOT / "packages" |
| 42 | PRIVACY_OBJECTS = PACKAGES / "app-privacy" / "objects" |
| 43 | RUNTIME = python_object_runtime.PythonObjectRuntime() |
| 44 | |
| 45 | |
| 46 | def _module(relative): |
| 47 | """Import a package object as a module, for the folds a test needs to |
| 48 | call directly rather than through a rendered page.""" |
| 49 | path = PACKAGES / relative |
| 50 | spec = importlib.util.spec_from_file_location(path.stem, path) |
| 51 | module = importlib.util.module_from_spec(spec) |
| 52 | spec.loader.exec_module(module) |
| 53 | return module |
| 54 | |
| 55 | |
| 56 | privacy = _module("app-privacy/objects/site/privacy.py") |
| 57 | |
| 58 | |
| 59 | # =========================================================================== |
| 60 | # 1. The sweep: every cookie this codebase sets is disclosed |
| 61 | # =========================================================================== |
| 62 | |
| 63 | # A file is worth reading at all only if it mentions setting a cookie. |
| 64 | _COOKIE_FILE = re.compile(r"set[_-]cookie|max-age", re.I) |
| 65 | # ...and a candidate name counts only if a cookie ATTRIBUTE is nearby, so |
| 66 | # an ordinary `days=7` in a query string is not mistaken for a cookie. |
| 67 | _COOKIE_ATTRIBUTE = re.compile(r"Path=/|HttpOnly|SameSite|Max-Age|set[_-]cookie", |
| 68 | re.I) |
| 69 | # The two shapes a cookie name is written in here: a constant interpolated |
| 70 | # into the header (`f"{COOKIE}={token}; Path=/"`) or a literal |
| 71 | # (`"consent=yes; Path=/"`). |
| 72 | _NAME_FROM_CONSTANT = re.compile(r"\{\s*([A-Za-z_][A-Za-z0-9_.]*)\s*\}\s*=") |
| 73 | _NAME_LITERAL = re.compile(r"""["']\s*([A-Za-z][A-Za-z0-9_-]*)=""") |
| 74 | _LITERAL_MAX_AGE = re.compile(r"Max-Age=(\d+)") |
| 75 | _WINDOW = 300 |
| 76 | |
| 77 | # Cookie attributes are not cookie names; they look identical to the |
| 78 | # literal pattern above and are the only false positives it produces. |
| 79 | _ATTRIBUTE_WORDS = {"path", "httponly", "samesite", "max-age", "secure", |
| 80 | "expires", "domain", "priority", "partitioned", "version"} |
| 81 | |
| 82 | |
| 83 | def _source_files(): |
| 84 | """Every Python file this server actually runs. Tests excluded: a test |
| 85 | fixture is allowed to write `cart=xyz` without that being a cookie the |
| 86 | product sets.""" |
| 87 | files = list(REPO_ROOT.glob("*.py")) + list(PACKAGES.rglob("*.py")) |
| 88 | return [path for path in files if "__pycache__" not in path.parts] |
| 89 | |
| 90 | |
| 91 | def _constant_values(): |
| 92 | """Every `NAME = "literal"` on the box, so a cookie whose name is a |
| 93 | constant resolves to the string a browser would actually see.""" |
| 94 | values = {} |
| 95 | pattern = re.compile( |
| 96 | r"^\s*([A-Za-z_][A-Za-z0-9_]*)\s*(?::[^=\n]+)?=\s*[\"']([^\"'\n]+)[\"']", |
| 97 | re.M) |
| 98 | for path in _source_files(): |
| 99 | for match in pattern.finditer(path.read_text(encoding="utf-8", |
| 100 | errors="replace")): |
| 101 | values.setdefault(match.group(1), match.group(2)) |
| 102 | return values |
| 103 | |
| 104 | |
| 105 | def _cookies_set_in_source(): |
| 106 | """(cookie name, file, literal Max-Age or None) for everything on this |
| 107 | box that puts a cookie in a browser. |
| 108 | |
| 109 | An unresolvable name is returned as-is rather than dropped: a cookie |
| 110 | written in a shape this sweep cannot read must fail the test too, |
| 111 | because the alternative is a sweep that goes quietly blind. |
| 112 | """ |
| 113 | constants = _constant_values() |
| 114 | found = [] |
| 115 | for path in _source_files(): |
| 116 | text = path.read_text(encoding="utf-8", errors="replace") |
| 117 | if not _COOKIE_FILE.search(text): |
| 118 | continue |
| 119 | for pattern, from_constant in ((_NAME_FROM_CONSTANT, True), |
| 120 | (_NAME_LITERAL, False)): |
| 121 | for match in pattern.finditer(text): |
| 122 | window = text[max(0, match.start() - _WINDOW): |
| 123 | match.end() + _WINDOW] |
| 124 | if not _COOKIE_ATTRIBUTE.search(window): |
| 125 | continue |
| 126 | raw = match.group(1) |
| 127 | if not from_constant and raw.lower() in _ATTRIBUTE_WORDS: |
| 128 | continue |
| 129 | name = (constants.get(raw.split(".")[-1], raw) if from_constant |
| 130 | else raw) |
| 131 | ages = set(_LITERAL_MAX_AGE.findall(window)) |
| 132 | age = int(ages.pop()) if len(ages) == 1 else None |
| 133 | found.append((name, path.relative_to(REPO_ROOT).as_posix(), age)) |
| 134 | return found |
| 135 | |
| 136 | |
| 137 | def test_every_cookie_this_codebase_sets_is_disclosed_in_the_policy(): |
| 138 | """THE test that keeps the policy true. |
| 139 | |
| 140 | Nothing else in this file can stop the drift this one stops, because |
| 141 | the drift happens in a file the policy module has never heard of: a |
| 142 | package adds a cookie, ships, and the policy goes on describing a |
| 143 | server that no longer exists. A policy that CAN be wrong about the |
| 144 | software is the normal case on the web; the only thing that makes one |
| 145 | that cannot is a check that reads the software rather than the policy. |
| 146 | """ |
| 147 | disclosed = privacy.disclosed_cookie_names() |
| 148 | undisclosed = sorted({(name, where) |
| 149 | for name, where, _age in _cookies_set_in_source() |
| 150 | if name not in disclosed}) |
| 151 | assert not undisclosed, ( |
| 152 | "these cookies are set by this codebase and disclosed nowhere in the " |
| 153 | f"privacy policy's cookie table: {undisclosed}. Add each one to " |
| 154 | "COOKIE_DISCLOSURES in packages/app-privacy/objects/site/privacy.py " |
| 155 | "with what it is for, when it is set, and whether it is strictly " |
| 156 | "necessary for something the visitor asked for -- that last answer is " |
| 157 | "the one with a consent consequence. If the name shown is an " |
| 158 | "unresolved identifier rather than a cookie name, the sweep could not " |
| 159 | "read the shape it is written in; write the name as a module-level " |
| 160 | "constant so it can.") |
| 161 | |
| 162 | |
| 163 | def test_the_sweep_can_actually_see_the_cookies_this_server_sets(): |
| 164 | """A guard on the guard. A regex that matches nothing passes the test |
| 165 | above forever, and the failure would be silent and permanent -- the |
| 166 | single most dangerous shape a test can have.""" |
| 167 | found = {name for name, _where, _age in _cookies_set_in_source()} |
| 168 | assert {"dbbasic_session", "cart", object_analytics.VISITOR_COOKIE_NAME} <= found, ( |
| 169 | f"the sweep found {sorted(found)}; it should at minimum see the " |
| 170 | "identity session cookie, the shop's basket cookie and the visitor " |
| 171 | "cookie. If one has moved, fix the sweep -- do not narrow it.") |
| 172 | |
| 173 | |
| 174 | def test_a_disclosed_lifetime_matches_the_one_the_code_actually_writes(): |
| 175 | """Disclosing a cookie is not enough if the number beside it is |
| 176 | wrong. Where the source writes a literal Max-Age, the policy must |
| 177 | publish that same number of seconds.""" |
| 178 | wrong = [] |
| 179 | for name, where, age in _cookies_set_in_source(): |
| 180 | if age is None or name not in privacy.disclosed_cookie_names(): |
| 181 | continue |
| 182 | published = privacy.cookie_max_age_seconds(name) |
| 183 | if published != age: |
| 184 | wrong.append(f"{name} in {where}: code sets {age}s, " |
| 185 | f"policy publishes {published}s") |
| 186 | assert not wrong, ( |
| 187 | f"the policy publishes a lifetime the code does not set: {wrong}") |
| 188 | |
| 189 | |
| 190 | def test_the_policy_states_no_law_it_cannot_verify(): |
| 191 | """The refusal, held as a property rather than a promise in a |
| 192 | docstring. Confident paragraphs about lawful bases and transfer |
| 193 | mechanisms would be legal claims about somebody else's business, |
| 194 | generated by a program that cannot know it -- and pasted into the one |
| 195 | document a regulator reads. Wrong legal text is worse than none.""" |
| 196 | fold = privacy.policy(base="/nonexistent", env={}) |
| 197 | rendered = json.dumps(fold) + privacy.GET({"_path": "/privacy"})["body"] |
| 198 | forbidden = ["standard contractual clause", "lawful basis", "lawful bases", |
| 199 | "legitimate interest", "article 6", "adequacy decision", |
| 200 | "gdpr compliant", "we are compliant"] |
| 201 | present = [phrase for phrase in forbidden if phrase in rendered.lower()] |
| 202 | assert not present, ( |
| 203 | f"the generated policy asserts legal conclusions it cannot verify: " |
| 204 | f"{present}. Facts about the system go here; legal framing goes in " |
| 205 | "privacy.extra_markdown, where a human signs it.") |
| 206 | |
| 207 | |
| 208 | # =========================================================================== |
| 209 | # 2. The fold: the policy says what the box is configured to do |
| 210 | # =========================================================================== |
| 211 | |
| 212 | SIGNED = ( |
| 213 | ("privacy.controller_name", "Q9 Ltd"), |
| 214 | ("privacy.contact_email", "privacy@example.test"), |
| 215 | ("privacy.jurisdiction", "England and Wales"), |
| 216 | ) |
| 217 | |
| 218 | |
| 219 | def box(tmp_path, monkeypatch, *, settings=SIGNED, apps=(), analytics=False, |
| 220 | visitor_cookie=False, retention=None, env=()): |
| 221 | """One configured server. `apps` names packages whose collection is |
| 222 | staged, which is what makes them installed as far as the policy is |
| 223 | concerned.""" |
| 224 | data_dir = tmp_path / "data" |
| 225 | rows = "".join(f"s{index}\t{key}\t{value}\t\n" |
| 226 | for index, (key, value) in enumerate(settings)) |
| 227 | stage_collection(data_dir, "app-settings", "app_settings", rows=rows) |
| 228 | for package_id, collection in apps: |
| 229 | stage_collection(data_dir, package_id, collection) |
| 230 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 231 | if analytics: |
| 232 | monkeypatch.setenv("DBBASIC_ANALYTICS", "on") |
| 233 | else: |
| 234 | monkeypatch.delenv("DBBASIC_ANALYTICS", raising=False) |
| 235 | if visitor_cookie: |
| 236 | monkeypatch.setenv("DBBASIC_ANALYTICS_VISITOR_COOKIE", "on") |
| 237 | else: |
| 238 | monkeypatch.delenv("DBBASIC_ANALYTICS_VISITOR_COOKIE", raising=False) |
| 239 | if retention is not None: |
| 240 | monkeypatch.setenv("DBBASIC_ANALYTICS_RETENTION_DAYS", str(retention)) |
| 241 | else: |
| 242 | monkeypatch.delenv("DBBASIC_ANALYTICS_RETENTION_DAYS", raising=False) |
| 243 | for key, value in env: |
| 244 | monkeypatch.setenv(key, value) |
| 245 | return data_dir |
| 246 | |
| 247 | |
| 248 | def render(path="/privacy"): |
| 249 | """Through the real execution path, exactly as a visitor reaches it.""" |
| 250 | return object_execution.execute_object( |
| 251 | RUNTIME, |
| 252 | object_execution.ObjectExecutionRequest( |
| 253 | "site_privacy", method="GET", payload={"_path": path}), |
| 254 | roots=[PRIVACY_OBJECTS]).result |
| 255 | |
| 256 | |
| 257 | def test_it_states_the_retention_the_box_is_actually_configured_with( |
| 258 | tmp_path, monkeypatch): |
| 259 | """The property the whole design is for. A policy that claims 30 days |
| 260 | while the box keeps 180 is the normal case on the web, and it is a |
| 261 | claim nobody can be held to because nobody wrote it deliberately.""" |
| 262 | box(tmp_path, monkeypatch, analytics=True, retention=45) |
| 263 | assert "45 days" in render()["body"] |
| 264 | |
| 265 | |
| 266 | def test_changing_the_setting_changes_the_page(tmp_path, monkeypatch): |
| 267 | """Not merely "a number appears" -- the number follows the box. This |
| 268 | is what makes the page a fold rather than a template with a variable |
| 269 | in it.""" |
| 270 | box(tmp_path, monkeypatch, analytics=True, retention=7) |
| 271 | assert "7 days" in render()["body"] |
| 272 | monkeypatch.setenv("DBBASIC_ANALYTICS_RETENTION_DAYS", "180") |
| 273 | body = render()["body"] |
| 274 | assert "180 days" in body and "7 days" not in body |
| 275 | |
| 276 | |
| 277 | def test_analytics_off_removes_the_section_entirely(tmp_path, monkeypatch): |
| 278 | """Not "0 page views kept" -- the honest statement is that nothing is |
| 279 | collected at all, and a section full of zeros would read as a |
| 280 | collection that happens to be empty.""" |
| 281 | box(tmp_path, monkeypatch, analytics=False) |
| 282 | body = render()["body"] |
| 283 | assert "This server keeps no log of page views" in body |
| 284 | assert "User agent" not in body and "Referrer" not in body |
| 285 | fold = privacy.policy() |
| 286 | assert fold["analytics"]["fields"] == [] |
| 287 | assert fold["analytics"]["retention_days"] == 0 |
| 288 | |
| 289 | |
| 290 | def test_analytics_on_names_the_fields_actually_recorded(tmp_path, monkeypatch): |
| 291 | """"Usage data" is the phrasing that lets a policy mean anything. The |
| 292 | fields are named one by one because they are knowable.""" |
| 293 | box(tmp_path, monkeypatch, analytics=True) |
| 294 | body = render()["body"] |
| 295 | for field in ("IP address", "User agent", "Referrer", "Path and method"): |
| 296 | assert field in body, field |
| 297 | |
| 298 | |
| 299 | def test_the_cookie_table_follows_the_configuration(tmp_path, monkeypatch): |
| 300 | """Off -- the default -- and the analytics cookie is not in the table, |
| 301 | because it cannot be set. On, and it appears with the real Max-Age |
| 302 | this server writes, not a number somebody typed.""" |
| 303 | box(tmp_path, monkeypatch, analytics=True, visitor_cookie=False) |
| 304 | off = render("/cookies")["body"] |
| 305 | assert object_analytics.VISITOR_COOKIE_NAME not in off |
| 306 | assert "dbbasic_session" in off # the necessary one is still listed |
| 307 | |
| 308 | box(tmp_path, monkeypatch, analytics=True, visitor_cookie=True) |
| 309 | on = render("/cookies")["body"] |
| 310 | assert object_analytics.VISITOR_COOKIE_NAME in on |
| 311 | assert f"Max-Age={object_analytics.DEFAULT_VISITOR_DAYS * 86400}" in on |
| 312 | # And the consent-relevant column is answered honestly: this is the |
| 313 | # one cookie here that is not strictly necessary. |
| 314 | rows = {row["name"]: row for row in privacy.cookie_table()} |
| 315 | assert rows[object_analytics.VISITOR_COOKIE_NAME]["strictly_necessary"] is False |
| 316 | assert rows["dbbasic_session"]["strictly_necessary"] is True |
| 317 | |
| 318 | |
| 319 | def test_the_basket_cookie_appears_only_where_there_is_a_shop( |
| 320 | tmp_path, monkeypatch): |
| 321 | box(tmp_path, monkeypatch) |
| 322 | assert "cart" not in {row["name"] for row in privacy.cookie_table()} |
| 323 | |
| 324 | box(tmp_path, monkeypatch, apps=[("app-shop", "carts")]) |
| 325 | row = {entry["name"]: entry for entry in privacy.cookie_table()}["cart"] |
| 326 | assert row["strictly_necessary"] is True |
| 327 | assert row["max_age_seconds"] == privacy.CART_COOKIE_MAX_AGE |
| 328 | |
| 329 | |
| 330 | def test_sections_appear_only_when_the_thing_exists(tmp_path, monkeypatch): |
| 331 | """A fulfilment paragraph on a box that ships nothing is invented |
| 332 | processing, and invented processing in a privacy policy is the |
| 333 | failure mode this page exists to remove.""" |
| 334 | box(tmp_path, monkeypatch) |
| 335 | bare = render()["body"] |
| 336 | assert "Delivery" not in bare and "Payments" not in bare |
| 337 | |
| 338 | box(tmp_path, monkeypatch, apps=[("app-shipping", "shipments"), |
| 339 | ("app-payments", "payments")]) |
| 340 | full = render()["body"] |
| 341 | assert "Delivery" in full and "Payments" in full |
| 342 | |
| 343 | |
| 344 | def test_the_subprocessor_list_contains_only_configured_third_parties( |
| 345 | tmp_path, monkeypatch): |
| 346 | """Derived from credentials that are actually present, so it is a list |
| 347 | nobody has to remember to update: a key removed from the environment |
| 348 | removes the row.""" |
| 349 | box(tmp_path, monkeypatch, apps=[("app-payments", "payments")]) |
| 350 | assert privacy.subprocessors() == [] |
| 351 | assert "No third party is configured" in render()["body"] |
| 352 | |
| 353 | monkeypatch.setenv("DBBASIC_STRIPE_SECRET_KEY", "sk_test_x") |
| 354 | monkeypatch.setenv("DBBASIC_STRIPE_WEBHOOK_SECRET", "whsec_x") |
| 355 | monkeypatch.setenv("DBBASIC_SMTP_MODE", "live") |
| 356 | monkeypatch.setenv("DBBASIC_SMTP_HOST", "smtp.example.test") |
| 357 | names = {row["name"] for row in privacy.subprocessors()} |
| 358 | assert names == {"Stripe", "smtp.example.test"} |
| 359 | |
| 360 | # Half-wired Stripe is not a sub-processor: without the webhook secret |
| 361 | # the integration cannot run at all, so claiming a data flow that |
| 362 | # cannot happen would be as wrong as omitting one that can. |
| 363 | monkeypatch.delenv("DBBASIC_STRIPE_WEBHOOK_SECRET") |
| 364 | assert "Stripe" not in {row["name"] for row in privacy.subprocessors()} |
| 365 | |
| 366 | |
| 367 | def test_a_carrier_is_a_subprocessor_only_when_one_is_connected( |
| 368 | tmp_path, monkeypatch): |
| 369 | """`manual` means the operator walks to the counter: no address |
| 370 | leaves this server, so no carrier belongs on the list.""" |
| 371 | for provider in ("", "none", "manual"): |
| 372 | settings = SIGNED + (("carrier.provider", provider),) if provider else SIGNED |
| 373 | box(tmp_path, monkeypatch, settings=settings, |
| 374 | apps=[("app-shipping", "shipments")]) |
| 375 | assert privacy.subprocessors() == [], provider |
| 376 | |
| 377 | box(tmp_path, monkeypatch, settings=SIGNED + (("carrier.provider", "ups"),), |
| 378 | apps=[("app-shipping", "shipments")]) |
| 379 | assert [row["name"] for row in privacy.subprocessors()] == ["ups"] |
| 380 | |
| 381 | |
| 382 | def test_it_refuses_to_publish_a_policy_nobody_signed(tmp_path, monkeypatch): |
| 383 | """An unsigned policy is worse than none: it looks like compliance |
| 384 | while giving a reader nobody to write to. So the page states no |
| 385 | claims at all and says exactly which settings are missing.""" |
| 386 | box(tmp_path, monkeypatch, settings=(), analytics=True, retention=45) |
| 387 | body = render()["body"] |
| 388 | assert "No privacy policy has been published" in body |
| 389 | for key, _value in SIGNED: |
| 390 | assert key in body |
| 391 | # And it makes no claims while unsigned -- not even true ones. |
| 392 | assert "45 days" not in body |
| 393 | |
| 394 | |
| 395 | def test_the_refusal_names_only_the_settings_actually_missing( |
| 396 | tmp_path, monkeypatch): |
| 397 | box(tmp_path, monkeypatch, settings=SIGNED[:1]) |
| 398 | fold = privacy.policy() |
| 399 | assert fold["controller"]["missing_settings"] == [ |
| 400 | "privacy.contact_email", "privacy.jurisdiction"] |
| 401 | assert fold["controller"]["signed"] is False |
| 402 | |
| 403 | box(tmp_path, monkeypatch, settings=SIGNED) |
| 404 | assert privacy.policy()["controller"]["signed"] is True |
| 405 | |
| 406 | |
| 407 | def test_the_cookie_table_is_published_even_when_the_policy_is_not( |
| 408 | tmp_path, monkeypatch): |
| 409 | """The browser-facing facts are true regardless of who signed them, |
| 410 | and a visitor asking what is in their browser deserves an answer even |
| 411 | on a box whose operator has not filled the form in.""" |
| 412 | box(tmp_path, monkeypatch, settings=()) |
| 413 | body = render("/cookies")["body"] |
| 414 | assert "dbbasic_session" in body |
| 415 | |
| 416 | |
| 417 | def test_a_signed_policy_names_the_controller_and_how_to_reach_them( |
| 418 | tmp_path, monkeypatch): |
| 419 | box(tmp_path, monkeypatch) |
| 420 | body = render()["body"] |
| 421 | assert "Q9 Ltd" in body |
| 422 | assert "England and Wales" in body |
| 423 | assert "privacy@example.test" in body |
| 424 | |
| 425 | |
| 426 | def test_the_operators_own_legal_text_is_rendered_and_attributed( |
| 427 | tmp_path, monkeypatch): |
| 428 | """Under its own heading, so a reader can tell which half of the page |
| 429 | is machine-generated fact and which half is a human's statement.""" |
| 430 | box(tmp_path, monkeypatch, |
| 431 | settings=SIGNED + (("privacy.extra_markdown", |
| 432 | "Disputes go to the courts of England and Wales."),)) |
| 433 | body = render()["body"] |
| 434 | assert "Additional terms from Q9 Ltd" in body |
| 435 | assert "Disputes go to the courts of England and Wales." in body |
| 436 | assert "the operator's own text" in body or "operator's own text" in body |
| 437 | |
| 438 | |
| 439 | def test_the_three_surfaces_are_one_fold(tmp_path, monkeypatch): |
| 440 | """/privacy, /cookies and /privacy.json cannot disagree because there |
| 441 | is nothing for them to disagree about.""" |
| 442 | box(tmp_path, monkeypatch, analytics=True, visitor_cookie=True, retention=90) |
| 443 | payload = json.loads(render("/privacy.json")["body"]) |
| 444 | assert payload["analytics"]["retention_days"] == 90 |
| 445 | assert {row["name"] for row in payload["cookies"]} == { |
| 446 | row["name"] for row in privacy.cookie_table()} |
| 447 | assert "90 days" in render()["body"] |
| 448 | for row in payload["cookies"]: |
| 449 | assert row["name"] in render("/cookies")["body"] |
| 450 | |
| 451 | |
| 452 | def test_the_json_carries_no_timestamp_so_it_can_be_diffed( |
| 453 | tmp_path, monkeypatch): |
| 454 | """A generated-at stamp would make every fetch differ from the last |
| 455 | one and hide the changes that matter -- which is the entire reason |
| 456 | somebody would fetch it twice.""" |
| 457 | box(tmp_path, monkeypatch, analytics=True) |
| 458 | assert render("/privacy.json")["body"] == render("/privacy.json")["body"] |
| 459 | |
| 460 | |
| 461 | def test_the_json_is_served_even_unsigned_and_says_it_is_unsigned( |
| 462 | tmp_path, monkeypatch): |
| 463 | """404ing would leave a monitor guessing. The honest answer is the |
| 464 | fold, with `signed: false` in it.""" |
| 465 | box(tmp_path, monkeypatch, settings=()) |
| 466 | payload = json.loads(render("/privacy.json")["body"]) |
| 467 | assert payload["controller"]["signed"] is False |
| 468 | assert payload["controller"]["missing_settings"] |
| 469 | |
| 470 | |
| 471 | def test_the_default_box_tells_a_visitor_nothing_is_stored_about_them( |
| 472 | tmp_path, monkeypatch): |
| 473 | """The posture the whole design is aimed at, read from the visitor's |
| 474 | side: analytics off, no cookie but the necessary ones, no third |
| 475 | party. This is the page a default install publishes.""" |
| 476 | box(tmp_path, monkeypatch) |
| 477 | body = render()["body"] |
| 478 | assert "This server keeps no log of page views" in body |
| 479 | assert "No third party is configured" in body |
| 480 | assert object_analytics.VISITOR_COOKIE_NAME not in body |
| 481 | |
| 482 | |
| 483 | # =========================================================================== |
| 484 | # 3. The export: this subject's rows, and nobody else's |
| 485 | # =========================================================================== |
| 486 | |
| 487 | EXPORT_OBJECT = "action_export_subject_data" |
| 488 | ADMIN = {"_identity": {"user_id": "dan", "roles": ["admin"]}} |
| 489 | |
| 490 | ANNA = "anna@example.test" |
| 491 | BEN = "ben@example.test" |
| 492 | |
| 493 | |
| 494 | def export(payload): |
| 495 | return object_execution.execute_object( |
| 496 | RUNTIME, |
| 497 | object_execution.ObjectExecutionRequest( |
| 498 | EXPORT_OBJECT, method="POST", payload=payload), |
| 499 | roots=[PRIVACY_OBJECTS]).result |
| 500 | |
| 501 | |
| 502 | @pytest.fixture() |
| 503 | def two_customers(tmp_path, monkeypatch): |
| 504 | """Anna and Ben, each with an order, an invoice, a payment against |
| 505 | that invoice and a shipment against that order. The whole point of |
| 506 | the fixture is that there are two of them.""" |
| 507 | data_dir = tmp_path / "data" |
| 508 | for package_id, collection in (("app-orders", "orders"), |
| 509 | ("app-invoices", "invoices"), |
| 510 | ("app-payments", "payments"), |
| 511 | ("app-shipping", "shipments"), |
| 512 | ("app-shop", "carts"), |
| 513 | ("app-disputes", "disputes"), |
| 514 | ("app-contacts", "contacts"), |
| 515 | ("app-analytics", "conversions")): |
| 516 | stage_collection(data_dir, package_id, collection) |
| 517 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 518 | |
| 519 | for who, tag in ((ANNA, "a"), (BEN, "b")): |
| 520 | object_records.create_collection_record( |
| 521 | "orders", {"id": f"ord-{tag}", "number": f"SO-{tag}", |
| 522 | "customer_email": who, |
| 523 | "customer_name": who.split("@")[0], "status": "confirmed"}, |
| 524 | base_dir=data_dir, actor="test") |
| 525 | object_records.create_collection_record( |
| 526 | "invoices", {"id": f"inv-{tag}", "number": f"INV-{tag}", |
| 527 | "customer_name": who.split("@")[0], |
| 528 | "customer_email": who, "status": "sent"}, |
| 529 | base_dir=data_dir, actor="test") |
| 530 | object_records.create_collection_record( |
| 531 | "payments", {"id": f"pay-{tag}", "invoice_id": f"inv-{tag}", |
| 532 | "amount_cents": "1000", "received_on": "2026-07-01"}, |
| 533 | base_dir=data_dir, actor="test") |
| 534 | object_records.create_collection_record( |
| 535 | "shipments", {"id": f"shp-{tag}", "order_id": f"ord-{tag}", |
| 536 | "status": "shipped"}, |
| 537 | base_dir=data_dir, actor="test") |
| 538 | object_records.create_collection_record( |
| 539 | "carts", {"id": f"cart-{tag}", "session_token": f"tok-{tag}", |
| 540 | "customer_email": who, "status": "open"}, |
| 541 | base_dir=data_dir, actor="test") |
| 542 | object_records.create_collection_record( |
| 543 | "disputes", {"id": f"dis-{tag}", "customer_email": who, |
| 544 | "kind": "delivery", "summary": "missing parcel", |
| 545 | "status": "open"}, |
| 546 | base_dir=data_dir, actor="test") |
| 547 | object_records.create_collection_record( |
| 548 | "contacts", {"id": f"con-{tag}", "first_name": who.split("@")[0], |
| 549 | "email": who}, |
| 550 | base_dir=data_dir, actor="test") |
| 551 | object_records.create_collection_record( |
| 552 | "conversions", {"id": f"cv-{tag}", "event_type": "order_confirmed", |
| 553 | "metadata": json.dumps({"source": f"orders/ord-{tag}"})}, |
| 554 | base_dir=data_dir, actor="test") |
| 555 | return data_dir |
| 556 | |
| 557 | |
| 558 | def test_the_export_returns_this_subjects_rows_and_nobody_elses(two_customers): |
| 559 | """The test that matters in this file's second half. |
| 560 | |
| 561 | An export that leaks a second customer's order into the first |
| 562 | customer's subject request is not a partial success; it is a data |
| 563 | breach committed by the compliance feature. |
| 564 | """ |
| 565 | result = export({**ADMIN, "email": ANNA}) |
| 566 | assert result["ok"] is True |
| 567 | |
| 568 | blob = json.dumps(result) |
| 569 | assert BEN not in blob |
| 570 | for row_id in ("ord-b", "inv-b", "pay-b", "shp-b", "cart-b", "dis-b", |
| 571 | "con-b", "cv-b"): |
| 572 | assert row_id not in blob, row_id |
| 573 | |
| 574 | ids = {collection: {row["id"] for row in rows} |
| 575 | for collection, rows in result["collections"].items()} |
| 576 | assert ids["orders"] == {"ord-a"} |
| 577 | assert ids["invoices"] == {"inv-a"} |
| 578 | assert ids["carts"] == {"cart-a"} |
| 579 | assert ids["disputes"] == {"dis-a"} |
| 580 | assert ids["contacts"] == {"con-a"} |
| 581 | |
| 582 | |
| 583 | def test_linked_rows_are_reached_only_through_the_subjects_own_records( |
| 584 | two_customers): |
| 585 | """Payments hang off an invoice and shipments off an order, so the |
| 586 | join runs in the safe direction only: the subject's ids select the |
| 587 | linked rows, and a payment whose invoice is not theirs is never |
| 588 | looked at again.""" |
| 589 | result = export({**ADMIN, "email": ANNA}) |
| 590 | assert {row["id"] for row in result["collections"]["payments"]} == {"pay-a"} |
| 591 | assert {row["id"] for row in result["collections"]["shipments"]} == {"shp-a"} |
| 592 | |
| 593 | |
| 594 | def test_a_conversion_is_returned_only_when_it_genuinely_names_the_subject( |
| 595 | two_customers): |
| 596 | """By provenance -- the row names one of their own orders. Never by |
| 597 | visitor token: that token is anonymous by construction and joining it |
| 598 | to a person here would perform the exact de-anonymisation the |
| 599 | analytics rules forbid, inside the feature meant to protect them.""" |
| 600 | result = export({**ADMIN, "email": ANNA}) |
| 601 | assert {row["id"] for row in result["collections"]["conversions"]} == {"cv-a"} |
| 602 | |
| 603 | |
| 604 | def test_the_export_says_what_it_cannot_search_rather_than_omitting_it( |
| 605 | two_customers): |
| 606 | """A silent omission reads as "we hold nothing about you", which is a |
| 607 | different and untrue answer.""" |
| 608 | result = export({**ADMIN, "email": ANNA}) |
| 609 | collections = {entry["collection"] for entry in result["not_searchable"]} |
| 610 | assert "page_views" in collections |
| 611 | |
| 612 | |
| 613 | def test_an_absent_collection_is_not_an_empty_one(tmp_path, monkeypatch): |
| 614 | """"This server does not record shipments" and "you have no |
| 615 | shipments" are different answers to a subject request, and only one |
| 616 | of them is about them.""" |
| 617 | data_dir = tmp_path / "data" |
| 618 | stage_collection(data_dir, "app-orders", "orders") |
| 619 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 620 | result = export({**ADMIN, "email": ANNA}) |
| 621 | assert "shipments" in result["unavailable"] |
| 622 | assert "shipments" not in result["collections"] |
| 623 | assert result["counts"]["orders"] == 0 |
| 624 | |
| 625 | |
| 626 | def test_the_export_refuses_anybody_who_is_not_an_operator(two_customers): |
| 627 | """No self-service door: it would be a lookup oracle for anyone who |
| 628 | knows a customer's email address.""" |
| 629 | assert export({"email": ANNA})["status"] == 403 |
| 630 | assert export({"_identity": {"user_id": "anna", "roles": []}, |
| 631 | "email": ANNA})["status"] == 403 |
| 632 | assert export({**ADMIN})["status"] == 400 # no email |
| 633 | |
| 634 | |
| 635 | def test_the_export_does_not_pretend_to_offer_erasure(two_customers): |
| 636 | """Deliberately absent rather than quietly half-implemented: erasure |
| 637 | needs a redaction-with-tombstone design and a doctrine decision about |
| 638 | which fields are contact and which are evidence. Code written before |
| 639 | that decision would make the choice silently, in the one place it can |
| 640 | never be undone.""" |
| 641 | result = export({**ADMIN, "email": ANNA}) |
| 642 | assert "Not available through this action" in result["erasure"] |
| 643 | |
| 644 | |
| 645 | def test_the_email_match_is_case_and_space_insensitive(two_customers): |
| 646 | """A subject request arrives typed by a human, not normalized by a |
| 647 | form.""" |
| 648 | result = export({**ADMIN, "email": " ANNA@Example.Test "}) |
| 649 | assert result["counts"]["orders"] == 1 |