clipwright/tests/test_account_erasure.py
juancarlos.cavero 1d9fef7445 feat(cloud): self-service account deletion (GDPR art. 17)
The privacy policy already told users that "deleting your account from the
dashboard removes your content" — there was no way to do it. This adds one:
Account -> Delete account in the dashboard, DELETE /api/account on the server.

The endpoint refuses API-key auth (a leaked osk_ must not destroy the account
it was issued from) and requires the caller to retype the account email; the UI
does the same, collapsed behind a danger zone that itemises what actually goes.
Erasure is immediate and irreversible, because after it there is nothing left
to authenticate a recovery request against.

The order of the steps is the design, each one a failure mode:

- Stripe cancel first, aborting everything if it fails, so we never erase a
  user we are still billing. An InvalidRequestError is treated as already
  cancelled: a portal cancellation whose webhook never landed leaves our row
  reading "active" forever, and the one account that could not be deleted
  would be the one Stripe already stopped charging.
- R2 before the database, because those rows are the only index of which
  objects belong to the user; dropping them first turns a failed purge into
  permanent orphans.
- The DB delete is one transaction over an explicit table list, not the
  declared ON DELETE CASCADEs: create_all never ALTERs an existing table, so a
  constraint added after a table shipped lives in the models and not in
  production. tests/test_account_erasure.py fails if a new table references
  users.id without joining that list.
- A reserved usage_ledger row blocks the delete: erasing under a running job
  would pull the output from beneath it and leave the reservation unsettled.

What survives, and why: the Stripe customer and its invoices (six years,
Spanish commercial law) and one account_deletions row holding a sha256 of the
email as proof the erasure happened, itself purged after five years by the
retention sweeper. app.py registers a callback so the working files in output/
and uploads/ go too instead of waiting for the hourly sweep.

Privacy policy sections 6 (EN + ES) now point at the button and name both
survivors; the Legal hub summary says the same in one line.
2026-08-24 19:12:05 +02:00

64 lines
2.8 KiB
Python

"""Account erasure (cloud/account.py): the GDPR Art. 17 delete.
The one thing worth testing without a database is coverage. The endpoint tells
the user everything is gone, so any table holding their rows that the delete
list forgets turns that sentence into a false statement — and a table added a
year from now is exactly the kind of thing nobody remembers to add here.
"""
import hashlib
from cloud import account
from cloud.database import Base
from cloud.models import AccountDeletion, User
def _tables_referencing_users():
"""Every table with a foreign key into ``users`` — the ground truth the
delete list has to keep up with."""
found = set()
for table in Base.metadata.tables.values():
for fk in table.foreign_keys:
if fk.column.table.name == User.__tablename__:
found.add(table.name)
return found
class TestCoverage:
def test_every_user_owned_table_is_erased(self):
listed = {m.__tablename__ for m in account.USER_OWNED_TABLES}
missing = _tables_referencing_users() - listed
assert not missing, (
f"{missing} reference users.id but are not erased on account "
f"deletion — add them to account.USER_OWNED_TABLES")
def test_children_are_deleted_before_their_parents(self):
# clip_expiry_warnings has an FK into user_videos, so deleting the
# videos first would trip that constraint.
order = [m.__tablename__ for m in account.USER_OWNED_TABLES]
assert order.index("clip_expiry_warnings") < order.index("user_videos")
def test_the_erasure_record_itself_is_not_erased(self):
# It has no FK to users on purpose (the row it would point at is gone),
# so it must not appear in the delete list either.
assert AccountDeletion not in account.USER_OWNED_TABLES
assert AccountDeletion.__tablename__ not in _tables_referencing_users()
class TestEmailFingerprint:
def test_is_a_plain_sha256_of_the_normalised_address(self):
assert account.email_fingerprint("user@example.com") == \
hashlib.sha256(b"user@example.com").hexdigest()
def test_does_not_contain_the_address(self):
assert "user@example.com" not in account.email_fingerprint("user@example.com")
def test_survives_the_aliasing_the_account_key_ignores(self):
# Sign-up normalises Gmail dots and +tags, so the fingerprint has to
# match the same way or a deleted user could not be found by the
# address they actually typed.
assert account.email_fingerprint("Foo.Bar+news@gmail.com") == \
account.email_fingerprint("foobar@gmail.com")
def test_different_addresses_differ(self):
assert account.email_fingerprint("a@example.com") != \
account.email_fingerprint("b@example.com")